我正在尝试解决微分方程系统
x'=具有x(0)= f(x)
的Ax在python中,其中A确实是一个复杂的稀疏矩阵。
目前我一直在使用scipy.integrate.complex_ode类以下列方式解决系统问题。
def to_solver_function(time,vector):
sendoff = np.dot(A, np.transpose(vector))
return sendoff
solver = complex_ode(to_solver_function)
solver.set_initial_value(f(x),0)
solution = [f(x)]
for time in time_grid:
next = solver.integrate(time)
solution.append(next)
这一直运作正常,但我需要“告诉解算器”我的矩阵很稀疏。我发现我应该使用
Asparse = sparse.lil_matrix(A)
但是如何更改我的求解器以使用它?
答案 0 :(得分:1)
A
有多大和稀疏?
看起来A
只是此函数中的常量:
def to_solver_function(time,vector):
sendoff = np.dot(A, np.transpose(vector))
return sendoff
vector
是1d吗?然后np.transpose(vector)
什么也没做。
出于计算目的,您需要
Asparse = sparse.csr_matrix(A)
np.dot(Asparse, vector)
是否有效? np.dot
应该是稀疏的。如果没有,请尝试Asparse*vector
。这可能会产生一个密集的矩阵,因此您可能需要(Asparse*vector).A1
来生成1d数组。
但检查时间。 Asparse
需要相当大且非常稀疏才能比点积中的A
执行速度更快。