Python中的无限求和

aid*_*ald 5 python numpy mathematical-optimization integral scipy

我有一个函数,我需要在数值上对所有整数进行无限求和.总和并不总是需要收敛,因为我可以改变内部参数.功能看起来像,

m(g, x, q0) = sum(abs(g(x - n*q0))^2 for n in Integers)
m(g, q0) = minimize(m(g, x, q0) for x in [0, q0])
Run Code Online (Sandbox Code Playgroud)

使用Pythonic伪代码

使用Scipy集成方法,我只是将n和i固定为固定x,

m(g, z, q0) = integrate.quad(lambda n:
                             abs(g(x - int(n)*q0))**2,
                             -inf, +inf)[0]
Run Code Online (Sandbox Code Playgroud)

这很好用,但后来我必须对x进行优化作为x的函数,然后对其进行另一个求和,得到积分优化的积分.差不多需要很长时间.

你知道更好的方法来做更快的求和吗?手工编码似乎变慢了.

目前,我正在与之合作

g(x) = (2/sqrt(3))*pi**(-0.25)*(1 - x**2)*exp(-x**2/2)
Run Code Online (Sandbox Code Playgroud)

但解决方案应该是一般的

这篇文章来自Daubechies的"小波变换,时频定位和信号分析"(IEEE 1990)

谢谢

小智 4

感谢所有有用的评论,我编写了自己的求和器,它似乎运行得相当快。如果有人有任何改进的建议,我会很乐意接受。

我将在我正在解决的问题上对此进行测试,一旦证明成功,我将声称它有效。

def integers(blk_size=100):
    x = arange(0, blk_size)
    while True:
        yield x
        yield -x -1
        x += blk_size

#                                                                                                                                                                                                            
# For convergent summation                                                                                                                                                                                   
# on not necessarily finite sequences                                                                                                                                                                        
# processes in blocks which can be any size                                                                                                                                                                  
# shape that the function can handle                                                                                                                                                                         
#                                                                                                                                                                                                            
def converge_sum(f, x_strm, eps=1e-5, axis=0):
    total = sum(f(x_strm.next()), axis=axis)
    for x_blk in x_strm:
        diff = sum(f(x_blk), axis=axis)
        if abs(linalg.norm(diff)) <= eps:
            # Converged                                                                                                                                                                                      
            return total + diff
        else:
            total += diff
Run Code Online (Sandbox Code Playgroud)