- All Implemented Interfaces:
Serializable
The QR decomposition always exists, even if the matrix does not have full rank, so the constructor will never fail. The primary use of the QR decomposition is in the least squares solution of non-square systems of simultaneous linear equations. This will fail if isFullRank() returns false.
The implementation is based on the one used in the Jama package (a public-domain package provided via a collaboration between MathWorks and the National Institute of Standards and Technology). Jama defines a matrix class whereas QRDecomp treats matrices as two-dimensional arrays, and was written so that the org.bzdev library is self-contained. Some additional runtime tests were added, and there are a few additional constructors and methods. The documentation was also extended. Since this is more or less a port of the Jama class, this particular class is in the public domain. The Jama implementation (and consequently this one) uses Householder vectors to compute the decomposition).
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondouble[][]
getH()
Return the Householder vectorsint
Get the number of columns for the matrix that was decomposed.int
Get the number of rows for the matrix that was decomposed.double[][]
getQ()
Get the orthogonal matrix Q such that A = QR, where A is the matrix passed to the constructor.double[][]
getR()
Get the upper triangular factor R such that A = QR, where A is the matrix passed to the constructor.boolean
Determine if the matrix passed to the constructor is a full-rank matrix.double[]
solve
(double[] b) Least squares solution of Ax = b, where A is the matrix passed to the constructor and x and b are column vectors.double[][]
solve
(double[][] B) Least squares solution of AX = B, where A is the matrix passed to the constructor.
-
Constructor Details
-
QRDecomp
public QRDecomp(double[][] A) Constructor.- Parameters:
A
- an m by n matrix with m ≥ n
-
QRDecomp
public QRDecomp(double[][] A, int m, int n, boolean strict) Constructor given explicit dimensions.- Parameters:
A
- the m by n matrix to decompose, with m ≥ nm
- the number of rowsn
- the number of columnsstrict
- true if A must have exactly the number of rows and columns required; false if A may have rows or columns longer than needed, in which case the additional entries will be ignored
-
QRDecomp
public QRDecomp(double[] flatMatrix, boolean columnMajorOrder, int m, int n) throws IllegalArgumentException Constructor given a flat representation of a matrix. The elements of the matrix can be stored in one of two orders. Given a matrix Aij, if the row index varies the fastest, the flat representation will consist of the sequence A00, A10, .... Otherwise the sequence is A00, A 01, ....- Parameters:
flatMatrix
- a one-dimensional array representing a two dimensional matrixcolumnMajorOrder
- true if the inverse is stored in column major order (e.g., the order used by Fortran); false if the inverse is stored in row-major-order (e.g., the order used by C)m
- the number of rowsn
- the number of columns- Throws:
IllegalArgumentException
- the flat matrix is too small to represent an m by n matrix
-
-
Method Details
-
getNumberOfRows
public int getNumberOfRows()Get the number of rows for the matrix that was decomposed.- Returns:
- the number of rows.
-
getNumberOfColumns
public int getNumberOfColumns()Get the number of columns for the matrix that was decomposed.- Returns:
- the number of columns.
-
isFullRank
public boolean isFullRank()Determine if the matrix passed to the constructor is a full-rank matrix. For an m by n matrix with m > n, a full-rank matrix will have a rank of n.- Returns:
- true if it is a full-rank matrix; false otherwise
-
getH
public double[][] getH()Return the Householder vectors- Returns:
- a lower trapezoidal matrix whose columns define the reflections represented by Householder vectors
-
getR
public double[][] getR()Get the upper triangular factor R such that A = QR, where A is the matrix passed to the constructor. The value is in a form sometimes called an "economic" decomposition as the matrix R is an n by n matrix when m ≠ n and where A is an m by n matrix.- Returns:
- a matrix equal to R
-
getQ
public double[][] getQ()Get the orthogonal matrix Q such that A = QR, where A is the matrix passed to the constructor. The value is in a form sometimes called an "economic" decomposition as the matrix is not an m by m matrix when m ≠ n and where A is an m by n matrix.- Returns:
- a matrix equal to Q
-
solve
public double[][] solve(double[][] B) Least squares solution of AX = B, where A is the matrix passed to the constructor. The solution is a matrix whose column vectors contain the solution for the corresponding column vector of B.- Parameters:
B
- a matrix with as many rows as A and any number of columns.- Returns:
- a matrix, with as many rows as A has columns and with as many columns as B, that minimizes the norms of each column vectors of A*X-B
- Throws:
IllegalArgumentException
- matrix row dimensions must agreeIllegalStateException
- The matrix A is rank deficient
-
solve
public double[] solve(double[] b) Least squares solution of Ax = b, where A is the matrix passed to the constructor and x and b are column vectors.- Parameters:
b
- a vector whose dimension is equal to the number of rows of A and any with any number of columns.- Returns:
- a vector, whose dimension is equal the number of columns of A, that minimizes the norm of A*x-b
- Throws:
IllegalArgumentException
- matrix row dimensions must agreeIllegalStateException
- The matrix A is rank deficient
-