【问题标题】:Complexity of scipy.special.binom?scipy.special.binom 的复杂性?
【发布时间】:2020-03-05 14:47:35
【问题描述】:

根据this answer,计算二项式系数有两个函数,也称为“N选K”。其中之一是scipy.special.binom()

这个功能在哪里实现?我只知道这是ufunc

另外,scipy.special.binom()的时间复杂度是多少?

【问题讨论】:

标签: scipy binomial-coefficients


【解决方案1】:

源代码可以在 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

【讨论】:

  • 感谢@jacques,算法对我来说看起来很神秘——如何判断它是 O(k)?
  • 这是因为有 k 次迭代的 for 循环。其余代码更复杂,需要检查辅助函数以确定复杂性。
  • @JacquesGaudin 声称“在整数情况下,复杂度为 O(k)”是否合适。如果你不知道辅助函数的时间复杂度是多少?
  • @lightbox142 这个答案可以追溯到一点,但是看着它,我认为它没有任何问题。你?哪个辅助函数有问题?
  • @JacquesGaudin,从你提到的代码中,我只关心 fabs() 函数。时间复杂度是多少?
猜你喜欢
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多