Python 矩阵与稀疏矩阵逆的左乘
我正在尝试计算 K = P*CT*S^-1 形式的表达式(卡尔曼滤波器的实现)
所有涉及的矩阵都是稀疏的,我当然希望避免计算实际的逆矩阵。
我尝试使用
import scipy.sparse.linalg as spln
self.K = self.P.dot(spln.spsolve(S.T, C).T)
问题是 spsolve 期望它的第二个参数是向量而不是矩阵。
编辑: 澄清一下,这个问题可以在 Matlab 中通过 K = P * (C / S) 来解决,所以我正在寻找一种类似于 spsolve 的方法,但它可以接受矩阵作为其第二个参数。这当然可以通过将 C 拆分为多个列向量 c1..cn 并解决每个列向量的问题,然后将它们重新组装成一个矩阵来完成,但我怀疑这样做既麻烦又低效。
编辑2和3: 矩阵的尺寸通常约为 P~10⁶x10^6、S~100x100、C=100x10⁶。 P 对角线和 S 对称,C 每行只有一个元素。 它将用于使用稀疏矩阵实现卡尔曼滤波器,请参阅
http://en.wikipedia .org/wiki/Kalman_filter#The_Kalman_filter
I'm trying to calculate an expression of the form K = P*C.T*S^-1 (implementation of a Kalman filter)
All the involved matrices are sparse, and I would of course like to avoid calculating the actual inverse.
I tried using
import scipy.sparse.linalg as spln
self.K = self.P.dot(spln.spsolve(S.T, C).T)
The problem is that spsolve expects it's second argument to be a vector and not a matrix.
edit:
Clarification, the problem could in Matlab be solved by K = P * (C / S), so what I'm looking for is a method similar to spsolve but which can accept a matrix as its second argument. This could of course be done by splitting C into a number of column vectors c1..cn and solving the problem for each of them and then reassembling them into a matrix, but I suspect doing that will be both cumbersome and inefficient.
edit2&3:
The dimensions of the matrices will typically be around P~10⁶x10^6, S~100x100, C=100x10⁶. P diagonal and S symmetric and C will only have one element per row.
It will be used for an implementation of a Kalman filter using sparse matrices, see
http://en.wikipedia.org/wiki/Kalman_filter#The_Kalman_filter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为解决方法可以做到
但是,是的,这是
spsolve
不接受矩阵的错误。然而,由于您的
S
不是很大,您也可以使用密集逆。As a workaround can do
But yes, it's a bug that
spsolve
does not accept matrices.However, as your
S
is not very large, you can as well use a dense inverse.