UNPKG

1.56 kBJavaScriptView Raw
1'use strict';
2
3function factory(type, config, load, typed) {
4 var combinations = load(require('../probability/combinations'));
5 var add = load(require('../arithmetic/addScalar'));
6 var isPositive = load(require('../utils/isPositive'));
7 var isInteger = load(require('../utils/isInteger'));
8 var larger = load(require('../relational/larger'));
9
10 /**
11 * The composition counts of n into k parts.
12 *
13 * composition only takes integer arguments.
14 * The following condition must be enforced: k <= n.
15 *
16 * Syntax:
17 *
18 * math.composition(n, k)
19 *
20 * Examples:
21 *
22 * math.composition(5, 3) // returns 6
23 *
24 * See also:
25 *
26 * combinations
27 *
28 * @param {Number | BigNumber} n Total number of objects in the set
29 * @param {Number | BigNumber} k Number of objects in the subset
30 * @return {Number | BigNumber} Returns the composition counts of n into k parts.
31 */
32 var composition = typed('composition', {
33 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(n, k) {
34 if (!isInteger(n) || !isPositive(n) || !isInteger(k) || !isPositive(k)) {
35 throw new TypeError('Positive integer value expected in function composition');
36 } else if (larger(k, n)) {
37 throw new TypeError('k must be less than or equal to n in function composition');
38 }
39
40 return combinations(add(n, -1), add(k, -1));
41 }
42 });
43
44 composition.toTex = undefined; // use default template
45
46 return composition;
47}
48
49exports.name = 'composition';
50exports.factory = factory;
\No newline at end of file