composition.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. * The composition counts of n into k parts.
  16. *
  17. * composition only takes integer arguments.
  18. * The following condition must be enforced: k <= n.
  19. *
  20. * Syntax:
  21. *
  22. * math.composition(n, k)
  23. *
  24. * Examples:
  25. *
  26. * math.composition(5, 3) // returns 6
  27. *
  28. * See also:
  29. *
  30. * combinations
  31. *
  32. * @param {Number | BigNumber} n Total number of objects in the set
  33. * @param {Number | BigNumber} k Number of objects in the subset
  34. * @return {Number | BigNumber} Returns the composition counts of n into k parts.
  35. */
  36. return typed(name, {
  37. 'number | BigNumber, number | BigNumber': function numberBigNumberNumberBigNumber(n, k) {
  38. if (!isInteger(n) || !isPositive(n) || !isInteger(k) || !isPositive(k)) {
  39. throw new TypeError('Positive integer value expected in function composition');
  40. } else if (larger(k, n)) {
  41. throw new TypeError('k must be less than or equal to n in function composition');
  42. }
  43. return combinations(addScalar(n, -1), addScalar(k, -1));
  44. }
  45. });
  46. });