反转 4x4 矩阵 - 需要最稳定的数值解
我想反转 4x4 矩阵。 我的数字以定点格式存储(确切地说是 1.15.16)。
对于浮点运算,我通常只是构建伴随矩阵并除以行列式(例如强力求解)。 到目前为止,这对我有用,但是在处理定点数时,由于使用了所有乘法,我得到了不可接受的精度损失。
注意:在定点算术中,我总是丢弃立即结果的一些最低有效位。
那么 - 求逆矩阵最数值稳定的方法是什么? 我不太关心性能,但简单地使用浮点会降低我的目标架构的速度。
I want to invert a 4x4 matrix. My numbers are stored in fixed-point format (1.15.16 to be exact).
With floating-point arithmetic I usually just build the adjoint matrix and divide by the determinant (e.g. brute force the solution). That worked for me so far, but when dealing with fixed point numbers I get an unacceptable precision loss due to all of the multiplications used.
Note: In fixed point arithmetic I always throw away some of the least significant bits of immediate results.
So - What's the most numerical stable way to invert a matrix? I don't mind much about the performance, but simply going to floating-point would be to slow on my target architecture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我想附议 Jason S 提出的问题:您确定需要反转矩阵吗? 这几乎没有必要。 不仅如此,这通常是一个坏主意。 如果需要求解 Ax = b,直接求解方程组比将 b 乘以 A 的倒数在数值上更稳定。
即使您必须针对 b 的许多值一遍又一遍地求解 Ax = b,反转 A 仍然不是一个好主意。您可以对 A 进行因式分解(例如 LU 因式分解或 Cholesky 因式分解)并保存这样您就不必每次都重做该工作,但每次仍然可以使用因式分解来求解系统。
I'd like to second the question Jason S raised: are you certain that you need to invert your matrix? This is almost never necessary. Not only that, it is often a bad idea. If you need to solve Ax = b, it is more numerically stable to solve the system directly than to multiply b by A inverse.
Even if you have to solve Ax = b over and over for many values of b, it's still not a good idea to invert A. You can factor A (say LU factorization or Cholesky factorization) and save the factors so you're not redoing that work every time, but you'd still solve the system each time using the factorization.
在执行正常算法之前,您可能会考虑加倍到 1.31。 它会使乘法次数加倍,但是您正在执行矩阵求逆,并且您所做的任何操作都将与处理器中的乘法器紧密相关。
对于任何有兴趣查找 4x4 逆方程的人,您可以使用符号数学包来解决它们。 TI-89 甚至可以做到这一点,尽管这需要几分钟的时间。
如果您让我们了解矩阵求逆对您有何作用,以及它如何与您的其余处理相适应,我们也许可以建议替代方案。
-亚当
You might consider doubling to 1.31 before doing your normal algorithm. It'll double the number of multiplications, but you're doing a matrix invert and anything you do is going to be pretty tied to the multiplier in your processor.
For anyone interested in finding the equations for a 4x4 invert, you can use a symbolic math package to resolve them for you. The TI-89 will do it even, although it'll take several minutes.
If you give us an idea of what the matrix invert does for you, and how it fits in with the rest of your processing we might be able to suggest alternatives.
-Adam
让我问一个不同的问题:你一定需要对矩阵求逆(称之为M),还是需要使用矩阵求逆来求解其他方程? (例如,对于已知的 M,b,Mx = b)通常还有其他方法可以做到这一点,而不需要明确地计算逆数。 或者如果矩阵 M 是时间 & 的函数 它变化缓慢,然后您可以计算一次完整的逆,& 有迭代的方法来更新它。
Let me ask a different question: do you definitely need to invert the matrix (call it M), or do you need to use the matrix inverse to solve other equations? (e.g. Mx = b for known M, b) Often there are other ways to do this w/o explicitly needing to calculate the inverse. Or if the matrix M is a function of time & it changes slowly then you could calculate the full inverse once, & there are iterative ways to update it.
如果矩阵表示仿射变换(很多时候,只要不引入缩放分量,4x4 矩阵就是这种情况),则逆矩阵就是上部 3x3 旋转部分的转置,最后一列取反。 显然,如果您需要通用解决方案,那么研究高斯消除可能是最简单的。
If the matrix represents an affine transformation (many times this is the case with 4x4 matrices so long as you don't introduce a scaling component) the inverse is simply the transpose of the upper 3x3 rotation part with the last column negated. Obviously if you require a generalized solution then looking into Gaussian elimination is probably the easiest.
我认为这个问题的答案取决于矩阵的确切形式。 具有旋转(必需)的标准分解方法(LU、QR、Cholesky 等)在定点上相当不错,特别是对于小型 4x4 矩阵。 请参阅 Press 等人所著的《数值食谱》一书。 有关这些方法的说明。
本文提供了一些有用的内容算法,但不幸的是它位于付费墙后面。 他们推荐采用(旋转的)Cholesky 分解,其中包含一些过于复杂的附加功能,无法在此列出。
I think the answer to this depends on the exact form of the matrix. A standard decomposition method (LU, QR, Cholesky etc.) with pivoting (an essential) is fairly good on fixed point, especially for a small 4x4 matrix. See the book 'Numerical Recipes' by Press et al. for a description of these methods.
This paper gives some useful algorithms, but is behind a paywall unfortunately. They recommend a (pivoted) Cholesky decomposition with some additional features too complicated to list here.
元答案:它真的是一个通用的 4x4 矩阵吗? 如果您的矩阵具有特殊形式,则可以使用直接的求逆公式,该公式会很快并减少您的操作计数。
例如,如果它是图形的标准齐次坐标变换,例如:(
假设旋转、缩放、平移矩阵的组合),
则有一个 易于推导的直接公式,这是
(从链接页面窃取的 ASCII 矩阵。)
您可能无法击败它,因为固定精度的损失观点。
如果您的矩阵来自某个您知道它具有更多结构的领域,那么可能会有一个简单的答案。
Meta-answer: Is it really a general 4x4 matrix? If your matrix has a special form, then there are direct formulas for inverting that would be fast and keep your operation count down.
For example, if it's a standard homogenous coordinate transform from graphics, like:
(assuming a composition of rotation, scale, translation matrices)
then there's an easily-derivable direct formula, which is
(ASCII matrices stolen from the linked page.)
You probably can't beat that for loss of precision in fixed point.
If your matrix comes from some domain where you know it has more structure, then there's likely to be an easy answer.