Top Description Inners Fields Constructors Methods
org.apache.commons.math3.linear

public Class QRDecomposition

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.commons.math3.linear.RRQRDecomposition
Imports
java.util.Arrays, org.apache.commons.math3.exception.DimensionMismatchException, org.apache.commons.math3.util.FastMath

Calculates the QR-decomposition of a matrix.

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:

Since
1.2 (changed to concrete class in 3.0)
See Also
MathWorld, Wikipedia

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
QRDecomposition.Solver

Specialized solver.

Field Summary

Modifier and TypeField 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.

Constructor Summary

AccessConstructor 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.

Method Summary

Modifier and TypeMethod and Description
protected void
decompose(double[][]
transposed matrix
matrix
)

Decompose matrix.

public RealMatrix

Returns:

a matrix containing the Householder reflector vectors
getH
()

Returns the Householder reflector vectors.

public RealMatrix

Returns:

the Q matrix
getQ
()

Returns the matrix Q of the decomposition.

public RealMatrix

Returns:

the transpose of the Q matrix, QT
getQT
()

Returns the transpose of the matrix Q of the decomposition.

public RealMatrix

Returns:

the R matrix
getR
()

Returns the matrix R of the decomposition.

public DecompositionSolver

Returns:

a solver
getSolver
()

Get a solver for finding the A × X = B solution in least square sense.

protected void
performHouseholderReflection(int
minor index
minor
,
double[][]
transposed matrix
matrix
)

Perform Householder reflection for a minor A(minor, minor) of A.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachedHback to summary
private RealMatrix cachedH

Cached value of H.

cachedQback to summary
private RealMatrix cachedQ

Cached value of Q.

cachedQTback to summary
private RealMatrix cachedQT

Cached value of QT.

cachedRback to summary
private RealMatrix cachedR

Cached value of R.

qrtback 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.

rDiagback to summary
private double[] rDiag

The diagonal elements of R.

thresholdback to summary
private final double threshold

Singularity threshold.

Constructor Detail

QRDecompositionback to summary
public QRDecomposition(RealMatrix matrix)

Calculates the QR-decomposition of the given matrix. The singularity threshold defaults to zero.

Parameters
matrix:RealMatrix

The matrix to decompose.

See Also
QRDecomposition(RealMatrix, double)
QRDecompositionback to summary
public QRDecomposition(RealMatrix matrix, double threshold)

Calculates the QR-decomposition of the given matrix.

Parameters
matrix:RealMatrix

The matrix to decompose.

threshold:double

Singularity threshold.

Method Detail

decomposeback to summary
protected void decompose(double[][] matrix)

Decompose matrix.

Parameters
matrix:double[][]

transposed matrix

Since
3.2
getHback 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.

Returns:RealMatrix

a matrix containing the Householder reflector vectors

getQback to summary
public RealMatrix getQ()

Returns the matrix Q of the decomposition.

Q is an orthogonal matrix

Returns:RealMatrix

the Q matrix

getQTback to summary
public RealMatrix getQT()

Returns the transpose of the matrix Q of the decomposition.

Q is an orthogonal matrix

Returns:RealMatrix

the transpose of the Q matrix, QT

getRback to summary
public RealMatrix getR()

Returns the matrix R of the decomposition.

R is an upper-triangular matrix

Returns:RealMatrix

the R matrix

getSolverback 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 construction, an error will be triggered when the solve method will be called.

Returns:DecompositionSolver

a solver

performHouseholderReflectionback to summary
protected void performHouseholderReflection(int minor, double[][] matrix)

Perform Householder reflection for a minor A(minor, minor) of A.

Parameters
minor:int

minor index

matrix:double[][]

transposed matrix

Since
3.2
org.apache.commons.math3.linear back to summary

private Class QRDecomposition.Solver

extends Object
implements DecompositionSolver
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.linear.DecompositionSolver

Specialized solver.

Field Summary

Modifier and TypeField 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.

Constructor Summary

AccessConstructor 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.

Method Summary

Modifier and TypeMethod and Description
public RealMatrix
public boolean
isNonSingular()

Implements org.apache.commons.math3.linear.DecompositionSolver.isNonSingular.

Check if the decomposed matrix is non-singular.

public RealVector
solve(RealVector
right-hand side of the equation A × X = B
b
)

Implements org.apache.commons.math3.linear.DecompositionSolver.solve.

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.apache.commons.math3.linear.DecompositionSolver.solve.

Solve the linear equation A × X = B for matrices A.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

qrtback 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.

rDiagback to summary
private final double[] rDiag

The diagonal elements of R.

thresholdback to summary
private final double threshold

Singularity threshold.

Constructor Detail

Solverback to summary
private Solver(final double[][] qrt, final double[] rDiag, final double threshold)

Build a solver from decomposed matrix.

Parameters
qrt:double[][]

Packed TRANSPOSED representation of the QR decomposition.

rDiag:double[]

Diagonal elements of R.

threshold:double

Singularity threshold.

Method Detail

getInverseback to summary
public RealMatrix getInverse()

Implements org.apache.commons.math3.linear.DecompositionSolver.getInverse.

Doc from org.apache.commons.math3.linear.DecompositionSolver.getInverse.

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 LUDecomposition is not defined for non-square matrices to begin with. The QRDecomposition can operate on non-square matrices, but will throw SingularMatrixException if the decomposed matrix is singular. Refer to the javadoc of specific decomposition implementations for more details.

Returns:RealMatrix

pseudo-inverse matrix (which is the inverse, if it exists), if the decomposition can pseudo-invert the decomposed matrix

Exceptions
SingularMatrixException:
if the decomposed matrix is singular.
isNonSingularback to summary
public boolean isNonSingular()

Implements org.apache.commons.math3.linear.DecompositionSolver.isNonSingular.

Doc from org.apache.commons.math3.linear.DecompositionSolver.isNonSingular.

Check if the decomposed matrix is non-singular.

Returns:boolean

true if the decomposed matrix is non-singular.

solveback to summary
public RealVector solve(RealVector b)

Implements org.apache.commons.math3.linear.DecompositionSolver.solve.

Doc from org.apache.commons.math3.linear.DecompositionSolver.solve.

Solve the linear equation A × X = B for matrices A.

The A matrix is implicit, it is provided by the underlying decomposition algorithm.

Parameters
b:RealVector

right-hand side of the equation A × X = B

Returns:RealVector

a vector X that minimizes the two norm of A × X - B

solveback to summary
public RealMatrix solve(RealMatrix b)

Implements org.apache.commons.math3.linear.DecompositionSolver.solve.

Doc from org.apache.commons.math3.linear.DecompositionSolver.solve.

Solve the linear equation A × X = B for matrices A.

The A matrix is implicit, it is provided by the underlying decomposition algorithm.

Parameters
b:RealMatrix

right-hand side of the equation A × X = B

Returns:RealMatrix

a matrix X that minimizes the two norm of A × X - B