MATLAB 中最有效的矩阵求逆

发布于 2024-10-17 09:02:32 字数 216 浏览 8 评论 0原文

当在 MATLAB 中计算某个方阵 A 的逆时,使用

Ai = inv(A)
% should be the same as:
Ai = A^-1

MATLAB 通常会通知我这不是最有效的求逆方法。 那么什么是更有效率的呢?如果我有一个方程系统,可能会使用 /,\ 运算符。 但有时我需要逆数来进行其他计算。

最有效的反转方式是什么?

When computing the inverse for some square matrix A in MATLAB, using

Ai = inv(A)
% should be the same as:
Ai = A^-1

MATLAB usually notifies me that this is not the most efficient way of inverting.
So what's more efficient? If I have an equation system, using the /,\ operators probably is.
But sometimes I need the inverse for other computations.

What's the most efficient way to invert?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

深空失忆 2024-10-24 09:02:32

我建议使用 svd (除非你真的绝对确定你的矩阵没有病态)。然后,根据奇异值,您决定采取进一步的行动。这听起来像是一种“杀伤力过大”的方法,但从长远来看,它会带来回报。

现在,如果您的矩阵 A 实际上是可逆的,那么 A 的伪逆矩阵与 inv(A) 重合,然而,如果您接近“奇点”,您将很容易做出适当的决定,如何继续实际进行伪逆。当然,这些决定将取决于您的应用。

添加一个简单的示例:

> A= randn(3, 2); A= [A A(:, 1)+ A(:, 2)]
A =
  -1.520342  -0.239380  -1.759722
   0.022604   0.381374   0.403978
   0.852420   1.521925   2.374346

> inv(A)
warning: inverse: matrix singular to machine precision, rcond = 0
ans =
   Inf   Inf   Inf
   Inf   Inf   Inf
   Inf   Inf   Inf

> [U, S, V]= svd(A)
U =
  -0.59828  -0.79038   0.13178
   0.13271  -0.25993  -0.95646
   0.79022  -0.55474   0.26040

S =
Diagonal Matrix
  3.6555e+000            0            0
            0  1.0452e+000            0
            0            0  1.4645e-016

V =
   0.433921   0.691650   0.577350
   0.382026  -0.721611   0.577350
   0.815947  -0.029962  -0.577350

> s= diag(S); k= sum(s> 1e-9) % simple thresholding based decision
k =  2

> Ainv= (U(:, 1: k)* diag(1./ s(1: k))* V(:, 1: k)')'
Ainv =
  -0.594055  -0.156258  -0.273302
   0.483170   0.193333   0.465592
  -0.110885   0.037074   0.192290

> A* Ainv
ans =
   0.982633   0.126045  -0.034317
   0.126045   0.085177   0.249068
  -0.034317   0.249068   0.932189

> A* pinv(A)
ans =
   0.982633   0.126045  -0.034317
   0.126045   0.085177   0.249068
  -0.034317   0.249068   0.932189

I would recommend to use svd (unless you are really absolute sure that your matrix is not ill-conditioned). Then, based on singular values you make your decisions on further actions to take. This may sound like a 'overkill' approach, but in long run it will pay back.

Now if your matrix A is actually invertible, then the pseudo inverse of A coincidence with inv(A), however if you are close to 'singularity' you'll easily make appropriate decision how to proceed to actually make the pseudo inverse. Naturally these decisions will depend on your application.

Added a straightforward example:

> A= randn(3, 2); A= [A A(:, 1)+ A(:, 2)]
A =
  -1.520342  -0.239380  -1.759722
   0.022604   0.381374   0.403978
   0.852420   1.521925   2.374346

> inv(A)
warning: inverse: matrix singular to machine precision, rcond = 0
ans =
   Inf   Inf   Inf
   Inf   Inf   Inf
   Inf   Inf   Inf

> [U, S, V]= svd(A)
U =
  -0.59828  -0.79038   0.13178
   0.13271  -0.25993  -0.95646
   0.79022  -0.55474   0.26040

S =
Diagonal Matrix
  3.6555e+000            0            0
            0  1.0452e+000            0
            0            0  1.4645e-016

V =
   0.433921   0.691650   0.577350
   0.382026  -0.721611   0.577350
   0.815947  -0.029962  -0.577350

> s= diag(S); k= sum(s> 1e-9) % simple thresholding based decision
k =  2

> Ainv= (U(:, 1: k)* diag(1./ s(1: k))* V(:, 1: k)')'
Ainv =
  -0.594055  -0.156258  -0.273302
   0.483170   0.193333   0.465592
  -0.110885   0.037074   0.192290

> A* Ainv
ans =
   0.982633   0.126045  -0.034317
   0.126045   0.085177   0.249068
  -0.034317   0.249068   0.932189

> A* pinv(A)
ans =
   0.982633   0.126045  -0.034317
   0.126045   0.085177   0.249068
  -0.034317   0.249068   0.932189
漫漫岁月 2024-10-24 09:02:32

我认为 LU 分解比反演更有效(如果使用旋转,则可能更稳定)。如果您需要求解多个右侧向量,它尤其有效,因为一旦您进行了 LU 分解,您就可以根据需要对每个向量进行前向反向替换。

我会推荐 LU 分解而不是完全逆分解。如果 MATLAB 就是这么说的,我同意。

更新:3x3 矩阵?如果需要,您可以手动将其以封闭形式反转。只需首先检查行列式以确保它不是奇异的或接近奇异的。

I think LU decomposition is more efficient than than inversion (and potentially more stable if you use pivoting). It works especially well if you need to solve for more than one right hand side vector, because once you have the LU decomposition you can do the forward back substitutions for each one as you need it.

I would recommend LU decomposition over a full inverse. I agree if that's what MATLAB is saying.

UPDATE: 3x3 matrix? You can invert that by hand in closed form if you need it. Just check the determinant first to make sure that it's not singular or nearly singular.

横笛休吹塞上声 2024-10-24 09:02:32

如果你只需要逆,那么就这样做,它在数值上比 inv(A) 更稳定:

inv_A = 1\A;

If you only need the inverse then just do, it will be numerically more stable than inv(A):

inv_A = 1\A;
枕头说它不想醒 2024-10-24 09:02:32

如果没有一种巧妙的方法来完成所有计算而不显式形成逆,那么您必须使用“inv”函数。您当然可以用矩阵和单位矩阵求解线性系统,但这样做没有任何好处。

If there isn't a clever way to do all your calculations without explicitly forming the inverse then you have to use the "inv" function. You could of course solve a linear system with your matrix and the identity matrix, but there is nothing to be gained by doing that.

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