python中上升最快的阶乘(Pochhammer函数)
我需要计算大数的上升阶乘,到目前为止我发现的最好的是上升阶乘sympy 包中的函数 sympy 包,这真的很好,但我仍然需要更快的东西。
我真正需要的是一个非常快速的版本:
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
我想要一些如何获得这些乘数之一(在本例中为 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]
I need to compute the rising factorial of big numbers, the best I found until now is the rising factorial function from the sympy package sympy package, that is really nice, but I would still need something faster.
What I would need exactly is a really fast version of this:
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)])
EDIT:
that is given a rising factorial, x(n) = x (x + 1)(x + 2)...(x + n-1), I would like to retrieve a given multiplier of its expanded formula.
eg:
given: x(6) = x(x + 1)(x + 2)(x + 3)(x + 5)(x + 6)
and the expanded form: x(6) = x**6 + 15*x**5 + 85*x**4 + 225*x**3 + 274*x**2 + 120*x
I want some how to get one of those multipliers (in this case 1, 15, 85, 225, 274, 120)
with "my_rising_factorial()" it works well... but really slowly
>>>[my_rising_factorial(6,i) for i in xrange (6)]
[1.0, 15, 85, 225, 274, 120]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个包: http://tnt.math.se.tmu.ac.jp/ nzmath/
正如job所说,你想要的函数是第一类斯特林数(我已经计算出了递归定义并正要发布它,但我不知道名字)。
函数是 nzmath.combinatorial.stirling1
Try this package: http://tnt.math.se.tmu.ac.jp/nzmath/
Like job said, the function you want is Stirling Number of the 1st kind (I'd worked out the recursive definition and was about to post it, but I didn't know the name).
The function is nzmath.combinatorial.stirling1
我知道的其他版本位于 mpmath.qfunctions 和 scipy.special.orthogonal。
如果这些和 SymPy 都不够快,您可以尝试 PyPy(Python 的另一种实现)来加速它们。如果这不起作用,请尝试 Psyco(扩展模块)、Shedskin 或 Nuitka(Python 编译器)、Cython 或用 C 编写。
The other versions I know about are in mpmath.qfunctions and scipy.special.orthogonal.
If neither of those nor SymPy are fast enough, you can try PyPy (another implementation of Python) to speed them up. If that doesn't work, try Psyco (an extension module), Shedskin or Nuitka (Python compilers), Cython, or writing it in C.
这些只是无符号的第一类斯特林数。我没有快速计算它们的方法,但您可能可以利用它们遵循简单的递归关系这一事实:S(n,k) = (n-1)*S(n-1,k) ) + S(n-1,k-1)
These are just the unsigned Stirling Numbers of the First Kind. I don't have a fast method of computing them, but you could probably use the fact that they follow a simple recursive relationship:
S(n,k) = (n-1)*S(n-1,k) + S(n-1,k-1)