template<typename _MatrixType>
FullPivLU class
LU decomposition of a matrix with complete pivoting, and related features.
Template parameters | |
---|---|
_MatrixType | the type of the matrix of which we are computing the LU decomposition |
Contents
This class represents a LU decomposition of any matrix, with complete pivoting: the matrix A is decomposed as where L is unit-lower-triangular, U is upper-triangular, and P and Q are permutation matrices. This is a rank-revealing LU decomposition. The eigenvalues (diagonal coefficients) of U are sorted in such a way that any zeros are at the end.
This decomposition provides the generic approach to solving systems of linear equations, computing the rank, invertibility, inverse, kernel, and determinant.
This LU decomposition is very stable and well tested with large matrices. However there are use cases where the SVD decomposition is inherently more stable and/or flexible. For example, when computing the kernel of a matrix, working with the SVD allows to select the smallest singular values of the matrix, something that the LU decomposition doesn't see.
The data of the LU decomposition can be directly accessed through the methods matrixLU(), permutationP(), permutationQ().
As an example, here is how the original matrix can be retrieved:
typedef Matrix<double, 5, 3> Matrix5x3; typedef Matrix<double, 5, 5> Matrix5x5; Matrix5x3 m = Matrix5x3::Random(); cout << "Here is the matrix m:" << endl << m << endl; Eigen::FullPivLU<Matrix5x3> lu(m); cout << "Here is, up to permutations, its LU decomposition matrix:" << endl << lu.matrixLU() << endl; cout << "Here is the L part:" << endl; Matrix5x5 l = Matrix5x5::Identity(); l.block<5,3>(0,0).triangularView<StrictlyLower>() = lu.matrixLU(); cout << l << endl; cout << "Here is the U part:" << endl; Matrix5x3 u = lu.matrixLU().triangularView<Upper>(); cout << u << endl; cout << "Let us now reconstruct the original matrix m:" << endl; cout << lu.permutationP().inverse() * l * u * lu.permutationQ().inverse() << endl;
Output:
Here is the matrix m: 0.68 -0.605 -0.0452 -0.211 -0.33 0.258 0.566 0.536 -0.27 0.597 -0.444 0.0268 0.823 0.108 0.904 Here is, up to permutations, its LU decomposition matrix: 0.904 0.823 0.108 -0.299 0.812 0.569 -0.05 0.888 -1.1 0.0296 0.705 0.768 0.285 -0.549 0.0436 Here is the L part: 1 0 0 0 0 -0.299 1 0 0 0 -0.05 0.888 1 0 0 0.0296 0.705 0.768 1 0 0.285 -0.549 0.0436 0 1 Here is the U part: 0.904 0.823 0.108 0 0.812 0.569 0 0 -1.1 0 0 0 0 0 0 Let us now reconstruct the original matrix m: 0.68 -0.605 -0.0452 -0.211 -0.33 0.258 0.566 0.536 -0.27 0.597 -0.444 0.0268 0.823 0.108 0.904
This class supports the inplace decomposition mechanism.
Base classes
-
template<typename Derived>class SolverBase
- A base class for matrix decomposition and solvers.
Public types
- enum (anonymous) { MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime }
- using Base = SolverBase<FullPivLU>
- using IntColVectorType = internal::plain_col_type<MatrixType, StorageIndex>::type
- using IntRowVectorType = internal::plain_row_type<MatrixType, StorageIndex>::type
- using MatrixType = _MatrixType
- using PermutationPType = PermutationMatrix<RowsAtCompileTime, MaxRowsAtCompileTime>
- using PermutationQType = PermutationMatrix<ColsAtCompileTime, MaxColsAtCompileTime>
- using PlainObject = MatrixType::PlainObject
Constructors, destructors, conversion operators
- FullPivLU()
- Default Constructor.
- FullPivLU(Index rows, Index cols)
- Default Constructor with memory preallocation.
-
template<typename InputType>FullPivLU(const EigenBase<InputType>& matrix) explicit
-
template<typename InputType>FullPivLU(EigenBase<InputType>& matrix) explicit
- Constructs a LU factorization from a given matrix.
Public functions
- auto cols() const -> Index
-
template<typename InputType>auto compute(const EigenBase<InputType>& matrix) -> FullPivLU&
- auto determinant() const -> internal::traits<MatrixType>::Scalar
- auto dimensionOfKernel() const -> Index
- auto image(const MatrixType& originalMatrix) const -> const internal::image_retval<FullPivLU>
- auto inverse() const -> const Inverse<FullPivLU>
- auto isInjective() const -> bool
- auto isInvertible() const -> bool
- auto isSurjective() const -> bool
- auto kernel() const -> const internal::kernel_retval<FullPivLU>
- auto matrixLU() const -> const MatrixType&
- auto maxPivot() const -> RealScalar
- auto nonzeroPivots() const -> Index
- auto permutationP() const -> const PermutationPType&
- auto permutationQ() const -> const PermutationQType&
- auto rank() const -> Index
- auto rcond() const -> RealScalar
- auto reconstructedMatrix() const -> MatrixType
- auto rows() const -> Index
- auto setThreshold(const RealScalar& threshold) -> FullPivLU&
- auto setThreshold(Default_t) -> FullPivLU&
-
template<typename Rhs>auto solve(const MatrixBase<Rhs>& b) const -> const Solve<FullPivLU, Rhs>
- auto threshold() const -> RealScalar
Protected static functions
- static void check_template_parameters()
Protected functions
- void computeInPlace()
Protected variables
- IntRowVectorType m_colsTranspositions
- signed char m_det_pq
- bool m_isInitialized
- RealScalar m_l1_norm
- MatrixType m_lu
- RealScalar m_maxpivot
- Index m_nonzero_pivots
- PermutationPType m_p
- RealScalar m_prescribedThreshold
- PermutationQType m_q
- IntColVectorType m_rowsTranspositions
- bool m_usePrescribedThreshold
Enum documentation
template<typename _MatrixType>
enum Eigen:: FullPivLU<_MatrixType>:: (anonymous)
Enumerators | |
---|---|
MaxRowsAtCompileTime | |
MaxColsAtCompileTime |
Typedef documentation
template<typename _MatrixType>
typedef SolverBase<FullPivLU> Eigen:: FullPivLU<_MatrixType>:: Base
template<typename _MatrixType>
typedef internal::plain_col_type<MatrixType, StorageIndex>::type Eigen:: FullPivLU<_MatrixType>:: IntColVectorType
template<typename _MatrixType>
typedef internal::plain_row_type<MatrixType, StorageIndex>::type Eigen:: FullPivLU<_MatrixType>:: IntRowVectorType
template<typename _MatrixType>
typedef _MatrixType Eigen:: FullPivLU<_MatrixType>:: MatrixType
template<typename _MatrixType>
typedef PermutationMatrix<RowsAtCompileTime, MaxRowsAtCompileTime> Eigen:: FullPivLU<_MatrixType>:: PermutationPType
template<typename _MatrixType>
typedef PermutationMatrix<ColsAtCompileTime, MaxColsAtCompileTime> Eigen:: FullPivLU<_MatrixType>:: PermutationQType
template<typename _MatrixType>
typedef MatrixType::PlainObject Eigen:: FullPivLU<_MatrixType>:: PlainObject
Function documentation
template<typename _MatrixType>
Eigen:: FullPivLU<_MatrixType>:: FullPivLU()
Default Constructor.
The default constructor is useful in cases in which the user intends to perform decompositions via LU::compute(const MatrixType&).
template<typename _MatrixType>
template<typename InputType>
Eigen:: FullPivLU<_MatrixType>:: FullPivLU(EigenBase<InputType>& matrix) explicit
Constructs a LU factorization from a given matrix.
This overloaded constructor is provided for inplace decomposition when MatrixType
is a Eigen::
template<typename _MatrixType>
template<typename InputType>
FullPivLU& Eigen:: FullPivLU<_MatrixType>:: compute(const EigenBase<InputType>& matrix)
Parameters | |
---|---|
matrix | the matrix of which to compute the LU decomposition. It is required to be nonzero. |
Returns | a reference to *this |
Computes the LU decomposition of the given matrix.
template<typename _MatrixType>
internal::traits<MatrixType>::Scalar Eigen:: FullPivLU<_MatrixType>:: determinant() const
Returns | the determinant of the matrix of which *this is the LU decomposition. It has only linear complexity (that is, O(n) where n is the dimension of the square matrix) as the LU decomposition has already been computed. |
---|
template<typename _MatrixType>
Index Eigen:: FullPivLU<_MatrixType>:: dimensionOfKernel() const
Returns | the dimension of the kernel of the matrix of which *this is the LU decomposition. |
---|
template<typename _MatrixType>
const internal::image_retval<FullPivLU> Eigen:: FullPivLU<_MatrixType>:: image(const MatrixType& originalMatrix) const
Parameters | |
---|---|
originalMatrix | the original matrix, of which *this is the LU decomposition. The reason why it is needed to pass it here, is that this allows a large optimization, as otherwise this method would need to reconstruct it from the LU decomposition. |
Returns | the image of the matrix, also called its column-space. The columns of the returned matrix will form a basis of the image (column-space). |
Example:
Matrix3d m; m << 1,1,0, 1,3,2, 0,1,1; cout << "Here is the matrix m:" << endl << m << endl; cout << "Notice that the middle column is the sum of the two others, so the " << "columns are linearly dependent." << endl; cout << "Here is a matrix whose columns have the same span but are linearly independent:" << endl << m.fullPivLu().image(m) << endl;
Output:
Here is the matrix m: 1 1 0 1 3 2 0 1 1 Notice that the middle column is the sum of the two others, so the columns are linearly dependent. Here is a matrix whose columns have the same span but are linearly independent: 1 1 3 1 1 0
template<typename _MatrixType>
bool Eigen:: FullPivLU<_MatrixType>:: isInjective() const
Returns | true if the matrix of which *this is the LU decomposition represents an injective linear map, i.e. has trivial kernel; false otherwise. |
---|
template<typename _MatrixType>
bool Eigen:: FullPivLU<_MatrixType>:: isInvertible() const
Returns | true if the matrix of which *this is the LU decomposition is invertible. |
---|
template<typename _MatrixType>
bool Eigen:: FullPivLU<_MatrixType>:: isSurjective() const
Returns | true if the matrix of which *this is the LU decomposition represents a surjective linear map; false otherwise. |
---|
template<typename _MatrixType>
const internal::kernel_retval<FullPivLU> Eigen:: FullPivLU<_MatrixType>:: kernel() const
Returns | the kernel of the matrix, also called its null-space. The columns of the returned matrix will form a basis of the kernel. |
---|
Example:
MatrixXf m = MatrixXf::Random(3,5); cout << "Here is the matrix m:" << endl << m << endl; MatrixXf ker = m.fullPivLu().kernel(); cout << "Here is a matrix whose columns form a basis of the kernel of m:" << endl << ker << endl; cout << "By definition of the kernel, m*ker is zero:" << endl << m*ker << endl;
Output:
Here is the matrix m: 0.68 0.597 -0.33 0.108 -0.27 -0.211 0.823 0.536 -0.0452 0.0268 0.566 -0.605 -0.444 0.258 0.904 Here is a matrix whose columns form a basis of the kernel of m: -0.219 0.763 0.00335 -0.447 0 1 1 0 -0.145 -0.285 By definition of the kernel, m*ker is zero: 7.45e-09 1.49e-08 -1.86e-09 -4.05e-08 0 -2.98e-08
template<typename _MatrixType>
const MatrixType& Eigen:: FullPivLU<_MatrixType>:: matrixLU() const
Returns | the LU decomposition matrix: the upper-triangular part is U, the unit-lower-triangular part is L (at least for square matrices; in the non-square case, special care is needed, see the documentation of class FullPivLU). |
---|
template<typename _MatrixType>
RealScalar Eigen:: FullPivLU<_MatrixType>:: maxPivot() const
Returns | the absolute value of the biggest pivot, i.e. the biggest diagonal coefficient of U. |
---|
template<typename _MatrixType>
Index Eigen:: FullPivLU<_MatrixType>:: nonzeroPivots() const
Returns | the number of nonzero pivots in the LU decomposition. Here nonzero is meant in the exact sense, not in a fuzzy sense. So that notion isn't really intrinsically interesting, but it is still useful when implementing algorithms. |
---|
template<typename _MatrixType>
const PermutationPType& Eigen:: FullPivLU<_MatrixType>:: permutationP() const
Returns | the permutation matrix P |
---|
template<typename _MatrixType>
const PermutationQType& Eigen:: FullPivLU<_MatrixType>:: permutationQ() const
Returns | the permutation matrix Q |
---|
template<typename _MatrixType>
RealScalar Eigen:: FullPivLU<_MatrixType>:: rcond() const
Returns | an estimate of the reciprocal condition number of the matrix of which *this is the LU decomposition. |
---|
template<typename _MatrixType>
MatrixType Eigen:: FullPivLU<_MatrixType>:: reconstructedMatrix() const
Returns | the matrix represented by the decomposition, i.e., it returns the product: . This function is provided for debug purposes. |
---|
template<typename _MatrixType>
FullPivLU& Eigen:: FullPivLU<_MatrixType>:: setThreshold(const RealScalar& threshold)
Parameters | |
---|---|
threshold | The new value to use as the threshold. |
Allows to prescribe a threshold to be used by certain methods, such as rank(), who need to determine when pivots are to be considered nonzero. This is not used for the LU decomposition itself.
When it needs to get the threshold value, Eigen calls threshold(). By default, this uses a formula to automatically determine a reasonable threshold. Once you have called the present method setThreshold(const RealScalar&), your value is used instead.
A pivot will be considered nonzero if its absolute value is strictly greater than where maxpivot is the biggest pivot.
If you want to come back to the default behavior, call setThreshold(Default_
template<typename _MatrixType>
FullPivLU& Eigen:: FullPivLU<_MatrixType>:: setThreshold(Default_t)
Allows to come back to the default behavior, letting Eigen use its default formula for determining the threshold.
You should pass the special object Eigen::Default as parameter here. lu.setThreshold(Eigen::Default);
See the documentation of setThreshold(const RealScalar&).
template<typename _MatrixType>
template<typename Rhs>
const Solve<FullPivLU, Rhs> Eigen:: FullPivLU<_MatrixType>:: solve(const MatrixBase<Rhs>& b) const
Parameters | |
---|---|
b | the right-hand-side of the equation to solve. Can be a vector or a matrix, the only requirement in order for the equation to make sense is that b.rows()==A.rows(), where A is the matrix of which *this is the LU decomposition. |
Returns | a solution x to the equation Ax=b, where A is the matrix of which *this is the LU decomposition. |
This method just tries to find as good a solution as possible. If you want to check whether a solution exists or if it is accurate, just call this function to get a result and then compute the error of this result, or use MatrixBase::bool a_solution_exists = (A*result).isApprox(b, precision);
This method avoids dividing by zero, so that the non-existence of a solution doesn't by itself mean that you'll get inf
or nan
values.
If there exists more than one solution, this method will arbitrarily choose one. If you need a complete analysis of the space of solutions, take the one solution obtained by this method and add to it elements of the kernel, as determined by kernel().
Example:
Matrix<float,2,3> m = Matrix<float,2,3>::Random(); Matrix2f y = Matrix2f::Random(); cout << "Here is the matrix m:" << endl << m << endl; cout << "Here is the matrix y:" << endl << y << endl; Matrix<float,3,2> x = m.fullPivLu().solve(y); if((m*x).isApprox(y)) { cout << "Here is a solution x to the equation mx=y:" << endl << x << endl; } else cout << "The equation mx=y does not have any solution." << endl;
Output:
Here is the matrix m: 0.68 0.566 0.823 -0.211 0.597 -0.605 Here is the matrix y: -0.33 -0.444 0.536 0.108 Here is a solution x to the equation mx=y: 0 0 0.291 -0.216 -0.6 -0.391
template<typename _MatrixType>
RealScalar Eigen:: FullPivLU<_MatrixType>:: threshold() const
Returns the threshold that will be used by certain methods such as rank().
See the documentation of setThreshold(const RealScalar&).