python中最快的上升阶乘(Pochhammer函数)

时间:2011-08-03 17:14:24

标签: python performance scipy sympy

我需要计算大数的rising factorial,我发现的最好的是来自sympy包sympy package的上升阶乘函数,这真的很好,但我仍然需要更快的东西

我真正需要的是一个非常快的版本:

from itertools import combinations
from numpy import prod

def my_rising_factorial(degree, elt):
    return sum([prod(i) for i in combinations(xrange(1,degree),elt)])

编辑: 给出一个上升阶乘,x(n)= x(x + 1)(x + 2)...(x + n-1),我想检索其扩展公式的给定乘数。

例如:

给出:x(6)= x(x + 1)(x + 2)(x + 3)(x + 5)(x + 6)

和扩展形式:x(6)= x ** 6 + 15 * x ** 5 + 85 * x ** 4 + 225 * x ** 3 + 274 * x ** 2 + 120 * x < / p>

我想要一些如何获得其中一个乘数(在这种情况下为1,15,85,225,274,120)

使用“my_rising_factorial()”它效果很好......但是很慢

>>>[my_rising_factorial(6,i) for i in xrange (6)]
[1.0, 15, 85, 225, 274, 120]

3 个答案:

答案 0 :(得分:4)

试试此套餐:http://tnt.math.se.tmu.ac.jp/nzmath/

就像工作说的那样,你想要的功能是第一种斯特林编号(我已经计算了递归定义并且即将发布它,但我不知道这个名字)。

该功能是nzmath.combinatorial.stirling1

答案 1 :(得分:3)

我所知道的其他版本位于mpmath.qfunctionsscipy.special.orthogonal

如果这些和SymPy都不够快,你可以尝试PyPy(Python的另一个实现)来加速它们。如果这不起作用,请尝试Psyco(扩展模块),Shedskin或Nuitka(Python编译器),Cython或用C语言编写。

答案 2 :(得分:2)

这些只是未签名的Stirling Numbers of the First Kind。我没有快速计算它们的方法,但你可能会使用它们遵循一个简单的递归关系的事实:S(n,k) = (n-1)*S(n-1,k) + S(n-1,k-1)