Matlab中有没有快速求逆矩阵的方法?
我有很多大型(大约 5000 x 5000)矩阵,需要在 Matlab 中进行求逆。我实际上需要逆,所以我不能使用 mldivide 来代替,这对于仅求解一个 b 的 Ax=b 来说要快得多。
我的矩阵来自一个问题,这意味着它们有一些很好的属性。首先,它们的行列式是 1,所以它们肯定是可逆的。不过,它们不可对角化,否则我会尝试对角化它们,反转它们,然后将它们放回去。他们的条目都是实数(实际上有理数)。
我正在使用 Matlab 来获取这些矩阵,并且对于这些东西,我需要对其进行逆运算,所以我更喜欢一种加快 Matlab 速度的方法。但如果我可以使用另一种语言,速度会更快,请告诉我。我不了解很多其他语言(一点点 C 和一点点 Java),所以如果其他语言真的很复杂,那么我可能无法使用它。不过,请继续提出建议,以防万一。
I have lots of large (around 5000 x 5000) matrices that I need to invert in Matlab. I actually need the inverse, so I can't use mldivide instead, which is a lot faster for solving Ax=b for just one b.
My matrices are coming from a problem that means they have some nice properties. First off, their determinant is 1 so they're definitely invertible. They aren't diagonalizable, though, or I would try to diagonlize them, invert them, and then put them back. Their entries are all real numbers (actually rational).
I'm using Matlab for getting these matrices and for this stuff I need to do with their inverses, so I would prefer a way to speed Matlab up. But if there is another language I can use that'll be faster, then please let me know. I don't know a lot of other languages (a little but of C and a little but of Java), so if it's really complicated in some other language, then I might not be able to use it. Please go ahead and suggest it, though, in case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是真的,因为您仍然可以使用
mldivide
来获得逆数。请注意,A-1 = A-1 * I
。在 MATLAB 中,这相当于在我的机器上,对于
5000x5000
矩阵,这大约需要 10.5 秒。请注意,MATLAB 确实有一个inv
函数计算矩阵的逆。尽管这将花费大约相同的时间,但在数值准确性方面效率较低(链接中的更多信息)。而不是
det(A)=1
,而是 矩阵的条件数,它决定了逆矩阵的精确度或稳定性。请注意,det(A)=∏i=1:n λi
。因此只需设置λ1=M
、λn=1/M
和λ i≠1,n=1
将为您提供det(A)=1
。但是,如M → ∞
、cond(A) = M2 → ∞
和λn → 0
,意味着你的矩阵正在接近奇点,并且在计算逆矩阵时会出现很大的数值误差。当然,如果您的矩阵稀疏或具有其他有利的属性,则可以采用其他更有效的算法。但如果没有关于您的具体问题的任何其他信息,就没有什么可说的了。
MATLAB 使用高斯消去法来计算一般矩阵(满秩、非稀疏、没有任何特殊属性)的逆,使用
mldivide
这就是θ (n3)
,其中n
是矩阵的大小。因此,在您的情况下,n=5000
并且有1.25 x 1011
浮点运算。因此,在一台具有大约 10 Gflops 计算能力的合理机器上,您将需要至少 12.5 秒来计算逆,并且没有办法解决这个问题,除非您利用“特殊属性”(如果它们是可利用的) )That's not true, because you can still use
mldivide
to get the inverse. Note thatA-1 = A-1 * I
. In MATLAB, this is equivalent toOn my machine, this takes about 10.5 seconds for a
5000x5000
matrix. Note that MATLAB does have aninv
function to compute the inverse of a matrix. Although this will take about the same amount of time, it is less efficient in terms of numerical accuracy (more info in the link).Rather than
det(A)=1
, it is the condition number of your matrix that dictates how accurate or stable the inverse will be. Note thatdet(A)=∏i=1:n λi
. So just settingλ1=M
,λn=1/M
andλi≠1,n=1
will give youdet(A)=1
. However, asM → ∞
,cond(A) = M2 → ∞
andλn → 0
, meaning your matrix is approaching singularity and there will be large numerical errors in computing the inverse.Of course, there are other more efficient algorithms that can be employed if your matrix is sparse or has other favorable properties. But without any additional info on your specific problem, there is nothing more that can be said.
MATLAB uses Gauss elimination to compute the inverse of a general matrix (full rank, non-sparse, without any special properties) using
mldivide
and this isΘ(n3)
, wheren
is the size of the matrix. So, in your case,n=5000
and there are1.25 x 1011
floating point operations. So on a reasonable machine with about 10 Gflops of computational power, you're going to require at least 12.5 seconds to compute the inverse and there is no way out of this, unless you exploit the "special properties" (if they're exploitable)无论您使用哪种语言,反转任意 5000 x 5000 矩阵在计算上都不容易。我建议研究近似值。如果您的矩阵是低秩的,您可能想尝试低秩近似 M = USV'
以下是 math-overflow 的更多想法:
https://mathoverflow.net/search?q=matrix+inversion+approximation
Inverting an arbitrary 5000 x 5000 matrix is not computationally easy no matter what language you are using. I would recommend looking into approximations. If your matrices are low rank, you might want to try a low-rank approximation M = USV'
Here are some more ideas from math-overflow:
https://mathoverflow.net/search?q=matrix+inversion+approximation
首先假设特征值均为
1
。令A
为矩阵的乔丹规范形式。然后,您可以仅使用矩阵乘法和加法计算A^{-1}
,其中k <暗淡(A)
。为什么这有效?因为生成函数非常棒。回想一下展开式这意味着我们可以使用无限和来反转
(1-x)
。您想要反转矩阵A
,因此您想要求解X
得到X = IA
。因此,通过替换,我们得到这里我刚刚使用单位矩阵
I
代替数字1
。现在我们需要处理收敛问题,但这实际上并不是问题。假设A
是Jordan形式并且所有特征值等于1
,我们知道A
是上三角且所有对角线上>1
。因此,IA
是上三角形,所有0
都在对角线上。因此IA
的所有特征值为0
,因此其特征多项式为x^dim(A)
,其最小多项式为x ^{k+1}
对于某些k <暗淡(A)
。由于矩阵满足其最小(和特征)多项式,这意味着(IA)^{k+1} = 0
。因此,上述级数是有限,最大的非零项是(IA)^k
。所以它收敛了。现在,对于一般情况,将矩阵放入 Jordan 形式,这样您就有一个块三角矩阵,例如:
其中每个块沿对角线都有一个值。如果该值为
A
的a
,则使用上述技巧反转1/a * A
,然后乘以a
回来。由于完整的矩阵是块三角形,因此逆矩阵将是具有三个块没有什么特别的,所以无论您有多少个块,这都有效。
请注意,只要您有 Jordan 形式的矩阵,此技巧就有效。在这种情况下,Matlab 中的逆计算会非常快,因为它只涉及矩阵乘法,而且您甚至可以使用技巧来加快计算速度,因为您只需要单个矩阵的幂。但是,如果将矩阵转换为 Jordan 形式的成本确实很高,那么这可能对您没有帮助。
First suppose the eigen values are all
1
. LetA
be the Jordan canonical form of your matrix. Then you can computeA^{-1}
using only matrix multiplication and addition bywhere
k < dim(A)
. Why does this work? Because generating functions are awesome. Recall the expansionThis means that we can invert
(1-x)
using an infinite sum. You want to invert a matrixA
, so you want to takeSolving for
X
givesX = I-A
. Therefore by substitution, we haveHere I've just used the identity matrix
I
in place of the number1
. Now we have the problem of convergence to deal with, but this isn't actually a problem. By the assumption thatA
is in Jordan form and has all eigen values equal to1
, we know thatA
is upper triangular with all1
s on the diagonal. ThereforeI-A
is upper triangular with all0
s on the diagonal. Therefore all eigen values ofI-A
are0
, so its characteristic polynomial isx^dim(A)
and its minimal polynomial isx^{k+1}
for somek < dim(A)
. Since a matrix satisfies its minimal (and characteristic) polynomial, this means that(I-A)^{k+1} = 0
. Therefore the above series is finite, with the largest nonzero term being(I-A)^k
. So it converges.Now, for the general case, put your matrix into Jordan form, so that you have a block triangular matrix, e.g.:
Where each block has a single value along the diagonal. If that value is
a
forA
, then use the above trick to invert1/a * A
, and then multiply thea
back through. Since the full matrix is block triangular the inverse will beThere is nothing special about having three blocks, so this works no matter how many you have.
Note that this trick works whenever you have a matrix in Jordan form. The computation of the inverse in this case will be very fast in Matlab because it only involves matrix multiplication, and you can even use tricks to speed that up since you only need powers of a single matrix. This may not help you, though, if it's really costly to get the matrix into Jordan form.