multinomial.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { deepForEach } from '../../utils/collection.js';
  2. import { factory } from '../../utils/factory.js';
  3. var name = 'multinomial';
  4. var dependencies = ['typed', 'add', 'divide', 'multiply', 'factorial', 'isInteger', 'isPositive'];
  5. export var createMultinomial = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed,
  8. add,
  9. divide,
  10. multiply,
  11. factorial,
  12. isInteger,
  13. isPositive
  14. } = _ref;
  15. /**
  16. * Multinomial Coefficients compute the number of ways of picking a1, a2, ..., ai unordered outcomes from `n` possibilities.
  17. *
  18. * multinomial takes one array of integers as an argument.
  19. * The following condition must be enforced: every ai <= 0
  20. *
  21. * Syntax:
  22. *
  23. * math.multinomial(a) // a is an array type
  24. *
  25. * Examples:
  26. *
  27. * math.multinomial([1,2,1]) // returns 12
  28. *
  29. * See also:
  30. *
  31. * combinations, factorial
  32. *
  33. * @param {number[] | BigNumber[]} a Integer numbers of objects in the subset
  34. * @return {Number | BigNumber} Multinomial coefficient.
  35. */
  36. return typed(name, {
  37. 'Array | Matrix': function ArrayMatrix(a) {
  38. var sum = 0;
  39. var denom = 1;
  40. deepForEach(a, function (ai) {
  41. if (!isInteger(ai) || !isPositive(ai)) {
  42. throw new TypeError('Positive integer value expected in function multinomial');
  43. }
  44. sum = add(sum, ai);
  45. denom = multiply(denom, factorial(ai));
  46. });
  47. return divide(factorial(sum), denom);
  48. }
  49. });
  50. });