跳到正文

算法·理论

yl-高斯消元

Luna_From_Starry约 9 分钟
原文

这是 2025年9月13日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

高斯消元

高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,

可以解出形如

{a1,1x1+a1,2x2+⋯+a1,nxn=b1a2,1x1+a2,2x2+⋯+a2,nxn=b2⋯an,1x1+an,2x2+⋯+an,nxn=bn \begin{cases} a_{1, 1} x_1 + a_{1, 2} x_2 + \cdots + a_{1, n} x_n = b_1 \\ a_{2, 1} x_1 + a_{2, 2} x_2 + \cdots + a_{2, n} x_n = b_2 \\ \cdots \\ a_{n,1} x_1 + a_{n, 2} x_2 + \cdots + a_{n, n} x_n = b_n \end{cases}

的 n 元一次方程组。

线性方程组

构造增广矩阵,即系数矩阵A增加上常数向量b(A|b)

通过以交换行乘以非负常数和两行相加这三种初等变化将原系统转化为更简单的三角形式(triangular form)

这时可以使用向后替换算法(Algorithm for Back Substitution)求解

复杂度:O(n3)\mathcal{O(n^3)}

例题

高斯消元法

mod

C++
/*

writer:  Luna_Frome_Starry
Problem: 不知名屑题
Knowledge: 垃圾算法
Time: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:

*/
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 1005;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
const double eps = 1e-6;
using point = struct ___{int x, y;};
using vn = struct _e__{int u, v;};
int n;
double a[N][N];
bool gauss(){
	int sum = 0;
	for(int line = 0; line < n; line++){
		int maxn = sum;
		for(int i = sum; i < n; i++){
			if(fabs(a[i][line]) > fabs(a[maxn][line])){
				maxn = i;
			}
		}
		if(fabs(a[maxn][line]) < eps){
			return 1;
		}
		for(int i = line; i < n + 1; i++){
			swap(a[maxn][i], a[sum][i]);
		}
		for(int i = n; i >= line; i--){
			a[sum][i] /= a[sum][line];
		}
		for(int i = sum + 1; i < n; i++){
			if(fabs(a[i][line]) > eps){
    			for(int j = n; j >= line; j--){
    				a[i][j] -= a[sum][j] * a[i][line];
    			}
			}
		}
		sum++;
	}
	for(int i = n - 1; i >= 0; i--){
		for(int j = i + 1; j < n; j++){
			a[i][n] -= a[j][n] * a[i][j];
		}
	}
	return 0;
}
void work(){
    //please write your code here
	cin >> n;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n + 1; j++){
			cin >> a[i][j];
		}
	}
	bool t = gauss();
	if(t == 0){
		for(int i = 0; i < n; i++){
			cout << fixed << setprecision(2) << a[i][n] << "\n";
		}
	}else{
		cout << "No Solution\n";
	}
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t = 1; while(t--){work();}
	return 0;
}

线性方程组

无解和无穷解的判定

而无解和无穷解情况的不同就在于,做到某一行你会找不到主元(即当前及下面的行的该列均为 0)

假设当我们第一次遇到找不到主元的情况时

我们可以发现:

  • 当前列左边的数仍均为 0
  • 当前操作列及其下面的数均为 0

于是便想到转而处理下一列,操作行不变,以维持这样的性质

现在我们对所有找不到主元的情况都按上述方法进行处理,当我们遍历完了所有列,当前矩阵也仍满足性质:

  • 每次操作列左边的数均为 0
  • 每次操作列从那次操作行下面一行开始全为 0
C++
/*

writer:  Luna_Frome_Starry
Problem: 不知名屑题
Knowledge: 垃圾算法
Time: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:

*/
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 1005;
const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
const double eps = 1e-9;
using point = struct ___{int x, y;};
using vn = struct _e__{int u, v;};
int n;
double a[N][N];
int gauss(){
	int sum = 0;
	for(int line = 0; line < n; line++){
		int maxn = sum;
		for(int i = sum; i < n; i++){
			if(fabs(a[i][line]) > fabs(a[maxn][line])){
				maxn = i;
			}
		}
		if(fabs(a[maxn][line]) < eps){
			continue;
		}
		for(int i = line; i < n + 1; i++){
			swap(a[maxn][i], a[sum][i]);
		}
		for(int i = n; i >= line; i--){
			a[sum][i] /= a[sum][line];
		}
		for(int i = sum + 1; i < n; i++){
			if(i == sum) continue;
			if(fabs(a[i][line]) > eps){
    			for(int j = n; j >= line; j--){
    				a[i][j] -= a[sum][j] * a[i][line];
    			}
			}
		}
		sum++;
	}
	if(sum < n){
		return sum;
	}
	for(int i = n - 1; i >= 0; i--){
		for(int j = i + 1; j < n; j++){
			a[i][n] -= a[j][n] * a[i][j];
		}
	}
	return 0;
}
void work(){
    //please write your code here
	cin >> n;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n + 1; j++){
			cin >> a[i][j];
		}
	}
	int t = gauss();
	if(t == 0){
		for(int i = 0; i < n; i++){
			cout << "x" << i + 1 << "=" << fixed << setprecision(2) << a[i][n] << "\n";
		}
		return ;
	}
	int f = 2;
	while(t < n){
		if(!(fabs(a[t++][n]) < eps)){
			f = -1;
			break;
		}
		f = 0;
	}
	if(f == -1){
		cout << "-1\n";//无解
	}else if(f == 0){
		cout << "0\n";//无数解
	}
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t = 1; while(t--){work();}
	return 0;
}