矩阵逆变器返回错误的反转
Matrix inverter returning the wrong inverse
这段代码旨在查找矩阵的逆矩阵,它几乎做到了这一点。最后一行数字不正确,我不知道为什么。 这不是找到相反的最佳方法,但是这是我在家庭作业中需要这样做的方式。 我已经重新设计了几次电源功能,所以现在应该是正确的。 正在使用的方法是 B=I-A, A^-1 = I+B+B^2+B^3...(一直到 B^20(。
#include <iostream>
#include <iomanip>
using namespace std;
void multiplinator(double invA[][3], double A[][3], double y[][3]) //multiplies the matrix
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
y[i][j] = 0;
for(int k = 0; k < 3; k++)
{
y[i][j] += invA[i][k] * A[k][j];
}
}
}
}
void printinator(double a[3][3]) //prints a matrix
{
for(int i=0; i<=2; i++)
{
for(int j=0; j<=2; j++)
cout << fixed << setprecision(2) << setw(12) << a[i][j] << " ";
cout << endl;
}
cout << endl;
}
void substitinator(double I[][3], double A[][3], double B[][3]) //Matrix subtraction
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
B[i][j] = I[i][j] - A[i][j];
}
void additinator(double I[3][3], double B[][3], double invA[][3]) //Matrix addition
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
invA[i][j] = I[i][j] + B[i][j];
}
void powernator(double B[][3],double pB[][3], int p) //function which is supposed to raise a matrix to a certain power
{
double temp[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
int i,j,w,k;
for(i = 0 ; i < 2 ; ++ i )
for(j = 0 ; j < 2 ; ++ j )
pB[i][j] = ( i == j ) ;
for(w = 0; w < p; w++)
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
temp[i][j]=0;
for(k=0;k<2;k++)
{
temp[i][j] += pB[i][k] * B[k][j];
}
}
}
for(i = 0; i < 2; i++){
for(j = 0; j < 2; j++)
{
pB[i][j] = temp[i][j];
}
}
}
}
void gettem(double pB[][3], double invA[][3]) //Matrix addition of power of B
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
invA[i][j] += pB[i][j];
}
int main()
{
double A[3][3] = { {1./2, 1, 0} , {0, 2./3, 0} , {-1./2, -1, 2./3} };
double invA[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
double I[3][3] = { {1, 0, 0} , {0, 1, 0} , {0, 0, 1} };
double B[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
double pB[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
double y[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
substitinator(I,A,B);
additinator(I,B,invA);
for(int p = 2; p <= 20; p++)
{
powernator(B,pB,p);
gettem(pB, invA);
}
cout << "ntt Inverse:" << endl;
printinator(invA);
cout << "ntt invA * A:" << endl;
multiplinator(invA, A, y);
printinator(y);
}
我同意@1201ProgramAlarm,你的powernator()
函数是错误的,你应该确保所有的for循环条件都是< 3
而不是< 2
void powernator(double B[3][3],double pB[3][3], int p) //function which is supposed to raise a matrix to a certain power
{
double temp[3][3] = { {0, 0, 0} , {0, 0, 0} , {0, 0, 0} };
int i,j,w,k;
for(i = 0 ; i < 3 ; ++ i )
for(j = 0 ; j < 3 ; ++ j )
pB[i][j] = ( i == j ) ;
for(w = 0; w < p; w++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
temp[i][j]=0;
for(k=0;k<3;k++)
{
temp[i][j] += pB[i][k] * B[k][j];
}
}
}
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++)
{
pB[i][j] = temp[i][j];
}
}
}
}
结果是:
Inverse:
2.00 -3.00 0.00
0.00 1.50 0.00
1.50 0.00 1.50
invA * A:
1.00 0.00 0.00
0.00 1.00 0.00
-0.00 0.00 1.00
希望这有帮助。
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- C++中函数的向量返回类型引发错误
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- glad 导致 glfwSwapBuffers 返回错误消息
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- QNX Momentics: GoogleMock - 返回有价值,在函数中返回 void 错误
- 我的模板类方法返回错误类型?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 替换WCHAR_T错误返回存储地址
- 错误:void 值未被忽略,因为它应该被忽略,错误:返回语句具有值
- 带有值的递归阶乘错误返回语句,在函数中返回 'void' [-fallowive]
- 存储在静态常量整数 (C++) 中的对数函数的错误返回值
- 更丰富的错误返回消息用于提升::精神::qi 解析
- C++初级逻辑错误-返回0
- 由于进程很多,pthread_create的错误返回代码是 35 错误,我用过pthread_exit应该杀死线程不是吗?
- c++错误处理返回值错误返回
- systememerror:没有异常集的错误返回