public class QRDecomposition
extends java.lang.Object
M = Q Rwhere M is the original matrix, Q is orthogonal, and R is upper-triangular. Nominally, if M has a size m X n, then if m
>=
n,
R is square with size n and Q is m X n. Otherwise, if m <
n, Q is
square with size m and R has size m X n.
If the decomposition is performed with pivoting, using the method factorWithPivoting(maspack.matrix.Matrix)
, then it takes the form
M P = Q Rwhere P is a permutation matrix.
Note that if m >
n, then R and M can actually have sizes p X n
and m X p, with n <=
p <=
m, with the additional rows of R
set to zero and the additional columns of Q formed by completing the
orthogonal basis.
Once constructed, a QR decomposition can be used to perform various computations related to M, such as solving equations (in particular, determining least-squares solutions), computing the determinant, or estimating the condition number.
Providing a separate class for the QR decomposition allows an application to perform such decompositions repeatedly without having to reallocate temporary storage space.
Constructor and Description |
---|
QRDecomposition()
Creates an uninitialized QRDecomposition.
|
QRDecomposition(Matrix M)
Creates a QRDecomposition for the Matrix specified by M.
|
Modifier and Type | Method and Description |
---|---|
double |
conditionEstimate()
Estimates the condition number of the triangular matrix R associated with
this decomposition.
|
double |
determinant()
Returns the determinant of the (square) upper partition of R, times the
determinant of Q.
|
void |
factor(Matrix M)
Peforms a QR decomposition on the Matrix M.
|
void |
factorInPlace(MatrixNd M)
Performs a QR decomposition on the Matrix M, placing the result in M and
using M as the storage for subsequent solve operations.
|
void |
factorWithPivoting(Matrix M)
Peforms a QR decomposition, with pivoting, on the Matrix M.
|
void |
get(MatrixNd Q,
MatrixNd R)
Gets the Q and R matrices associated with this QR decomposition.
|
void |
get(MatrixNd Q,
MatrixNd R,
int[] cperm)
Gets the Q and R matrices, and the column permutation, associated with
this QR decomposition.
|
MatrixNd |
getQ()
Returns the Q matrix associated with this QR decomposition.
|
MatrixNd |
getR()
Returns the R matrix associated with this QR decomposition.
|
boolean |
inverse(DenseMatrix X)
Computes the inverse of the original matrix M associated with this
decomposition, and places the result in X.
|
boolean |
leftSolve(DenseMatrix X,
Matrix B)
Computes a left solution to the linear equation
x M = b where M is the original matrix associated with this decomposition, and X and B are matrices. |
boolean |
leftSolve(Vector x,
Vector b)
Computes a left solution to the linear equation
x M = b where M is the original matrix associated with this decomposition, and x and b are row vectors. |
boolean |
leftSolveR(DenseMatrix X,
Matrix B)
Computes a left solution to the linear equation
|
boolean |
leftSolveR(Vector x,
Vector b)
Computes a left solution to the linear equation
|
static void |
main(java.lang.String[] args) |
void |
postMulQ(MatrixNd MR,
MatrixNd M1)
Computes
|
void |
postMulQTranspose(MatrixNd MR,
MatrixNd M1)
Computes
|
void |
preMulQ(MatrixNd MR,
MatrixNd M1)
Computes
|
void |
preMulQTranspose(MatrixNd MR,
MatrixNd M1)
Computes
|
int |
rank(double tol) |
boolean |
solve(DenseMatrix X,
Matrix B)
Computes a least-squares solution to the linear equation
M X = B where M is the original matrix associated with this decomposition, and X and B are matrices. |
boolean |
solve(Vector x,
Vector b)
Computes a solution to the linear equation
M x = b where M is the original matrix associated with this decomposition, and x and b are vectors. |
boolean |
solveR(DenseMatrix X,
Matrix B)
Computes a solution to the linear equation
|
boolean |
solveR(Vector x,
Vector b)
Computes a solution to the linear equation
|
public QRDecomposition(Matrix M)
M
- matrix to perform the QR decomposition onpublic QRDecomposition()
public void factor(Matrix M)
M
- matrix to perform the QR decomposition onpublic void factorInPlace(MatrixNd M)
public void factorWithPivoting(Matrix M)
M
- matrix to perform the QR decomposition onpublic MatrixNd getR()
ImproperStateException
- if this QRDecomposition is uninitializedpublic MatrixNd getQ()
ImproperStateException
- if this QRDecomposition is uninitializedpublic void get(MatrixNd Q, MatrixNd R) throws ImproperStateException, ImproperSizeException
get(Q,R,cperm)
.Q
- returns the orthogonal matrixR
- returns the upper triangular matrix.ImproperStateException
- if this QRDecomposition is uninitializedImproperSizeException
- if Q or R are not of the proper dimension and cannot be resized.public void get(MatrixNd Q, MatrixNd R, int[] cperm) throws ImproperStateException, ImproperSizeException
factorWithPivoting
. Each argument is optional; values will be returned
into them if they are present. Q is an orthogonal matrix which can be m X
p, where min(n,m) <=
p <=
m (and so must be square if m
<=
n). Extra columns of Q are formed by completing the original
basis. R is an upper triangular matrix which can be q X n, where min(n,m)
<=
q <=
m (and so must be m X n if m <=
n). Extra
rows of R are set to zero.Q
- returns the orthogonal matrixR
- returns the upper triangular matrixcperm
- returns the indices of the column permutation matrix P, such that j-th
column of M P is given by column perm[j]
of M.ImproperStateException
- if this QRDecomposition is uninitializedImproperSizeException
- if Q or R are not of the proper dimension and cannot be resized.public boolean solve(Vector x, Vector b) throws ImproperStateException, ImproperSizeException
m X n
with m > n
, then the system
is overdetermined and solution with the minimum least square error is
computed. Alternatively, if m < n
, then the system is
underdetermined and the a solution is found by using only the left-most
m X m
block of R (which is the same method used by the MATLAB \
operator).x
- unknown vector to solve forb
- constant vectorImproperStateException
- if this decomposition is uninitializedImproperSizeException
- if b does not have a size compatible
with M, or if x does not have a size compatible with M and cannot be
resized.public boolean solve(DenseMatrix X, Matrix B) throws ImproperStateException, ImproperSizeException
m X n
with m > n
, then
the system is overdetermined and solution with the minimum least square
error is computed. Alternatively, if m < n
, then the system is
underdetermined and the a solution is found by using only the left-most
m X m
block of R (which is the same method used by the MATLAB \
operator).X
- unknown matrix to solve forB
- constant matrixImproperStateException
- if this decomposition is uninitializedImproperSizeException
- If B has a different number of rows than
M, or if X has a different number of rows than M or a different number of
columns than B and cannot be resized.public boolean leftSolve(Vector x, Vector b) throws ImproperStateException, ImproperSizeException
The number of rows m
in M must equal or exceed the number of
columns n
(which implies that R is a square matrix with a size
equal to n
).
x
- unknown vector to solve forb
- constant vectorImproperStateException
- if this decomposition is uninitialized or if m < n
for
the original matrixImproperSizeException
- if b does not have a size compatible
with M, or if x does not have a size compatible with M and cannot be
resized.public boolean leftSolve(DenseMatrix X, Matrix B) throws ImproperStateException, ImproperSizeException
The number of rows m
in M must equal or exceed the number of
columns n
(which implies that R is a square matrix with a size
equal to n
).
X
- unknown matrix to solve forB
- constant matrixImproperStateException
- if this decomposition is uninitialized or if m < n
for
the original matrixImproperSizeException
- if b does not have a size compatible
with M, or if x does not have a size compatible with M and cannot be
resized.public boolean solveR(Vector x, Vector b) throws ImproperStateException, ImproperSizeException
R P^T x = bwhere R is the upper triangular matrix associated with this decomposition, P is the permutation matrix (which is the identity unless the decomposition was formed with
factorWithPivoting(maspack.matrix.Matrix)
), and
x and b are vectors.
If the original matrix M has size m X n
, and m >= n
, then
R is a square matrix with a size equal to n
, and x
and
b
should each have size n
. Otherise, if m < n
,
then R is m X n
, b
should have size m
, and x
will have size n
and will be solved using only the left-most
m X m
block of R with trailing elements set to zero.
x
- unknown vector to solve forb
- constant vectorImproperStateException
- if this decomposition is uninitializedImproperSizeException
- if b does not have a size compatible with R, or if x does not have a size
compatible with R and cannot be resized.public boolean solveR(DenseMatrix X, Matrix B) throws ImproperStateException, ImproperSizeException
R P^T X = Bwhere R is the upper triangular matrix associated with this decomposition, P is the permutation matrix (which is the identity unless the decomposition was formed with
factorWithPivoting(maspack.matrix.Matrix)
), and
X and B are matrices.
If the original matrix M has size m X n
, and m >= n
, then
R is a square matrix with a size equal to n
, and X
and
B
should each have n
rows. Otherise, if m < n
,
then R is m X n
, B
should have m
rows, and X
will have n
rows and will be solved using only the left-most
m X m
block of R with trailing elements set to zero.
X
- unknown matrix to solve forB
- constant matrixImproperStateException
- if this decomposition is uninitializedImproperSizeException
- if the size of B is incompatible with R, or if the size of X is
incompatible with R or B and X cannot be resized.public boolean leftSolveR(Vector x, Vector b) throws ImproperStateException, ImproperSizeException
x R P^T = bwhere R is the upper triangular matrix associated with this decomposition, P is the permutation matrix (which is the identity unless the decomposition was formed with
factorWithPivoting(maspack.matrix.Matrix)
), and
x and b are matrices.
The number of rows m
in the original matrix M must equal or
exceed the number of columns n
(which implies that R is a square
matrix with a size equal to n
). x
and b
should
each have size n
.
x
- unknown vector to solve forb
- constant vectorImproperStateException
- if this decomposition is uninitialized or if m < n
for
the original matrixImproperSizeException
- if b does not have a size compatible with R, or if x does not have a size
compatible with R and cannot be resized.public boolean leftSolveR(DenseMatrix X, Matrix B) throws ImproperStateException, ImproperSizeException
X R P^T = Bwhere R is the upper triangular matrix associated with this decomposition, P is the permutation matrix (which is the identity unless the decomposition was formed with
factorWithPivoting(maspack.matrix.Matrix)
), and
X and B are matrices.
The number of rows m
in the original matrix M must equal or
exceed the number of columns n
(which implies that R is a square
matrix with a size equal to n
). X
and B
should
each have n
rows.
X
- unknown matrix to solve forB
- constant matrixImproperStateException
- if this decomposition is uninitialized or if m < n
for
the original matrixImproperSizeException
- if the size of B is incompatible with R, or if the size of X is
incompatible with R or B and X cannot be resized.public double conditionEstimate() throws ImproperStateException
m X m
block of R. The algorithm for estimating the
condition number is given in Section 3.5.4 of Golub and Van Loan, Matrix
Computations (Second Edition).ImproperStateException
- if this QRDecomposition is uninitializedpublic double determinant() throws ImproperStateException
ImproperStateException
- if this decomposition is uninitializedpublic boolean inverse(DenseMatrix X) throws ImproperStateException
X
- matrix in which the inverse is storedImproperStateException
- if this decomposition is uninitializedImproperSizeException
- if M is not square, or if X does not have the same size as M and cannot be
resized.public int rank(double tol)
public void preMulQ(MatrixNd MR, MatrixNd M1)
MR = Q M1where Q is the full m X m orthogonal matrix associated with the decomposition. Before calling this method, the decomposition must be initialized with a factorization. In order to conform with Q, M1 must have m rows.
MR
- result matrixM1
- matrix to pre-multiply by Qpublic void preMulQTranspose(MatrixNd MR, MatrixNd M1)
MR = Q^T M1where Q is the full m X m orthogonal matrix associated with the decomposition. Before calling this method, the decomposition must be initialized with a factorization. In order to conform with Q, M1 must have m rows.
MR
- result matrixM1
- matrix to pre-multiply by Q transposepublic void postMulQ(MatrixNd MR, MatrixNd M1)
MR = M1 Qwhere Q is the full m X m orthogonal matrix associated with the decomposition. Before calling this method, the decomposition must be initialized with a factorization. In order to conform with Q, M1 must have m columns.
MR
- result matrixM1
- matrix to post-multiply by Qpublic void postMulQTranspose(MatrixNd MR, MatrixNd M1)
MR = M1 Q^Twhere Q is the full m X m orthogonal matrix associated with the decomposition. Before calling this method, the decomposition must be initialized with a factorization. In order to conform with Q, M1 must have m columns.
MR
- result matrixM1
- matrix to post-multiply by Q transposepublic static void main(java.lang.String[] args)