Python 矩阵与稀疏矩阵逆的左乘

发布于 2024-12-09 14:08:15 字数 677 浏览 1 评论 0原文

我正在尝试计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-12-16 14:08:15

作为解决方法可以做到

import numpy as np
from scipy.sparse.linalg import splu

def spsolve2(a, b):
    a_lu = splu(a)
    out = np.empty((A.shape[1], b.shape[1]))
    for j in xrange(b.shape[1]):
        out[:,j] = a_lu.solve(b[:,j])
    return out

self.K = self.P.dot(spsolve2(S.T, C).T)

但是,是的,这是 spsolve 不接受矩阵的错误。

然而,由于您的 S 不是很大,您也可以使用密集逆。

As a workaround can do

import numpy as np
from scipy.sparse.linalg import splu

def spsolve2(a, b):
    a_lu = splu(a)
    out = np.empty((A.shape[1], b.shape[1]))
    for j in xrange(b.shape[1]):
        out[:,j] = a_lu.solve(b[:,j])
    return out

self.K = self.P.dot(spsolve2(S.T, C).T)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文