Class LU<T extends MatrixMixin<T,?,?,?>>
- Type Parameters:
T
- The type of matrix on which LU decomposition is performed.
An abstract base class for LU decomposition of a matrix.
The LU decomposition decomposes a matrix A into the product of a unit-lower triangular matrix L and an upper triangular matrix U, such that: \[ A = LU \]
Pivoting Strategies:
Pivoting may be used to improve the stability of the decomposition. Pivoting involves swapping rows and/or columns within the matrix during decomposition.
This class supports three pivoting strategies via the LU.Pivoting
enum:
LU.Pivoting.NONE
: No pivoting is performed. This pivoting strategy is generally not recommended. \[ A = LU \]LU.Pivoting.PARTIAL
: Only row pivoting is performed to improve numerical stability. Generally, this is the preferred pivoting strategy. The decomposition then becomes, \[ PA = LU \]
where P is a LU.Pivoting.FULL
: Both row and column pivoting are performed to enhance numerical robustness. The decomposition then becomes, \[ PAQ = LU \] where P and Q arepermutation matrices
representing the row and column swaps respectively.Full pivoting may be useful for highly ill-conditioned matrices but, for practical purposes, partial pivoting is generally sufficient and more performant.
permutation matrix
representing the row swaps.
Storage Format:
The computed LU decomposition is stored within a single matrixLU
, where:
- The upper triangular part (including the diagonal) represents the non-zero values of U.
- The strictly lower triangular part represents the non-zero, non-diagonal values of L. Since L is unit-lower triangular, the diagonal is not stored as it is known to be all zeros.
Usage:
The decomposition workflow typically follows these steps:- Instantiate a concrete subclass of
LU
. - Call
decompose(MatrixMixin)
to perform the factorization. - Retrieve the resulting matrices using
getL()
,getU()
,getP()
, andgetQ()
.
Implementation Notes:
Subclasses must implement the specific pivoting strategies by defining:
noPivot()
- LU decomposition without pivoting.partialPivot()
- LU decomposition with row pivoting.fullPivot()
- LU decomposition with full pivoting.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Simple enum containing pivoting options for pivoting in LU decomposition. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]
Array to track column swaps made with full pivoting.protected final boolean
Flag indicating if the decomposition should be done in/out-of-place.protected T
Storage for L and U matrices.protected int
Tracks the number of column swaps made during full pivoting.protected int
Tracks the number of row swaps made during partial/full pivoting.protected PermutationMatrix
Permutation matrix to store row swaps ifpartial pivoting
is used.final LU.Pivoting
Flag indicating what pivoting to use.protected PermutationMatrix
Permutation matrix to store column swaps iffull pivoting
is used.protected int[]
Array to track row swaps made with full/partial pivoting.protected static final String
Error message for when a zero pivot is encountered.Fields inherited from class org.flag4j.linalg.decompositions.Decomposition
hasDecomposed
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
LU
(LU.Pivoting pivoting, boolean inPlace) Constructs a LU decomposer with the specified pivoting. -
Method Summary
Modifier and TypeMethodDescriptionApplies LU decomposition to the source matrix using the pivoting specified in the constructor.protected abstract void
Computes the LU decomposition using full/rook pivoting (i.e. row and column swapping).abstract T
getL()
Gets the unit lower triangular matrix of the decomposition.getLU()
Gets the L and U matrices of the decomposition combined in a single matrix.int
Gets the number of column swaps used in the last decomposition.int
Gets the number of row swaps used in the last decomposition.getP()
Gets the row permutation matrix of the decomposition.getQ()
Gets the column permutation matrix of the decomposition.abstract T
getU()
Gets the upper triangular matrix of the decomposition.protected void
Initializes theLU
matrix by copying the source matrix to decompose.protected abstract void
noPivot()
Computes the LU decomposition using no pivoting (i.e. rows and columns are not swapped).protected abstract void
Computes the LU decomposition using partial pivoting (i.e. row swapping).protected void
swapCols
(int colIdx1, int colIdx2) Tracks the swapping of two columns during gaussian elimination with full pivoting.protected void
swapRows
(int rowIdx1, int rowIdx2) Tracks the swapping of two rows during gaussian elimination with partial or full pivoting.Methods inherited from class org.flag4j.linalg.decompositions.Decomposition
ensureHasDecomposed
-
Field Details
-
ZERO_PIV_ERR
Error message for when a zero pivot is encountered.- See Also:
-
pivotFlag
Flag indicating what pivoting to use. -
inPlace
protected final boolean inPlaceFlag indicating if the decomposition should be done in/out-of-place.- If
true
, then the decomposition will be done in-place. - If
true
, then the decomposition will be done out-of-place.
- If
-
LU
Storage for L and U matrices. Stored in a single matrix.
The upper triangular portion of
LU
, including the diagonal, stores the non-zero values of U while the lower triangular portion, excluding the diagonal, stores the non-zero, non-diagonal values of L. Since L is unit-lower triangular, the diagonal need not be stored as it is known to be all ones. -
P
Permutation matrix to store row swaps ifpartial pivoting
is used. -
Q
Permutation matrix to store column swaps iffull pivoting
is used. -
numRowSwaps
protected int numRowSwapsTracks the number of row swaps made during partial/full pivoting. -
numColSwaps
protected int numColSwapsTracks the number of column swaps made during full pivoting. -
rowSwaps
protected int[] rowSwapsArray to track row swaps made with full/partial pivoting. -
colSwaps
protected int[] colSwapsArray to track column swaps made with full pivoting.
-
-
Constructor Details
-
LU
Constructs a LU decomposer with the specified pivoting.- Parameters:
pivoting
- Pivoting to use.inPlace
- Flag indicating if the decomposition should be done in/out-of-place.- If
true
, then the decomposition will be done in-place. - If
true
, then the decomposition will be done out-of-place.
- If
-
-
Method Details
-
decompose
Applies LU decomposition to the source matrix using the pivoting specified in the constructor.- Specified by:
decompose
in classDecomposition<T extends MatrixMixin<T,
?, ?, ?>> - Parameters:
src
- The source matrix to decompose. IfinPlace
wastrue
in the constructor then this matrix will be modified.- Returns:
- A reference to this decomposer.
-
initLU
Initializes theLU
matrix by copying the source matrix to decompose.- Parameters:
src
- Source matrix to decompose.
-
noPivot
protected abstract void noPivot()Computes the LU decomposition using no pivoting (i.e. rows and columns are not swapped). -
partialPivot
protected abstract void partialPivot()Computes the LU decomposition using partial pivoting (i.e. row swapping). -
fullPivot
protected abstract void fullPivot()Computes the LU decomposition using full/rook pivoting (i.e. row and column swapping). -
getLU
Gets the L and U matrices of the decomposition combined in a single matrix.- Returns:
- The L and U matrices of the decomposition stored together in a single matrix. The diagonal of L is all ones and is not stored allowing the diagonal of U to be stored along the diagonal of the combined matrix.
- Throws:
IllegalStateException
- If this method is called beforedecompose(MatrixMixin)
.
-
getL
Gets the unit lower triangular matrix of the decomposition.- Returns:
- The unit lower triangular matrix of the decomposition.
- Throws:
IllegalStateException
- If this method is called beforedecompose(MatrixMixin)
.
-
getU
Gets the upper triangular matrix of the decomposition.- Returns:
- The upper triangular matrix of the decomposition.
- Throws:
IllegalStateException
- If this method is called beforedecompose(MatrixMixin)
.
-
getP
Gets the row permutation matrix of the decomposition.- Returns:
- The row permutation matrix of the decomposition. If no pivoting was used,
null
will be returned. - Throws:
IllegalStateException
- If this method is called beforedecompose(MatrixMixin)
.
-
getQ
Gets the column permutation matrix of the decomposition.- Returns:
- The column permutation matrix of the decomposition. If full pivoting was not used,
null
will be returned. - Throws:
IllegalStateException
- If this method is called beforedecompose(MatrixMixin)
.
-
getNumRowSwaps
public int getNumRowSwaps()Gets the number of row swaps used in the last decomposition.- Returns:
- The number of row swaps used in the last decomposition.
- Throws:
IllegalStateException
- If this method is called beforedecompose(MatrixMixin)
.
-
getNumColSwaps
public int getNumColSwaps()Gets the number of column swaps used in the last decomposition.- Returns:
- The number of column swaps used in the last decomposition.
- Throws:
IllegalStateException
- If this method is called beforedecompose(MatrixMixin)
.
-
swapRows
protected void swapRows(int rowIdx1, int rowIdx2) Tracks the swapping of two rows during gaussian elimination with partial or full pivoting.- Parameters:
rowIdx1
- First row index in swap.rowIdx2
- Second row index in swap.
-
swapCols
protected void swapCols(int colIdx1, int colIdx2) Tracks the swapping of two columns during gaussian elimination with full pivoting.- Parameters:
colIdx1
- First column index in swap.colIdx2
- Second column index in swap.
-