dmon
dmon

Reputation: 1422

Scipy sparse dia_matrix solver

In a scipy program I'm creating a dia_matrix (sparse matrix type) with 5 diagonals. The centre diagonal the +1 & -1 diagonals and the +4 & -4 diagonals (usually >> 4, but the principle is the same), i.e. I have a typical PDE system matrix of the form:

[ a0  b0  0   0   0   d0  0   0   0  ... 0.0 ]
[ c1  a1  b1  0   0   0   d1  0   0  ... 0.0 ]
[ 0   c2  a2  b2  0   0   0   d2  0  ... 0.0 ]
[ 0   0   c3  a3  b3  0   0   0   d3 ... 0.0 ]
[ 0   0   0   c4  a4  b4  0   0   0  ... 0.0 ]
[ e5  0   0   0   c5  a5  b5  0   0  ... 0.0 ]
[ :   :   :   :   :   :   :   :   :   :  :   ]
[ 0   0   0   0   0   0   0   0   0  ... aN  ]

When I use scipy.linalg.dsolve.spsolve() to solve the matrix equation it works but I get the following reported back to me

>>> SparseEfficiencyWarning: spsolve requires CSC or CSR matrix format
    warn('spsolve requires CSC or CSR matrix format', SparseEfficiencyWarning)

If spsolve() is not efficient for solving the sparse matrix type dia_matrix's then what should I be using?

Upvotes: 4

Views: 6740

Answers (2)

Emmet
Emmet

Reputation: 6421

I'm a bit late with this answer, but I hope you found that adding:

from scipy.linalg import solve_banded

Allows you to use a DIA matrix rather than having to resort to CSR or CSC.

Upvotes: 2

John Vinyard
John Vinyard

Reputation: 13515

The warning says it all, I think. Looks like it wants you to use a csr_matrix or a csc_matrix.

I'm assuming you're creating your matrix with scipy.sparse.diags. You should just be able to use format = 'csr' or format = 'csc' when you construct the matrix.

Upvotes: 2

Related Questions