001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.linear;
018
019 import org.apache.commons.math.exception.NumberIsTooLargeException;
020 import org.apache.commons.math.exception.util.LocalizedFormats;
021 import org.apache.commons.math.util.FastMath;
022 import org.apache.commons.math.util.MathUtils;
023
024 /**
025 * Calculates the compact Singular Value Decomposition of a matrix.
026 * <p>
027 * The Singular Value Decomposition of matrix A is a set of three matrices: U,
028 * Σ and V such that A = U × Σ × V<sup>T</sup>. Let A be
029 * a m × n matrix, then U is a m × p orthogonal matrix, Σ is a
030 * p × p diagonal matrix with positive or null elements, V is a p ×
031 * n orthogonal matrix (hence V<sup>T</sup> is also orthogonal) where
032 * p=min(m,n).
033 * </p>
034 * <p>This class is similar to the class with similar name from the
035 * <a href="http://math.nist.gov/javanumerics/jama/">JAMA</a> library, with the
036 * following changes:</p>
037 * <ul>
038 * <li>the {@code norm2} method which has been renamed as {@link #getNorm()
039 * getNorm},</li>
040 * <li>the {@code cond} method which has been renamed as {@link
041 * #getConditionNumber() getConditionNumber},</li>
042 * <li>the {@code rank} method which has been renamed as {@link #getRank()
043 * getRank},</li>
044 * <li>a {@link #getUT() getUT} method has been added,</li>
045 * <li>a {@link #getVT() getVT} method has been added,</li>
046 * <li>a {@link #getSolver() getSolver} method has been added,</li>
047 * <li>a {@link #getCovariance(double) getCovariance} method has been added.</li>
048 * </ul>
049 * @see <a href="http://mathworld.wolfram.com/SingularValueDecomposition.html">MathWorld</a>
050 * @see <a href="http://en.wikipedia.org/wiki/Singular_value_decomposition">Wikipedia</a>
051 * @version $Id: SingularValueDecomposition.java 1177938 2011-10-01 07:57:19Z celestin $
052 * @since 2.0 (changed to concrete class in 3.0)
053 */
054 public class SingularValueDecomposition {
055 /** Relative threshold for small singular values. */
056 private static final double EPS = 0x1.0p-52;
057 /** Absolute threshold for small singular values. */
058 private static final double TINY = 0x1.0p-966;
059 /** Computed singular values. */
060 private final double[] singularValues;
061 /** max(row dimension, column dimension). */
062 private final int m;
063 /** min(row dimension, column dimension). */
064 private final int n;
065 /** Indicator for transposed matrix. */
066 private final boolean transposed;
067 /** Cached value of U matrix. */
068 private final RealMatrix cachedU;
069 /** Cached value of transposed U matrix. */
070 private RealMatrix cachedUt;
071 /** Cached value of S (diagonal) matrix. */
072 private RealMatrix cachedS;
073 /** Cached value of V matrix. */
074 private final RealMatrix cachedV;
075 /** Cached value of transposed V matrix. */
076 private RealMatrix cachedVt;
077 /**
078 * Tolerance value for small singular values, calculated once we have
079 * populated "singularValues".
080 **/
081 private final double tol;
082
083 /**
084 * Calculates the compact Singular Value Decomposition of the given matrix.
085 *
086 * @param matrix Matrix to decompose.
087 */
088 public SingularValueDecomposition(final RealMatrix matrix) {
089 final double[][] A;
090
091 // "m" is always the largest dimension.
092 if (matrix.getRowDimension() < matrix.getColumnDimension()) {
093 transposed = true;
094 A = matrix.transpose().getData();
095 m = matrix.getColumnDimension();
096 n = matrix.getRowDimension();
097 } else {
098 transposed = false;
099 A = matrix.getData();
100 m = matrix.getRowDimension();
101 n = matrix.getColumnDimension();
102 }
103
104 singularValues = new double[n];
105 final double[][] U = new double[m][n];
106 final double[][] V = new double[n][n];
107 final double[] e = new double[n];
108 final double[] work = new double[m];
109 // Reduce A to bidiagonal form, storing the diagonal elements
110 // in s and the super-diagonal elements in e.
111 final int nct = FastMath.min(m - 1, n);
112 final int nrt = FastMath.max(0, n - 2);
113 for (int k = 0; k < FastMath.max(nct, nrt); k++) {
114 if (k < nct) {
115 // Compute the transformation for the k-th column and
116 // place the k-th diagonal in s[k].
117 // Compute 2-norm of k-th column without under/overflow.
118 singularValues[k] = 0;
119 for (int i = k; i < m; i++) {
120 singularValues[k] = FastMath.hypot(singularValues[k], A[i][k]);
121 }
122 if (singularValues[k] != 0) {
123 if (A[k][k] < 0) {
124 singularValues[k] = -singularValues[k];
125 }
126 for (int i = k; i < m; i++) {
127 A[i][k] /= singularValues[k];
128 }
129 A[k][k] += 1;
130 }
131 singularValues[k] = -singularValues[k];
132 }
133 for (int j = k + 1; j < n; j++) {
134 if (k < nct &&
135 singularValues[k] != 0) {
136 // Apply the transformation.
137 double t = 0;
138 for (int i = k; i < m; i++) {
139 t += A[i][k] * A[i][j];
140 }
141 t = -t / A[k][k];
142 for (int i = k; i < m; i++) {
143 A[i][j] += t * A[i][k];
144 }
145 }
146 // Place the k-th row of A into e for the
147 // subsequent calculation of the row transformation.
148 e[j] = A[k][j];
149 }
150 if (k < nct) {
151 // Place the transformation in U for subsequent back
152 // multiplication.
153 for (int i = k; i < m; i++) {
154 U[i][k] = A[i][k];
155 }
156 }
157 if (k < nrt) {
158 // Compute the k-th row transformation and place the
159 // k-th super-diagonal in e[k].
160 // Compute 2-norm without under/overflow.
161 e[k] = 0;
162 for (int i = k + 1; i < n; i++) {
163 e[k] = FastMath.hypot(e[k], e[i]);
164 }
165 if (e[k] != 0) {
166 if (e[k + 1] < 0) {
167 e[k] = -e[k];
168 }
169 for (int i = k + 1; i < n; i++) {
170 e[i] /= e[k];
171 }
172 e[k + 1] += 1;
173 }
174 e[k] = -e[k];
175 if (k + 1 < m &&
176 e[k] != 0) {
177 // Apply the transformation.
178 for (int i = k + 1; i < m; i++) {
179 work[i] = 0;
180 }
181 for (int j = k + 1; j < n; j++) {
182 for (int i = k + 1; i < m; i++) {
183 work[i] += e[j] * A[i][j];
184 }
185 }
186 for (int j = k + 1; j < n; j++) {
187 final double t = -e[j] / e[k + 1];
188 for (int i = k + 1; i < m; i++) {
189 A[i][j] += t * work[i];
190 }
191 }
192 }
193
194 // Place the transformation in V for subsequent
195 // back multiplication.
196 for (int i = k + 1; i < n; i++) {
197 V[i][k] = e[i];
198 }
199 }
200 }
201 // Set up the final bidiagonal matrix or order p.
202 int p = n;
203 if (nct < n) {
204 singularValues[nct] = A[nct][nct];
205 }
206 if (m < p) {
207 singularValues[p - 1] = 0;
208 }
209 if (nrt + 1 < p) {
210 e[nrt] = A[nrt][p - 1];
211 }
212 e[p - 1] = 0;
213
214 // Generate U.
215 for (int j = nct; j < n; j++) {
216 for (int i = 0; i < m; i++) {
217 U[i][j] = 0;
218 }
219 U[j][j] = 1;
220 }
221 for (int k = nct - 1; k >= 0; k--) {
222 if (singularValues[k] != 0) {
223 for (int j = k + 1; j < n; j++) {
224 double t = 0;
225 for (int i = k; i < m; i++) {
226 t += U[i][k] * U[i][j];
227 }
228 t = -t / U[k][k];
229 for (int i = k; i < m; i++) {
230 U[i][j] += t * U[i][k];
231 }
232 }
233 for (int i = k; i < m; i++) {
234 U[i][k] = -U[i][k];
235 }
236 U[k][k] = 1 + U[k][k];
237 for (int i = 0; i < k - 1; i++) {
238 U[i][k] = 0;
239 }
240 } else {
241 for (int i = 0; i < m; i++) {
242 U[i][k] = 0;
243 }
244 U[k][k] = 1;
245 }
246 }
247
248 // Generate V.
249 for (int k = n - 1; k >= 0; k--) {
250 if (k < nrt &&
251 e[k] != 0) {
252 for (int j = k + 1; j < n; j++) {
253 double t = 0;
254 for (int i = k + 1; i < n; i++) {
255 t += V[i][k] * V[i][j];
256 }
257 t = -t / V[k + 1][k];
258 for (int i = k + 1; i < n; i++) {
259 V[i][j] += t * V[i][k];
260 }
261 }
262 }
263 for (int i = 0; i < n; i++) {
264 V[i][k] = 0;
265 }
266 V[k][k] = 1;
267 }
268
269 // Main iteration loop for the singular values.
270 final int pp = p - 1;
271 int iter = 0;
272 while (p > 0) {
273 int k;
274 int kase;
275 // Here is where a test for too many iterations would go.
276 // This section of the program inspects for
277 // negligible elements in the s and e arrays. On
278 // completion the variables kase and k are set as follows.
279 // kase = 1 if s(p) and e[k-1] are negligible and k<p
280 // kase = 2 if s(k) is negligible and k<p
281 // kase = 3 if e[k-1] is negligible, k<p, and
282 // s(k), ..., s(p) are not negligible (qr step).
283 // kase = 4 if e(p-1) is negligible (convergence).
284 for (k = p - 2; k >= 0; k--) {
285 final double threshold
286 = TINY + EPS * (FastMath.abs(singularValues[k]) +
287 FastMath.abs(singularValues[k + 1]));
288 if (FastMath.abs(e[k]) <= threshold) {
289 e[k] = 0;
290 break;
291 }
292 }
293
294 if (k == p - 2) {
295 kase = 4;
296 } else {
297 int ks;
298 for (ks = p - 1; ks >= k; ks--) {
299 if (ks == k) {
300 break;
301 }
302 final double t = (ks != p ? FastMath.abs(e[ks]) : 0) +
303 (ks != k + 1 ? FastMath.abs(e[ks - 1]) : 0);
304 if (FastMath.abs(singularValues[ks]) <= TINY + EPS * t) {
305 singularValues[ks] = 0;
306 break;
307 }
308 }
309 if (ks == k) {
310 kase = 3;
311 } else if (ks == p - 1) {
312 kase = 1;
313 } else {
314 kase = 2;
315 k = ks;
316 }
317 }
318 k++;
319 // Perform the task indicated by kase.
320 switch (kase) {
321 // Deflate negligible s(p).
322 case 1: {
323 double f = e[p - 2];
324 e[p - 2] = 0;
325 for (int j = p - 2; j >= k; j--) {
326 double t = FastMath.hypot(singularValues[j], f);
327 final double cs = singularValues[j] / t;
328 final double sn = f / t;
329 singularValues[j] = t;
330 if (j != k) {
331 f = -sn * e[j - 1];
332 e[j - 1] = cs * e[j - 1];
333 }
334
335 for (int i = 0; i < n; i++) {
336 t = cs * V[i][j] + sn * V[i][p - 1];
337 V[i][p - 1] = -sn * V[i][j] + cs * V[i][p - 1];
338 V[i][j] = t;
339 }
340 }
341 }
342 break;
343 // Split at negligible s(k).
344 case 2: {
345 double f = e[k - 1];
346 e[k - 1] = 0;
347 for (int j = k; j < p; j++) {
348 double t = FastMath.hypot(singularValues[j], f);
349 final double cs = singularValues[j] / t;
350 final double sn = f / t;
351 singularValues[j] = t;
352 f = -sn * e[j];
353 e[j] = cs * e[j];
354
355 for (int i = 0; i < m; i++) {
356 t = cs * U[i][j] + sn * U[i][k - 1];
357 U[i][k - 1] = -sn * U[i][j] + cs * U[i][k - 1];
358 U[i][j] = t;
359 }
360 }
361 }
362 break;
363 // Perform one qr step.
364 case 3: {
365 // Calculate the shift.
366 final double scale = FastMath.max(FastMath.max(FastMath.max(FastMath.max(
367 FastMath.abs(singularValues[p - 1]), FastMath.abs(singularValues[p - 2])), FastMath.abs(e[p - 2])),
368 FastMath.abs(singularValues[k])), FastMath.abs(e[k]));
369 final double sp = singularValues[p - 1] / scale;
370 final double spm1 = singularValues[p - 2] / scale;
371 final double epm1 = e[p - 2] / scale;
372 final double sk = singularValues[k] / scale;
373 final double ek = e[k] / scale;
374 final double b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0;
375 final double c = (sp * epm1) * (sp * epm1);
376 double shift = 0;
377 if (b != 0 ||
378 c != 0) {
379 shift = FastMath.sqrt(b * b + c);
380 if (b < 0) {
381 shift = -shift;
382 }
383 shift = c / (b + shift);
384 }
385 double f = (sk + sp) * (sk - sp) + shift;
386 double g = sk * ek;
387 // Chase zeros.
388 for (int j = k; j < p - 1; j++) {
389 double t = FastMath.hypot(f, g);
390 double cs = f / t;
391 double sn = g / t;
392 if (j != k) {
393 e[j - 1] = t;
394 }
395 f = cs * singularValues[j] + sn * e[j];
396 e[j] = cs * e[j] - sn * singularValues[j];
397 g = sn * singularValues[j + 1];
398 singularValues[j + 1] = cs * singularValues[j + 1];
399
400 for (int i = 0; i < n; i++) {
401 t = cs * V[i][j] + sn * V[i][j + 1];
402 V[i][j + 1] = -sn * V[i][j] + cs * V[i][j + 1];
403 V[i][j] = t;
404 }
405 t = FastMath.hypot(f, g);
406 cs = f / t;
407 sn = g / t;
408 singularValues[j] = t;
409 f = cs * e[j] + sn * singularValues[j + 1];
410 singularValues[j + 1] = -sn * e[j] + cs * singularValues[j + 1];
411 g = sn * e[j + 1];
412 e[j + 1] = cs * e[j + 1];
413 if (j < m - 1) {
414 for (int i = 0; i < m; i++) {
415 t = cs * U[i][j] + sn * U[i][j + 1];
416 U[i][j + 1] = -sn * U[i][j] + cs * U[i][j + 1];
417 U[i][j] = t;
418 }
419 }
420 }
421 e[p - 2] = f;
422 iter = iter + 1;
423 }
424 break;
425 // Convergence.
426 default: {
427 // Make the singular values positive.
428 if (singularValues[k] <= 0) {
429 singularValues[k] = singularValues[k] < 0 ? -singularValues[k] : 0;
430
431 for (int i = 0; i <= pp; i++) {
432 V[i][k] = -V[i][k];
433 }
434 }
435 // Order the singular values.
436 while (k < pp) {
437 if (singularValues[k] >= singularValues[k + 1]) {
438 break;
439 }
440 double t = singularValues[k];
441 singularValues[k] = singularValues[k + 1];
442 singularValues[k + 1] = t;
443 if (k < n - 1) {
444 for (int i = 0; i < n; i++) {
445 t = V[i][k + 1];
446 V[i][k + 1] = V[i][k];
447 V[i][k] = t;
448 }
449 }
450 if (k < m - 1) {
451 for (int i = 0; i < m; i++) {
452 t = U[i][k + 1];
453 U[i][k + 1] = U[i][k];
454 U[i][k] = t;
455 }
456 }
457 k++;
458 }
459 iter = 0;
460 p--;
461 }
462 break;
463 }
464 }
465
466 // Set the small value tolerance used to calculate rank and pseudo-inverse
467 tol = FastMath.max(m * singularValues[0] * EPS,
468 FastMath.sqrt(MathUtils.SAFE_MIN));
469
470 if (!transposed) {
471 cachedU = MatrixUtils.createRealMatrix(U);
472 cachedV = MatrixUtils.createRealMatrix(V);
473 } else {
474 cachedU = MatrixUtils.createRealMatrix(V);
475 cachedV = MatrixUtils.createRealMatrix(U);
476 }
477 }
478
479 /**
480 * Returns the matrix U of the decomposition.
481 * <p>U is an orthogonal matrix, i.e. its transpose is also its inverse.</p>
482 * @return the U matrix
483 * @see #getUT()
484 */
485 public RealMatrix getU() {
486 // return the cached matrix
487 return cachedU;
488
489 }
490
491 /**
492 * Returns the transpose of the matrix U of the decomposition.
493 * <p>U is an orthogonal matrix, i.e. its transpose is also its inverse.</p>
494 * @return the U matrix (or null if decomposed matrix is singular)
495 * @see #getU()
496 */
497 public RealMatrix getUT() {
498 if (cachedUt == null) {
499 cachedUt = getU().transpose();
500 }
501 // return the cached matrix
502 return cachedUt;
503 }
504
505 /**
506 * Returns the diagonal matrix Σ of the decomposition.
507 * <p>Σ is a diagonal matrix. The singular values are provided in
508 * non-increasing order, for compatibility with Jama.</p>
509 * @return the Σ matrix
510 */
511 public RealMatrix getS() {
512 if (cachedS == null) {
513 // cache the matrix for subsequent calls
514 cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues);
515 }
516 return cachedS;
517 }
518
519 /**
520 * Returns the diagonal elements of the matrix Σ of the decomposition.
521 * <p>The singular values are provided in non-increasing order, for
522 * compatibility with Jama.</p>
523 * @return the diagonal elements of the Σ matrix
524 */
525 public double[] getSingularValues() {
526 return singularValues.clone();
527 }
528
529 /**
530 * Returns the matrix V of the decomposition.
531 * <p>V is an orthogonal matrix, i.e. its transpose is also its inverse.</p>
532 * @return the V matrix (or null if decomposed matrix is singular)
533 * @see #getVT()
534 */
535 public RealMatrix getV() {
536 // return the cached matrix
537 return cachedV;
538 }
539
540 /**
541 * Returns the transpose of the matrix V of the decomposition.
542 * <p>V is an orthogonal matrix, i.e. its transpose is also its inverse.</p>
543 * @return the V matrix (or null if decomposed matrix is singular)
544 * @see #getV()
545 */
546 public RealMatrix getVT() {
547 if (cachedVt == null) {
548 cachedVt = getV().transpose();
549 }
550 // return the cached matrix
551 return cachedVt;
552 }
553
554 /**
555 * Returns the n × n covariance matrix.
556 * <p>The covariance matrix is V × J × V<sup>T</sup>
557 * where J is the diagonal matrix of the inverse of the squares of
558 * the singular values.</p>
559 * @param minSingularValue value below which singular values are ignored
560 * (a 0 or negative value implies all singular value will be used)
561 * @return covariance matrix
562 * @exception IllegalArgumentException if minSingularValue is larger than
563 * the largest singular value, meaning all singular values are ignored
564 */
565 public RealMatrix getCovariance(final double minSingularValue) {
566 // get the number of singular values to consider
567 final int p = singularValues.length;
568 int dimension = 0;
569 while (dimension < p &&
570 singularValues[dimension] >= minSingularValue) {
571 ++dimension;
572 }
573
574 if (dimension == 0) {
575 throw new NumberIsTooLargeException(LocalizedFormats.TOO_LARGE_CUTOFF_SINGULAR_VALUE,
576 minSingularValue, singularValues[0], true);
577 }
578
579 final double[][] data = new double[dimension][p];
580 getVT().walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
581 /** {@inheritDoc} */
582 @Override
583 public void visit(final int row, final int column,
584 final double value) {
585 data[row][column] = value / singularValues[row];
586 }
587 }, 0, dimension - 1, 0, p - 1);
588
589 RealMatrix jv = new Array2DRowRealMatrix(data, false);
590 return jv.transpose().multiply(jv);
591 }
592
593 /**
594 * Returns the L<sub>2</sub> norm of the matrix.
595 * <p>The L<sub>2</sub> norm is max(|A × u|<sub>2</sub> /
596 * |u|<sub>2</sub>), where |.|<sub>2</sub> denotes the vectorial 2-norm
597 * (i.e. the traditional euclidian norm).</p>
598 * @return norm
599 */
600 public double getNorm() {
601 return singularValues[0];
602 }
603
604 /**
605 * Return the condition number of the matrix.
606 * @return condition number of the matrix
607 */
608 public double getConditionNumber() {
609 return singularValues[0] / singularValues[n - 1];
610 }
611
612 /**
613 * Computes the inverse of the condition number.
614 * In cases of rank deficiency, the {@link #getConditionNumber() condition
615 * number} will become undefined.
616 *
617 * @return the inverse of the condition number.
618 */
619 public double getInverseConditionNumber() {
620 return singularValues[n - 1] / singularValues[0];
621 }
622
623 /**
624 * Return the effective numerical matrix rank.
625 * <p>The effective numerical rank is the number of non-negligible
626 * singular values. The threshold used to identify non-negligible
627 * terms is max(m,n) × ulp(s<sub>1</sub>) where ulp(s<sub>1</sub>)
628 * is the least significant bit of the largest singular value.</p>
629 * @return effective numerical matrix rank
630 */
631 public int getRank() {
632 int r = 0;
633 for (int i = 0; i < singularValues.length; i++) {
634 if (singularValues[i] > tol) {
635 r++;
636 }
637 }
638 return r;
639 }
640
641 /**
642 * Get a solver for finding the A × X = B solution in least square sense.
643 * @return a solver
644 */
645 public DecompositionSolver getSolver() {
646 return new Solver(singularValues, getUT(), getV(), getRank() == m, tol);
647 }
648
649 /** Specialized solver. */
650 private static class Solver implements DecompositionSolver {
651 /** Pseudo-inverse of the initial matrix. */
652 private final RealMatrix pseudoInverse;
653 /** Singularity indicator. */
654 private boolean nonSingular;
655
656 /**
657 * Build a solver from decomposed matrix.
658 *
659 * @param singularValues Singular values.
660 * @param uT U<sup>T</sup> matrix of the decomposition.
661 * @param v V matrix of the decomposition.
662 * @param nonSingular Singularity indicator.
663 * @param tol tolerance for singular values
664 */
665 private Solver(final double[] singularValues, final RealMatrix uT,
666 final RealMatrix v, final boolean nonSingular, final double tol) {
667 final double[][] suT = uT.getData();
668 for (int i = 0; i < singularValues.length; ++i) {
669 final double a;
670 if (singularValues[i] > tol) {
671 a = 1 / singularValues[i];
672 } else {
673 a = 0;
674 }
675 final double[] suTi = suT[i];
676 for (int j = 0; j < suTi.length; ++j) {
677 suTi[j] *= a;
678 }
679 }
680 pseudoInverse = v.multiply(new Array2DRowRealMatrix(suT, false));
681 this.nonSingular = nonSingular;
682 }
683
684 /**
685 * Solve the linear equation A × X = B in least square sense.
686 * <p>
687 * The m×n matrix A may not be square, the solution X is such that
688 * ||A × X - B|| is minimal.
689 * </p>
690 * @param b Right-hand side of the equation A × X = B
691 * @return a vector X that minimizes the two norm of A × X - B
692 * @throws org.apache.commons.math.exception.DimensionMismatchException
693 * if the matrices dimensions do not match.
694 */
695 public RealVector solve(final RealVector b) {
696 return pseudoInverse.operate(b);
697 }
698
699 /**
700 * Solve the linear equation A × X = B in least square sense.
701 * <p>
702 * The m×n matrix A may not be square, the solution X is such that
703 * ||A × X - B|| is minimal.
704 * </p>
705 *
706 * @param b Right-hand side of the equation A × X = B
707 * @return a matrix X that minimizes the two norm of A × X - B
708 * @throws org.apache.commons.math.exception.DimensionMismatchException
709 * if the matrices dimensions do not match.
710 */
711 public RealMatrix solve(final RealMatrix b) {
712 return pseudoInverse.multiply(b);
713 }
714
715 /**
716 * Check if the decomposed matrix is non-singular.
717 *
718 * @return {@code true} if the decomposed matrix is non-singular.
719 */
720 public boolean isNonSingular() {
721 return nonSingular;
722 }
723
724 /**
725 * Get the pseudo-inverse of the decomposed matrix.
726 *
727 * @return the inverse matrix.
728 */
729 public RealMatrix getInverse() {
730 return pseudoInverse;
731 }
732 }
733 }