FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/function/combinatorics/stirlingS2.js at develop · CalmWeb/mathjs · GitHub
CalmWeb
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
src
/
function
/
combinatorics
/
stirlingS2.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (101 loc) · 2.64 KB
Breadcrumbs
mathjs
/
src
/
function
/
combinatorics
/
stirlingS2.js
Copy path
File metadata and controls
104 lines (101 loc) · 2.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import
{
factory
}
from
'../../utils/factory.js'
import
{
isNumber
}
from
'../../utils/is.js'
const
name
=
'stirlingS2'
const
dependencies
=
[
'typed'
,
'addScalar'
,
'subtract'
,
'multiplyScalar'
,
'divideScalar'
,
'pow'
,
'factorial'
,
'combinations'
,
'isNegative'
,
'isInteger'
,
'number'
,
'?bignumber'
,
'larger'
]
export
const
createStirlingS2
=
/* #__PURE__ */
factory
(
name
,
dependencies
,
(
{
typed
,
addScalar
,
subtract
,
multiplyScalar
,
divideScalar
,
pow
,
factorial
,
combinations
,
isNegative
,
isInteger
,
number
,
bignumber
,
larger
}
)
=>
{
const
smallCache
=
[
]
const
bigCache
=
[
]
/**
* The Stirling numbers of the second kind, counts the number of ways to partition
* a set of n labelled objects into k nonempty unlabelled subsets.
* stirlingS2 only takes integer arguments.
* The following condition must be enforced: k <= n.
*
* If n = k or k = 1 <= n, then s(n,k) = 1
* If k = 0 < n, then s(n,k) = 0
*
* Note that if either n or k is supplied as a BigNumber, the result will be
* as well.
*
* Syntax:
*
* math.stirlingS2(n, k)
*
* Examples:
*
* math.stirlingS2(5, 3) //returns 25
*
* See also:
*
* bellNumbers
*
*
@param
{
Number | BigNumber
} n Total number of objects in the set
*
@param
{
Number | BigNumber
} k Number of objects in the subset
*
@return
{
Number | BigNumber
} S(n,k)
*/
return
typed
(
name
,
{
'number | BigNumber, number | BigNumber'
:
function
(
n
,
k
)
{
if
(
!
isInteger
(
n
)
||
isNegative
(
n
)
||
!
isInteger
(
k
)
||
isNegative
(
k
)
)
{
throw
new
TypeError
(
'Non-negative integer value expected in function stirlingS2'
)
}
else
if
(
larger
(
k
,
n
)
)
{
throw
new
TypeError
(
'k must be less than or equal to n in function stirlingS2'
)
}
const
big
=
!
(
isNumber
(
n
)
&&
isNumber
(
k
)
)
const
cache
=
big
?
bigCache
:
smallCache
const
make
=
big
?
bignumber
:
number
const
nn
=
number
(
n
)
const
nk
=
number
(
k
)
/* See if we already have the value: */
if
(
cache
[
nn
]
&&
cache
[
nn
]
.
length
>
nk
)
{
return
cache
[
nn
]
[
nk
]
}
/* Fill the cache */
for
(
let
m
=
0
;
m
<=
nn
;
++
m
)
{
if
(
!
cache
[
m
]
)
{
cache
[
m
]
=
[
m
===
0
?
make
(
1
)
:
make
(
0
)
]
}
if
(
m
===
0
)
continue
const
row
=
cache
[
m
]
const
prev
=
cache
[
m
-
1
]
for
(
let
i
=
row
.
length
;
i
<=
m
&&
i
<=
nk
;
++
i
)
{
if
(
i
===
m
)
{
row
[
i
]
=
1
}
else
{
row
[
i
]
=
addScalar
(
multiplyScalar
(
make
(
i
)
,
prev
[
i
]
)
,
prev
[
i
-
1
]
)
}
}
}
return
cache
[
nn
]
[
nk
]
}
}
)
}
)
Back
|
FazBrowse Home
|
New Git URL