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