UNPKG

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