Use of scipy sparse in ode solver
<p>I am trying to solve a differential equation system </p>

<p>xÂ´=Ax with x(0) = f(x)</p>

<p>in python, where A indeed is a complex sparse matrix.</p>

<p>For now i have been solving the system using the scipy.integrate.complex_ode class in the following way.</p>

<pre><code>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)
</code></pre>

<p>This has been working OK, but I need to "tell the solver" that my matrix is sparse. I figured out that i should use</p>

<pre><code>Asparse = sparse.lil_matrix(A)
</code></pre>

<p>but how do i change my solver to work with this?</p>

<p>How large and sparse is <code>A</code>?</p>

<p>It looks like <code>A</code> is just a constant in this function:</p>

<pre><code>def to_solver_function(time,vector):
    sendoff = np.dot(A, np.transpose(vector))
    return sendoff
</code></pre>

<p>Is <code>vector</code> 1d?  Then <code>np.transpose(vector)</code> does nothing.</p>

<p>For calculation purposes you want</p>

<pre><code>Asparse = sparse.csr_matrix(A)
</code></pre>

<p>Does <code>np.dot(Asparse, vector)</code> work? <code>np.dot</code> is supposed to be sparse aware.  If not,  try <code>Asparse*vector</code>. This probably produces a dense matrix, so you may need <code>(Asparse*vector).A1</code> to produce a 1d array.</p>

<p>But check the timings.  <code>Asparse</code> needs to quite large and very sparse to perform faster than <code>A</code> in a dot product.</p>

python scipy sparse-matrix scientific-computing