尝试整合一个作为变量函数的矩阵,但遇到了一个我不知道如何解决的问题

问题描述 投票:0回答:1

我想对矩阵进行积分,作为求解具有时间相关势的薛定谔方程的一部分。

矩阵由4个8x8矩阵和标量函数组成。第一个矩阵描述了代码中表示为 U 的旋转,第二个矩阵描述了作为时间函数的缩放(代码中的 eE),第三个矩阵描述了旋转(代码中的 U_H)。它表示执行变换的特征库基础的变化。第四个矩阵由特征向量和每个特征向量的系数(ket)组成,它应该表示初始猜测的波函数(代码中的 psi_0)。标量函数描述了与时间相关的电势(代码中的 V(t))。

我想将矩阵与最初猜测的波函数积分以计算新的波函数。然后我想取波函数之间差异的范数。我想重新进行矩阵的积分,但使用之前计算的波函数来获得另一个新的波函数并计算新的范数。我想执行这个过程直到差异范数收敛。

为了进行积分,我想到使用 scipy.integrate.quad 来积分矩阵的每个元素。由于矩阵与时间相关,因此我使用 lambda 函数将矩阵转换为变量 t 的函数。然后我想我可以调用矩阵的每个元素来整合它,但似乎不可能做到这一点。这部分代码:

tol = 10**(-5)                                                            #Set the tolerance
angfreq = pi/3                                                            #Set the angular frequency
error = 1                                                                 #Set initial error to get into while-loop
ti = 0                                                                    #Integration limits
tf = 1

c_0 = np.array([np.random.rand(len(eigenvectors[0]))])                    #Initial guess coefficients
psi_0 = norm(c_0*eigenvectors)                                            #Initial wave function guess
psi = np.empty(np.shape(psi_0))                                           #Construct next wave function to be calculated

while tol < error:
    func = lambda t, psi_0: U@eE(t, tf)@U_H@psi_0*V(t, angfreq)             #The composite matrix that is a function of t
    for row in range(len(psi_0[0])):                                        #Get into row
        for column in range(len(psi_0[0])):                                 #Get into column
            psi[row][column] = quad(func[row][column], ti, tf)              #Accesss the specific element (row and column) in psi-vector and change it to a new value, the new value will be the value of the integral of the corresponding element 
    error = np.linalg.norm(psi-psi_0)
    psi_0 = norm(psi)

我收到以下错误:“‘函数’对象不可下标”。我不知道如何解决它。

python numpy scipy numpy-ndarray integral
1个回答
0
投票

这个替代块可能会起作用。

它的效率不会很高,因为对于每个

row, column, t
,都会调用
func
并返回一个数组,只有一个元素被传递到
quad

row, column, psi_0
foo
的全局变量,但
U
U_H
angfreq
func
的全局变量,
eE
V
是外部定义的函数。

    func = lambda t, psi_0: U@eE(t, tf)@U_H@psi_0*V(t, angfreq)  
    for row in range(len(psi_0[0])):                                        
        for column in range(len(psi_0[0])):
            foo = lambda t: func(t, psi_0)[row, column]                              
            psi[row, column] = quad(foo, ti, tf)              
    error = np.linalg.norm(psi-psi_0)
    psi_0 = norm(psi)
© www.soinside.com 2019 - 2024. All rights reserved.