Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
PartialPivLU.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2009 Benoit Jacob <[email protected]>
5// Copyright (C) 2009 Gael Guennebaud <[email protected]>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_PARTIALLU_H
12#define EIGEN_PARTIALLU_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
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;
26 enum { Flags = BaseTraits::Flags & RowMajorBit, CoeffReadCost = Dynamic };
27};
28
29template <typename T, typename Derived>
30struct enable_if_ref;
31// {
32// typedef Derived type;
33// };
34
35template <typename T, typename Derived>
36struct enable_if_ref<Ref<T>, Derived> {
37 typedef Derived type;
38};
39
40} // end namespace internal
41
76template <typename MatrixType_, typename PermutationIndex_>
77class PartialPivLU : public SolverBase<PartialPivLU<MatrixType_, PermutationIndex_> > {
78 public:
79 typedef MatrixType_ MatrixType;
81 friend class SolverBase<PartialPivLU>;
82
83 EIGEN_GENERIC_PUBLIC_INTERFACE(PartialPivLU)
84 enum {
85 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
86 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
87 };
88 using PermutationIndex = PermutationIndex_;
91 typedef typename MatrixType::PlainObject PlainObject;
92
100
107 explicit PartialPivLU(Index size);
108
116 template <typename InputType>
117 explicit PartialPivLU(const EigenBase<InputType>& matrix);
118
126 template <typename InputType>
127 explicit PartialPivLU(EigenBase<InputType>& matrix);
128
129 template <typename InputType>
130 PartialPivLU& compute(const EigenBase<InputType>& matrix) {
131 m_lu = matrix.derived();
132 compute();
133 return *this;
134 }
135
142 inline const MatrixType& matrixLU() const {
143 eigen_assert(m_isInitialized && "PartialPivLU is not initialized.");
144 return m_lu;
145 }
146
149 inline const PermutationType& permutationP() const {
150 eigen_assert(m_isInitialized && "PartialPivLU is not initialized.");
151 return m_p;
152 }
153
154#ifdef EIGEN_PARSED_BY_DOXYGEN
172 template <typename Rhs>
173 inline const Solve<PartialPivLU, Rhs> solve(const MatrixBase<Rhs>& b) const;
174#endif
175
179 inline RealScalar rcond() const {
180 eigen_assert(m_isInitialized && "PartialPivLU is not initialized.");
181 return internal::rcond_estimate_helper(m_l1_norm, *this);
182 }
183
191 inline const Inverse<PartialPivLU> inverse() const {
192 eigen_assert(m_isInitialized && "PartialPivLU is not initialized.");
193 return Inverse<PartialPivLU>(*this);
194 }
195
209 Scalar determinant() const;
210
211 MatrixType reconstructedMatrix() const;
212
213 EIGEN_CONSTEXPR inline Index rows() const EIGEN_NOEXCEPT { return m_lu.rows(); }
214 EIGEN_CONSTEXPR inline Index cols() const EIGEN_NOEXCEPT { return m_lu.cols(); }
215
216#ifndef EIGEN_PARSED_BY_DOXYGEN
217 template <typename RhsType, typename DstType>
218 EIGEN_DEVICE_FUNC void _solve_impl(const RhsType& rhs, DstType& dst) const {
219 /* The decomposition PA = LU can be rewritten as A = P^{-1} L U.
220 * So we proceed as follows:
221 * Step 1: compute c = Pb.
222 * Step 2: replace c by the solution x to Lx = c.
223 * Step 3: replace c by the solution x to Ux = c.
224 */
225
226 // Step 1
227 dst = permutationP() * rhs;
228
229 // Step 2
230 m_lu.template triangularView<UnitLower>().solveInPlace(dst);
231
232 // Step 3
233 m_lu.template triangularView<Upper>().solveInPlace(dst);
234 }
235
236 template <bool Conjugate, typename RhsType, typename DstType>
237 EIGEN_DEVICE_FUNC void _solve_impl_transposed(const RhsType& rhs, DstType& dst) const {
238 /* The decomposition PA = LU can be rewritten as A^T = U^T L^T P.
239 * So we proceed as follows:
240 * Step 1: compute c as the solution to L^T c = b
241 * Step 2: replace c by the solution x to U^T x = c.
242 * Step 3: update c = P^-1 c.
243 */
244
245 eigen_assert(rhs.rows() == m_lu.cols());
246
247 // Step 1
248 dst = m_lu.template triangularView<Upper>().transpose().template conjugateIf<Conjugate>().solve(rhs);
249 // Step 2
250 m_lu.template triangularView<UnitLower>().transpose().template conjugateIf<Conjugate>().solveInPlace(dst);
251 // Step 3
252 dst = permutationP().transpose() * dst;
253 }
254#endif
255
256 protected:
257 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
258
259 void compute();
260
261 MatrixType m_lu;
262 PermutationType m_p;
263 TranspositionType m_rowsTranspositions;
264 RealScalar m_l1_norm;
265 signed char m_det_p;
266 bool m_isInitialized;
267};
268
269template <typename MatrixType, typename PermutationIndex>
271 : m_lu(), m_p(), m_rowsTranspositions(), m_l1_norm(0), m_det_p(0), m_isInitialized(false) {}
272
273template <typename MatrixType, typename PermutationIndex>
275 : m_lu(size, size), m_p(size), m_rowsTranspositions(size), m_l1_norm(0), m_det_p(0), m_isInitialized(false) {}
276
277template <typename MatrixType, typename PermutationIndex>
278template <typename InputType>
280 : m_lu(matrix.rows(), matrix.cols()),
281 m_p(matrix.rows()),
282 m_rowsTranspositions(matrix.rows()),
283 m_l1_norm(0),
284 m_det_p(0),
285 m_isInitialized(false) {
286 compute(matrix.derived());
287}
288
289template <typename MatrixType, typename PermutationIndex>
290template <typename InputType>
292 : m_lu(matrix.derived()),
293 m_p(matrix.rows()),
294 m_rowsTranspositions(matrix.rows()),
295 m_l1_norm(0),
296 m_det_p(0),
297 m_isInitialized(false) {
298 compute();
299}
300
301namespace internal {
302
304template <typename Scalar, int StorageOrder, typename PivIndex, int SizeAtCompileTime = Dynamic>
305struct partial_lu_impl {
306 static constexpr int UnBlockedBound = 16;
307 static constexpr bool UnBlockedAtCompileTime = SizeAtCompileTime != Dynamic && SizeAtCompileTime <= UnBlockedBound;
308 static constexpr int ActualSizeAtCompileTime = UnBlockedAtCompileTime ? SizeAtCompileTime : Dynamic;
309 // Remaining rows and columns at compile-time:
310 static constexpr int RRows = SizeAtCompileTime == 2 ? 1 : Dynamic;
311 static constexpr int RCols = SizeAtCompileTime == 2 ? 1 : Dynamic;
313 typedef Ref<MatrixType> MatrixTypeRef;
315 typedef typename MatrixType::RealScalar RealScalar;
316
327 static Index unblocked_lu(MatrixTypeRef& lu, PivIndex* row_transpositions, PivIndex& nb_transpositions) {
328 typedef scalar_score_coeff_op<Scalar> Scoring;
329 typedef typename Scoring::result_type Score;
330 const Index rows = lu.rows();
331 const Index cols = lu.cols();
332 const Index size = (std::min)(rows, cols);
333 // For small compile-time matrices it is worth processing the last row separately:
334 // speedup: +100% for 2x2, +10% for others.
335 const Index endk = UnBlockedAtCompileTime ? size - 1 : size;
336 nb_transpositions = 0;
337 Index first_zero_pivot = -1;
338 for (Index k = 0; k < endk; ++k) {
339 int rrows = internal::convert_index<int>(rows - k - 1);
340 int rcols = internal::convert_index<int>(cols - k - 1);
341
342 Index row_of_biggest_in_col;
343 Score biggest_in_corner = lu.col(k).tail(rows - k).unaryExpr(Scoring()).maxCoeff(&row_of_biggest_in_col);
344 row_of_biggest_in_col += k;
345
346 row_transpositions[k] = PivIndex(row_of_biggest_in_col);
347
348 if (!numext::is_exactly_zero(biggest_in_corner)) {
349 if (k != row_of_biggest_in_col) {
350 lu.row(k).swap(lu.row(row_of_biggest_in_col));
351 ++nb_transpositions;
352 }
353
354 lu.col(k).tail(fix<RRows>(rrows)) /= lu.coeff(k, k);
355 } else if (first_zero_pivot == -1) {
356 // the pivot is exactly zero, we record the index of the first pivot which is exactly 0,
357 // and continue the factorization such we still have A = PLU
358 first_zero_pivot = k;
359 }
360
361 if (k < rows - 1)
362 lu.bottomRightCorner(fix<RRows>(rrows), fix<RCols>(rcols)).noalias() -=
363 lu.col(k).tail(fix<RRows>(rrows)) * lu.row(k).tail(fix<RCols>(rcols));
364 }
365
366 // special handling of the last entry
367 if (UnBlockedAtCompileTime) {
368 Index k = endk;
369 row_transpositions[k] = PivIndex(k);
370 if (numext::is_exactly_zero(Scoring()(lu(k, k))) && first_zero_pivot == -1) first_zero_pivot = k;
371 }
372
373 return first_zero_pivot;
374 }
375
391 static Index blocked_lu(Index rows, Index cols, Scalar* lu_data, Index luStride, PivIndex* row_transpositions,
392 PivIndex& nb_transpositions, Index maxBlockSize = 256) {
393 MatrixTypeRef lu = MatrixType::Map(lu_data, rows, cols, OuterStride<>(luStride));
394
395 const Index size = (std::min)(rows, cols);
396
397 // if the matrix is too small, no blocking:
398 if (UnBlockedAtCompileTime || size <= UnBlockedBound) {
399 return unblocked_lu(lu, row_transpositions, nb_transpositions);
400 }
401
402 // automatically adjust the number of subdivisions to the size
403 // of the matrix so that there is enough sub blocks:
404 Index blockSize;
405 {
406 blockSize = size / 8;
407 blockSize = (blockSize / 16) * 16;
408 blockSize = (std::min)((std::max)(blockSize, Index(8)), maxBlockSize);
409 }
410
411 nb_transpositions = 0;
412 Index first_zero_pivot = -1;
413 for (Index k = 0; k < size; k += blockSize) {
414 Index bs = (std::min)(size - k, blockSize); // actual size of the block
415 Index trows = rows - k - bs; // trailing rows
416 Index tsize = size - k - bs; // trailing size
417
418 // partition the matrix:
419 // A00 | A01 | A02
420 // lu = A_0 | A_1 | A_2 = A10 | A11 | A12
421 // A20 | A21 | A22
422 BlockType A_0 = lu.block(0, 0, rows, k);
423 BlockType A_2 = lu.block(0, k + bs, rows, tsize);
424 BlockType A11 = lu.block(k, k, bs, bs);
425 BlockType A12 = lu.block(k, k + bs, bs, tsize);
426 BlockType A21 = lu.block(k + bs, k, trows, bs);
427 BlockType A22 = lu.block(k + bs, k + bs, trows, tsize);
428
429 PivIndex nb_transpositions_in_panel;
430 // recursively call the blocked LU algorithm on [A11^T A21^T]^T
431 // with a very small blocking size:
432 Index ret = blocked_lu(trows + bs, bs, &lu.coeffRef(k, k), luStride, row_transpositions + k,
433 nb_transpositions_in_panel, 16);
434 if (ret >= 0 && first_zero_pivot == -1) first_zero_pivot = k + ret;
435
436 nb_transpositions += nb_transpositions_in_panel;
437 // update permutations and apply them to A_0
438 for (Index i = k; i < k + bs; ++i) {
439 Index piv = (row_transpositions[i] += internal::convert_index<PivIndex>(k));
440 A_0.row(i).swap(A_0.row(piv));
441 }
442
443 if (trows) {
444 // apply permutations to A_2
445 for (Index i = k; i < k + bs; ++i) A_2.row(i).swap(A_2.row(row_transpositions[i]));
446
447 // A12 = A11^-1 A12
448 A11.template triangularView<UnitLower>().solveInPlace(A12);
449
450 A22.noalias() -= A21 * A12;
451 }
452 }
453 return first_zero_pivot;
454 }
455};
456
459template <typename MatrixType, typename TranspositionType>
460void partial_lu_inplace(MatrixType& lu, TranspositionType& row_transpositions,
461 typename TranspositionType::StorageIndex& nb_transpositions) {
462 // Special-case of zero matrix.
463 if (lu.rows() == 0 || lu.cols() == 0) {
464 nb_transpositions = 0;
465 return;
466 }
467 eigen_assert(lu.cols() == row_transpositions.size());
468 eigen_assert(row_transpositions.size() < 2 ||
469 (&row_transpositions.coeffRef(1) - &row_transpositions.coeffRef(0)) == 1);
470
471 partial_lu_impl<typename MatrixType::Scalar, MatrixType::Flags & RowMajorBit ? RowMajor : ColMajor,
472 typename TranspositionType::StorageIndex,
473 internal::min_size_prefer_fixed(MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime)>::
474 blocked_lu(lu.rows(), lu.cols(), &lu.coeffRef(0, 0), lu.outerStride(), &row_transpositions.coeffRef(0),
475 nb_transpositions);
476}
477
478} // end namespace internal
479
480template <typename MatrixType, typename PermutationIndex>
481void PartialPivLU<MatrixType, PermutationIndex>::compute() {
482 eigen_assert(m_lu.rows() < NumTraits<PermutationIndex>::highest());
483
484 if (m_lu.cols() > 0)
485 m_l1_norm = m_lu.cwiseAbs().colwise().sum().maxCoeff();
486 else
487 m_l1_norm = RealScalar(0);
488
489 eigen_assert(m_lu.rows() == m_lu.cols() && "PartialPivLU is only for square (and moreover invertible) matrices");
490 const Index size = m_lu.rows();
491
492 m_rowsTranspositions.resize(size);
493
494 typename TranspositionType::StorageIndex nb_transpositions;
495 internal::partial_lu_inplace(m_lu, m_rowsTranspositions, nb_transpositions);
496 m_det_p = (nb_transpositions % 2) ? -1 : 1;
497
498 m_p = m_rowsTranspositions;
499
500 m_isInitialized = true;
501}
502
503template <typename MatrixType, typename PermutationIndex>
504typename PartialPivLU<MatrixType, PermutationIndex>::Scalar PartialPivLU<MatrixType, PermutationIndex>::determinant()
505 const {
506 eigen_assert(m_isInitialized && "PartialPivLU is not initialized.");
507 return Scalar(m_det_p) * m_lu.diagonal().prod();
508}
509
513template <typename MatrixType, typename PermutationIndex>
515 eigen_assert(m_isInitialized && "LU is not initialized.");
516 // LU
517 MatrixType res = m_lu.template triangularView<UnitLower>().toDenseMatrix() * m_lu.template triangularView<Upper>();
518
519 // P^{-1}(LU)
520 res = m_p.inverse() * res;
521
522 return res;
523}
524
525/***** Implementation details *****************************************************/
526
527namespace internal {
528
529/***** Implementation of inverse() *****************************************************/
530template <typename DstXprType, typename MatrixType, typename PermutationIndex>
531struct Assignment<
532 DstXprType, Inverse<PartialPivLU<MatrixType, PermutationIndex> >,
533 internal::assign_op<typename DstXprType::Scalar, typename PartialPivLU<MatrixType, PermutationIndex>::Scalar>,
534 Dense2Dense> {
536 typedef Inverse<LuType> SrcXprType;
537 static void run(DstXprType& dst, const SrcXprType& src,
538 const internal::assign_op<typename DstXprType::Scalar, typename LuType::Scalar>&) {
539 dst = src.nestedExpression().solve(MatrixType::Identity(src.rows(), src.cols()));
540 }
541};
542} // end namespace internal
543
544/******** MatrixBase methods *******/
545
552template <typename Derived>
553template <typename PermutationIndex>
554inline const PartialPivLU<typename MatrixBase<Derived>::PlainObject, PermutationIndex>
558
567template <typename Derived>
568template <typename PermutationIndex>
572
573} // end namespace Eigen
574
575#endif // EIGEN_PARTIALLU_H
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
MatrixType reconstructedMatrix() const
Definition PartialPivLU.h:514
const MatrixType & matrixLU() const
Definition PartialPivLU.h:142
RealScalar rcond() const
Definition PartialPivLU.h:179
PartialPivLU()
Default Constructor.
Definition PartialPivLU.h:270
const PermutationType & permutationP() const
Definition PartialPivLU.h:149
const Solve< PartialPivLU, Rhs > solve(const MatrixBase< Rhs > &b) const
const Inverse< PartialPivLU > inverse() const
Definition PartialPivLU.h:191
Scalar determinant() const
Definition PartialPivLU.h:504
InverseReturnType transpose() const
Definition PermutationMatrix.h:177
Permutation matrix.
Definition PermutationMatrix.h:280
A matrix or vector expression mapping an existing expression.
Definition Ref.h:264
Pseudo expression representing a solving operation.
Definition Solve.h:62
A base class for matrix decomposition and solvers.
Definition SolverBase.h:72
Represents a sequence of transpositions (row/column interchange)
Definition Transpositions.h:141
@ 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 Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const int Dynamic
Definition Constants.h:25
Definition EigenBase.h:33
Derived & derived()
Definition EigenBase.h:49
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition EigenBase.h:64