Python中的无限和

2024-04-28 19:50:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,我需要对(所有的整数)进行无限的求和。求和并不总是需要收敛,因为我可以改变内部参数。函数看起来像

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])

使用python伪代码

使用Scipy积分方法,我只是把n加起来,然后像对固定的x一样进行积分

^{pr2}$

这很好,但是我必须对x作为x的函数进行优化,然后再对它进行求和,得到一个积分的优化积分。差不多要花很长时间。在

你知道有什么更好的方法可以更快地求和吗?手工编码的速度似乎变慢了。在

目前,我正在

g(x) = (2/sqrt(3))*pi**(-0.25)*(1 - x**2)*exp(-x**2/2)

但解决办法应该是笼统的

本文来源于Daubechies(IEEE 1990)的“小波变换、时频局部化和信号分析”

谢谢你


Tags: 方法integers函数代码infor参数整数
3条回答

g(x)几乎可以肯定是您的瓶颈。一个非常快速和肮脏的解决方案是将其矢量化以对整数数组进行运算,然后使用^{}使用梯形规则来估计积分:

import numpy as np

# appropriate range and step size depends on how accurate you need to be and how
# quickly the sum converges
xmin = -1000000
xmax = 1000000
dx = 1

x = np.arange(xmin, xmax + dx, dx)
gx = (2 / np.sqrt(3)) * np.pi**(-0.25)*(1 - x**2) * np.exp(-x**2 / 2)
sum_gx = np.trapz(gx, x, dx)

除此之外,您还可以使用Cython或numba重写g(x),以加快速度。在

Numba有可能显著提高速度-http://numba.pydata.org

它的安装有点痛苦,但非常容易使用。看看: https://jakevdp.github.io/blog/2015/02/24/optimizing-python-with-numpy-and-numba/

多亏了所有有用的评论,我写了我自己的summator,看起来运行得很快。如果任何人有任何建议,使它更好,我将乐意接受他们。在

我将在我正在处理的问题上测试这个问题,一旦它证明成功,我将声称它是有效的。在

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

相关问题 更多 >