1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.linear;
19
20 import java.util.Arrays;
21
22 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
23 import org.apache.commons.math4.core.jdkmath.JdkMath;
24 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
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 public class QRDecomposition {
52
53
54
55
56
57
58 private double[][] qrt;
59
60 private double[] rDiag;
61
62 private RealMatrix cachedQ;
63
64 private RealMatrix cachedQT;
65
66 private RealMatrix cachedR;
67
68 private RealMatrix cachedH;
69
70 private final double threshold;
71
72
73
74
75
76
77
78
79
80 public QRDecomposition(RealMatrix matrix) {
81 this(matrix, 0d);
82 }
83
84
85
86
87
88
89
90
91
92
93 public QRDecomposition(RealMatrix matrix,
94 double threshold) {
95 this.threshold = threshold;
96
97 final int m = matrix.getRowDimension();
98 final int n = matrix.getColumnDimension();
99 qrt = matrix.transpose().getData();
100 rDiag = new double[JdkMath.min(m, n)];
101 cachedQ = null;
102 cachedQT = null;
103 cachedR = null;
104 cachedH = null;
105
106 decompose(qrt);
107 }
108
109
110
111
112
113 protected void decompose(double[][] matrix) {
114 for (int minor = 0; minor < JdkMath.min(matrix.length, matrix[0].length); minor++) {
115 performHouseholderReflection(minor, matrix);
116 }
117 }
118
119
120
121
122
123
124 protected void performHouseholderReflection(int minor, double[][] matrix) {
125
126 final double[] qrtMinor = matrix[minor];
127
128
129
130
131
132
133
134
135 double xNormSqr = 0;
136 for (int row = minor; row < qrtMinor.length; row++) {
137 final double c = qrtMinor[row];
138 xNormSqr += c * c;
139 }
140 final double a = (qrtMinor[minor] > 0) ? -JdkMath.sqrt(xNormSqr) : JdkMath.sqrt(xNormSqr);
141 rDiag[minor] = a;
142
143 if (a != 0.0) {
144
145
146
147
148
149
150
151
152
153 qrtMinor[minor] -= a;
154
155
156
157
158
159
160
161
162
163
164
165
166
167 for (int col = minor+1; col < matrix.length; col++) {
168 final double[] qrtCol = matrix[col];
169 double alpha = 0;
170 for (int row = minor; row < qrtCol.length; row++) {
171 alpha -= qrtCol[row] * qrtMinor[row];
172 }
173 alpha /= a * qrtMinor[minor];
174
175
176 for (int row = minor; row < qrtCol.length; row++) {
177 qrtCol[row] -= alpha * qrtMinor[row];
178 }
179 }
180 }
181 }
182
183
184
185
186
187
188
189 public RealMatrix getR() {
190
191 if (cachedR == null) {
192
193
194 final int n = qrt.length;
195 final int m = qrt[0].length;
196 double[][] ra = new double[m][n];
197
198 for (int row = JdkMath.min(m, n) - 1; row >= 0; row--) {
199 ra[row][row] = rDiag[row];
200 for (int col = row + 1; col < n; col++) {
201 ra[row][col] = qrt[col][row];
202 }
203 }
204 cachedR = MatrixUtils.createRealMatrix(ra);
205 }
206
207
208 return cachedR;
209 }
210
211
212
213
214
215
216 public RealMatrix getQ() {
217 if (cachedQ == null) {
218 cachedQ = getQT().transpose();
219 }
220 return cachedQ;
221 }
222
223
224
225
226
227
228 public RealMatrix getQT() {
229 if (cachedQT == null) {
230
231
232 final int n = qrt.length;
233 final int m = qrt[0].length;
234 double[][] qta = new double[m][m];
235
236
237
238
239
240
241 for (int minor = m - 1; minor >= JdkMath.min(m, n); minor--) {
242 qta[minor][minor] = 1.0d;
243 }
244
245 for (int minor = JdkMath.min(m, n)-1; minor >= 0; minor--){
246 final double[] qrtMinor = qrt[minor];
247 qta[minor][minor] = 1.0d;
248 if (qrtMinor[minor] != 0.0) {
249 for (int col = minor; col < m; col++) {
250 double alpha = 0;
251 for (int row = minor; row < m; row++) {
252 alpha -= qta[col][row] * qrtMinor[row];
253 }
254 alpha /= rDiag[minor] * qrtMinor[minor];
255
256 for (int row = minor; row < m; row++) {
257 qta[col][row] += -alpha * qrtMinor[row];
258 }
259 }
260 }
261 }
262 cachedQT = MatrixUtils.createRealMatrix(qta);
263 }
264
265
266 return cachedQT;
267 }
268
269
270
271
272
273
274
275
276 public RealMatrix getH() {
277 if (cachedH == null) {
278
279 final int n = qrt.length;
280 final int m = qrt[0].length;
281 double[][] ha = new double[m][n];
282 for (int i = 0; i < m; ++i) {
283 for (int j = 0; j < JdkMath.min(i + 1, n); ++j) {
284 ha[i][j] = qrt[j][i] / -rDiag[j];
285 }
286 }
287 cachedH = MatrixUtils.createRealMatrix(ha);
288 }
289
290
291 return cachedH;
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306 public DecompositionSolver getSolver() {
307 return new Solver(qrt, rDiag, threshold);
308 }
309
310
311 private static final class Solver implements DecompositionSolver {
312
313
314
315
316
317
318 private final double[][] qrt;
319
320 private final double[] rDiag;
321
322 private final double threshold;
323
324
325
326
327
328
329
330
331 private Solver(final double[][] qrt,
332 final double[] rDiag,
333 final double threshold) {
334 this.qrt = qrt;
335 this.rDiag = rDiag;
336 this.threshold = threshold;
337 }
338
339
340 @Override
341 public boolean isNonSingular() {
342 return !checkSingular(rDiag, threshold, false);
343 }
344
345
346 @Override
347 public RealVector solve(RealVector b) {
348 final int n = qrt.length;
349 final int m = qrt[0].length;
350 if (b.getDimension() != m) {
351 throw new DimensionMismatchException(b.getDimension(), m);
352 }
353 checkSingular(rDiag, threshold, true);
354
355 final double[] x = new double[n];
356 final double[] y = b.toArray();
357
358
359 for (int minor = 0; minor < JdkMath.min(m, n); minor++) {
360
361 final double[] qrtMinor = qrt[minor];
362 double dotProduct = 0;
363 for (int row = minor; row < m; row++) {
364 dotProduct += y[row] * qrtMinor[row];
365 }
366 dotProduct /= rDiag[minor] * qrtMinor[minor];
367
368 for (int row = minor; row < m; row++) {
369 y[row] += dotProduct * qrtMinor[row];
370 }
371 }
372
373
374 for (int row = rDiag.length - 1; row >= 0; --row) {
375 y[row] /= rDiag[row];
376 final double yRow = y[row];
377 final double[] qrtRow = qrt[row];
378 x[row] = yRow;
379 for (int i = 0; i < row; i++) {
380 y[i] -= yRow * qrtRow[i];
381 }
382 }
383
384 return new ArrayRealVector(x, false);
385 }
386
387
388 @Override
389 public RealMatrix solve(RealMatrix b) {
390 final int n = qrt.length;
391 final int m = qrt[0].length;
392 if (b.getRowDimension() != m) {
393 throw new DimensionMismatchException(b.getRowDimension(), m);
394 }
395 checkSingular(rDiag, threshold, true);
396
397 final int columns = b.getColumnDimension();
398 final int blockSize = BlockRealMatrix.BLOCK_SIZE;
399 final int cBlocks = (columns + blockSize - 1) / blockSize;
400 final double[][] xBlocks = BlockRealMatrix.createBlocksLayout(n, columns);
401 final double[][] y = new double[b.getRowDimension()][blockSize];
402 final double[] alpha = new double[blockSize];
403
404 for (int kBlock = 0; kBlock < cBlocks; ++kBlock) {
405 final int kStart = kBlock * blockSize;
406 final int kEnd = JdkMath.min(kStart + blockSize, columns);
407 final int kWidth = kEnd - kStart;
408
409
410 b.copySubMatrix(0, m - 1, kStart, kEnd - 1, y);
411
412
413 for (int minor = 0; minor < JdkMath.min(m, n); minor++) {
414 final double[] qrtMinor = qrt[minor];
415 final double factor = 1.0 / (rDiag[minor] * qrtMinor[minor]);
416
417 Arrays.fill(alpha, 0, kWidth, 0.0);
418 for (int row = minor; row < m; ++row) {
419 final double d = qrtMinor[row];
420 final double[] yRow = y[row];
421 for (int k = 0; k < kWidth; ++k) {
422 alpha[k] += d * yRow[k];
423 }
424 }
425 for (int k = 0; k < kWidth; ++k) {
426 alpha[k] *= factor;
427 }
428
429 for (int row = minor; row < m; ++row) {
430 final double d = qrtMinor[row];
431 final double[] yRow = y[row];
432 for (int k = 0; k < kWidth; ++k) {
433 yRow[k] += alpha[k] * d;
434 }
435 }
436 }
437
438
439 for (int j = rDiag.length - 1; j >= 0; --j) {
440 final int jBlock = j / blockSize;
441 final int jStart = jBlock * blockSize;
442 final double factor = 1.0 / rDiag[j];
443 final double[] yJ = y[j];
444 final double[] xBlock = xBlocks[jBlock * cBlocks + kBlock];
445 int index = (j - jStart) * kWidth;
446 for (int k = 0; k < kWidth; ++k) {
447 yJ[k] *= factor;
448 xBlock[index++] = yJ[k];
449 }
450
451 final double[] qrtJ = qrt[j];
452 for (int i = 0; i < j; ++i) {
453 final double rIJ = qrtJ[i];
454 final double[] yI = y[i];
455 for (int k = 0; k < kWidth; ++k) {
456 yI[k] -= yJ[k] * rIJ;
457 }
458 }
459 }
460 }
461
462 return new BlockRealMatrix(n, columns, xBlocks, false);
463 }
464
465
466
467
468
469 @Override
470 public RealMatrix getInverse() {
471 return solve(MatrixUtils.createRealIdentityMatrix(qrt[0].length));
472 }
473
474
475
476
477
478
479
480
481
482
483
484
485
486 private static boolean checkSingular(double[] diag,
487 double min,
488 boolean raise) {
489 final int len = diag.length;
490 for (int i = 0; i < len; i++) {
491 final double d = diag[i];
492 if (JdkMath.abs(d) <= min) {
493 if (raise) {
494 final SingularMatrixException e = new SingularMatrixException();
495 e.getContext().addMessage(LocalizedFormats.NUMBER_TOO_SMALL, d, min);
496 e.getContext().addMessage(LocalizedFormats.INDEX, i);
497 throw e;
498 } else {
499 return true;
500 }
501 }
502 }
503 return false;
504 }
505 }
506 }