当前位置:网站首页>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;
}
边栏推荐
- C#/VB.NET 给PDF文档添加文本/图像水印
- Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
- Tensorflow and torch code verify whether CUDA is successfully installed
- 第五期个人能力认证考核通过名单公布
- 上海部分招工市场对新冠阳性康复者拒绝招录
- 抽象类与抽象方法
- Solve DoS attack production cases
- 渲大师携手向日葵,远控赋能云渲染及GPU算力服务
- ModuleNotFoundError: No module named ‘PIL‘解决方法
- Use map function and split function to type multiple elements in one line
猜你喜欢

安装Mysql报错:Could not create or access the registry key needed for the...
Interview assault 63: how to remove duplication in MySQL?

视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」

朗坤智慧冲刺科创板:年营收4亿 拟募资7亿

星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿

Method of accessing mobile phone storage location permission under non root condition

Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller

About static type, dynamic type, ID, instancetype

MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!

If you have any problems, you can contact me. A rookie ~
随机推荐
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
test about BinaryTree
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
Multithreading Basics: basic concepts of threads and creation of threads
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
安装及管理程序
2022.2.12
线代笔记....
R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组均值(mean)
受益匪浅,安卓面试问题
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
Precautions for binding shortcut keys of QPushButton
helm部署etcd集群
QPushButton绑定快捷键的注意事项