1 | 'use strict'
|
2 |
|
3 | function factory (type, config, load, typed) {
|
4 | const add = load(require('../arithmetic/add'))
|
5 | const subtract = load(require('../arithmetic/subtract'))
|
6 | const multiply = load(require('../arithmetic/multiply'))
|
7 | const divide = load(require('../arithmetic/divide'))
|
8 | const pow = load(require('../arithmetic/pow'))
|
9 | const factorial = load(require('../probability/factorial'))
|
10 | const combinations = load(require('../probability/combinations'))
|
11 | const isNegative = load(require('../utils/isNegative'))
|
12 | const isInteger = load(require('../utils/isInteger'))
|
13 | const larger = load(require('../relational/larger'))
|
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 | const stirlingS2 = typed('stirlingS2', {
|
40 | 'number | BigNumber, number | BigNumber': function (n, k) {
|
41 | if (!isInteger(n) || isNegative(n) || !isInteger(k) || isNegative(k)) {
|
42 | throw new TypeError('Non-negative integer value expected in function stirlingS2')
|
43 | } else if (larger(k, n)) {
|
44 | throw new TypeError('k must be less than or equal to n in function stirlingS2')
|
45 | }
|
46 |
|
47 |
|
48 | const kFactorial = factorial(k)
|
49 | let result = 0
|
50 | for (let i = 0; i <= k; i++) {
|
51 | const negativeOne = pow(-1, subtract(k, i))
|
52 | const kChooseI = combinations(k, i)
|
53 | const iPower = pow(i, n)
|
54 |
|
55 | result = add(result, multiply(multiply(kChooseI, iPower), negativeOne))
|
56 | }
|
57 |
|
58 | return divide(result, kFactorial)
|
59 | }
|
60 | })
|
61 |
|
62 | stirlingS2.toTex = { 2: `\\mathrm{S}\\left(\${args}\\right)` }
|
63 |
|
64 | return stirlingS2
|
65 | }
|
66 |
|
67 | exports.name = 'stirlingS2'
|
68 | exports.factory = factory
|