UNPKG

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