【发布时间】:2020-03-05 14:47:35
【问题描述】:
根据this answer,计算二项式系数有两个函数,也称为“N选K”。其中之一是scipy.special.binom()。
这个功能在哪里实现?我只知道这是ufunc。
另外,scipy.special.binom()的时间复杂度是多少?
【问题讨论】:
标签: scipy binomial-coefficients
根据this answer,计算二项式系数有两个函数,也称为“N选K”。其中之一是scipy.special.binom()。
这个功能在哪里实现?我只知道这是ufunc。
另外,scipy.special.binom()的时间复杂度是多少?
【问题讨论】:
标签: scipy binomial-coefficients
源代码可以在 Github 上的orthogonal_eval.pxd找到。
在整数情况下,复杂度为 O(k)。
kx = floor(k)
if k == kx and (fabs(n) > 1e-8 or n == 0):
# Integer case: use multiplication formula for less rounding error
# for cases where the result is an integer.
#
# This cannot be used for small nonzero n due to loss of
# precision.
nx = floor(n)
if nx == n and kx > nx/2 and nx > 0:
# Reduce kx by symmetry
kx = nx - kx
if kx >= 0 and kx < 20:
num = 1.0
den = 1.0
for i in range(1, 1 + <int>kx):
num *= i + n - kx
den *= i
if fabs(num) > 1e50:
num /= den
den = 1.0
return num/den
【讨论】: