UNPKG

1.4 kBJavaScriptView Raw
1'use strict';
2
3function factory(type, config, load, typed) {
4 var add = load(require('../arithmetic/add'));
5 var stirlingS2 = load(require('./stirlingS2'));
6 var isNegative = load(require('../utils/isNegative'));
7 var isInteger = load(require('../utils/isInteger'));
8
9 /**
10 * The Bell Numbers count the number of partitions of a set. A partition is a pairwise disjoint subset of S whose union is S.
11 * bellNumbers only takes integer arguments.
12 * The following condition must be enforced: n >= 0
13 *
14 * Syntax:
15 *
16 * math.bellNumbers(n)
17 *
18 * Examples:
19 *
20 * math.bellNumbers(3) // returns 5
21 * math.bellNumbers(8) // returns 4140
22 *
23 * See also:
24 *
25 * stirlingS2
26 *
27 * @param {Number | BigNumber} n Total number of objects in the set
28 * @return {Number | BigNumber} B(n)
29 */
30 var bellNumbers = typed('bellNumbers', {
31 'number | BigNumber': function numberBigNumber(n) {
32 if (!isInteger(n) || isNegative(n)) {
33 throw new TypeError('Non-negative integer value expected in function bellNumbers');
34 }
35
36 // Sum (k=0, n) S(n,k).
37 var result = 0;
38 for (var i = 0; i <= n; i++) {
39 result = add(result, stirlingS2(n, i));
40 }
41
42 return result;
43 }
44 });
45
46 bellNumbers.toTex = { 1: '\\mathrm{B}_{${args[0]}}' };
47
48 return bellNumbers;
49}
50
51exports.name = 'bellNumbers';
52exports.factory = factory;
\No newline at end of file