factorial.js 1.5 KB

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