Matlab中有没有快速求逆矩阵的方法?

发布于 2024-11-17 16:35:17 字数 385 浏览 3 评论 0原文

我有很多大型(大约 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 技术交流群。

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

发布评论

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

评论(3

调妓 2024-11-24 16:35:17

我实际上需要逆,所以我不能使用 mldivide 代替,...

这不是真的,因为您仍然可以使用 mldivide 来获得逆数。请注意,A-1 = A-1 * I。在 MATLAB 中,这相当于

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

在我的机器上,对于 5000x5000 矩阵,这大约需要 10.5 秒。请注意,MATLAB 确实有一个 inv 函数计算矩阵的逆。尽管这将花费大约相同的时间,但在数值准确性方面效率较低(链接中的更多信息)。


首先,它们的行列式是 1,所以它们肯定是可逆的

而不是 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 的方法

MATLAB 使用高斯消去法来计算一般矩阵(满秩、非稀疏、没有任何特殊属性)的逆,使用 mldivide 这就是 θ (n3),其中 n 是矩阵的大小。因此,在您的情况下,n=5000 并且有 1.25 x 1011 浮点运算。因此,在一台具有大约 10 Gflops 计算能力的合理机器上,您将需要至少 12.5 秒来计算逆,并且没有办法解决这个问题,除非您利用“特殊属性”(如果它们是可利用的) )

I actually need the inverse, so I can't use mldivide instead,...

That's not true, because you can still use mldivide to get the inverse. Note that A-1 = A-1 * I. In MATLAB, this is equivalent to

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

On my machine, this takes about 10.5 seconds for a 5000x5000 matrix. Note that MATLAB does have an inv 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).


First off, their determinant is 1 so they're definitely invertible

Rather than det(A)=1, it is the condition number of your matrix that dictates how accurate or stable the inverse will be. Note that 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. However, as M → ∞, cond(A) = M2 → ∞ and λn → 0, meaning your matrix is approaching singularity and there will be large numerical errors in computing the inverse.


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 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), where n is the size of the matrix. So, in your case, n=5000 and there are 1.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)

旧梦荧光笔 2024-11-24 16:35:17

无论您使用哪种语言,反转任意 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

心舞飞扬 2024-11-24 16:35:17

首先假设特征值均为1。令 A 为矩阵的乔丹规范形式。然后,您可以仅使用矩阵乘法和加法计算 A^{-1},其中

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

k <暗淡(A)。为什么这有效?因为生成函数非常棒。回想一下展开式

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

这意味着我们可以使用无限和来反转(1-x)。您想要反转矩阵 A,因此您想要求解

A = I - X

X 得到 X = IA。因此,通过替换,我们得到

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

这里我刚刚使用单位矩阵 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 0 0
0 B 0
0 0 C

其中每个块沿对角线都有一个值。如果该值为 Aa,则使用上述技巧反转 1/a * A,然后乘以 a 回来。由于完整的矩阵是块三角形,因此逆矩阵将是

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

具有三个块没有什么特别的,所以无论您有多少个块,这都有效。

请注意,只要您有 Jordan 形式的矩阵,此技巧就有效。在这种情况下,Matlab 中的逆计算会非常快,因为它只涉及矩阵乘法,而且您甚至可以使用技巧来加快计算速度,因为您只需要单个矩阵的幂。但是,如果将矩阵转换为 Jordan 形式的成本确实很高,那么这可能对您没有帮助。

First suppose the eigen values are all 1. Let A be the Jordan canonical form of your matrix. Then you can compute A^{-1} using only matrix multiplication and addition by

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

where 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. You want to invert a matrix A, so you want to take

A = I - X

Solving for X gives X = I-A. 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. 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 1s on the diagonal. Therefore I-A is upper triangular with all 0s on the diagonal. Therefore all eigen values of I-A are 0, so its characteristic polynomial is x^dim(A) and its minimal polynomial is x^{k+1} for some k < 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.:

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. 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. 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.

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