Python - Multiply sparse matrix row with non-sparse vector by index

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 01:54:50

问题


My current software project involves implementing a numeric method for solving linear systems with python (the so called SOR-Method). It basically works like this:

Given a linear system:

ax1 + bx2 + cx3 = b1
dx1 + ex2 + fx3 = b2
gx1 + hx2 + ix3 = b3

Re-arrange the equations to:

x1_new = (b1 - (bx2 + cx3)) / a
x2_new = (b2 - (dx1_new + fx3)) / e
x3_new = (b3 - (gx1_new + hx2_new)) / i

As you can see, the x*_new values are updating with each line - that's the unfortunate reason why I can't use simple vector-vector prodcuts (I also have to divide by the diagonal element - not multiply it). I have to go element by element.

I would like to work with sparse-Matrices. But I didn't find a way yet how to index a sparse matrix and how to perform the operation described above? Like, the only thing I found (regarding this https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html) is to use A[i].data which returns the non-zero elements of the row i. But there can be a lot of zeros in a row. And also working with a lot of if/else to get the element at the index is not very efficient with a 100.000 x 100.000 matrix.

Any ideas?

来源:https://stackoverflow.com/questions/56173520/python-multiply-sparse-matrix-row-with-non-sparse-vector-by-index

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