1
我有一個非常複雜的積分計算:SciPy的:方法,以加快複雜的整體
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)
數學我想計算是什麼樣的this(這是integrand
功能),與paremeters here。但是,計算這個積分需要幾個小時的時間。我不知道有什麼方法可以加速嗎?非常感謝你!
因爲一開始我會嘗試重寫'hartree_potential'。這是一個雙循環,生成16個項,然後求和到1.我敢打賭,可以不用循環完成,使用所有'alpha'和'coeff'進行數組計算。 – hpaulj
爲了清楚起見,我將'nquad'中複雜的'lamda'作爲函數重寫。很難看到雙循環中正在評估什麼。我想這是'nquad'。 – hpaulj