-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfactorial.js
More file actions
53 lines (47 loc) · 1.35 KB
/
factorial.js
File metadata and controls
53 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { deepMap } from '../../utils/collection.js'
import { factory } from '../../utils/factory.js'
const name = 'factorial'
const dependencies = ['typed', 'isInteger', 'gamma']
export const createFactorial = /* #__PURE__ */ factory(name, dependencies, ({
typed, isInteger, bignumber, gamma
}) => {
/**
* Compute the factorial of a value
*
* Factorial only supports an integer value as argument.
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.factorial(n)
*
* Examples:
*
* math.factorial(5) // returns 120
* math.factorial(3) // returns 6
*
* See also:
*
* combinations, combinationsWithRep, gamma, permutations
*
* @param {number | BigNumber | Array | Matrix} n An integer number
* @return {number | BigNumber | Array | Matrix} The factorial of `n`
*/
return typed(name, {
number: function (n) {
if (n < 0) {
throw new Error('Value must be non-negative')
}
return gamma(n + 1)
},
BigNumber: function (n) {
if (n.isNegative()) {
throw new Error('Value must be non-negative')
}
if (!n.isFinite()) return n
if (!isInteger(n)) throw new Error('Value must be integer')
return gamma(n.plus(1))
},
'Array | Matrix': typed.referToSelf(self => n => deepMap(n, self))
})
})