当前位置:网站首页>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;
}
边栏推荐
- Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- 2022.2.12
- R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
- 包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
- Medical image segmentation
- Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
- Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
- ACTF 2022圆满落幕,0ops战队二连冠!!
- AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
猜你喜欢
Optical blood pressure estimation based on PPG and FFT neural network [translation]
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
Method of accessing mobile phone storage location permission under non root condition
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
Multithreading Basics: basic concepts of threads and creation of threads
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
二叉搜索树
能源行业的数字化“新”运维
The second day of rhcsa study
随机推荐
[depth first search] Ji suanke: Square
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
Abstract classes and abstract methods
RT-Thread 组件 FinSH 使用时遇到的问题
test about BinaryTree
ORACLE进阶(四)表连接讲解
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
史上超级详细,想找工作的你还不看这份资料就晚了
Reptiles have a good time. Are you full? These three bottom lines must not be touched!
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
第五期个人能力认证考核通过名单公布
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
Interview assault 63: how to remove duplication in MySQL?
多线程基础:线程基本概念与线程的创建
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Modulenotfounderror: no module named 'PIL' solution
ACTF 2022圆满落幕,0ops战队二连冠!!
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
被疫情占据的上半年,你还好么?| 2022年中总结