C++ 是否有一种直接的方法来反转三角形(上或下)矩阵?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/420612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there around a straightforward way to invert a triangular (upper or lower) matrix?
提问by tunnuz
I'm trying to implement some basic linear algebra operations and one of these operations is the inversion of a triangular (upper and/or lower) matrix. Is there an easy and stable algorithm to do that?
我正在尝试实现一些基本的线性代数运算,其中一个运算是三角(上和/或下)矩阵的求逆。有没有一种简单而稳定的算法来做到这一点?
Thank you.
谢谢你。
回答by jason
Yes, use back substitution. A standard algorithm to invert a matrix is to find its LU decomposition (decomposition into a lower-triangular and an upper-triangular matrix), use back subsitution on the triangular pieces, and then combine the results to obtain the inverse of the original matrix.
是的,使用反向替换。矩阵求逆的标准算法是找到它的LU分解(分解成下三角矩阵和上三角矩阵),对三角块使用反代,然后组合结果以获得原始矩阵的逆。
回答by tunnuz
Don't invert it if you can. It's one of the basic commandments of numerical linear algebra.
如果可以,请不要颠倒它。它是数值线性代数的基本戒律之一。
It is much faster and numerically stabler to keep the matrix L itself in memory and compute
将矩阵 L 本身保存在内存中并计算速度更快且数值更稳定
inv(L)b
每当您需要使用 inv(L) 执行其他操作时,都可以使用反向替换。Note that the customary algorithm for inverting it requires solving the systems
请注意,用于反转它的惯用算法需要求解系统
inv(L)[1 0 0 ...],
inv(L)[0 1 0 ....],
inv(L)[0 0 1 ....]
等等,所以你会发现根本不反转它要容易得多。回答by Fanfan
Given a lower triangular matrix L, backsubstitution allows you to solve the system L x = b quickly for any right-hand side b.
给定一个下三角矩阵 L,回代允许您快速求解系统 L x = b 的任何右侧 b。
To invert L, you can solve this system for right-hand sides e1=(1,0,...,0), e2=(0,1,...,0), ..., en=(0,0,...,1) and combine the resulting solution vectors into a single (necessarily lower-triangular) matrix.
要反转 L,您可以求解该系统的右侧 e1=(1,0,...,0), e2=(0,1,...,0), ..., en=(0 ,0,...,1) 并将结果解向量组合成单个(必须是下三角)矩阵。
If you are interested in a closed-form solution, the diagonal elements of the inverse are the inverses of the original diagonal elements, and the formula for the rest of the elements of the inverse gets more and more complicated as you move aways from the diagonal.
如果您对封闭形式的解感兴趣,则逆的对角元素是原始对角元素的逆,并且随着远离对角线,逆的其余元素的公式变得越来越复杂.
回答by Greg Rogers
回答by Charlie Martin
Wow, that's practically half the contents of a numerical analysis course. The standard algorithms will do it, and there is a bunch of canned code here. The ultimate source for this and most other usual numerical analysis problems is Numerical Recipes.
哇,这几乎是数值分析课程内容的一半。标准算法将做到这一点,并有一堆罐头代码在这里。这个问题和大多数其他常见的数值分析问题的最终来源是数值配方。
回答by Guilherme Barros
Being B inverse of A, a triangular matrix, you can use the following MATLAB code:
作为 A 的 B 逆矩阵,一个三角矩阵,您可以使用以下 MATLAB 代码:
n = size(A,1);
B = zeros(n);
for i=1:n
B(i,i) = 1/A(i,i);
for j=1:i-1
s = 0;
for k=j:i-1
s = s + A(i,k)*B(k,j);
end
B(i,j) = -s*B(i,i);
end
end