scipy.special.stirling2#

scipy.special.stirling2(N, K, *, exact=False)[source]#

Generate Stirling number(s) of the second kind.

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

\[{N \brace{K}}\]

see [1] for details. This is often expressed-verbally-as “N subset K”.

Parameters:
Nint, ndarray

Number of things.

Kint, ndarray

Number of non-empty subsets taken.

exactbool, optional

Uses dynamic programming (DP) with floating point numbers for smaller arrays and uses a second order approximation due to Temme for larger entries of N and K that allows trading speed for accuracy. See [2] for a description. Temme approximation is used for values n>50. The max error from the DP has max relative error 4.5*10^-16 for n<=50 and the max error from the Temme approximation has max relative error 5*10^-5 for 51 <= n < 70 and 9*10^-6 for 70 <= n < 101. Note that these max relative errors will decrease further as n increases.

Returns:
valint, float, ndarray

The number of partitions.

See also

comb

The number of combinations of N things taken k at a time.

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.

References

[1]

R. L. Graham, D. E. Knuth and O. Patashnik, “Concrete Mathematics: A Foundation for Computer Science,” Addison-Wesley Publishing Company, Boston, 1989. Chapter 6, page 258.

[2]

Temme, Nico M. “Asymptotic estimates of Stirling numbers.” Studies in Applied Mathematics 89.3 (1993): 233-243.

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], dtype=object)