当前位置:网站首页>数学知识——高斯消元(初等行变换解方程组)代码实现
数学知识——高斯消元(初等行变换解方程组)代码实现
2022-07-06 11:18:00 【吴雨4】
高斯消元的作用:
求解n个未知数、n个方程的方程组
例如如下n个方程、n个未知数:
写成n*(n+1)的矩阵:
枚举每一列
第一步:找到绝对值最大的一行
第二步:将该行换到最上面
第三步:将该行的第一个数变成1
第四步:将下面行的此列变为0
注意的是,换好了的行不需要动!
举一个例子:
求解有三个方程、三个未知数的方程组:
写成矩阵形式:
枚举第一列,找到最大行:
将该行换到最上面,并将该行第一个数变为1:
将下面行的第1列都变成0:
枚举第二列,找到绝对值最大的一行,将该行的第一个数变成1:
将下面行的第1列都变成0:
遍历第三列(由于第一行和第二行都已经改变好了,不需要动了),这里直接把第三行的第一个数变为1就行了:
最后化简为:
由此可以依次算出:
x3=3;
x2=-2;
x1=3;
例题链接: 883. 高斯消元解线性方程组 - AcWing题库
代码(详细注释):
#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()// 高斯消元,答案存于a[i][n]中,0 <= i < n
{
int c,r;//c代表列,r代表行
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;
if(fabs(a[t][c])<eps)//最大值为0
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++)// 用当前行将下面所有的列消成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;//无解
return 1;//多解
}
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;//无解
else if(t==1) cout<<"Infinite group solutions"<<endl;//多解
else//唯一解
{
for(int i=0;i<n;i++)
{
if(fabs(a[i][n])<eps) a[i][n]=0;//去掉输出-0.00的情况
printf("%.2lf\n", a[i][n]);
}
}
return 0;
}
边栏推荐
- The role of applet in industrial Internet
- 安装Mysql报错:Could not create or access the registry key needed for the...
- 如何提高网站权重
- When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
- Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
- Test technology stack arrangement -- self cultivation of test development engineers
- C#/VB. Net to add text / image watermarks to PDF documents
- 渲大师携手向日葵,远控赋能云渲染及GPU算力服务
- Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
- 提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
猜你喜欢
Online notes
PMP每日一练 | 考试不迷路-7.6
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
ACTF 2022圆满落幕,0ops战队二连冠!!
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
应用使用Druid连接池经常性断链问题分析
【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
On AAE
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
Nuc11 cheetah Canyon setting U disk startup
随机推荐
Multithreading Basics: basic concepts of threads and creation of threads
[depth first search] Ji suanke: Square
ROS custom message publishing subscription example
SQL injection Foundation
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
Analysis of frequent chain breaks in applications using Druid connection pools
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
test about BinaryTree
人体骨骼点检测:自顶向下(部分理论)
English topic assignment (25)
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
If you have any problems, you can contact me. A rookie ~