71 typedef typename MatrixType::Scalar
Scalar;
73 typedef typename internal::traits<MatrixType>::StorageKind
StorageKind;
74 typedef typename MatrixType::Index
Index;
176 inline const internal::kernel_retval<FullPivLU>
kernel()
const
179 return internal::kernel_retval<FullPivLU>(*this);
201 inline const internal::image_retval<FullPivLU>
205 return internal::image_retval<FullPivLU>(*
this, originalMatrix);
227 template<
typename Rhs>
228 inline const internal::solve_retval<FullPivLU, Rhs>
232 return internal::solve_retval<FullPivLU, Rhs>(*
this, b.derived());
250 typename internal::traits<MatrixType>::Scalar
determinant()
const;
376 inline const internal::solve_retval<FullPivLU,typename MatrixType::IdentityReturnType>
inverse()
const
379 eigen_assert(
m_lu.rows() ==
m_lu.cols() &&
"You can't take the inverse of a non-square matrix!");
380 return internal::solve_retval<FullPivLU,typename MatrixType::IdentityReturnType>
381 (*
this, MatrixType::Identity(
m_lu.rows(),
m_lu.cols()));
400 template<
typename MatrixType>
402 : m_isInitialized(false), m_usePrescribedThreshold(false)
406 template<
typename MatrixType>
411 m_rowsTranspositions(rows),
412 m_colsTranspositions(cols),
413 m_isInitialized(false),
414 m_usePrescribedThreshold(false)
418 template<
typename MatrixType>
420 : m_lu(matrix.rows(), matrix.cols()),
423 m_rowsTranspositions(matrix.rows()),
424 m_colsTranspositions(matrix.cols()),
425 m_isInitialized(false),
426 m_usePrescribedThreshold(false)
431 template<
typename MatrixType>
434 m_isInitialized =
true;
437 const Index size = matrix.diagonalSize();
439 const Index cols = matrix.cols();
443 m_rowsTranspositions.resize(matrix.rows());
444 m_colsTranspositions.resize(matrix.cols());
445 Index number_of_transpositions = 0;
447 m_nonzero_pivots = size;
450 for(
Index k = 0; k < size; ++k)
455 Index row_of_biggest_in_corner, col_of_biggest_in_corner;
457 biggest_in_corner = m_lu.bottomRightCorner(rows-k, cols-k)
459 .maxCoeff(&row_of_biggest_in_corner, &col_of_biggest_in_corner);
460 row_of_biggest_in_corner += k;
461 col_of_biggest_in_corner += k;
467 m_nonzero_pivots = k;
468 for(
Index i = k; i < size; ++i)
470 m_rowsTranspositions.coeffRef(i) = i;
471 m_colsTranspositions.coeffRef(i) = i;
476 if(biggest_in_corner > m_maxpivot) m_maxpivot = biggest_in_corner;
481 m_rowsTranspositions.coeffRef(k) = row_of_biggest_in_corner;
482 m_colsTranspositions.coeffRef(k) = col_of_biggest_in_corner;
483 if(k != row_of_biggest_in_corner) {
484 m_lu.row(k).swap(m_lu.row(row_of_biggest_in_corner));
485 ++number_of_transpositions;
487 if(k != col_of_biggest_in_corner) {
488 m_lu.col(k).swap(m_lu.col(col_of_biggest_in_corner));
489 ++number_of_transpositions;
496 m_lu.col(k).tail(rows-k-1) /= m_lu.coeff(k,k);
498 m_lu.block(k+1,k+1,rows-k-1,cols-k-1).noalias() -= m_lu.col(k).tail(rows-k-1) * m_lu.row(k).tail(cols-k-1);
504 m_p.setIdentity(rows);
505 for(
Index k = size-1; k >= 0; --k)
506 m_p.applyTranspositionOnTheRight(k, m_rowsTranspositions.coeff(k));
508 m_q.setIdentity(cols);
509 for(
Index k = 0; k < size; ++k)
510 m_q.applyTranspositionOnTheRight(k, m_colsTranspositions.coeff(k));
512 m_det_pq = (number_of_transpositions%2) ? -1 : 1;
516 template<
typename MatrixType>
519 eigen_assert(m_isInitialized &&
"LU is not initialized.");
520 eigen_assert(m_lu.rows() == m_lu.cols() &&
"You can't take the determinant of a non-square matrix!");
521 return Scalar(m_det_pq) *
Scalar(m_lu.diagonal().prod());
527 template<
typename MatrixType>
530 eigen_assert(m_isInitialized &&
"LU is not initialized.");
531 const Index smalldim = (std::min)(m_lu.rows(), m_lu.cols());
535 res = m_lu.leftCols(smalldim)
536 .template triangularView<UnitLower>().toDenseMatrix()
537 * m_lu.topRows(smalldim)
538 .template triangularView<Upper>().toDenseMatrix();
541 res = m_p.inverse() * res;
544 res = res * m_q.inverse();
552 template<
typename _MatrixType>
553 struct kernel_retval<
FullPivLU<_MatrixType> >
554 : kernel_retval_base<FullPivLU<_MatrixType> >
559 MatrixType::MaxColsAtCompileTime,
560 MatrixType::MaxRowsAtCompileTime)
563 template<
typename Dest>
void evalTo(Dest& dst)
const
565 const Index cols = dec().matrixLU().cols(), dimker = cols - rank();
591 Matrix<Index, Dynamic, 1, 0, MaxSmallDimAtCompileTime, 1> pivots(rank());
592 RealScalar premultiplied_threshold = dec().maxPivot() * dec().threshold();
594 for(Index i = 0; i < dec().nonzeroPivots(); ++i)
595 if(
abs(dec().matrixLU().coeff(i,i)) > premultiplied_threshold)
596 pivots.coeffRef(p++) = i;
603 Matrix<
typename MatrixType::Scalar,
Dynamic,
Dynamic, MatrixType::Options,
604 MaxSmallDimAtCompileTime, MatrixType::MaxColsAtCompileTime>
605 m(dec().matrixLU().
block(0, 0, rank(), cols));
606 for(Index i = 0; i < rank(); ++i)
608 if(i) m.row(i).head(i).setZero();
609 m.row(i).tail(cols-i) = dec().matrixLU().row(pivots.coeff(i)).tail(cols-i);
611 m.block(0, 0, rank(), rank());
612 m.block(0, 0, rank(), rank()).template triangularView<StrictlyLower>().setZero();
613 for(Index i = 0; i < rank(); ++i)
614 m.col(i).swap(m.col(pivots.coeff(i)));
619 m.topLeftCorner(rank(), rank())
620 .template triangularView<Upper>().solveInPlace(
621 m.topRightCorner(rank(), dimker)
625 for(Index i = rank()-1; i >= 0; --i)
626 m.col(i).swap(m.col(pivots.coeff(i)));
629 for(Index i = 0; i < rank(); ++i) dst.row(dec().permutationQ().indices().coeff(i)) = -m.row(i).tail(dimker);
630 for(Index i = rank(); i < cols; ++i) dst.row(dec().permutationQ().indices().coeff(i)).setZero();
631 for(Index k = 0; k < dimker; ++k) dst.coeffRef(dec().permutationQ().indices().coeff(rank()+k), k) = Scalar(1);
637 template<
typename _MatrixType>
638 struct image_retval<FullPivLU<_MatrixType> >
639 : image_retval_base<FullPivLU<_MatrixType> >
644 MatrixType::MaxColsAtCompileTime,
645 MatrixType::MaxRowsAtCompileTime)
648 template<
typename Dest>
void evalTo(Dest& dst)
const
659 Matrix<Index, Dynamic, 1, 0, MaxSmallDimAtCompileTime, 1> pivots(rank());
660 RealScalar premultiplied_threshold = dec().maxPivot() * dec().threshold();
662 for(Index i = 0; i < dec().nonzeroPivots(); ++i)
663 if(
abs(dec().matrixLU().coeff(i,i)) > premultiplied_threshold)
664 pivots.coeffRef(p++) = i;
667 for(Index i = 0; i < rank(); ++i)
668 dst.col(i) = originalMatrix().col(dec().permutationQ().indices().coeff(pivots.coeff(i)));
674 template<
typename _MatrixType,
typename Rhs>
675 struct solve_retval<FullPivLU<_MatrixType>, Rhs>
676 : solve_retval_base<FullPivLU<_MatrixType>, Rhs>
680 template<typename Dest>
void evalTo(Dest& dst)
const
690 const Index rows = dec().rows(), cols = dec().cols(),
691 nonzero_pivots = dec().nonzeroPivots();
693 const Index smalldim = (std::min)(rows, cols);
695 if(nonzero_pivots == 0)
701 typename Rhs::PlainObject c(rhs().rows(), rhs().cols());
704 c = dec().permutationP() * rhs();
708 .topLeftCorner(smalldim,smalldim)
709 .template triangularView<UnitLower>()
710 .solveInPlace(c.topRows(smalldim));
713 c.bottomRows(rows-cols)
714 -= dec().matrixLU().bottomRows(rows-cols)
720 .topLeftCorner(nonzero_pivots, nonzero_pivots)
721 .template triangularView<Upper>()
722 .solveInPlace(c.topRows(nonzero_pivots));
725 for(Index i = 0; i < nonzero_pivots; ++i)
726 dst.row(dec().permutationQ().indices().coeff(i)) = c.row(i);
727 for(Index i = nonzero_pivots; i < dec().matrixLU().cols(); ++i)
728 dst.row(dec().permutationQ().indices().coeff(i)).setZero();
742 template<
typename Derived>
743 inline const FullPivLU<typename MatrixBase<Derived>::PlainObject>