在c++中求解一个行简化形式的简单矩阵

Solving a simple matrix in row-reduced form in C++

本文关键字:简单 一个 c++      更新时间:2023-10-16

好吧,我在这上面扯了我所有的头发,尽管,作为一个新手,我确信有几个问题。我想取一个矩阵,通过初等行变换,把它化简成行简化阶梯形。我们假设(1)它是可解的,(2)它是唯一解。不需要检查零或其他任何东西;它只是行变换。下面是代码:

#include <iostream>
#include <cstdlib>
using namespace std;
void printmatrix(float A[][4]);
void RowReduce (float A[][4]);
int main() {
    // answer should be { 2, 4, -3 }
    float A[3][4] = {
        { 5, -6, -7,   7 },
        { 3, -2,  5, -17 },
        { 2,  4, -3,  29 }
    };
    printmatrix(A);
    RowReduce(A);
}
// Outputs the matrix
void printmatrix(float A[][4]) { 
    int p = 3;
    int q = 4;
    for (int i = 0; i < p; i++) {
        for (int j = 0; j < q; j++) {
            cout << A[i][j] << " ";
        }
        cout << endl;
    } 
}
void RowReduce (float A[][4]){
    //rows
    int p = 3;  
    //columns
    int q = 4;  
    // the determines the column we are at which holds the diagonal,
    // the basis for all elimination above and below
    int lead = 0; 
    cout << endl;
    while ( lead < q - 1 ) {
        // for each row . . .
        for (int i = 0; i < p; i++)  {
            // ignore the diagonal, and we will not have a tree rref
            // as the diagonal will not be divided by itself. I can fix that.
            if ( i != lead )  {
                cout << A[lead][lead] << "  " << A[i][lead];
                for (int j = 0; j < q; j++) {
                    //here is the math . . . . probably where the problem is?
                    A[i][j]    = A[lead][lead] * A[i][j]; 
                    A[i][lead] = A[i][lead]    * A[lead][j];
                    A[i][j]    = A[i][j]       - A[i][lead];
                }
                cout << endl;
            }
        }
        // now go to the next pivot
        lead++;  
        cout << endl;
    }
}

我试着手工做,但我得到的,当然是正确的答案,但这得到的是一个对角矩阵——这很好——但答案是错误的!

代码中的主要错误是在for循环中计算除数或乘数。您应该在遍历单元格之前计算它们。

提示:如果代码格式良好,并且变量名称有意义,则调试更容易。

参见RowReduce()的实现:

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void printmatrix(float A[][4]);
void RowReduce(float A[][4]);
int main()
{
    float A[3][4] = {{5, -6, -7,   7},
                     {3, -2,  5, -17},
                     {2,  4, -3,  29}}; //answer should be {2, 4, -3}
    printmatrix(A);
    RowReduce(A);
}
void printmatrix(float A[][4]) // Outputs the matrix
{
    int p=3;
    int q=4;
    for (int i=0; i<p; i++) {
            for (int j=0; j<q; j++) {
                    cout << setw(7) << setprecision(4) << A[i][j] << " ";
            }
            cout << endl;
    }
    cout << endl;
}
void RowReduce(float A[][4])
{
    const int nrows = 3; // number of rows
    const int ncols = 4; // number of columns
    int lead = 0; 
    while (lead < nrows) {
        float d, m;
        for (int r = 0; r < nrows; r++) { // for each row ...
            /* calculate divisor and multiplier */
            d = A[lead][lead];
            m = A[r][lead] / A[lead][lead];
            for (int c = 0; c < ncols; c++) { // for each column ...
                if (r == lead)
                    A[r][c] /= d;               // make pivot = 1
                else
                    A[r][c] -= A[lead][c] * m;  // make other = 0
            }
        }
        lead++;
        printmatrix(A);
    }
}
输出:

  5      -6      -7       7 
  3      -2       5     -17 
  2       4      -3      29 

  1    -1.2    -1.4     1.4 
  0     1.6     9.2   -21.2 
  0     6.4    -0.2    26.2 
  1       0     5.5   -14.5 
  0       1    5.75  -13.25 
  0       0     -37     111 
  1       0       0       2 
  0       1       0       4 
  0       0       1      -3