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

public Class RRQRDecomposition

extends QRDecomposition
Class Inheritance
Imports
org.apache.commons.math3.util.FastMath

Calculates the rank-revealing QR-decomposition of a matrix, with column pivoting.

The rank-revealing QR-decomposition of a matrix A consists of three matrices Q, R and P such that AP=QR. Q is orthogonal (QTQ = I), and R is upper triangular. If A is m×n, Q is m×m and R is m×n and P is n×n.

QR decomposition with column pivoting produces a rank-revealing QR decomposition and the getRank(double) method may be used to return the rank of the input matrix A.

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
3.2
See Also
MathWorld, Wikipedia

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
RRQRDecomposition.Solver

Specialized solver.

Field Summary

Modifier and TypeField and Description
private RealMatrix
cachedP

Cached value of P.

private int[]
p

An array to record the column pivoting for later creation of P.

Constructor Summary

AccessConstructor and Description
public
RRQRDecomposition(RealMatrix
The matrix to decompose.
matrix
)

Calculates the QR-decomposition of the given matrix.

public
RRQRDecomposition(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
qrt
)

Overrides org.apache.commons.math3.linear.QRDecomposition.decompose.

Decompose matrix.

public RealMatrix

Returns:

a permutation matrix.
getP
()

Returns the pivot matrix, P, used in the QR Decomposition of matrix A such that AP = QR.

public int

Returns:

effective numerical matrix rank
getRank
(final double
threshold triggering rank computation
dropThreshold
)

Return the effective numerical matrix rank.

public DecompositionSolver

Returns:

a solver
getSolver
()

Overrides org.apache.commons.math3.linear.QRDecomposition.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
qrt
)

Overrides org.apache.commons.math3.linear.QRDecomposition.performHouseholderReflection.

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

Inherited from org.apache.commons.math3.linear.QRDecomposition:
getHgetQgetQTgetR

Field Detail

cachedPback to summary
private RealMatrix cachedP

Cached value of P.

pback to summary
private int[] p

An array to record the column pivoting for later creation of P.

Constructor Detail

RRQRDecompositionback to summary
public RRQRDecomposition(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
RRQRDecomposition(RealMatrix, double)
RRQRDecompositionback to summary
public RRQRDecomposition(RealMatrix matrix, double threshold)

Calculates the QR-decomposition of the given matrix.

Parameters
matrix:RealMatrix

The matrix to decompose.

threshold:double

Singularity threshold.

See Also
RRQRDecomposition(RealMatrix)

Method Detail

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

Overrides org.apache.commons.math3.linear.QRDecomposition.decompose.

Decompose matrix.

Parameters
qrt:double[][]

transposed matrix

Annotations
@Override
getPback to summary
public RealMatrix getP()

Returns the pivot matrix, P, used in the QR Decomposition of matrix A such that AP = QR. If no pivoting is used in this decomposition then P is equal to the identity matrix.

Returns:RealMatrix

a permutation matrix.

getRankback to summary
public int getRank(final double dropThreshold)

Return the effective numerical matrix rank.

The effective numerical rank is the number of non-negligible singular values.

This implementation looks at Frobenius norms of the sequence of bottom right submatrices. When a large fall in norm is seen, the rank is returned. The drop is computed as:

  (thisNorm/lastNorm) * rNorm < dropThreshold

where thisNorm is the Frobenius norm of the current submatrix, lastNorm is the Frobenius norm of the previous submatrix, rNorm is is the Frobenius norm of the complete matrix

Parameters
dropThreshold:double

threshold triggering rank computation

Returns:int

effective numerical matrix rank

getSolverback to summary
public DecompositionSolver getSolver()

Overrides org.apache.commons.math3.linear.QRDecomposition.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

Annotations
@Override
performHouseholderReflectionback to summary
protected void performHouseholderReflection(int minor, double[][] qrt)

Overrides org.apache.commons.math3.linear.QRDecomposition.performHouseholderReflection.

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

Parameters
minor:int

minor index

qrt:double[][]

transposed matrix

Annotations
@Override
org.apache.commons.math3.linear back to summary

private Class RRQRDecomposition.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 RealMatrix
p

A permutation matrix for the pivots used in the QR decomposition

private final DecompositionSolver
upper

Upper level solver.

Constructor Summary

AccessConstructor and Description
private
Solver(final DecompositionSolver
upper level solver.
upper
,
final RealMatrix
permutation matrix
p
)

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

pback to summary
private RealMatrix p

A permutation matrix for the pivots used in the QR decomposition

upperback to summary
private final DecompositionSolver upper

Upper level solver.

Constructor Detail

Solverback to summary
private Solver(final DecompositionSolver upper, final RealMatrix p)

Build a solver from decomposed matrix.

Parameters
upper:DecompositionSolver

upper level solver.

p:RealMatrix

permutation matrix

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