当前位置:网站首页>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;
}
边栏推荐
- 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?
- Tensorflow and torch code verify whether CUDA is successfully installed
- 线代笔记....
- Method of accessing mobile phone storage location permission under non root condition
- Optical blood pressure estimation based on PPG and FFT neural network [translation]
- Digital "new" operation and maintenance of energy industry
- Online notes
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- Helm deploy etcd cluster
猜你喜欢
深度循环网络长期血压预测【翻译】
helm部署etcd集群
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
[paper notes] transunet: transformers make strongencoders for medical image segmentation
用于远程医疗的无创、无袖带血压测量【翻译】
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
被疫情占据的上半年,你还好么?| 2022年中总结
A wearable arm device for night and sleeveless blood pressure measurement [translation]
随机推荐
How to improve website weight
Word如何显示修改痕迹
如何提高网站权重
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
史上超级详细,想找工作的你还不看这份资料就晚了
LeetCode-1279. 红绿灯路口
ROS自定义消息发布订阅示例
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
R语言使用dt函数生成t分布密度函数数据、使用plot函数可视化t分布密度函数数据(t Distribution)
A wearable arm device for night and sleeveless blood pressure measurement [translation]
Precautions for binding shortcut keys of QPushButton
Method of accessing mobile phone storage location permission under non root condition
Use map function and split function to type multiple elements in one line