Gaussian elimination to solve linear equations

Gauss

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const double eps = 1e-6;
const int N =110;

int n;
double a[N][N];
void out()
{
    for(int i = 0;i < n;i++){
        for(int j = 0;j <= n; j++)
            printf("%10.2lf ",a[i][j]);
        cout<<endl;
    }
    cout<<endl;
}
int gauss ()
{
    int c, r; // c means column, r means row 
    for (c = 0 , r = 0 ; c <n; c ++ )
    {
        int t = r;
         for ( int i = r; i <n; i ++ )
             if (fabs (a [i] [c])> fabs (a [t] [c])) t = i; // find the current The row with the largest absolute value
        
        if(fabs(a[t][c]) < eps) continue;
        
        for(int i = c; i <= n; i++) swap(a[t][i], a[r][i]);
        for(int i = n; i >= c; i--) a[r][i] /= a[r][c];//把该行第一个数的系数变成1
        for(int i = r + 1; i < n; i++) 
            if(fabs(a[i][c]) > eps)
                for(int j = n; j >= c; j--)
                    a[i][j] -= a[r][j] * a[i][c];
        out();
        r++;
    }
    if(r < n)
    {
        for ( int i = r; i <n; i ++ )
             if (fabs (a [i] [n])> eps)
                 return  2 ; // Without solving an unknown number, a [i] [n] after two equations All should be 0, if not 0, contradiction. 
        return  1 ; // Countless solutions <n equations n unknowns (like two unknowns, one equation) 
    }
    
    
    for(int i = n - 1; i >= 0; i--)
        for(int j = i + 1; j < n; j++)
            a[i][n] -= a[i][j] * a[j][n];
            
    
    return 0;
}

int main ()
{
    cin>>n;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < n + 1;j++)
            cin>>a[i][j];
    
    int t = gauss();
    
    if(t == 0)
    {
        for(int i = 0; i < n; i++) printf("%.2lf\n",a[i][n]);
    }
    else if(t == 1) puts("Infinite group solutions");
    else puts("No solution");
}
            

Enter the coefficient:

3
1.00 2.00 -1.00 -6.00
2.00 1.00 -3.00 -9.00
-1.00 -1.00 2.00 7.00

Process variable output: 1.00 0.50 -1.50 -4.50 0.00 1.50 0.50 -1.50        0.00 -0.50 0.50 2.50 


      1.00       0.50      -1.50      -4.50 
      0.00       1.00       0.33      -1.00 
      0.00       0.00       0.67       2.00 

      1.00       0.50      -1.50      -4.50 
      0.00       1.00       0.33      -1.00 
      0.00       0.00       1.00       3.00 

-4.50
-1.00
3.00 
Matrix row and column of the elementary three laws:
1) the number of a row by a non-zero.
2) Swap a certain two lines
3) Add multiples of a certain line to another line. (The purpose is to eliminate another row to become 0)
The steps of Gaussian elimination:
1) Enumerate each column and find the row with the largest current absolute value.
2) Change this line to the top.
3) Reduce the current column column of all the rows below to 0.

What should turn out to be an upper triangle.
This step of elimination requires a keen observation of the matrix two-dimensional array:
for(int i = r + 1; i < n; i++) 
            if(fabs(a[i][c]) > eps)
                for(int j = n; j >= c; j--)
                    a [i] [j]-= a [r] [j] * a [i] [c]; For 
example, here it means: eliminate the current column c of all other rows except the r row, which is 0. Is that all subsequent numbers a [i] [j] are subtracted from the first number a [i] [c] in this row (because the number of columns c in row r has been set to 1) and multiplied by the previous row to compare row a [r] [j] The number of each column.
for(int i = n - 1; i >= 0; i--)
        for(int j = i + 1; j < n; j++)
            a [i] [n]-= a [i] [j] * a [j] [n];
and the result obtained here is the last one of each expression, namely the value on the right: a [i] [n ], Because there is only one result left for each unknown on the left. Each one is based on the next to the last formula to eliminate the element, hitting the left only one number 1 to the right of the value is the purpose of the result.
a [n-1] [n] is the solution of the last unknown. The value of the penultimate unknown: a [n-2] [n] = a [n-2] [n]-a [n-2] [n-1] * a [n-1] [n]
                               a [i] [n] = a [i] [n]-a [i] [j] * a [j] [n] (i: n-1-> 0, j: i + 1->> n- 1) (Because the next line a [j] [j] is already 1, it is the value obtained by subtracting the solution of a [i] [j] * a [j] [n] on the right side of the next line of this line)

 

Guess you like

Origin www.cnblogs.com/longxue1991/p/12731053.html