12#ifndef EIGEN_PARTIALLU_H
13#define EIGEN_PARTIALLU_H
16#include "./InternalHeaderCheck.h"
21template <
typename MatrixType_,
typename PermutationIndex_>
22struct traits<PartialPivLU<MatrixType_, PermutationIndex_> > : traits<MatrixType_> {
23 using XprKind = MatrixXpr;
24 using StorageKind = SolverStorage;
25 using StorageIndex = PermutationIndex_;
26 using BaseTraits = traits<MatrixType_>;
27 enum { Flags = BaseTraits::Flags &
RowMajorBit, CoeffReadCost = Dynamic };
66template <
typename MatrixType_,
typename PermutationIndex_>
69 using MatrixType = MatrixType_;
75 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
76 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
78 using PermutationIndex = PermutationIndex_;
81 using PlainObject =
typename MatrixType::PlainObject;
90 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
117 template <
typename InputType>
127 template <
typename InputType>
130 template <
typename InputType>
144 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
151 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
155#ifdef EIGEN_PARSED_BY_DOXYGEN
173 template <
typename Rhs>
181 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
182 return internal::rcond_estimate_helper(m_l1_norm, *
this);
193 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
262 constexpr Index rows() const noexcept {
return m_lu.rows(); }
263 constexpr Index cols() const noexcept {
return m_lu.cols(); }
265#ifndef EIGEN_PARSED_BY_DOXYGEN
266 template <
typename RhsType,
typename DstType>
267 EIGEN_DEVICE_FUNC
void _solve_impl(
const RhsType& rhs, DstType& dst)
const {
279 m_lu.template triangularView<UnitLower>().solveInPlace(dst);
282 m_lu.template triangularView<Upper>().solveInPlace(dst);
285 template <
bool Conjugate,
typename RhsType,
typename DstType>
286 EIGEN_DEVICE_FUNC
void _solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const {
294 eigen_assert(rhs.rows() == m_lu.cols());
297 dst = m_lu.template triangularView<Upper>().transpose().template conjugateIf<Conjugate>().solve(rhs);
299 m_lu.template triangularView<UnitLower>().transpose().template conjugateIf<Conjugate>().solveInPlace(dst);
306 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
312 TranspositionType m_rowsTranspositions;
313 RealScalar m_l1_norm;
315 bool m_isInitialized;
318template <
typename MatrixType,
typename PermutationIndex>
320 : m_lu(), m_p(), m_rowsTranspositions(), m_l1_norm(0), m_det_p(0), m_isInitialized(false) {}
322template <
typename MatrixType,
typename PermutationIndex>
324 : m_lu(
size,
size), m_p(
size), m_rowsTranspositions(
size), m_l1_norm(0), m_det_p(0), m_isInitialized(false) {}
326template <
typename MatrixType,
typename PermutationIndex>
327template <
typename InputType>
329 : m_lu(matrix.rows(), matrix.cols()),
331 m_rowsTranspositions(matrix.rows()),
334 m_isInitialized(false) {
338template <
typename MatrixType,
typename PermutationIndex>
339template <
typename InputType>
343 m_rowsTranspositions(matrix.rows()),
346 m_isInitialized(false) {
354template <
int K,
int Size,
bool Last = (K == Size - 1)>
355struct unrolled_partial_lu_step {
356 template <
typename MatrixTypeRef,
typename PivIndex>
357 static EIGEN_STRONG_INLINE
void run(MatrixTypeRef& lu, PivIndex* row_transpositions, PivIndex& nb_transpositions,
358 Index& first_zero_pivot) {
359 using Scalar =
typename MatrixTypeRef::Scalar;
360 using Scoring = scalar_score_coeff_op<Scalar>;
361 using Score =
typename Scoring::result_type;
362 constexpr int Remaining = Size - K - 1;
364 Index row_of_biggest_in_col;
365 const Score biggest_in_corner =
366 lu.col(K).template tail<Size - K>().unaryExpr(Scoring()).maxCoeff(&row_of_biggest_in_col);
367 row_of_biggest_in_col += K;
368 row_transpositions[K] = PivIndex(row_of_biggest_in_col);
369 if (!numext::is_exactly_zero(biggest_in_corner)) {
370 if (K != row_of_biggest_in_col) {
371 lu.row(K).swap(lu.row(row_of_biggest_in_col));
374 lu.col(K).template tail<Remaining>() /= lu.coeff(K, K);
375 }
else if (first_zero_pivot == -1) {
376 first_zero_pivot = K;
378 lu.template bottomRightCorner<Remaining, Remaining>().noalias() -=
379 lu.col(K).template tail<Remaining>() * lu.row(K).template tail<Remaining>();
380 unrolled_partial_lu_step<K + 1, Size>::run(lu, row_transpositions, nb_transpositions, first_zero_pivot);
384template <
int K,
int Size>
385struct unrolled_partial_lu_step<K, Size, true> {
386 template <
typename MatrixTypeRef,
typename PivIndex>
387 static EIGEN_STRONG_INLINE
void run(MatrixTypeRef& lu, PivIndex* row_transpositions, PivIndex&,
388 Index& first_zero_pivot) {
389 using Scoring = scalar_score_coeff_op<typename MatrixTypeRef::Scalar>;
390 row_transpositions[K] = PivIndex(K);
391 if (numext::is_exactly_zero(Scoring()(lu.coeff(K, K))) && first_zero_pivot == -1) first_zero_pivot = K;
396template <
bool Unroll,
int Size>
397struct unrolled_partial_lu {
398 template <
typename MatrixTypeRef,
typename PivIndex>
399 static EIGEN_STRONG_INLINE
bool run(MatrixTypeRef&, PivIndex*, PivIndex&, Index&) {
405struct unrolled_partial_lu<true, Size> {
406 template <
typename MatrixTypeRef,
typename PivIndex>
407 static EIGEN_STRONG_INLINE
bool run(MatrixTypeRef& lu, PivIndex* row_transpositions, PivIndex& nb_transpositions,
408 Index& first_zero_pivot) {
409 nb_transpositions = 0;
410 first_zero_pivot = -1;
411 unrolled_partial_lu_step<0, Size>::run(lu, row_transpositions, nb_transpositions, first_zero_pivot);
417template <
typename Scalar,
int StorageOrder,
typename PivIndex,
int SizeAtCompileTime = Dynamic>
418struct generic_partial_lu_impl {
419 static constexpr int UnBlockedBound = 16;
420 static constexpr bool UnBlockedAtCompileTime = SizeAtCompileTime != Dynamic && SizeAtCompileTime <= UnBlockedBound;
421 static constexpr int ActualSizeAtCompileTime = UnBlockedAtCompileTime ? SizeAtCompileTime : Dynamic;
425 static constexpr int UnrolledBound = 12;
426 static constexpr bool UnrolledAtCompileTime = UnBlockedAtCompileTime && SizeAtCompileTime <= UnrolledBound;
428 static constexpr int RRows = SizeAtCompileTime == 2 ? 1 : Dynamic;
429 static constexpr int RCols = SizeAtCompileTime == 2 ? 1 : Dynamic;
430 using MatrixType = Matrix<Scalar, ActualSizeAtCompileTime, ActualSizeAtCompileTime, StorageOrder>;
431 using MatrixTypeRef = Ref<MatrixType>;
432 using BlockType = Ref<Matrix<Scalar, Dynamic, Dynamic, StorageOrder>>;
433 using RealScalar =
typename MatrixType::RealScalar;
435 static void apply_row_transpositions(BlockType& matrix, Index first, Index count,
const PivIndex* transpositions) {
436 EIGEN_IF_CONSTEXPR (StorageOrder ==
ColMajor) {
438 for (Index j = 0; j < matrix.cols(); ++j)
439 for (Index i = first; i < first + count; ++i)
440 numext::swap(matrix.coeffRef(i, j), matrix.coeffRef(transpositions[i], j));
442 for (Index i = first; i < first + count; ++i) matrix.row(i).swap(matrix.row(transpositions[i]));
456 static Index unblocked_lu(MatrixTypeRef& lu, PivIndex* row_transpositions, PivIndex& nb_transpositions) {
457 using Scoring = scalar_score_coeff_op<Scalar>;
458 using Score =
typename Scoring::result_type;
460 Index first_zero_pivot;
461 if (unrolled_partial_lu<UnrolledAtCompileTime, SizeAtCompileTime>::run(lu, row_transpositions, nb_transpositions,
463 return first_zero_pivot;
465 const Index rows = lu.rows();
466 const Index cols = lu.cols();
467 const Index size = (std::min)(rows, cols);
470 const bool process_last_row_separately = UnBlockedAtCompileTime || rows == cols;
471 const Index endk = process_last_row_separately ? size - 1 : size;
472 nb_transpositions = 0;
473 Index first_zero_pivot = -1;
474 for (Index k = 0; k < endk; ++k) {
475 int rrows = internal::convert_index<int>(rows - k - 1);
476 int rcols = internal::convert_index<int>(cols - k - 1);
478 Index row_of_biggest_in_col;
479 Score biggest_in_corner = lu.col(k).tail(rows - k).unaryExpr(Scoring()).maxCoeff(&row_of_biggest_in_col);
480 row_of_biggest_in_col += k;
482 row_transpositions[k] = PivIndex(row_of_biggest_in_col);
484 if (!numext::is_exactly_zero(biggest_in_corner)) {
485 if (k != row_of_biggest_in_col) {
486 lu.row(k).swap(lu.row(row_of_biggest_in_col));
490 lu.col(k).tail(
fix<RRows>(rrows)) /= lu.coeff(k, k);
491 }
else if (first_zero_pivot == -1) {
494 first_zero_pivot = k;
498 if (rrows > 0 && rcols > 0)
504 if (process_last_row_separately) {
506 row_transpositions[k] = PivIndex(k);
507 if (numext::is_exactly_zero(Scoring()(lu(k, k))) && first_zero_pivot == -1) first_zero_pivot = k;
510 return first_zero_pivot;
528 static Index blocked_lu(Index rows, Index cols, Scalar* lu_data, Index luStride, PivIndex* row_transpositions,
529 PivIndex& nb_transpositions, Index maxBlockSize = 256) {
530 MatrixTypeRef lu = MatrixType::Map(lu_data, rows, cols, OuterStride<>(luStride));
532 const Index size = (std::min)(rows, cols);
535 EIGEN_IF_CONSTEXPR (UnBlockedAtCompileTime) {
536 return unblocked_lu(lu, row_transpositions, nb_transpositions);
537 }
else if (size <= UnBlockedBound) {
538 return unblocked_lu(lu, row_transpositions, nb_transpositions);
545 blockSize = size / 8;
546 blockSize = (blockSize / 16) * 16;
547 blockSize = (std::min)((std::max)(blockSize, Index(8)), maxBlockSize);
550 nb_transpositions = 0;
551 Index first_zero_pivot = -1;
552 for (Index k = 0; k < size; k += blockSize) {
553 Index bs = (std::min)(size - k, blockSize);
554 Index trows = rows - k - bs;
555 Index tsize = cols - k - bs;
561 BlockType A_2 = lu.block(0, k + bs, rows, tsize);
562 BlockType A11 = lu.block(k, k, bs, bs);
563 BlockType A12 = lu.block(k, k + bs, bs, tsize);
564 BlockType A21 = lu.block(k + bs, k, trows, bs);
565 BlockType A22 = lu.block(k + bs, k + bs, trows, tsize);
567 PivIndex nb_transpositions_in_panel;
570 Index ret = blocked_lu(trows + bs, bs, &lu.coeffRef(k, k), luStride, row_transpositions + k,
571 nb_transpositions_in_panel, 16);
572 if (ret >= 0 && first_zero_pivot == -1) first_zero_pivot = k + ret;
574 nb_transpositions += nb_transpositions_in_panel;
577 BlockType A_0 = lu.block(0, 0, rows, k);
578 for (Index i = k; i < k + bs; ++i) row_transpositions[i] += internal::convert_index<PivIndex>(k);
579 apply_row_transpositions(A_0, k, bs, row_transpositions);
584 apply_row_transpositions(A_2, k, bs, row_transpositions);
587 A11.template triangularView<UnitLower>().solveInPlace(A12);
589 A22.noalias() -= A21 * A12;
592 return first_zero_pivot;
596template <
typename Scalar,
int StorageOrder,
typename PivIndex,
int SizeAtCompileTime = Dynamic>
597struct partial_lu_impl : generic_partial_lu_impl<Scalar, StorageOrder, PivIndex, SizeAtCompileTime> {};
601template <
typename MatrixType,
typename TranspositionType>
602void partial_lu_inplace(MatrixType& lu, TranspositionType& row_transpositions,
603 typename TranspositionType::StorageIndex& nb_transpositions) {
605 if (lu.rows() == 0 || lu.cols() == 0) {
606 nb_transpositions = 0;
609 eigen_assert(lu.cols() == row_transpositions.size());
610 eigen_assert(row_transpositions.size() < 2 ||
611 (&row_transpositions.coeffRef(1) - &row_transpositions.coeffRef(0)) == 1);
614 typename TranspositionType::StorageIndex,
615 internal::min_size_prefer_fixed(MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime)>::
616 blocked_lu(lu.rows(), lu.cols(), &lu.coeffRef(0, 0), lu.outerStride(), &row_transpositions.coeffRef(0),
623template <
typename Derived>
624typename traits<Derived>::Scalar partial_lu_determinant(
const Derived& m) {
625 using Scalar =
typename traits<Derived>::Scalar;
626 if (m.rows() == 0)
return Scalar(1);
627 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
629 using PlainObject =
typename plain_matrix_type<Derived>::type;
630 using TranspositionType =
631 Transpositions<PlainObject::RowsAtCompileTime, PlainObject::MaxRowsAtCompileTime, DefaultPermutationIndex>;
633 eigen_assert(m.rows() < NumTraits<DefaultPermutationIndex>::highest());
636 TranspositionType row_transpositions(lu.rows());
637 typename TranspositionType::StorageIndex nb_transpositions;
638 partial_lu_inplace(lu, row_transpositions, nb_transpositions);
640 return Scalar((nb_transpositions % 2) ? -1 : 1) * lu.diagonal().prod();
645template <
typename MatrixType,
typename PermutationIndex>
646void PartialPivLU<MatrixType, PermutationIndex>::compute() {
647 eigen_assert(m_lu.rows() < NumTraits<PermutationIndex>::highest());
650 m_l1_norm = m_lu.cwiseAbs().colwise().sum().maxCoeff();
652 m_l1_norm = RealScalar(0);
654 eigen_assert(m_lu.rows() == m_lu.cols() &&
"PartialPivLU is only for square (and moreover invertible) matrices");
655 const Index size = m_lu.rows();
657 m_rowsTranspositions.resize(size);
659 typename TranspositionType::StorageIndex nb_transpositions;
660 internal::partial_lu_inplace(m_lu, m_rowsTranspositions, nb_transpositions);
661 m_det_p = (nb_transpositions % 2) ? -1 : 1;
663 m_p = m_rowsTranspositions;
665 m_isInitialized =
true;
668template <
typename MatrixType,
typename PermutationIndex>
671 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
672 return Scalar(m_det_p) * m_lu.diagonal().prod();
675template <
typename MatrixType,
typename PermutationIndex>
678 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
679 return numext::abs(m_lu.diagonal().prod());
682template <
typename MatrixType,
typename PermutationIndex>
685 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
686 return m_lu.diagonal().cwiseAbs().array().log().sum();
689template <
typename MatrixType,
typename PermutationIndex>
690typename PartialPivLU<MatrixType, PermutationIndex>::Scalar
692 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
693 return Scalar(m_det_p) * m_lu.diagonal().array().sign().prod();
699template <
typename MatrixType,
typename PermutationIndex>
701 eigen_assert(m_isInitialized &&
"LU is not initialized.");
703 MatrixType res = m_lu.template triangularView<UnitLower>().toDenseMatrix() * m_lu.template triangularView<Upper>();
706 res = m_p.inverse() * res;
716template <
typename DstXprType,
typename MatrixType,
typename PermutationIndex>
719 internal::assign_op<typename DstXprType::Scalar, typename PartialPivLU<MatrixType, PermutationIndex>::Scalar>,
723 static void run(DstXprType& dst,
const SrcXprType& src,
724 const internal::assign_op<typename DstXprType::Scalar, typename LuType::Scalar>&) {
725 dst = src.nestedExpression().solve(MatrixType::Identity(src.rows(), src.cols()));
738template <
typename Derived>
739template <
typename PermutationIndex>
753template <
typename Derived>
754template <
typename PermutationIndex>
EvalReturnType eval() const
Definition DenseBase.h:385
Expression of the inverse of another expression.
Definition Inverse.h:44
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
LU decomposition of a matrix with partial pivoting, and related features.
Definition PartialPivLU.h:67
PartialPivLU(Index size)
Default Constructor with memory preallocation.
Definition PartialPivLU.h:323
RealScalar logAbsDeterminant() const
Definition PartialPivLU.h:684
Solve< PartialPivLU, Rhs > solve(const MatrixBase< Rhs > &b) const
MatrixType reconstructedMatrix() const
Definition PartialPivLU.h:700
Scalar signDeterminant() const
Definition PartialPivLU.h:691
const MatrixType & matrixLU() const
Definition PartialPivLU.h:143
ComputationInfo info() const
Reports whether the LU factorization was successful.
Definition PartialPivLU.h:89
RealScalar rcond() const
Definition PartialPivLU.h:180
PartialPivLU(const EigenBase< InputType > &matrix)
Definition PartialPivLU.h:328
PartialPivLU()
Default Constructor.
Definition PartialPivLU.h:319
const PermutationType & permutationP() const
Definition PartialPivLU.h:150
PartialPivLU(EigenBase< InputType > &matrix)
Definition PartialPivLU.h:340
Inverse< PartialPivLU > inverse() const
Definition PartialPivLU.h:192
Scalar determinant() const
Definition PartialPivLU.h:669
RealScalar absDeterminant() const
Definition PartialPivLU.h:677
InverseReturnType transpose() const
Definition PermutationMatrix.h:234
Permutation matrix.
Definition PermutationMatrix.h:346
Pseudo expression representing a solving operation.
Definition Solve.h:63
constexpr PartialPivLU< MatrixType_, PermutationIndex_ > & derived()
Represents a sequence of transpositions (row/column interchange)
Definition Transpositions.h:144
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50
constexpr Index size() const noexcept
Definition EigenBase.h:65
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44