英文:
inverse of a 2D array (matrix)
问题
我已经编写了一个用于计算矩阵的逆的函数,但在运行程序时,我遇到了获取正确结果的问题。
例如,当我插入以下矩阵时:
A1 A2 A3
(2 1 0)
(1 2 1)
(0 1 2)
我得到了这个矩阵:
0.1875 -0.125 0.0625
-0.125 0.25 -0.125
0.0625 -0.125 0.1875
而正确的矩阵解决方案应该是:
3/4 -1/2 1/4
-1/2 1 -1/2
1/4 -1/2 3/4
以下是我的代码:
void inverse(double A[][MAX], int n) {
double B[MAX][MAX];
double det = 1.0;
int i, j, k;
// 将B初始化为单位矩阵
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
B[i][j] = (i == j) ? 1 : 0;
// 执行行操作以将A转换为单位矩阵
for(k = 0; k < n; k++) {
// 寻找主元素
int max_row = k;
for (i = k + 1; i < n; i++) {
if (abs(A[i][k]) > abs(A[max_row][k])) {
max_row = i;
}
}
// 交换当前行与具有最大主元素的行
if (max_row != k) {
for (int col = 0; col < n; col++) {
swap(A[k][col], A[max_row][col]);
swap(B[k][col], B[max_row][col]);
}
det *= -1;
}
// 通过主元素除以当前行,使其等于1
double pivot = A[k][k];
if (pivot == 0) {
cout << "错误:奇异矩阵。" << endl;
return;
}
det *= pivot;
for(j = 0; j < n; j++) {
A[k][j] /= pivot;
B[k][j] /= pivot;
}
// 使用当前行消除其他行中的主元素
for(i = 0; i < n; i++) {
if(i == k) continue;
double factor = A[i][k];
for(j = 0; j < n; j++) {
A[i][j] -= A[k][j] * factor;
B[i][j] -= B[k][j] * factor;
}
}
}
// 乘以行列式的倒数
if(det == 0) {
cout << "错误:奇异矩阵。" << endl;
return;
}
det = 1/det;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
B[i][j] *= det;
// 将逆矩阵复制到A
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
A[i][j] = B[i][j];
}
注意:为了获得正确的分数形式,你可能需要更改输出格式。
英文:
I've made a function to calculate the inverse of a matrix, but I'm having an issue getting the correct result when I run my program.
For example, when I insert the following matrix:
A1 A2 A3
(2 1 0)
(1 2 1)
(0 1 2)
I get this matrix:
0.1875 -0.125 0.0625
-0.125 0.25 -0.125
0.0625 -0.125 0.1875
While the correct matrix solution is this:
3/4 -1/2 1/4
-1/2 1 -1/2
1/4 -1/2 3/4
Here is my code:
void inverse(double A[][MAX], int n) {
double B[MAX][MAX];
double det = 1.0;
int i, j, k;
// Initializing B to identity matrix
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
B[i][j] = (i == j) ? 1 : 0;
// Performing row operations to convert A to identity matrix
for(k = 0; k < n; k++) {
// Find the pivot element
int max_row = k;
for (i = k + 1; i < n; i++) {
if (abs(A[i][k]) > abs(A[max_row][k])) {
max_row = i;
}
}
// Swap the current row with the row with the largest pivot element
if (max_row != k) {
for (int col = 0; col < n; col++) {
swap(A[k][col], A[max_row][col]);
swap(B[k][col], B[max_row][col]);
}
det *= -1;
}
// Divide the current row by the pivot element to make it equal to 1
double pivot = A[k][k];
if (pivot == 0) {
cout << "Error: Singular matrix." << endl;
return;
}
det *= pivot;
for(j = 0; j < n; j++) {
A[k][j] /= pivot;
B[k][j] /= pivot;
}
// Use the current row to eliminate the pivot elements in the other rows
for(i = 0; i < n; i++) {
if(i == k) continue;
double factor = A[i][k];
for(j = 0; j < n; j++) {
A[i][j] -= A[k][j] * factor;
B[i][j] -= B[k][j] * factor;
}
}
}
// Multiplying by reciprocal of the determinant
if(det == 0) {
cout << "Error: Singular matrix." << endl;
return;
}
det = 1/det;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
B[i][j] *= det;
// Copying the inverse matrix to A
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
A[i][j] = B[i][j];
}
答案1
得分: 2
请注意行列式为4,而您的结果是预期结果的四分之一。
// 乘以行列式的倒数
您不应该乘以行列式的倒数。如果您已经计算出A的伴随矩阵,那么乘以行列式的倒数将给您逆矩阵。但您已经有了逆矩阵:当A被转化为单位矩阵时,B必须是逆矩阵。
英文:
Note that the determinant is 4 and your result is 1/4th of the intended result
> // Multiplying by reciprocal of the determinant
You should not multiply by the reciprocal of the determinant. If you had computed the adjugate of A, in that case yes, multiplying it by the reciprocal of the determinant would give you the inverse. But you already have the inverse: when A has been turned into the identity matrix, B must be the inverse.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论