bundles / scipy latest / scipy / special / _basic / stirling2
function
scipy.special._basic:stirling2
source: build-install/usr/lib/python3.14/site-packages/scipy/special/_basic.py:3567
Signature
def stirling2(N, K, *, exact = False)Summary
Generate Stirling number(s) of the second kind.
Extended Summary
Stirling numbers of the second kind count the number of ways to partition a set with N elements into K non-empty subsets.
The values this function returns are calculated using a dynamic program which avoids redundant computation across the subproblems in the solution. For array-like input, this implementation also avoids redundant computation across the different Stirling number calculations.
The numbers are sometimes denoted
see [1] for details. This is often expressed-verbally-as "N subset K".
Parameters
N: int, ndarrayNumber of things.
K: int, ndarrayNumber of non-empty subsets taken.
exact: bool, optionalUses dynamic programming (DP) with floating point numbers for smaller arrays and uses a second order approximation due to Temme for larger entries of
NandKthat allows trading speed for accuracy. See [2] for a description. Temme approximation is used for valuesn>50. The max error from the DP has max relative error4.5*10^-16forn<=50and the max error from the Temme approximation has max relative error5*10^-5for51 <= n < 70and9*10^-6for70 <= n < 101. Note that these max relative errors will decrease further asnincreases.
Returns
val: int, float, ndarrayThe number of partitions.
Notes
If N < 0, or K < 0, then 0 is returned.
If K > N, then 0 is returned.
The output type will always be int or ndarray of object. The input must contain either numpy or python integers otherwise a TypeError is raised.
Examples
import numpy as np from scipy.special import stirling2 k = np.array([3, -1, 3]) n = np.array([10, 10, 9])✓
stirling2(n, k)
✗array([9330., 0., 3025.])
See also
- comb
The number of combinations of N things taken k at a time.
Aliases
scipy.special.stirling2