在颂求解算器

时间:2016-09-28 09:31:09

标签: python scipy sparse-matrix scientific-computing

我正在尝试解决微分方程系统

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)

但是如何更改我的求解器以使用它?

1 个答案:

答案 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执行速度更快。