java.lang.Object
org.bzdev.math.CholeskyDecomp
- All Implemented Interfaces:
TriangularDecomp
Cholesky Decomposition class.
This class implements Cholesky decomposition, which factors
a matrix A so that A = LLT, where L is a lower-triangular
matrix. The matrix A must be positive definite and symmetric.
This class implements the TriangleDecomp interface, which assumes that classes implementing it will create a lower triangular matrix L, an upper triangular matrix U, and a permutation matrix P such that PA = LU. For the case of Cholesky decomposition, P is the identity matrix and U is the transpose of L.
-
Constructor Summary
ConstructorsConstructorDescriptionCholeskyDecomp
(double[][] matrix) Constructor.CholeskyDecomp
(double[][] matrix, double[][] LU) Constructor providing a matrix and a workspace.CholeskyDecomp
(double[][] A, int n, boolean strict) Constructor specifying matrix dimensions.CholeskyDecomp
(double[] flatMatrix, int n) Constructor given a flat representation of a matrix. -
Method Summary
Modifier and TypeMethodDescriptiondouble
det()
Get the determinate of the matrix to which this decomposition applies.double[][]
Get the inverse of a matrix.void
getInverse
(double[][] result) Get the inverse of a matrix and store the inverse.void
getInverse
(double[] result, boolean columnMajorOrder) Get the inverse of a matrix and store the inverse in a flat matrix.double[][]
getL()
Get the lower triangular matrix associated with the decomposition.int
Get the number of columns in the triangular matrices associated with an instance of this class.int
Get the number of rows in the triangular matrices associated with an instance of this class.getP()
Get the permutation to which this decomposition applies.double[][]
getU()
Get the upper triangular matrix associated with the decomposition.boolean
Determine if the matrix to which this decomposition applies is nonsingular.double[]
solve
(double[] b) Solve the system of linear equations Ax = b where x and b are vectors and A is the matrix to which this decomposition applies.void
solve
(double[][] x, double[][] b) Solve the system of linear equations AX = B where X and B are matrices and A is the matrix to which this decomposition applies.void
solve
(double[] x, double[] b) Solve the system of linear equations Ax = b where x and b are vectors and A is the matrix to which this decomposition applies.
-
Constructor Details
-
CholeskyDecomp
public CholeskyDecomp(double[][] matrix, double[][] LU) throws IllegalArgumentException, MathException Constructor providing a matrix and a workspace. The dimensions of matrix are used to compute the number of rows and columns. The matrix L must not be modified after the constructor is called until the instance created is no longer used. Both arguments may reference the same object, but in this case the original matrix will be overwritten.- Parameters:
matrix
- the matrix whose LU decomposition is to be computed.LU
- the matrix used to store the triangular matrices L and U.- Throws:
IllegalArgumentException
- the matrices are not consistent with each otherMathException
-
CholeskyDecomp
Constructor.- Parameters:
matrix
- a matrix whose Cholesky decomposition is to be computed- Throws:
IllegalArgumentException
MathException
-
CholeskyDecomp
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 matrixn
- the number of rows and columns- Throws:
IllegalArgumentException
- the flat matrix is too small to represent an m by n matrix
-
CholeskyDecomp
Constructor specifying matrix dimensions. This is provided to allow a single array to be used multiple times in order to avoid unnecessary array allocations.- Parameters:
A
- the matrix whose LU decomposition is to be computedn
- the number of rows and columnsstrict
- true if matrixA must have exactly the number of rows and columns required; false if matrixA may have rows or columns longer than needed, in which case the additional entries will be ignored- Throws:
IllegalArgumentException
- the matrix and other parameters are not consistent with each other
-
-
Method Details
-
getNumberOfRows
public int getNumberOfRows()Description copied from interface:TriangularDecomp
Get the number of rows in the triangular matrices associated with an instance of this class.- Specified by:
getNumberOfRows
in interfaceTriangularDecomp
- Returns:
- the number of rows.
-
getNumberOfColumns
public int getNumberOfColumns()Description copied from interface:TriangularDecomp
Get the number of columns in the triangular matrices associated with an instance of this class.- Specified by:
getNumberOfColumns
in interfaceTriangularDecomp
- Returns:
- the number of columns.
-
getP
Description copied from interface:TriangularDecomp
Get the permutation to which this decomposition applies. The permutation returned has the property that its getMatrix() method returns the permutation matrix.- Specified by:
getP
in interfaceTriangularDecomp
- Returns:
- the permutation
-
getL
public double[][] getL()Description copied from interface:TriangularDecomp
Get the lower triangular matrix associated with the decomposition.- Specified by:
getL
in interfaceTriangularDecomp
- Returns:
- the lower triangular matrix
-
getU
public double[][] getU()Description copied from interface:TriangularDecomp
Get the upper triangular matrix associated with the decomposition. For Cholesky decomposition, U is the transpose of the lower triangular matrix returned by getL().- Specified by:
getU
in interfaceTriangularDecomp
- Returns:
- the upper triangular matrix
-
det
public double det()Description copied from interface:TriangularDecomp
Get the determinate of the matrix to which this decomposition applies.- Specified by:
det
in interfaceTriangularDecomp
- Returns:
- the determinate
-
solve
Description copied from interface:TriangularDecomp
Solve the system of linear equations Ax = b where x and b are vectors and A is the matrix to which this decomposition applies.- Specified by:
solve
in interfaceTriangularDecomp
- Parameters:
b
- the vector b in the equation Ax = b- Returns:
- the vector x that satisfies the equation Ax = b
- Throws:
IllegalArgumentException
- the argument has the wrong sizeIllegalStateException
- the matrix this object decomposed is a singular matrix
-
solve
Description copied from interface:TriangularDecomp
Solve the system of linear equations Ax = b where x and b are vectors and A is the matrix to which this decomposition applies. The vector x will be modified.- Specified by:
solve
in interfaceTriangularDecomp
- Parameters:
x
- the vector x that satisfies the equation Ax = bb
- the vector b in the equation Ax = b- Throws:
IllegalArgumentException
- the argument has the wrong sizeIllegalStateException
- the matrix this object decomposed is a singular matrix
-
solve
public void solve(double[][] x, double[][] b) throws IllegalArgumentException, IllegalStateException Description copied from interface:TriangularDecomp
Solve the system of linear equations AX = B where X and B are matrices and A is the matrix to which this decomposition applies. The matrix X will be modified.- Specified by:
solve
in interfaceTriangularDecomp
- Parameters:
x
- the matrix X that satisfies the equation AX = Bb
- the matrix B in the equation AX = B- Throws:
IllegalArgumentException
- the arguments have the wrong sizeIllegalStateException
- the matrix this object decomposed is a singular matrix
-
getInverse
Description copied from interface:TriangularDecomp
Get the inverse of a matrix. The matrix A whose inverse is computed satisfies PA = LU, where P is the permutation matrix associated with this instance, L is the lower triangular matrix associated with this instance, and U is the upper triangular matrix associated with this instance.For some cases (e.g., Cholesky decomposition), the permutation matrix is the identity matrix.
- Specified by:
getInverse
in interfaceTriangularDecomp
- Returns:
- the inverse of the matrix that this instance decomposed
- Throws:
IllegalStateException
- the matrix is singular
-
isNonsingular
public boolean isNonsingular()Description copied from interface:TriangularDecomp
Determine if the matrix to which this decomposition applies is nonsingular.- Specified by:
isNonsingular
in interfaceTriangularDecomp
- Returns:
- true if the matrix is nonsingular; false otherwise
-
getInverse
Description copied from interface:TriangularDecomp
Get the inverse of a matrix and store the inverse. The matrix A whose inverse is computed satisfies PA = LU, where P is the permutation matrix associated with this instance, L is the lower triangular matrix associated with this instance, and U is the upper triangular matrix associated with this instance.- Specified by:
getInverse
in interfaceTriangularDecomp
- Parameters:
result
- the matrix holding the inverse- Throws:
IllegalStateException
- the matrix is singularIllegalArgumentException
- - if result has the wrong dimensions
-
getInverse
public void getInverse(double[] result, boolean columnMajorOrder) throws IllegalStateException, IllegalArgumentException Description copied from interface:TriangularDecomp
Get the inverse of a matrix and store the inverse in a flat matrix. The matrix A whose inverse is computed satisfies A = LLT, where L is the lower triangular matrix associated with this instance.- Specified by:
getInverse
in interfaceTriangularDecomp
- Parameters:
result
- the matrix holding the inversecolumnMajorOrder
- 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)- Throws:
IllegalStateException
- the matrix is singularIllegalArgumentException
- - if result has the wrong dimensions
-