binomial.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:mathpy 作者: aschleg 项目源码 文件源码
def binomial_factorial(self):
        r"""
        Implementation of the binomial coefficient computation. Not meant for actual computation
        as the other methods available are more efficient.

        Parameters
        ----------
        n : int
            Number of possibilities
        k : int
            number of unordered outcomes

        Returns
        -------
        float
            The binomial coefficient

        Notes
        -----
        The binomial coefficient equation (in compact form) is defined as:

        .. math::

            \binom{n}{k} = \frac{n!}{k!(n-k)!} \qquad 0 \leq k \leq n

        References
        ----------
        Binomial coefficient. (2017, April 17). In Wikipedia, The Free Encyclopedia.
            From https://en.wikipedia.org/w/index.php?title=Binomial_coefficient&oldid=775905810

        Press, W., Teukolsky, S., Vetterling, W., & Flannery, B. (2007). Numerical recipes (3rd ed.).
            Cambridge: Cambridge University Press.

        Weisstein, Eric W. "Binomial Coefficient." From MathWorld--A Wolfram Web Resource.
            http://mathworld.wolfram.com/BinomialCoefficient.html

        """
        nk = np.minimum(self.n, self.n - self.k)

        if nk >= 100:
            with localcontext() as ctx:
                ctx.prec = 50
                bico = Decimal(factorial(self.n)) / (Decimal(factorial(self.k)) * Decimal(factorial(nk)))
        else:
            bico = float(factorial(self.n)) / float(factorial(self.k) * factorial(nk))

        return bico
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号