The QR-decomposition of a matrix A consists of two matrices Q and R that satisfy: A = QR, Q is orthogonal (QTQ = I), and R is upper triangular. If A is m×n, Q is m×m and R m×n.
This class compute the decomposition using Householder reflectors.
For efficiency purposes, the decomposition in packed form is transposed. This allows inner loop to iterate inside rows, which is much more cache-efficient in Java.
This class is based on the class with similar name from the JAMA library, with the following changes:
getQT method has been added,solve and isFullRank methods have been replaced
by a getSolver method and the equivalent methods
provided by the returned DecompositionSolver.| Modifier and Type | Class and Description |
|---|---|
| private static class | QRDecomposition.
Specialized solver. |
| Modifier and Type | Field and Description |
|---|---|
| private RealMatrix | cachedH
Cached value of H. |
| private RealMatrix | cachedQ
Cached value of Q. |
| private RealMatrix | cachedQT
Cached value of QT. |
| private RealMatrix | cachedR
Cached value of R. |
| private double[][] | qrt
A packed TRANSPOSED representation of the QR decomposition. |
| private double[] | rDiag
The diagonal elements of R. |
| private final double | threshold
Singularity threshold. |
| Access | Constructor and Description |
|---|---|
| public | QRDecomposition(RealMatrix
The matrix to decompose. matrix)Calculates the QR-decomposition of the given matrix. |
| public | QRDecomposition(RealMatrix
The matrix to decompose. matrix, double Singularity threshold. threshold)Calculates the QR-decomposition of the given matrix. |
| Modifier and Type | Method and Description |
|---|---|
| protected void | |
| public RealMatrix | Returns: a matrix containing the Householder reflector vectorsReturns the Householder reflector vectors. |
| public RealMatrix | |
| public RealMatrix | Returns: the transpose of the Q matrix, QTReturns the transpose of the matrix Q of the decomposition. |
| public RealMatrix | |
| public DecompositionSolver | |
| protected void | performHouseholderReflection(int
minor index minor, double[][] transposed matrix matrix)Perform Householder reflection for a minor A(minor, minor) of A. |
| cachedH | back to summary |
|---|---|
| private RealMatrix cachedH Cached value of H. | |
| cachedQ | back to summary |
|---|---|
| private RealMatrix cachedQ Cached value of Q. | |
| cachedQT | back to summary |
|---|---|
| private RealMatrix cachedQT Cached value of QT. | |
| cachedR | back to summary |
|---|---|
| private RealMatrix cachedR Cached value of R. | |
| qrt | back to summary |
|---|---|
| private double[][] qrt A packed TRANSPOSED representation of the QR decomposition. The elements BELOW the diagonal are the elements of the UPPER triangular matrix R, and the rows ABOVE the diagonal are the Householder reflector vectors from which an explicit form of Q can be recomputed if desired. | |
| rDiag | back to summary |
|---|---|
| private double[] rDiag The diagonal elements of R. | |
| threshold | back to summary |
|---|---|
| private final double threshold Singularity threshold. | |
| QRDecomposition | back to summary |
|---|---|
| public QRDecomposition(RealMatrix matrix) Calculates the QR-decomposition of the given matrix. The singularity threshold defaults to zero.
| |
| QRDecomposition | back to summary |
|---|---|
| public QRDecomposition(RealMatrix matrix, double threshold) Calculates the QR-decomposition of the given matrix.
| |
| decompose | back to summary |
|---|---|
| protected void decompose(double[][] matrix) Decompose matrix.
| |
| getH | back to summary |
|---|---|
| public RealMatrix getH() Returns the Householder reflector vectors. H is a lower trapezoidal matrix whose columns represent each successive Householder reflector vector. This matrix is used to compute Q.
| |
| getQ | back to summary |
|---|---|
| public RealMatrix getQ() Returns the matrix Q of the decomposition. Q is an orthogonal matrix
| |
| getQT | back to summary |
|---|---|
| public RealMatrix getQT() Returns the transpose of the matrix Q of the decomposition. Q is an orthogonal matrix
| |
| getR | back to summary |
|---|---|
| public RealMatrix getR() Returns the matrix R of the decomposition. R is an upper-triangular matrix
| |
| getSolver | back to summary |
|---|---|
| public DecompositionSolver getSolver() Get a solver for finding the A × X = B solution in least square sense.
Least Square sense means a solver can be computed for an overdetermined system,
(i.e. a system with more equations than unknowns, which corresponds to a tall A
matrix with more rows than columns). In any case, if the matrix is singular
within the tolerance set at
| |
| performHouseholderReflection | back to summary |
|---|---|
| protected void performHouseholderReflection(int minor, double[][] matrix) Perform Householder reflection for a minor A(minor, minor) of A.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final double[][] | qrt
A packed TRANSPOSED representation of the QR decomposition. |
| private final double[] | rDiag
The diagonal elements of R. |
| private final double | threshold
Singularity threshold. |
| Access | Constructor and Description |
|---|---|
| private | Solver(final double[][]
Packed TRANSPOSED representation of the QR decomposition. qrt, final double[] Diagonal elements of R. rDiag, final double Singularity threshold. threshold)Build a solver from decomposed matrix. |
| Modifier and Type | Method and Description |
|---|---|
| public RealMatrix | getInverse()
Implements org. Get the pseudo-inverse of the decomposed matrix. |
| public boolean | isNonSingular()
Implements org. Check if the decomposed matrix is non-singular. |
| public RealVector | solve(RealVector
right-hand side of the equation A × X = B b)Implements org. Solve the linear equation A × X = B for matrices A. |
| public RealMatrix | solve(RealMatrix
right-hand side of the equation A × X = B b)Implements org. Solve the linear equation A × X = B for matrices A. |
| qrt | back to summary |
|---|---|
| private final double[][] qrt A packed TRANSPOSED representation of the QR decomposition. The elements BELOW the diagonal are the elements of the UPPER triangular matrix R, and the rows ABOVE the diagonal are the Householder reflector vectors from which an explicit form of Q can be recomputed if desired. | |
| rDiag | back to summary |
|---|---|
| private final double[] rDiag The diagonal elements of R. | |
| threshold | back to summary |
|---|---|
| private final double threshold Singularity threshold. | |
| Solver | back to summary |
|---|---|
| private Solver(final double[][] qrt, final double[] rDiag, final double threshold) Build a solver from decomposed matrix.
| |
| getInverse | back to summary |
|---|---|
| public RealMatrix getInverse() Implements org. Doc from org. Get the pseudo-inverse of the decomposed matrix. This is equal to the inverse of the decomposed matrix, if such an inverse exists. If no such inverse exists, then the result has properties that resemble that of an inverse. In particular, in this case, if the decomposed matrix is A, then the system of equations \( A x = b \) may have no solutions, or many. If it has no solutions, then the pseudo-inverse \( A^+ \) gives the "closest" solution \( z = A^+ b \), meaning \( \left \| A z - b \right \|_2 \) is minimized. If there are many solutions, then \( z = A^+ b \) is the smallest solution, meaning \( \left \| z \right \|_2 \) is minimized.
Note however that some decompositions cannot compute a pseudo-inverse for all matrices.
For example, the
| |
| isNonSingular | back to summary |
|---|---|
| public boolean isNonSingular() Implements org. Doc from org. Check if the decomposed matrix is non-singular.
| |
| solve | back to summary |
|---|---|
| public RealVector solve(RealVector b) Implements org. Doc from org. Solve the linear equation A × X = B for matrices A. The A matrix is implicit, it is provided by the underlying decomposition algorithm.
| |
| solve | back to summary |
|---|---|
| public RealMatrix solve(RealMatrix b) Implements org. Doc from org. Solve the linear equation A × X = B for matrices A. The A matrix is implicit, it is provided by the underlying decomposition algorithm.
| |