UNPKG

1.3 kBJavaScriptView Raw
1import { deepMap } from '../../utils/collection.js';
2import { factory } from '../../utils/factory.js';
3var name = 'factorial';
4var dependencies = ['typed', 'gamma'];
5export var createFactorial = /* #__PURE__ */factory(name, dependencies, (_ref) => {
6 var {
7 typed,
8 gamma
9 } = _ref;
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 return 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, this);
50 }
51 });
52});
\No newline at end of file