反转 4x4 矩阵
我正在寻找有关如何反转 4x4 矩阵的示例代码实现。 我知道有高斯消除、LU 分解等,但我并没有详细研究它们,而是只是在寻找执行此操作的代码。
理想的语言是 C++,数据以列优先顺序以 16 个浮点数的数组形式提供。
I am looking for a sample code implementation on how to invert a 4x4 matrix. I know there is Gaussian eleminiation, LU decomposition, etc., but instead of looking at them in detail I am really just looking for the code to do this.
Language ideally C++, data is available in array of 16 floats in column-major order.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
此处:
这是从 GLU 库的 MESA 实现中提取的。
here:
This was lifted from MESA implementation of the GLU library.
如果有人寻找更定制的代码和“更易于阅读”,那么我得到了这个,
我不写代码,但我的程序写了。 我编写了一个小程序来编写一个计算任意 N 矩阵的行列式和逆矩阵的程序。
我这样做是因为过去我需要一个反转 5x5 矩阵的代码,但地球上没有人这样做过,所以我做了一个。
此处查看该程序。
编辑:矩阵布局是逐行的(意味着 m01 位于第一行第二列)。 语言也是 C#,但应该很容易转换为 C。
If anyone looking for more costumized code and "easier to read", then I got this
I don't write the code, but my program did. I made a small program to make a program that calculate the determinant and inverse of any N-matrix.
I do it because once in the past I need a code that inverses 5x5 matrix, but nobody in the earth have done this so I made one.
Take a look about the program here.
EDIT: The matrix layout is row-by-row (meaning
m01
is in the first row and second column). Also the language is C#, but should be easy to convert into C.我“汇总”了 MESA 实现(还编写了几个单元测试以确保它确实有效)。
在这里:
我写了一些关于此的内容,并显示了正/负因素的模式 在我的博客上。
正如@LiraNuna 所建议的,在许多平台上都可以使用此类例程的硬件加速版本,因此我很高兴拥有一个可读且简洁的“备份版本”。
注意:这可能比 MESA 实现慢 3.5 倍或更差。 您可以改变因素的模式以删除一些添加内容等...但它会降低可读性并且仍然不会很快。
I 'rolled up' the MESA implementation (also wrote a couple of unit tests to ensure it actually works).
Here:
I wrote a little about this and display the pattern of positive/negative factors on my blog.
As suggested by @LiraNuna, on many platforms hardware accelerated versions of such routines are available so I'm happy to have a 'backup version' that's readable and concise.
Note: this may run 3.5 times slower or worse than the MESA implementation. You can shift the pattern of factors to remove some additions etc... but it would lose in readability and still won't be very fast.
如果您需要具有大量函数的 C++ 矩阵库,请查看 Eigen 库 - http://eigen.tuxfamily.org
If you need a C++ matrix library with a lot of functions, have a look at Eigen library - http://eigen.tuxfamily.org
这是 @willnode 的答案的 C++ 版本
This is the C++ version for @willnode's answer
您可以使用 GNU 科学库 或在其中查找代码。
编辑:您似乎想要 线性代数 部分。
You can use the GNU Scientific Library or look the code up in it.
Edit: You seem to want the Linear Algebra section.
受 @shoosh 的启发,我检查了 MESA 实现,发现矩阵求逆在最近的 mesa 版本中看起来非常不同。 我想这些都是很好的改进。 这是来自 Mesa-17.3.9 的矩阵求逆代码:
注意:您可以在 mesa 代码库中找到这段代码:
mesa-17.3.9/src/mesa/math/m_matrix.c
。Inspired by @shoosh to check out MESA implementations, I found that matrix inversion looks quite different in more recent mesa releases. I suppose those are good improvements. Here's the matrix inversion code from Mesa-17.3.9:
Note: you can find this piece of code in the mesa code base:
mesa-17.3.9/src/mesa/math/m_matrix.c
.这是一个小型(只有一个标头)C++ 向量数学 库(齿轮面向 3D 编程)。 如果你使用它,请记住,与 OpenGL 期望的相比,它在内存中的矩阵布局是相反的,我很高兴弄清楚它......
Here is a small (just one header) C++ vector math library (geared towards 3D programming). If you use it, keep in mind that layout of its matrices in memory is inverted comparing to what OpenGL expects, I had fun time figuring it out...
您可以根据此博客使其更快。
这不是一个完整的实现,因为 P 可能不可逆,但您可以将此代码与 MESA 实现结合起来以获得更好的性能。
You can make it faster according to this blog.
This is not a complete implementation because P may not be invertible, but you can combine this code with MESA implementation to get a better performance.
添加一个可能对其他人有用的二维案例:
Adding a 2d case which might be useful for someone else:
如果你想计算 4x4 矩阵的逆矩阵,那么我建议使用像 OpenGL数学(GLM):
无论如何,你可以从头开始。 以下实现类似于
glm 的实现: :inverse
,但它没有那么高度优化:If you want to compute the inverse matrix of 4x4 matrix, then I recommend to use a library like OpenGL Mathematics (GLM) :
Anyway, you can do it from scratch. The following implementation is similar to the implementation of
glm::inverse
, but it is not as highly optimized: