当前位置:网站首页>Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)

Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)

2022-07-06 19:09:00 Wu Yu 4

The function of Gauss elimination

solve n An unknown number 、n A system of equations of two equations

  For example, as follows n An equation 、n An unknown number :

  It's written in n*(n+1) Matrix :

Enumerate each column

First step : Find the line with the largest absolute value

The second step : Change the line to the top

The third step : Change the first number of the line to 1

Step four : Change this column in the following row to 0

Pay attention to is , There is no need to move the changed line !

  For example :

There are three equations to solve 、 A system of equations with three unknowns :

In matrix form :

  Enumerate the first column , Maximum row found :

  Change the line to the top , And change the first number of the line to 1:

  Put line 1 Columns become 0:

  Enumerate the second column , Find the line with the largest absolute value , Change the first number of the line to 1:

  Put line 1 Columns become 0:

  Traverse the third column ( Because the first and second lines have been changed , No need to move ), Here, directly change the first number in the third line to 1 That's it :

  Finally, it is reduced to :

From this, we can calculate :

x3=3;

x2=-2;

x1=3;

Example Links : 883. Gauss elimination solves linear equations - AcWing Question bank

 

Code ( Detailed notes ):

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
const int N=110;
const double eps=1e-8;
int n;
double a[N][N];
int gauss()//  Gauss elimination , The answer lies in a[i][n] in ,0 <= i < n
{
    int c,r;//c Representative column ,r On behalf of the line 
    for(c=0,r=0;c<n;c++)// Enumerate each column 
    {
        int t=r;// Save the line with the largest absolute value 
        for(int i=r;i<n;i++)// The row with the largest absolute value was found 
            if(fabs(a[i][c])>fabs(a[t][c])) t=i;

        if(fabs(a[t][c])<eps)// The maximum value is 0
            continue;

        for(int i=c;i<=n;i++) swap(a[t][i],a[r][i]);//  Change the line with the largest absolute value to the top 

		for(int i=n;i>=c;i--) a[r][i]/=a[r][c];// Change the first place of the current line to 1

		for(int i=r+1;i<n;i++)//  Use the current row to eliminate all the following columns into 0
            if(fabs(a[i][c])>eps)
                for(int j=n;j>=c;j--)
                    a[i][j]=a[i][j]-a[r][j]*a[i][c];
            r++;
    }
    if(r<n)
    {
    	for(int i=r;i<n;i++)
    		if(fabs(a[i][n])>eps) return 2;// unsolvable 
		return 1;// More solutions 
	}
	for(int i=n-1;i>=0;i--)
		for(int j=i+1;j<n;j++)
			a[i][n]=a[i][n]-a[i][j]*a[j][n];
	return 0;
}
int main()
{
    IOS;
    cin>>n;
    for(int i=0;i<n;i++)
        for(int j=0;j<=n;j++)
            cin>>a[i][j];

    int t=gauss();

    if(t==2) cout<<"No solution"<<endl;// unsolvable 

    else if(t==1) cout<<"Infinite group solutions"<<endl;// More solutions 

    else// Unique solution 
    {
        for(int i=0;i<n;i++)
        {
            if(fabs(a[i][n])<eps) a[i][n]=0;// Remove the output -0.00 The situation of 
             printf("%.2lf\n", a[i][n]);
        }
    }
    return 0;
}

原网站

版权声明
本文为[Wu Yu 4]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061117431398.html