SciPy:加快复杂积分的方法

时间:2017-01-23 02:17:39

标签: performance scipy numerical-integration

我有一个非常复杂的积分来计算:

from __future__ import division
from scipy.integrate import quad, nquad
import numpy as np

alpha = np.array([0.298073, 1.242567, 5.782948, 38.474970])
trial = np.array([0.08704173, 0.52509737, 0.51920929, 0.31233737])


class EigenvalueProblem:

    def __init__(self, a, t):
        self.alpha = a
        self.trial = t

    # Hamiltonian, interaction part
    def hartree_integrand(self, coeff):
        def hartree_potential(rr2):
            return np.array([coeff[ii] * coeff[jj] *
                             np.exp(-(self.alpha[ii] +
                                      self.alpha[jj]) * rr2 ** 2)
                             for ii in range(0, 4) for jj in range(0, 4)]).sum()

        def length(theta, rr1, rr2):
            return 1 / np.sqrt(rr1 ** 2 + rr2 ** 2 -
                               2 * rr1 * rr2 * np.cos(theta))

        def tmp(theta, rr1, rr2):
            return 8 * np.pi ** 2 * rr1 ** 2 * rr2 ** 2 * \
                np.sin(theta) * hartree_potential(rr2) * \
                length(theta, rr1, rr2)

        def integrand(ii, jj, theta, rr1, rr2):
            return np.exp(-(self.alpha[ii] + self.alpha[jj]) * rr1 ** 2) * tmp(theta, rr1, rr2)

        return [
            nquad(lambda theta, rr1, rr2: integrand(i, j, theta, rr1, rr2),
                  [[0, np.pi], [0, np.inf], [0, np.inf]]) for i in range(0, 4) for j in range(0, 4)]


hat = EigenvalueProblem(alpha, trial)
print hat.hartree_integrand(trial)

在数学上我想要计算的是thisintegrand函数),其中包含参数here。但是,计算此积分需要花费几个小时。我想知道有什么方法可以加快它吗?非常感谢你!

1 个答案:

答案 0 :(得分:2)

你应该首先扩展r 1 和r 2 的积分限制,从-Infinity到+ Infinity - 扩展限制,乘以1/2 * 1 / 2等

其次,切换到使用Gauss-Hermite quadrature,这非常适合将函数与e -x 2 内核集成。

适当的代码在NumPy中,请参阅其中的参考资料