forked from t3nsor/codebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rref.cpp
43 lines (39 loc) · 938 Bytes
/
rref.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Reduced row echelon form via Gauss-Jordan elimination
// with partial pivoting. This can be used for computing
// the rank of a matrix.
//
// Running time: O(n^3)
//
// INPUT: a[][] = an nxn matrix
//
// OUTPUT: rref[][] = an nxm matrix (stored in a[][])
// returns rank of a[][]
const double EPSILON = 1e-10;
typedef double T;
typedef vector<T> VT;
typedef vector<VT> VVT;
int rref(VVT &a) {
int n = a.size();
int m = a[0].size();
int r = 0;
for (int c = 0; c < m; c++) {
int j = r;
for (int i = r + 1; i < n; i++)
if (fabs(a[i][c]) > fabs(a[j][c]))
j = i;
if (fabs(a[j][c]) < EPSILON)
continue;
swap(a[j], a[r]);
T s = 1.0 / a[r][c];
for (int j = 0; j < m; j++)
a[r][j] *= s;
for (int i = 0; i < n; i++)
if (i != r) {
T t = a[i][c];
for (int j = 0; j < m; j++)
a[i][j] -= t * a[r][j];
}
r++;
}
return r;
}