简体   繁体   English

在 C++ 中更高效、更快速的矩阵求逆方法(大和小)

[英]More efficient and fast way of inverting matrices in c++ (big and small)

EDIT .编辑 Since this question was asked, I got a PhD on solving linear system of equations for tomography.由于这个问题被问到,我获得了解决断层扫描线性方程组的博士学位。 As this question still gets a lot of traffic, I want to stress out the first sentence from the answer by @sellibitze: There is not simple answer .由于这个问题仍然有很多流量,我想强调@sellibitze 回答中的第一句话:没有简单的答案 It highly depends on the nature of the matrix, and almost always you don't wan't to invert the matrix.这在很大程度上取决于矩阵的性质,并且几乎总是您不想反转矩阵。

Now, to the original question by this very innocent man who thinks this can be answered easily...现在,对于这个认为可以轻松回答的非常无辜的人提出的原始问题......


While googleing about matrix inversion algorithms I found that there are several ways (and opinions!) about how to do this in code.在谷歌搜索矩阵求逆算法时,我发现有几种方法(和意见!)关于如何在代码中做到这一点。 I wondered which method is the fastest, or the one with the best performance, and trying to found that answer I found nothing.我想知道哪种方法最快,或者哪种方法性能最好,但在试图找到答案时却一无所获。

I know that for some cases a pseudo-inverse can be computed (using SVD, cholevsky,...), I actually use some of those in my code, and I know that several times an inverse just doesn't exist, etc. It is easy to find an specific answer for an specific problem but not a general intuition for this big (HUGE!) problem that is matrix inversion.我知道在某些情况下可以计算伪逆(使用 SVD、cholevsky 等),我实际上在我的代码中使用了其中的一些,并且我知道有几次逆不存在,等等。很容易找到特定问题的特定答案,但不是矩阵求逆这个大(巨大!)问题的一般直觉。

So my question is:所以我的问题是:

What method is best in performance for small matrices?什么方法对小矩阵的性能最好? And in precision?并且精确? What about big matrices?大矩阵呢?

My personal case is a 6x6 (EDIT:symetric) matrix that have to be inverted thousands of times (yes,yes, with different values) and I need high precision, but for sure speed would come really handy.我的个人情况是一个 6x6(编辑:对称)矩阵,它必须反转数千次(是的,是的,具有不同的值),我需要高精度,但速度肯定会非常方便。

Note that I am not looking for code, I will code myself whatever answer fits most to my case, but I think this is a question that lots of programmers would like to know.请注意,我不是在寻找代码,我会自己编写最适合我情况的答案,但我认为这是许多程序员都想知道的问题。

There is not simple answer.没有简单的答案。 Make sure that you've read and understood this article.请确保您已经阅读并理解文章。

For 2x2 matrices computing the inverse can be done with a simple formular involving the determinant.对于 2x2 矩阵,可以使用包含行列式的简单公式来计算逆矩阵。 But for anything larger I would prefer factorizations, for example a pivoted LU factorization.但是对于更大的东西,我更喜欢因式分解,例如旋转 LU 因式分解。 If you're concerned about performance and deal with large sparse matrices, an iterative solver might be appropriate.如果您关心性能并处理大型稀疏矩阵,迭代求解器可能是合适的。 Alternativly you can try MUMPS (multifrontal massivly parallel solver) and measure the performance.或者,您可以尝试 MUMPS(多前沿大规模并行求解器)并测量性能。 The problem with iterative solvers is that their speed of convergence heavily depends on the condition of the inverse problem and whether you find good preconditioners.迭代求解器的问题在于它们的收敛速度在很大程度上取决于逆问题的条件以及您是否找到了好的预处理器。

Perhaps you should start with the Eigen library and try pivoted LU factorizations first.也许您应该从Eigen库开始,并首先尝试旋转 LU 分解。

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

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