1 | 'use strict'
|
2 |
|
3 | const deepForEach = require('../../utils/collection/deepForEach')
|
4 |
|
5 | function factory (type, config, load, typed) {
|
6 | const add = load(require('../arithmetic/add'))
|
7 | const multiply = load(require('../arithmetic/multiply'))
|
8 | const divide = load(require('../arithmetic/divide'))
|
9 | const factorial = load(require('../probability/factorial'))
|
10 | const isInteger = load(require('../utils/isInteger'))
|
11 | const isPositive = load(require('../utils/isPositive'))
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | return typed('multinomial', {
|
35 | 'Array | Matrix': function (a) {
|
36 | let sum = 0
|
37 | let denom = 1
|
38 |
|
39 | deepForEach(a, function (ai) {
|
40 | if (!isInteger(ai) || !isPositive(ai)) {
|
41 | throw new TypeError('Positive integer value expected in function multinomial')
|
42 | }
|
43 | sum = add(sum, ai)
|
44 | denom = multiply(denom, factorial(ai))
|
45 | })
|
46 |
|
47 | return divide(factorial(sum), denom)
|
48 | }
|
49 | })
|
50 | }
|
51 |
|
52 | exports.name = 'multinomial'
|
53 | exports.factory = factory
|