factorial.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { deepMap } from '../../utils/collection.js';
  2. import { factory } from '../../utils/factory.js';
  3. var name = 'factorial';
  4. var dependencies = ['typed', 'gamma'];
  5. export var createFactorial = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed,
  8. gamma
  9. } = _ref;
  10. /**
  11. * Compute the factorial of a value
  12. *
  13. * Factorial only supports an integer value as argument.
  14. * For matrices, the function is evaluated element wise.
  15. *
  16. * Syntax:
  17. *
  18. * math.factorial(n)
  19. *
  20. * Examples:
  21. *
  22. * math.factorial(5) // returns 120
  23. * math.factorial(3) // returns 6
  24. *
  25. * See also:
  26. *
  27. * combinations, combinationsWithRep, gamma, permutations
  28. *
  29. * @param {number | BigNumber | Array | Matrix} n An integer number
  30. * @return {number | BigNumber | Array | Matrix} The factorial of `n`
  31. */
  32. return typed(name, {
  33. number: function number(n) {
  34. if (n < 0) {
  35. throw new Error('Value must be non-negative');
  36. }
  37. return gamma(n + 1);
  38. },
  39. BigNumber: function BigNumber(n) {
  40. if (n.isNegative()) {
  41. throw new Error('Value must be non-negative');
  42. }
  43. return gamma(n.plus(1));
  44. },
  45. 'Array | Matrix': typed.referToSelf(self => n => deepMap(n, self))
  46. });
  47. });