高斯消除:逆 4x4 矩阵
Gaussian Elimination: Inverse 4x4 Matrix
问题
下面会给我一个变量"cur"的单位矩阵,并试图给我变量"tmp"的逆矩阵,但会失败。谁能告诉我我做错了什么?
附言矩阵是列主矩阵。
矩阵输入
1 | 0| 0 | 0
0 | -4.37114e-08 | 1 | 0
0 | -1 | -4.37114e-08 | 0
0 | 0 | 0 | 1
矩阵输出
1 | 0| 0 | 0
0 | 0 | 0| 0 | 0
0 | 1 | 0 | 0
0 | 0 | 0 | 1
期望的输出
1 | 0| 0 | 0
0 | -4.3711399999999916e-8 | -0.9999999999999981 | 0
0 | 0.999999999999998 | -4.3711399999999916e-8 | 0
0 | 0 | 0 | 1
#include <iostream>
template <typename T>
class Matrix
{
public:
T matrix[4][4];
Matrix(T matrix[4][4])
: matrix()
{
for (unsigned int y = 0; y < 4; ++y)
{
for (unsigned int x = 0; x < 4; ++x)
{
this->matrix[y][x] = matrix[y][x];
}
}
}
Matrix()
: matrix()
{
T zero = static_cast<T>(0);
T one = static_cast<T>(1);
matrix[0][0] = one;
matrix[1][0] = zero;
matrix[2][0] = zero;
matrix[3][0] = zero;
matrix[0][1] = zero;
matrix[1][1] = one;
matrix[2][1] = zero;
matrix[3][1] = zero;
matrix[0][2] = zero;
matrix[1][2] = zero;
matrix[2][2] = one;
matrix[3][2] = zero;
matrix[0][3] = zero;
matrix[1][3] = zero;
matrix[2][3] = zero;
matrix[3][3] = one;
}
Matrix<T> GetInverse() const
{
T zero = static_cast<T>(0);
T one = static_cast<T>(1);
Matrix<T> tmp;
Matrix<T> cur;
for (unsigned int y = 0; y < 4; ++y)
{
for (unsigned int x = 0; x < 4; ++x)
{
cur.matrix[y][x] = matrix[y][x];
}
}
cur.Print();
for (unsigned int x = 0; x < 4; x++)
{
if (cur.matrix[x][x] != zero)
{
T denominator = cur.matrix[x][x];
for (unsigned int a = x; a < 4; ++a)
{
cur.matrix[x][a] = cur.matrix[x][a] / denominator;
}
for (unsigned int a = 0; a < 4; ++a)
{
tmp.matrix[x][a] = tmp.matrix[x][a] / denominator;
}
}
for (unsigned int y = 0; y < 4; ++y)
{
if (y != x && cur.matrix[y][x] != 0)
{
T difference = cur.matrix[y][x];
for (unsigned int a = x; a < 4; ++a)
{
cur.matrix[y][a] = (cur.matrix[y][a] - difference) * cur.matrix[x][a];
}
for (unsigned int a = 0; a < 4; ++a)
{
tmp.matrix[y][a] = (tmp.matrix[y][a] - difference) * tmp.matrix[x][a];
}
}
}
}
cur.Print();
tmp.Print();
return tmp;
}
void Print()
{
for (unsigned int y = 0; y < 4; ++y)
{
for (unsigned int x = 0; x < 4; ++x)
{
std::cout << matrix[y][x];
if (x < 3)
std::cout << " | ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
}
int main()
{
float matrix[4][4];
matrix[0][0] = 1.0f;
matrix[0][1] = 0.0f;
matrix[0][2] = 0.0f;
matrix[0][3] = 0.0f;
matrix[1][0] = 0.0f;
matrix[1][1] = -4.37114e-08;
matrix[1][2] = 1.0f;
matrix[1][3] = 0.0f;
matrix[2][0] = 0.0f;
matrix[2][1] = -1.0f;
matrix[2][2] = -4.37114e-08;
matrix[2][3] = 0.0f;
matrix[3][0] = 0.0f;
matrix[3][1] = 0.0f;
matrix[3][2] = 0.0f;
matrix[3][3] = 1.0f;
Matrix<float> inverseMatrix(matrix);
inverseMatrix.GetInverse();
return 0;
}
首先,你的类不是一个合适的类。在尝试找出算法或操作错误之前,您可以将其转换为更合理的代码,以便于调试。
现在,您的Matrix
类并没有真正定义对象,而只是一组要在 2D 数组上使用的操作。一个好的对象具有属性和数据,然后具有使用该数据的特殊函数。我会在 https://en.wikipedia.org/wiki/Object-oriented_programming 阅读一些介绍
你想要的是类似的东西
// Matrix.h
template <typename T>
class Matrix {
public:
// exactly how you choose to construct the object is up to you
// i.e. if you want to pass a 2d array as a parameter or something
Matrix(T inputMatrix[4][4]) {
for (int column = 0; column < 4; ++column) {
for (int row = 0; row < 4; ++row) {
matrix[column][row] = inputMatrix[column][row];
}
}
}
Matrix reduce() {
// will return a new Matrix object
. . .
}
Matrix inverse() {
// probably will end up calling reduce()
. . .
}
void print() {
. . .
}
// any other operations you want to do on matrices
private:
T matrix[4][4];
};
因此,当您想进行操作时,它非常简单明了:
#include "Matrix.h"
int main() {
float matrix[4][4] = {
{ 1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, -4.37114e-08, 1.0f, 0.0f },
{ 0.0f, -1.0f, -4.37114e-08, 0.0f },
{ 0.0f, 0.0f, 0.0f, 0.0f };
Matrix<float> A = Matrix<float>(matrix);
Matrix<float> B = A.inverse();
B.print();
return 0;
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 使用std::vector的OpenCL矩阵乘法
- C++ - 如何从上到下制作 4x4 矩阵和 cout 列(需要解密字符串)
- 在opencv中创建4x4垫矩阵时出错
- 我需要声明一个包含整数向量的 4x4 矩阵
- OpenGL - 从矩阵 4x4 开始本地向上和正确?
- AVX 内在澄清,4x4 矩阵乘法奇数
- 如何在C 中乘以4x4浮点矩阵
- 循环矩阵Ordo 4x4
- RcppArmadillo expmat挂在4x4矩阵上
- 程序 使用 C++ 中的函数测试 4x4 矩阵是否是魔方
- 如何在 GLSL 中使用 4x4 矩阵作为顶点属性
- 如何在Eigen中平移矩阵(4x4)
- 旋转 4x4 矩阵会导致随时间推移而缩放
- 如何将运动学轨迹矩阵转换为4x4均质矩阵(示例)
- 了解 GLM 4x4 矩阵函数
- 高斯消除:逆 4x4 矩阵
- 有没有一个Math3d函数可以将顶点数组与4x4矩阵相乘
- 使用“度”设置4x4旋转矩阵
- 4x4矩阵乘法前vs乘法后