-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr.js
149 lines (132 loc) · 3.32 KB
/
qr.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import Matrix from './matrix.js';
import WrapperMatrix2D from './WrapperMatrix2D.js';
import { hypotenuse } from './utilDCMatrix.js';
export default class QrDecomposition {
constructor(value) {
value = WrapperMatrix2D.checkMatrix(value);
let qr = value.clone();
let m = value.rows;
let n = value.columns;
let rdiag = new Float64Array(n);
let i, j, k, s;
for (k = 0; k < n; k++) {
let nrm = 0;
for (i = k; i < m; i++) {
nrm = hypotenuse(nrm, qr.get(i, k));
}
if (nrm !== 0) {
if (qr.get(k, k) < 0) {
nrm = -nrm;
}
for (i = k; i < m; i++) {
qr.set(i, k, qr.get(i, k) / nrm);
}
qr.set(k, k, qr.get(k, k) + 1);
for (j = k + 1; j < n; j++) {
s = 0;
for (i = k; i < m; i++) {
s += qr.get(i, k) * qr.get(i, j);
}
s = -s / qr.get(k, k);
for (i = k; i < m; i++) {
qr.set(i, j, qr.get(i, j) + s * qr.get(i, k));
}
}
}
rdiag[k] = -nrm;
}
this.QR = qr;
this.Rdiag = rdiag;
}
solve(value) {
value = Matrix.checkMatrix(value);
let qr = this.QR;
let m = qr.rows;
if (value.rows !== m) {
throw new Error('Matrix row dimensions must agree');
}
if (!this.isFullRank()) {
throw new Error('Matrix is rank deficient');
}
let count = value.columns;
let X = value.clone();
let n = qr.columns;
let i, j, k, s;
for (k = 0; k < n; k++) {
for (j = 0; j < count; j++) {
s = 0;
for (i = k; i < m; i++) {
s += qr.get(i, k) * X.get(i, j);
}
s = -s / qr.get(k, k);
for (i = k; i < m; i++) {
X.set(i, j, X.get(i, j) + s * qr.get(i, k));
}
}
}
for (k = n - 1; k >= 0; k--) {
for (j = 0; j < count; j++) {
X.set(k, j, X.get(k, j) / this.Rdiag[k]);
}
for (i = 0; i < k; i++) {
for (j = 0; j < count; j++) {
X.set(i, j, X.get(i, j) - X.get(k, j) * qr.get(i, k));
}
}
}
return X.subMatrix(0, n - 1, 0, count - 1);
}
isFullRank() {
let columns = this.QR.columns;
for (let i = 0; i < columns; i++) {
if (this.Rdiag[i] === 0) {
return false;
}
}
return true;
}
get upperTriangularMatrix() {
let qr = this.QR;
let n = qr.columns;
let X = new Matrix(n, n);
let i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i < j) {
X.set(i, j, qr.get(i, j));
} else if (i === j) {
X.set(i, j, this.Rdiag[i]);
} else {
X.set(i, j, 0);
}
}
}
return X;
}
get orthogonalMatrix() {
let qr = this.QR;
let rows = qr.rows;
let columns = qr.columns;
let X = new Matrix(rows, columns);
let i, j, k, s;
for (k = columns - 1; k >= 0; k--) {
for (i = 0; i < rows; i++) {
X.set(i, k, 0);
}
X.set(k, k, 1);
for (j = k; j < columns; j++) {
if (qr.get(k, k) !== 0) {
s = 0;
for (i = k; i < rows; i++) {
s += qr.get(i, k) * X.get(i, j);
}
s = -s / qr.get(k, k);
for (i = k; i < rows; i++) {
X.set(i, j, X.get(i, j) + s * qr.get(i, k));
}
}
}
}
return X;
}
}