15#include "./InternalHeaderCheck.h"
20template <
typename DecompositionType>
22template <
typename DecompositionType>
25template <
typename MatrixType_,
typename PermutationIndex_>
26struct traits<FullPivLU<MatrixType_, PermutationIndex_> > : traits<MatrixType_> {
27 using XprKind = MatrixXpr;
28 using StorageKind = SolverStorage;
29 using StorageIndex = PermutationIndex_;
68template <
typename MatrixType_,
typename PermutationIndex_>
70 public RankRevealingBase<FullPivLU<MatrixType_, PermutationIndex_> > {
72 using MatrixType = MatrixType_;
74 using RankRevealingBase_ = RankRevealingBase<FullPivLU>;
76 friend class RankRevealingBase<
FullPivLU>;
89 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
90 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
92 using PermutationIndex = PermutationIndex_;
93 using IntRowVectorType =
typename internal::plain_row_type<MatrixType, PermutationIndex>::type;
94 using IntColVectorType =
typename internal::plain_col_type<MatrixType, PermutationIndex>::type;
97 using PlainObject =
typename MatrixType::PlainObject;
106 eigen_assert(m_isInitialized &&
"FullPivLU is not initialized.");
131 template <
typename InputType>
141 template <
typename InputType>
151 template <
typename InputType>
154 m_lu.resize(matrix.
rows(), matrix.
cols());
167 eigen_assert(m_isInitialized &&
"LU is not initialized.");
175 EIGEN_DEVICE_FUNC
inline const PermutationPType&
permutationP()
const {
176 eigen_assert(m_isInitialized &&
"LU is not initialized.");
185 eigen_assert(m_isInitialized &&
"LU is not initialized.");
203 inline const internal::kernel_retval<FullPivLU>
kernel()
const {
204 eigen_assert(m_isInitialized &&
"LU is not initialized.");
205 return internal::kernel_retval<FullPivLU>(*
this);
227 inline const internal::image_retval<FullPivLU>
image(
const MatrixType& originalMatrix)
const {
228 eigen_assert(m_isInitialized &&
"LU is not initialized.");
229 return internal::image_retval<FullPivLU>(*
this, originalMatrix);
232#ifdef EIGEN_PARSED_BY_DOXYGEN
252 template <
typename Rhs>
260 eigen_assert(m_isInitialized &&
"FullPivLU is not initialized.");
262 return RealScalar(0);
264 return internal::rcond_estimate_helper(m_l1_norm, *
this);
285 typename internal::traits<MatrixType>::Scalar
determinant()
const;
350 eigen_assert(m_isInitialized &&
"LU is not initialized.");
351 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_lu,
"You can't take the inverse of a non-square matrix!");
357 EIGEN_DEVICE_FUNC
constexpr Index rows() const noexcept {
return m_lu.rows(); }
358 EIGEN_DEVICE_FUNC
constexpr Index cols() const noexcept {
return m_lu.cols(); }
360#ifndef EIGEN_PARSED_BY_DOXYGEN
361 template <
typename RhsType,
typename DstType>
362 void _solve_impl(
const RhsType& rhs, DstType& dst)
const;
364 template <
bool Conjugate,
typename RhsType,
typename DstType>
365 void _solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const;
369 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
371 void computeInPlace();
374 PermutationPType m_p;
375 PermutationQType m_q;
376 IntColVectorType m_rowsTranspositions;
377 IntRowVectorType m_colsTranspositions;
378 RealScalar m_l1_norm;
379 signed char m_det_pq;
380 bool m_isInitialized;
383template <
typename MatrixType,
typename PermutationIndex>
386template <
typename MatrixType,
typename PermutationIndex>
391 m_rowsTranspositions(rows),
392 m_colsTranspositions(cols),
393 m_isInitialized(false) {}
395template <
typename MatrixType,
typename PermutationIndex>
396template <
typename InputType>
398 : m_lu(matrix.rows(), matrix.cols()),
401 m_rowsTranspositions(matrix.rows()),
402 m_colsTranspositions(matrix.cols()),
403 m_isInitialized(false) {
407template <
typename MatrixType,
typename PermutationIndex>
408template <
typename InputType>
413 m_rowsTranspositions(matrix.rows()),
414 m_colsTranspositions(matrix.cols()),
415 m_isInitialized(false) {
419template <
typename MatrixType,
typename PermutationIndex>
420void FullPivLU<MatrixType, PermutationIndex>::computeInPlace() {
421 eigen_assert(m_lu.rows() <= NumTraits<PermutationIndex>::highest() &&
422 m_lu.cols() <= NumTraits<PermutationIndex>::highest());
425 m_l1_norm = m_lu.cwiseAbs().colwise().sum().maxCoeff();
427 m_l1_norm = RealScalar(0);
429 const Index size = m_lu.diagonalSize();
430 const Index rows = m_lu.rows();
431 const Index cols = m_lu.cols();
435 m_rowsTranspositions.resize(m_lu.rows());
436 m_colsTranspositions.resize(m_lu.cols());
437 Index number_of_transpositions = 0;
439 this->m_nonzero_pivots = size;
440 this->m_maxpivot = RealScalar(0);
442 for (Index k = 0; k < size; ++k) {
446 Index row_of_biggest_in_corner, col_of_biggest_in_corner;
447 using Scoring = internal::scalar_score_coeff_op<Scalar>;
448 using Score =
typename Scoring::result_type;
449 Score biggest_in_corner;
450 biggest_in_corner = m_lu.bottomRightCorner(rows - k, cols - k)
451 .unaryExpr(Scoring())
452 .maxCoeff(&row_of_biggest_in_corner, &col_of_biggest_in_corner);
453 row_of_biggest_in_corner += k;
454 col_of_biggest_in_corner += k;
456 if (numext::is_exactly_zero(biggest_in_corner)) {
459 this->m_nonzero_pivots = k;
460 for (Index i = k; i < size; ++i) {
461 m_rowsTranspositions.coeffRef(i) = internal::convert_index<StorageIndex>(i);
462 m_colsTranspositions.coeffRef(i) = internal::convert_index<StorageIndex>(i);
467 RealScalar abs_pivot = internal::abs_knowing_score<Scalar>()(
468 m_lu(row_of_biggest_in_corner, col_of_biggest_in_corner), biggest_in_corner);
469 if (abs_pivot > this->m_maxpivot) this->m_maxpivot = abs_pivot;
474 m_rowsTranspositions.coeffRef(k) = internal::convert_index<StorageIndex>(row_of_biggest_in_corner);
475 m_colsTranspositions.coeffRef(k) = internal::convert_index<StorageIndex>(col_of_biggest_in_corner);
476 if (k != row_of_biggest_in_corner) {
477 m_lu.row(k).swap(m_lu.row(row_of_biggest_in_corner));
478 ++number_of_transpositions;
480 if (k != col_of_biggest_in_corner) {
481 m_lu.col(k).swap(m_lu.col(col_of_biggest_in_corner));
482 ++number_of_transpositions;
488 if (k < rows - 1) m_lu.col(k).tail(rows - k - 1) /= m_lu.coeff(k, k);
490 m_lu.block(k + 1, k + 1, rows - k - 1, cols - k - 1).noalias() -=
491 m_lu.col(k).tail(rows - k - 1) * m_lu.row(k).tail(cols - k - 1);
497 m_p.setIdentity(rows);
498 for (Index k = size - 1; k >= 0; --k) m_p.applyTranspositionOnTheRight(k, m_rowsTranspositions.coeff(k));
500 m_q.setIdentity(cols);
501 for (Index k = 0; k < size; ++k) m_q.applyTranspositionOnTheRight(k, m_colsTranspositions.coeff(k));
503 m_det_pq = (number_of_transpositions % 2) ? -1 : 1;
505 m_isInitialized =
true;
508template <
typename MatrixType,
typename PermutationIndex>
510 eigen_assert(m_isInitialized &&
"LU is not initialized.");
511 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_lu,
"You can't take the determinant of a non-square matrix!");
512 return Scalar(m_det_pq) * Scalar(internal::scaled_prod(m_lu.diagonal()).value());
515template <
typename MatrixType,
typename PermutationIndex>
518 eigen_assert(m_isInitialized &&
"LU is not initialized.");
519 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_lu,
"You can't take the determinant of a non-square matrix!");
520 return isInjective() ? internal::scaled_prod(m_lu.diagonal()).abs() : RealScalar(0);
523template <
typename MatrixType,
typename PermutationIndex>
524typename FullPivLU<MatrixType, PermutationIndex>::RealScalar
526 eigen_assert(m_isInitialized &&
"LU is not initialized.");
527 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_lu,
"You can't take the determinant of a non-square matrix!");
528 return isInjective() ? internal::log_abs_prod(m_lu.diagonal()) : -NumTraits<RealScalar>::infinity();
531template <
typename MatrixType,
typename PermutationIndex>
534 eigen_assert(m_isInitialized &&
"LU is not initialized.");
535 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_lu,
"You can't take the determinant of a non-square matrix!");
536 return isInjective() ? Scalar(m_det_pq) * m_lu.diagonal().array().sign().prod() : Scalar(0);
542template <
typename MatrixType,
typename PermutationIndex>
544 eigen_assert(m_isInitialized &&
"LU is not initialized.");
545 const Index smalldim = (std::min)(m_lu.rows(), m_lu.cols());
547 MatrixType res(m_lu.rows(), m_lu.cols());
549 res.noalias() = m_lu.leftCols(smalldim).template triangularView<UnitLower>().toDenseMatrix() *
550 m_lu.topRows(smalldim).template triangularView<Upper>().toDenseMatrix();
553 res = m_p.inverse() * res;
556 res = res * m_q.inverse();
564template <
typename MatrixType,
typename PermutationIndex>
566 constexpr int MaxSmallDimAtCompileTime =
567 min_size_prefer_fixed(MatrixType::MaxColsAtCompileTime, MatrixType::MaxRowsAtCompileTime);
569 const typename MatrixType::RealScalar premultiplied_threshold = dec.
maxPivot() * dec.
threshold();
572 if (numext::abs(dec.
matrixLU().coeff(i, i)) > premultiplied_threshold) pivots.coeffRef(p++) = i;
573 eigen_internal_assert(p == rank);
577template <
typename MatrixType_,
typename PermutationIndex_>
578struct traits<kernel_retval<FullPivLU<MatrixType_, PermutationIndex_>>> {
579 using ReturnType = Matrix<
typename MatrixType_::Scalar, MatrixType_::ColsAtCompileTime, Dynamic,
580 plain_object_options<MatrixType_>::value, MatrixType_::MaxColsAtCompileTime,
581 MatrixType_::MaxColsAtCompileTime>;
584template <
typename MatrixType_,
typename PermutationIndex_>
585struct kernel_retval<FullPivLU<MatrixType_, PermutationIndex_>>
586 : ReturnByValue<kernel_retval<FullPivLU<MatrixType_, PermutationIndex_>>> {
587 using DecompositionType = FullPivLU<MatrixType_, PermutationIndex_>;
588 using MatrixType = MatrixType_;
589 using Scalar =
typename MatrixType::Scalar;
591 static constexpr int MaxSmallDimAtCompileTime =
592 min_size_prefer_fixed(MatrixType::MaxColsAtCompileTime, MatrixType::MaxRowsAtCompileTime);
594 explicit kernel_retval(
const DecompositionType& dec)
595 : m_dec(dec), m_rank(dec.rank()), m_cols(m_rank == dec.cols() ? 1 : dec.cols() - m_rank) {}
597 Index rows()
const {
return m_dec.cols(); }
598 Index cols()
const {
return m_cols; }
600 template <
typename Dest>
601 void evalTo(Dest& dst)
const {
602 const Index cols = m_dec.matrixLU().cols(), dimker = cols - m_rank;
610 const auto pivots = fullpivlu_nonzero_pivots(m_dec, m_rank);
616 Matrix<typename MatrixType::Scalar, Dynamic, Dynamic, plain_object_options<MatrixType>::value,
617 MaxSmallDimAtCompileTime, MatrixType::MaxColsAtCompileTime>
618 m(m_dec.matrixLU().block(0, 0, m_rank, cols));
619 for (Index i = 0; i < m_rank; ++i) {
620 if (i) m.row(i).head(i).setZero();
621 m.row(i).tail(cols - i) = m_dec.matrixLU().row(pivots.coeff(i)).tail(cols - i);
623 m.block(0, 0, m_rank, m_rank).template triangularView<StrictlyLower>().setZero();
624 for (Index i = 0; i < m_rank; ++i) m.col(i).swap(m.col(pivots.coeff(i)));
629 m.topLeftCorner(m_rank, m_rank).template triangularView<Upper>().solveInPlace(m.topRightCorner(m_rank, dimker));
632 for (Index i = m_rank - 1; i >= 0; --i) m.col(i).swap(m.col(pivots.coeff(i)));
635 for (Index i = 0; i < m_rank; ++i) dst.row(m_dec.permutationQ().indices().coeff(i)) = -m.row(i).tail(dimker);
636 for (Index i = m_rank; i < cols; ++i) dst.row(m_dec.permutationQ().indices().coeff(i)).setZero();
637 for (Index k = 0; k < dimker; ++k) dst.coeffRef(m_dec.permutationQ().indices().coeff(m_rank + k), k) = Scalar(1);
641 const DecompositionType& m_dec;
642 Index m_rank, m_cols;
647template <
typename MatrixType_,
typename PermutationIndex_>
648struct traits<image_retval<FullPivLU<MatrixType_, PermutationIndex_>>> {
649 using ReturnType = Matrix<
typename MatrixType_::Scalar, MatrixType_::RowsAtCompileTime, Dynamic,
650 plain_object_options<MatrixType_>::value, MatrixType_::MaxRowsAtCompileTime,
651 MatrixType_::MaxColsAtCompileTime>;
654template <
typename MatrixType_,
typename PermutationIndex_>
655struct image_retval<FullPivLU<MatrixType_, PermutationIndex_>>
656 : ReturnByValue<image_retval<FullPivLU<MatrixType_, PermutationIndex_>>> {
657 using DecompositionType = FullPivLU<MatrixType_, PermutationIndex_>;
658 using MatrixType = MatrixType_;
660 image_retval(
const DecompositionType& dec,
const MatrixType& originalMatrix)
661 : m_dec(dec), m_rank(dec.rank()), m_originalMatrix(originalMatrix) {}
663 Index rows()
const {
return m_dec.rows(); }
664 Index cols()
const {
return m_rank == 0 ? 1 : m_rank; }
666 template <
typename Dest>
667 void evalTo(Dest& dst)
const {
675 const auto pivots = fullpivlu_nonzero_pivots(m_dec, m_rank);
676 for (Index i = 0; i < m_rank; ++i)
677 dst.col(i) = m_originalMatrix.col(m_dec.permutationQ().indices().coeff(pivots.coeff(i)));
681 const DecompositionType& m_dec;
683 const MatrixType& m_originalMatrix;
690#ifndef EIGEN_PARSED_BY_DOXYGEN
691template <
typename MatrixType_,
typename PermutationIndex_>
692template <
typename RhsType,
typename DstType>
702 const Index rows = this->rows(), cols = this->cols(), nonzero_pivots = this->rank();
703 const Index smalldim = (std::min)(rows, cols);
705 if (nonzero_pivots == 0) {
710 typename RhsType::PlainObject c(rhs.rows(), rhs.cols());
713 c = permutationP() * rhs;
716 m_lu.topLeftCorner(smalldim, smalldim).template triangularView<UnitLower>().solveInPlace(c.topRows(smalldim));
717 if (rows > cols) c.bottomRows(rows - cols).noalias() -= m_lu.bottomRows(rows - cols) * c.topRows(cols);
720 m_lu.topLeftCorner(nonzero_pivots, nonzero_pivots)
721 .template triangularView<Upper>()
722 .solveInPlace(c.topRows(nonzero_pivots));
725 for (Index i = 0; i < nonzero_pivots; ++i) dst.row(permutationQ().indices().coeff(i)) = c.row(i);
726 for (Index i = nonzero_pivots; i < m_lu.cols(); ++i) dst.row(permutationQ().indices().coeff(i)).setZero();
729template <
typename MatrixType_,
typename PermutationIndex_>
730template <
bool Conjugate,
typename RhsType,
typename DstType>
743 const Index rows = this->rows(), cols = this->cols(), nonzero_pivots = this->rank();
744 const Index smalldim = (std::min)(rows, cols);
746 if (nonzero_pivots == 0) {
751 typename RhsType::PlainObject c(rhs.rows(), rhs.cols());
754 c = permutationQ().inverse() * rhs;
757 m_lu.topLeftCorner(nonzero_pivots, nonzero_pivots)
758 .template triangularView<Upper>()
760 .template conjugateIf<Conjugate>()
761 .solveInPlace(c.topRows(nonzero_pivots));
764 m_lu.topLeftCorner(smalldim, smalldim)
765 .template triangularView<UnitLower>()
767 .template conjugateIf<Conjugate>()
768 .solveInPlace(c.topRows(smalldim));
771 PermutationPType invp = permutationP().inverse().eval();
772 for (Index i = 0; i < smalldim; ++i) dst.row(invp.indices().coeff(i)) = c.row(i);
773 for (Index i = smalldim; i < rows; ++i) dst.row(invp.indices().coeff(i)).setZero();
781template <
typename DstXprType,
typename MatrixType,
typename PermutationIndex>
783 DstXprType, Inverse<FullPivLU<MatrixType, PermutationIndex> >,
784 internal::assign_op<typename DstXprType::Scalar, typename FullPivLU<MatrixType, PermutationIndex>::Scalar>,
786 using LuType = FullPivLU<MatrixType, PermutationIndex>;
787 using SrcXprType = Inverse<LuType>;
788 static void run(DstXprType& dst,
const SrcXprType& src,
789 const internal::assign_op<typename DstXprType::Scalar, typename MatrixType::Scalar>&) {
790 dst = src.nestedExpression().solve(MatrixType::Identity(src.rows(), src.cols()));
803template <
typename Derived>
804template <
typename PermutationIndex>
EvalReturnType eval() const
Definition DenseBase.h:389
LU decomposition of a matrix with complete pivoting, and related features.
Definition FullPivLU.h:70
FullPivLU(EigenBase< InputType > &matrix)
Constructs a LU factorization from a given matrix.
Definition FullPivLU.h:409
const internal::image_retval< FullPivLU > image(const MatrixType &originalMatrix) const
Definition FullPivLU.h:227
RealScalar threshold() const
Definition RankRevealingBase.h:110
const PermutationPType & permutationP() const
Definition FullPivLU.h:175
ComputationInfo info() const
Reports whether the LU factorization was successful.
Definition FullPivLU.h:105
Scalar signDeterminant() const
Definition FullPivLU.h:532
RealScalar pivotCoeff(Index i) const
Definition FullPivLU.h:340
internal::traits< MatrixType >::Scalar determinant() const
Definition FullPivLU.h:509
Index nonzeroPivots() const
Definition RankRevealingBase.h:186
RealScalar rcond() const
Definition FullPivLU.h:259
bool isInvertible() const
Definition RankRevealingBase.h:174
Inverse< FullPivLU > inverse() const
Definition FullPivLU.h:349
MatrixType reconstructedMatrix() const
Definition FullPivLU.h:543
RealScalar absDeterminant() const
Definition FullPivLU.h:516
RealScalar logAbsDeterminant() const
Definition FullPivLU.h:525
bool isInjective() const
Definition RankRevealingBase.h:151
const internal::kernel_retval< FullPivLU > kernel() const
Definition FullPivLU.h:203
RealScalar maxPivot() const
Definition RankRevealingBase.h:194
FullPivLU(const EigenBase< InputType > &matrix)
Definition FullPivLU.h:397
const MatrixType & matrixLU() const
Definition FullPivLU.h:166
Solve< FullPivLU, Rhs > solve(const MatrixBase< Rhs > &b) const
FullPivLU(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition FullPivLU.h:387
const PermutationQType & permutationQ() const
Definition FullPivLU.h:184
FullPivLU & compute(const EigenBase< InputType > &matrix)
Definition FullPivLU.h:152
FullPivLU()
Default Constructor.
Definition FullPivLU.h:384
Expression of the inverse of another expression.
Definition Inverse.h:44
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Permutation matrix.
Definition PermutationMatrix.h:346
Index dimensionOfKernel() const
Definition RankRevealingBase.h:139
bool isInjective() const
Definition RankRevealingBase.h:151
bool isSurjective() const
Definition RankRevealingBase.h:163
FullPivLU & setThreshold(const RealScalar &threshold)
Definition RankRevealingBase.h:87
Index nonzeroPivots() const
Definition RankRevealingBase.h:186
RealScalar threshold() const
Definition RankRevealingBase.h:110
RealScalar maxPivot() const
Definition RankRevealingBase.h:194
Index rank() const
Definition RankRevealingBase.h:125
bool isInvertible() const
Definition RankRevealingBase.h:174
Pseudo expression representing a solving operation.
Definition Solve.h:63
constexpr FullPivLU< MatrixType_, PermutationIndex_ > & derived()
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457
Definition EigenBase.h:34
constexpr Index cols() const noexcept
Definition EigenBase.h:62
constexpr Derived & derived()
Definition EigenBase.h:50
constexpr Index rows() const noexcept
Definition EigenBase.h:60
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44