问题
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