UNPKG

1.43 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2
3const name = 'composition'
4const dependencies = [
5 'typed',
6 'addScalar',
7 'combinations',
8 'isNegative',
9 'isPositive',
10 'isInteger',
11 'larger'
12]
13
14export const createComposition = /* #__PURE__ */ factory(name, dependencies, (
15 {
16 typed,
17 addScalar,
18 combinations,
19 isPositive,
20 isNegative,
21 isInteger,
22 larger
23 }
24) => {
25 /**
26 * The composition counts of n into k parts.
27 *
28 * composition only takes integer arguments.
29 * The following condition must be enforced: k <= n.
30 *
31 * Syntax:
32 *
33 * math.composition(n, k)
34 *
35 * Examples:
36 *
37 * math.composition(5, 3) // returns 6
38 *
39 * See also:
40 *
41 * combinations
42 *
43 * @param {Number | BigNumber} n Total number of objects in the set
44 * @param {Number | BigNumber} k Number of objects in the subset
45 * @return {Number | BigNumber} Returns the composition counts of n into k parts.
46 */
47 return typed(name, {
48 'number | BigNumber, number | BigNumber': function (n, k) {
49 if (!isInteger(n) || !isPositive(n) || !isInteger(k) || !isPositive(k)) {
50 throw new TypeError('Positive integer value expected in function composition')
51 } else if (larger(k, n)) {
52 throw new TypeError('k must be less than or equal to n in function composition')
53 }
54
55 return combinations(addScalar(n, -1), addScalar(k, -1))
56 }
57 })
58})