简体   繁体   English

有没有一种快速的方法来反转 Matlab 中的矩阵?

[英]Is there a fast way to invert a matrix in Matlab?

I have lots of large (around 5000 x 5000) matrices that I need to invert in Matlab.我有很多大型(大约 5000 x 5000)矩阵需要在 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.我实际上需要倒数,所以我不能使用 mldivide 来代替,这对于仅解决一个 b 的 Ax=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.首先,它们的行列式是 1,所以它们绝对是可逆的。 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.我正在使用 Matlab 来获取这些矩阵,为此我需要处理它们的逆矩阵,所以我更喜欢一种加快 Matlab 的方法。 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.我不懂很多其他语言(C 一点点,Java 一点点),所以如果它在其他语言中真的很复杂,那么我可能无法使用它。 Please go ahead and suggest it, though, in case.请提前 go 并提出建议,以防万一。

I actually need the inverse, so I can't use mldivide instead,...我实际上需要逆,所以我不能使用 mldivide 代替,...

That's not true, because you can still use mldivide to get the inverse.这不是真的,因为您仍然可以使用mldivide得到逆。 Note that A -1 = A -1 * I .请注意A -1 = A -1 * I In MATLAB, this is equivalent to在 MATLAB 中,这相当于

invA = A\speye(size(A));

On my machine, this takes about 10.5 seconds for a 5000x5000 matrix.在我的机器上, 5000x5000矩阵大约需要 10.5 秒。 Note that MATLAB does have an inv function to compute the inverse of a matrix.请注意,MATLAB 确实有一个inv function 来计算矩阵的逆。 Although this will take about the same amount of time, it is less efficient in terms of numerical accuracy (more info in the link).尽管这将花费大约相同的时间,但在数值准确性方面效率较低(链接中的更多信息)。


First off, their determinant is 1 so they're definitely invertible首先,它们的行列式是 1,所以它们绝对是可逆的

Rather than det(A)=1 , it is the condition number of your matrix that dictates how accurate or stable the inverse will be.而不是det(A)=1 ,它是矩阵的条件数,它决定了逆矩阵的准确度或稳定性。 Note that det(A)=∏ i=1:n λ i .注意det(A)=∏ i=1:n λ i So just setting λ 1 =M , λ n =1/M and λ i≠1,n =1 will give you det(A)=1 .因此,只需设置λ 1 =Mλ n =1/Mλ i≠1,n =1即可得到det(A)=1 However, as M → ∞ , cond(A) = M 2 → ∞ and λ n → 0 , meaning your matrix is approaching singularity and there will be large numerical errors in computing the inverse.但是,当M → ∞cond(A) = M 2 → ∞λ n → 0时,这意味着您的矩阵正在接近奇点,并且在计算逆时会有很大的数值误差。


My matrices are coming from a problem that means they have some nice properties.我的矩阵来自一个问题,这意味着它们具有一些不错的属性。

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.但是,如果没有关于您的具体问题的任何其他信息,就没有什么可以说的了。


I would prefer a way to speed Matlab up我更喜欢加快 Matlab 速度的方法

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 Θ(n 3 ) , where n is the size of the matrix. MATLAB 使用高斯消元法使用mldivide计算一般矩阵(满秩、非稀疏、没有任何特殊属性)的逆矩阵,这是Θ(n 3 ) ,其中n是矩阵的大小。 So, in your case, n=5000 and there are 1.25 x 10 11 floating point operations.因此,在您的情况下, n=5000并且有1.25 x 10 11浮点运算。 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)因此,在一台具有大约 10 Gflops 计算能力的合理机器上,您将需要至少 12.5 秒来计算逆,并且没有办法解决这个问题,除非您利用“特殊属性”(如果它们是可利用的) )

Inverting an arbitrary 5000 x 5000 matrix is not computationally easy no matter what language you are using.无论您使用什么语言,反转任意 5000 x 5000 矩阵在计算上都不容易。 I would recommend looking into approximations.我建议研究近似值。 If your matrices are low rank, you might want to try a low-rank approximation M = USV'如果你的矩阵是低秩的,你可能想尝试一个低秩近似 M = USV'

Here are some more ideas from math-overflow:以下是数学溢出的更多想法:

https://mathoverflow.net/search?q=matrix+inversion+approximation https://mathoverflow.net/search?q=matrix+inversion+approximation

First suppose the eigen values are all 1 .首先假设特征值都是1 Let A be the Jordan canonical form of your matrix.A为矩阵的 Jordan 规范形式。 Then you can compute A^{-1} using only matrix multiplication and addition by然后您可以仅使用矩阵乘法和加法计算A^{-1}

A^{-1} = I + (I-A) + (I-A)^2 + ... + (I-A)^k

where k < dim(A) .其中k < dim(A) Why does this work?为什么这行得通? Because generating functions are awesome.因为生成函数很棒。 Recall the expansion回忆一下展开

(1-x)^{-1} = 1/(1-x) = 1 + x + x^2 + ...

This means that we can invert (1-x) using an infinite sum.这意味着我们可以使用无限和来反转(1-x) You want to invert a matrix A , so you want to take你想反转一个矩阵A ,所以你想采取

A = I - X

Solving for X gives X = IA .求解X得到X = IA Therefore by substitution, we have因此,通过替换,我们有

A^{-1} = (I - (I-A))^{-1} = 1 + (I-A) + (I-A)^2 + ...

Here I've just used the identity matrix I in place of the number 1 .在这里,我刚刚使用单位矩阵I代替了数字1 Now we have the problem of convergence to deal with, but this isn't actually a problem.现在我们要处理收敛问题,但这实际上不是问题。 By the assumption that A is in Jordan form and has all eigen values equal to 1 , we know that A is upper triangular with all 1 s on the diagonal.通过假设A是 Jordan 形式并且所有特征值都等于1 ,我们知道A是上三角形,所有1 s 在对角线上。 Therefore IA is upper triangular with all 0 s on the diagonal.因此IA是对角线上所有0的上三角形。 Therefore all eigen values of IA are 0 , so its characteristic polynomial is x^dim(A) and its minimal polynomial is x^{k+1} for some k < dim(A) .因此IA的所有特征值都是0 ,所以它的特征多项式是x^dim(A) ,对于一些k < dim(A) A) ,它的最小多项式是x^{k+1} Since a matrix satisfies its minimal (and characteristic) polynomial, this means that (IA)^{k+1} = 0 .由于矩阵满足其最小(和特征)多项式,这意味着(IA)^{k+1} = 0 Therefore the above series is finite , with the largest nonzero term being (IA)^k .因此,上述级数是有限的,最大的非零项是(IA)^k So it converges.所以它收敛。

Now, for the general case, put your matrix into Jordan form, so that you have a block triangular matrix, eg:现在,对于一般情况,将您的矩阵放入 Jordan 形式,这样您就有了一个块三角矩阵,例如:

A 0 0
0 B 0
0 0 C

Where each block has a single value along the diagonal.每个块在对角线上都有一个值。 If that value is a for A , then use the above trick to invert 1/a * A , and then multiply the a back through.如果该值为a ,则使用上述技巧反转1/a * A A然后将a乘回去。 Since the full matrix is block triangular the inverse will be由于整个矩阵是块三角形,因此逆矩阵为

A^{-1} 0      0
0      B^{-1} 0
0      0      C^{-1}

There 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.请注意,只要您有 Jordan 形式的矩阵,此技巧就可以使用。 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.在这种情况下,在 Matlab 中的逆计算将非常快,因为它只涉及矩阵乘法,您甚至可以使用技巧来加快计算速度,因为您只需要单个矩阵的幂。 This may not help you, though, if it's really costly to get the matrix into Jordan form.但是,如果将矩阵转换为 Jordan 形式的成本确实很高,那么这可能对您没有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM