对于一般的方阵求逆可以直接用 from numpy.linalg import inv
中的 inv
求解.
而相应的, 对于 csr_matrix 格式的稀疏矩阵求逆, 可以用 from scipy.sparse.linalg import inv
中的 inv
求解.
但是需要注意的是,
There is a sparse linalg
inv
, but it is justspsolve(A,I)
. It also warns that If the inverse ofA
is expected to be non-sparse, it will likely be faster to convertA
to dense and usescipy.linalg.inv
. The same warning likely applies to thepinv
or equivalents.
ref: python how to inverse a sparse matrix
所以, 一般的话, 可以直接用 np.linalg.inv(A.todense())
来求 A
的逆.