Scipy sparse dia_matrix solver

我只是一个虾纸丫 提交于 2019-12-08 01:32:35

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12978518/scipy-sparse-dia-matrix-solver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!