Skip to content

Instantly share code, notes, and snippets.

@edwardmjm
Created January 22, 2014 15:12
Embed
What would you like to do?
#include <cmath>
const int MAXN = 100;
const double EPS = 1e-10;
//列主元gauss消去求解a[][]x[]=b[]
//返回是否有唯一解,若有解在b[]中
bool gaussCpivot(int n, double a[][MAXN], double b[]) {
int i, j, k, row;
double maxp, t;
for (k = 0; k < n; k++) {
for (maxp = 0, i = k; i < n; i++) {
if (fabs(a[i][k]) > fabs(maxp)) {
maxp = a[row = i][k];
}
}
if (fabs(maxp) < EPS) {
return false;
}
if (row != k) {
for (j = k; j < n; j++) {
swap(a[k][j], a[row][j]);
}
swap(b[k], b[row]);
}
for (j = k + 1; j < n; j++) {
a[k][j] /= maxp;
for (i = k + 1; i < n; i++) {
a[i][j] -= a[i][k] * a[k][j];
}
}
b[k] /= maxp;
for (i = k + 1; i < n; i++) {
b[i] -= b[k] * a[i][k];
}
}
for (i = n - 1; i >= 0; i--) {
for (j = i + 1; j < n; j++) {
b[i] -= a[i][j] * b[j];
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment