-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrref.cpp
57 lines (42 loc) · 1.06 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "rref.hpp"
#include <armadillo>
using namespace arma;
arma::Mat<double> mat_to_rref(const arma::Mat<double>& b, const double EPSILON)
{
// based on https://github.com/t3nsor/codebook
mat a = b;
uword n_rows = a.n_rows;
uword n_cols = a.n_cols;
uword target_row = 0;
for (uword col = 0; col < n_cols; ++col)
{
uword source_row = target_row;
for (uword row = target_row + 1; row < n_rows; ++row)
{
if (fabs(a(row, col)) > fabs(a(source_row, col)))
{
source_row = row;
}
}
if (fabs(a(source_row, col)) < EPSILON) continue;
a.swap_rows(source_row, target_row);
double s = 1.0 / a(target_row, col);
for (uword col = 0; col < n_cols; ++col)
{
a(target_row, col) *= s;
}
for (uword row = 0; row < n_rows; ++row)
{
if (row != target_row)
{
double s = a(row, col);
for (int col = 0; col < n_cols; ++col)
{
a(row, col) -= s * a(target_row, col);
}
}
}
if (++target_row == n_rows) break;
}
return a;
}