11#ifndef EIGEN_PARTIALLU_H
12#define EIGEN_PARTIALLU_H
15#include "./InternalHeaderCheck.h"
20template <
typename MatrixType_,
typename PermutationIndex_>
21struct traits<PartialPivLU<MatrixType_, PermutationIndex_> > : traits<MatrixType_> {
22 typedef MatrixXpr XprKind;
23 typedef SolverStorage StorageKind;
24 typedef PermutationIndex_ StorageIndex;
25 typedef traits<MatrixType_> BaseTraits;
29template <
typename T,
typename Derived>
35template <
typename T,
typename Derived>
36struct enable_if_ref<Ref<T>, Derived> {
76template <
typename MatrixType_,
typename PermutationIndex_>
79 typedef MatrixType_ MatrixType;
85 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
86 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
88 using PermutationIndex = PermutationIndex_;
91 typedef typename MatrixType::PlainObject PlainObject;
100 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
127 template <
typename InputType>
137 template <
typename InputType>
140 template <
typename InputType>
154 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
161 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
165#ifdef EIGEN_PARSED_BY_DOXYGEN
183 template <
typename Rhs>
191 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
192 return internal::rcond_estimate_helper(m_l1_norm, *
this);
203 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
224 constexpr Index rows() const noexcept {
return m_lu.rows(); }
225 constexpr Index cols() const noexcept {
return m_lu.cols(); }
227#ifndef EIGEN_PARSED_BY_DOXYGEN
228 template <
typename RhsType,
typename DstType>
229 EIGEN_DEVICE_FUNC
void _solve_impl(
const RhsType& rhs, DstType& dst)
const {
241 m_lu.template triangularView<UnitLower>().solveInPlace(dst);
244 m_lu.template triangularView<Upper>().solveInPlace(dst);
247 template <
bool Conjugate,
typename RhsType,
typename DstType>
248 EIGEN_DEVICE_FUNC
void _solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const {
256 eigen_assert(rhs.rows() == m_lu.cols());
259 dst = m_lu.template triangularView<Upper>().transpose().template conjugateIf<Conjugate>().solve(rhs);
261 m_lu.template triangularView<UnitLower>().transpose().template conjugateIf<Conjugate>().solveInPlace(dst);
268 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
274 TranspositionType m_rowsTranspositions;
275 RealScalar m_l1_norm;
277 bool m_isInitialized;
280template <
typename MatrixType,
typename PermutationIndex>
282 : m_lu(), m_p(), m_rowsTranspositions(), m_l1_norm(0), m_det_p(0), m_isInitialized(false) {}
284template <
typename MatrixType,
typename PermutationIndex>
286 : m_lu(
size,
size), m_p(
size), m_rowsTranspositions(
size), m_l1_norm(0), m_det_p(0), m_isInitialized(false) {}
288template <
typename MatrixType,
typename PermutationIndex>
289template <
typename InputType>
291 : m_lu(matrix.rows(), matrix.cols()),
293 m_rowsTranspositions(matrix.rows()),
296 m_isInitialized(false) {
300template <
typename MatrixType,
typename PermutationIndex>
301template <
typename InputType>
305 m_rowsTranspositions(matrix.rows()),
308 m_isInitialized(false) {
315template <
typename Scalar,
int StorageOrder,
typename PivIndex,
int SizeAtCompileTime = Dynamic>
316struct partial_lu_impl {
317 static constexpr int UnBlockedBound = 16;
318 static constexpr bool UnBlockedAtCompileTime = SizeAtCompileTime !=
Dynamic && SizeAtCompileTime <= UnBlockedBound;
319 static constexpr int ActualSizeAtCompileTime = UnBlockedAtCompileTime ? SizeAtCompileTime :
Dynamic;
321 static constexpr int RRows = SizeAtCompileTime == 2 ? 1 :
Dynamic;
322 static constexpr int RCols = SizeAtCompileTime == 2 ? 1 :
Dynamic;
326 typedef typename MatrixType::RealScalar RealScalar;
338 static Index unblocked_lu(MatrixTypeRef& lu, PivIndex* row_transpositions, PivIndex& nb_transpositions) {
339 typedef scalar_score_coeff_op<Scalar> Scoring;
340 typedef typename Scoring::result_type Score;
341 const Index rows = lu.rows();
342 const Index cols = lu.cols();
343 const Index size = (std::min)(rows, cols);
346 const Index endk = UnBlockedAtCompileTime ? size - 1 : size;
347 nb_transpositions = 0;
348 Index first_zero_pivot = -1;
349 for (
Index k = 0; k < endk; ++k) {
350 int rrows = internal::convert_index<int>(rows - k - 1);
351 int rcols = internal::convert_index<int>(cols - k - 1);
353 Index row_of_biggest_in_col;
354 Score biggest_in_corner = lu.col(k).tail(rows - k).unaryExpr(Scoring()).maxCoeff(&row_of_biggest_in_col);
355 row_of_biggest_in_col += k;
357 row_transpositions[k] = PivIndex(row_of_biggest_in_col);
359 if (!numext::is_exactly_zero(biggest_in_corner)) {
360 if (k != row_of_biggest_in_col) {
361 lu.row(k).swap(lu.row(row_of_biggest_in_col));
365 lu.col(k).tail(
fix<RRows>(rrows)) /= lu.coeff(k, k);
366 }
else if (first_zero_pivot == -1) {
369 first_zero_pivot = k;
378 if (UnBlockedAtCompileTime) {
380 row_transpositions[k] = PivIndex(k);
381 if (numext::is_exactly_zero(Scoring()(lu(k, k))) && first_zero_pivot == -1) first_zero_pivot = k;
384 return first_zero_pivot;
402 static Index blocked_lu(
Index rows,
Index cols, Scalar* lu_data,
Index luStride, PivIndex* row_transpositions,
403 PivIndex& nb_transpositions,
Index maxBlockSize = 256) {
404 MatrixTypeRef lu = MatrixType::Map(lu_data, rows, cols, OuterStride<>(luStride));
406 const Index size = (std::min)(rows, cols);
409 if (UnBlockedAtCompileTime || size <= UnBlockedBound) {
410 return unblocked_lu(lu, row_transpositions, nb_transpositions);
417 blockSize = size / 8;
418 blockSize = (blockSize / 16) * 16;
419 blockSize = (std::min)((std::max)(blockSize,
Index(8)), maxBlockSize);
422 nb_transpositions = 0;
423 Index first_zero_pivot = -1;
424 for (
Index k = 0; k < size; k += blockSize) {
425 Index bs = (std::min)(size - k, blockSize);
426 Index trows = rows - k - bs;
427 Index tsize = size - k - bs;
433 BlockType A_0 = lu.block(0, 0, rows, k);
434 BlockType A_2 = lu.block(0, k + bs, rows, tsize);
435 BlockType A11 = lu.block(k, k, bs, bs);
436 BlockType A12 = lu.block(k, k + bs, bs, tsize);
437 BlockType A21 = lu.block(k + bs, k, trows, bs);
438 BlockType A22 = lu.block(k + bs, k + bs, trows, tsize);
440 PivIndex nb_transpositions_in_panel;
443 Index ret = blocked_lu(trows + bs, bs, &lu.coeffRef(k, k), luStride, row_transpositions + k,
444 nb_transpositions_in_panel, 16);
445 if (ret >= 0 && first_zero_pivot == -1) first_zero_pivot = k + ret;
447 nb_transpositions += nb_transpositions_in_panel;
449 for (
Index i = k; i < k + bs; ++i) {
450 Index piv = (row_transpositions[i] += internal::convert_index<PivIndex>(k));
451 A_0.row(i).swap(A_0.row(piv));
456 for (
Index i = k; i < k + bs; ++i) A_2.row(i).swap(A_2.row(row_transpositions[i]));
459 A11.template triangularView<UnitLower>().solveInPlace(A12);
461 A22.noalias() -= A21 * A12;
464 return first_zero_pivot;
470template <
typename MatrixType,
typename TranspositionType>
471void partial_lu_inplace(MatrixType& lu, TranspositionType& row_transpositions,
472 typename TranspositionType::StorageIndex& nb_transpositions) {
474 if (lu.rows() == 0 || lu.cols() == 0) {
475 nb_transpositions = 0;
478 eigen_assert(lu.cols() == row_transpositions.size());
479 eigen_assert(row_transpositions.size() < 2 ||
480 (&row_transpositions.coeffRef(1) - &row_transpositions.coeffRef(0)) == 1);
483 typename TranspositionType::StorageIndex,
484 internal::min_size_prefer_fixed(MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime)>::
485 blocked_lu(lu.rows(), lu.cols(), &lu.coeffRef(0, 0), lu.outerStride(), &row_transpositions.coeffRef(0),
491template <
typename MatrixType,
typename PermutationIndex>
492void PartialPivLU<MatrixType, PermutationIndex>::compute() {
493 eigen_assert(m_lu.rows() < NumTraits<PermutationIndex>::highest());
496 m_l1_norm = m_lu.cwiseAbs().colwise().sum().maxCoeff();
498 m_l1_norm = RealScalar(0);
500 eigen_assert(m_lu.rows() == m_lu.cols() &&
"PartialPivLU is only for square (and moreover invertible) matrices");
501 const Index size = m_lu.rows();
503 m_rowsTranspositions.resize(size);
505 typename TranspositionType::StorageIndex nb_transpositions;
506 internal::partial_lu_inplace(m_lu, m_rowsTranspositions, nb_transpositions);
507 m_det_p = (nb_transpositions % 2) ? -1 : 1;
509 m_p = m_rowsTranspositions;
511 m_isInitialized =
true;
514template <
typename MatrixType,
typename PermutationIndex>
517 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
518 return Scalar(m_det_p) * m_lu.diagonal().prod();
524template <
typename MatrixType,
typename PermutationIndex>
526 eigen_assert(m_isInitialized &&
"LU is not initialized.");
528 MatrixType res = m_lu.template triangularView<UnitLower>().toDenseMatrix() * m_lu.template triangularView<Upper>();
531 res = m_p.inverse() * res;
541template <
typename DstXprType,
typename MatrixType,
typename PermutationIndex>
544 internal::assign_op<typename DstXprType::Scalar, typename PartialPivLU<MatrixType, PermutationIndex>::Scalar>,
548 static void run(DstXprType& dst,
const SrcXprType& src,
549 const internal::assign_op<typename DstXprType::Scalar, typename LuType::Scalar>&) {
550 dst = src.nestedExpression().solve(MatrixType::Identity(src.rows(), src.cols()));
563template <
typename Derived>
564template <
typename PermutationIndex>
566MatrixBase<Derived>::partialPivLu()
const {
578template <
typename Derived>
579template <
typename PermutationIndex>
EvalReturnType eval() const
Definition DenseBase.h:386
Expression of the inverse of another expression.
Definition Inverse.h:43
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
LU decomposition of a matrix with partial pivoting, and related features.
Definition PartialPivLU.h:77
PartialPivLU(Index size)
Default Constructor with memory preallocation.
Definition PartialPivLU.h:285
MatrixType reconstructedMatrix() const
Definition PartialPivLU.h:525
const MatrixType & matrixLU() const
Definition PartialPivLU.h:153
ComputationInfo info() const
Reports whether the LU factorization was successful.
Definition PartialPivLU.h:99
RealScalar rcond() const
Definition PartialPivLU.h:190
PartialPivLU(const EigenBase< InputType > &matrix)
Definition PartialPivLU.h:290
PartialPivLU()
Default Constructor.
Definition PartialPivLU.h:281
const PermutationType & permutationP() const
Definition PartialPivLU.h:160
PartialPivLU(EigenBase< InputType > &matrix)
Definition PartialPivLU.h:302
const Solve< PartialPivLU, Rhs > solve(const MatrixBase< Rhs > &b) const
const Inverse< PartialPivLU > inverse() const
Definition PartialPivLU.h:202
Scalar determinant() const
Definition PartialPivLU.h:515
InverseReturnType transpose() const
Definition PermutationMatrix.h:180
Permutation matrix.
Definition PermutationMatrix.h:283
A matrix or vector expression mapping an existing expression.
Definition Ref.h:264
Pseudo expression representing a solving operation.
Definition Solve.h:62
constexpr PartialPivLU< MatrixType_, PermutationIndex_ > & derived()
SolverBase()
Definition SolverBase.h:105
Represents a sequence of transpositions (row/column interchange)
Definition Transpositions.h:141
ComputationInfo
Definition Constants.h:438
@ Success
Definition Constants.h:440
@ ColMajor
Definition Constants.h:318
@ RowMajor
Definition Constants.h:320
const unsigned int RowMajorBit
Definition Constants.h:70
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
const int Dynamic
Definition Constants.h:25
Definition EigenBase.h:33
constexpr Derived & derived()
Definition EigenBase.h:49
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:43
constexpr Index size() const noexcept
Definition EigenBase.h:64