用于逆矩阵和稀疏矩阵乘积的高效 numpy / lapack 例程?
我有一个方形且密集的矩阵 B 和一个矩形且稀疏的矩阵 A 。
有没有一种方法可以有效地计算乘积B^-1 * A
?
到目前为止,我使用(在 numpy 中)
tmp = B.inv()
return tmp * A
,我相信这使我们能够利用 A 的稀疏性。我正在考虑使用稀疏方法 numpy.sparse.linalg.spsolve
,但这要求 B 而不是 A 是稀疏的。
还有其他方法可以加快速度吗?
I have a matrix B that is square and dense, and a matrix A that is rectangular and sparse.
Is there a way to efficiently compute the product B^-1 * A
?
So far, I use (in numpy)
tmp = B.inv()
return tmp * A
which, I believe, makes us of A's sparsity. I was thinking about using the sparse methodnumpy.sparse.linalg.spsolve
, but this requires B, and not A, to be sparse.
Is there another way to speed things up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于要求逆的矩阵是稠密的,所以
spsolve
不是您想要的工具。此外,计算矩阵的逆并将其乘以另一个矩阵是不好的数值实践 - 使用 LU分解,scipy支持。另一点是,除非您使用矩阵类(我认为 ndarray 类更好,这是一个品味问题),否则您需要使用
dot
而不是乘法运算符。而如果想要高效地将稀疏矩阵与稠密矩阵相乘,则需要使用稀疏矩阵的dot
方法。不幸的是,这仅在第一个矩阵稀疏时才有效,因此您需要使用 Anycorn 建议采用转置来交换操作顺序的技巧。这是一个惰性实现,它不使用 LU 分解,但在其他方面应该是高效的:
正确使用 LU 分解涉及找到一种有效地将三角矩阵乘以稀疏矩阵的方法,目前我还没有做到这一点。
Since the matrix to be inverted is dense,
spsolve
is not the tool you want. In addition, it is bad numerical practice to calculate the inverse of a matrix and multiply it by another - you are much better off using LU decomposition, which is supported by scipy.Another point is that unless you are using the matrix class (I think that the ndarray class is better, this is something of a question of taste), you need to use
dot
instead of the multiplication operator. And if you want to efficiently multiply a sparse matrix by a dense matrix, you need to use thedot
method of the sparse matrix. Unfortunately this only works if the first matrix is sparse, so you need to use the trick which Anycorn suggested of taking the transpose to swap the order of operations.Here is a lazy implementation which doesn't use the LU decomposition, but which should otherwise be efficient:
Doing it properly with the LU decomposition involves finding a way to efficiently multiply a triangular matrix by a sparse matrix, which currently eludes me.