48 typedef _MatrixType MatrixType;
50 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
51 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
52 Options = MatrixType::Options,
53 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
54 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
56 typedef typename MatrixType::Scalar Scalar;
58 typedef typename internal::traits<MatrixType>::StorageKind StorageKind;
59 typedef typename MatrixType::Index Index;
60 typedef typename internal::plain_row_type<MatrixType, Index>::type IntRowVectorType;
61 typedef typename internal::plain_col_type<MatrixType, Index>::type IntColVectorType;
105 eigen_assert(m_isInitialized &&
"LU is not initialized.");
118 eigen_assert(m_isInitialized &&
"LU is not initialized.");
119 return m_nonzero_pivots;
133 eigen_assert(m_isInitialized &&
"LU is not initialized.");
143 eigen_assert(m_isInitialized &&
"LU is not initialized.");
161 inline const internal::kernel_retval<FullPivLU>
kernel()
const
163 eigen_assert(m_isInitialized &&
"LU is not initialized.");
164 return internal::kernel_retval<FullPivLU>(*this);
186 inline const internal::image_retval<FullPivLU>
187 image(
const MatrixType& originalMatrix)
const
189 eigen_assert(m_isInitialized &&
"LU is not initialized.");
190 return internal::image_retval<FullPivLU>(*
this, originalMatrix);
212 template<
typename Rhs>
213 inline const internal::solve_retval<FullPivLU, Rhs>
216 eigen_assert(m_isInitialized &&
"LU is not initialized.");
217 return internal::solve_retval<FullPivLU, Rhs>(*
this, b.derived());
235 typename internal::traits<MatrixType>::Scalar
determinant()
const;
256 m_usePrescribedThreshold =
true;
271 m_usePrescribedThreshold =
false;
281 eigen_assert(m_isInitialized || m_usePrescribedThreshold);
282 return m_usePrescribedThreshold ? m_prescribedThreshold
296 eigen_assert(m_isInitialized &&
"LU is not initialized.");
299 for(Index i = 0; i < m_nonzero_pivots; ++i)
300 result += (
internal::abs(m_lu.coeff(i,i)) > premultiplied_threshold);
312 eigen_assert(m_isInitialized &&
"LU is not initialized.");
313 return cols() -
rank();
325 eigen_assert(m_isInitialized &&
"LU is not initialized.");
326 return rank() == cols();
338 eigen_assert(m_isInitialized &&
"LU is not initialized.");
339 return rank() == rows();
350 eigen_assert(m_isInitialized &&
"LU is not initialized.");
351 return isInjective() && (m_lu.rows() == m_lu.cols());
361 inline const internal::solve_retval<FullPivLU,typename MatrixType::IdentityReturnType>
inverse()
const
363 eigen_assert(m_isInitialized &&
"LU is not initialized.");
364 eigen_assert(m_lu.rows() == m_lu.cols() &&
"You can't take the inverse of a non-square matrix!");
365 return internal::solve_retval<FullPivLU,typename MatrixType::IdentityReturnType>
366 (*
this, MatrixType::Identity(m_lu.rows(), m_lu.cols()));
371 inline Index rows()
const {
return m_lu.rows(); }
372 inline Index cols()
const {
return m_lu.cols(); }
376 PermutationPType m_p;
377 PermutationQType m_q;
378 IntColVectorType m_rowsTranspositions;
379 IntRowVectorType m_colsTranspositions;
380 Index m_det_pq, m_nonzero_pivots;
381 RealScalar m_maxpivot, m_prescribedThreshold;
382 bool m_isInitialized, m_usePrescribedThreshold;
385 template<
typename MatrixType>
387 : m_isInitialized(false), m_usePrescribedThreshold(false)
391 template<
typename MatrixType>
396 m_rowsTranspositions(rows),
397 m_colsTranspositions(cols),
398 m_isInitialized(false),
399 m_usePrescribedThreshold(false)
403 template<
typename MatrixType>
405 : m_lu(matrix.rows(), matrix.cols()),
408 m_rowsTranspositions(matrix.rows()),
409 m_colsTranspositions(matrix.cols()),
410 m_isInitialized(false),
411 m_usePrescribedThreshold(false)
416 template<
typename MatrixType>
419 m_isInitialized =
true;
422 const Index size = matrix.diagonalSize();
423 const Index rows = matrix.rows();
424 const Index cols = matrix.cols();
428 m_rowsTranspositions.resize(matrix.rows());
429 m_colsTranspositions.resize(matrix.cols());
430 Index number_of_transpositions = 0;
432 m_nonzero_pivots = size;
433 m_maxpivot = RealScalar(0);
435 for(Index k = 0; k < size; ++k)
440 Index row_of_biggest_in_corner, col_of_biggest_in_corner;
441 RealScalar biggest_in_corner;
442 biggest_in_corner = m_lu.bottomRightCorner(rows-k, cols-k)
444 .maxCoeff(&row_of_biggest_in_corner, &col_of_biggest_in_corner);
445 row_of_biggest_in_corner += k;
446 col_of_biggest_in_corner += k;
448 if(biggest_in_corner==RealScalar(0))
452 m_nonzero_pivots = k;
453 for(Index i = k; i < size; ++i)
455 m_rowsTranspositions.coeffRef(i) = i;
456 m_colsTranspositions.coeffRef(i) = i;
461 if(biggest_in_corner > m_maxpivot) m_maxpivot = biggest_in_corner;
466 m_rowsTranspositions.coeffRef(k) = row_of_biggest_in_corner;
467 m_colsTranspositions.coeffRef(k) = col_of_biggest_in_corner;
468 if(k != row_of_biggest_in_corner) {
469 m_lu.row(k).swap(m_lu.row(row_of_biggest_in_corner));
470 ++number_of_transpositions;
472 if(k != col_of_biggest_in_corner) {
473 m_lu.col(k).swap(m_lu.col(col_of_biggest_in_corner));
474 ++number_of_transpositions;
481 m_lu.col(k).tail(rows-k-1) /= m_lu.coeff(k,k);
483 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);
489 m_p.setIdentity(rows);
490 for(Index k = size-1; k >= 0; --k)
491 m_p.applyTranspositionOnTheRight(k, m_rowsTranspositions.coeff(k));
493 m_q.setIdentity(cols);
494 for(Index k = 0; k < size; ++k)
495 m_q.applyTranspositionOnTheRight(k, m_colsTranspositions.coeff(k));
497 m_det_pq = (number_of_transpositions%2) ? -1 : 1;
501 template<
typename MatrixType>
504 eigen_assert(m_isInitialized &&
"LU is not initialized.");
505 eigen_assert(m_lu.rows() == m_lu.cols() &&
"You can't take the determinant of a non-square matrix!");
506 return Scalar(m_det_pq) * Scalar(m_lu.diagonal().prod());
512 template<
typename MatrixType>
515 eigen_assert(m_isInitialized &&
"LU is not initialized.");
516 const Index smalldim = (std::min)(m_lu.rows(), m_lu.cols());
518 MatrixType res(m_lu.rows(),m_lu.cols());
520 res = m_lu.leftCols(smalldim)
521 .template triangularView<UnitLower>().toDenseMatrix()
522 * m_lu.topRows(smalldim)
523 .template triangularView<Upper>().toDenseMatrix();
526 res = m_p.inverse() * res;
529 res = res * m_q.inverse();
537 template<
typename _MatrixType>
538 struct kernel_retval<
FullPivLU<_MatrixType> >
539 : kernel_retval_base<FullPivLU<_MatrixType> >
543 enum { MaxSmallDimAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(
544 MatrixType::MaxColsAtCompileTime,
545 MatrixType::MaxRowsAtCompileTime)
548 template<
typename Dest>
void evalTo(Dest& dst)
const
550 const Index cols = dec().matrixLU().cols(), dimker = cols - rank();
576 Matrix<Index, Dynamic, 1, 0, MaxSmallDimAtCompileTime, 1> pivots(rank());
577 RealScalar premultiplied_threshold = dec().maxPivot() * dec().threshold();
579 for(Index i = 0; i < dec().nonzeroPivots(); ++i)
580 if(
internal::abs(dec().matrixLU().coeff(i,i)) > premultiplied_threshold)
581 pivots.coeffRef(p++) = i;
582 eigen_internal_assert(p == rank());
588 Matrix<
typename MatrixType::Scalar,
Dynamic,
Dynamic, MatrixType::Options,
589 MaxSmallDimAtCompileTime, MatrixType::MaxColsAtCompileTime>
590 m(dec().matrixLU().block(0, 0, rank(), cols));
591 for(Index i = 0; i < rank(); ++i)
593 if(i) m.row(i).head(i).setZero();
594 m.row(i).tail(cols-i) = dec().matrixLU().row(pivots.coeff(i)).tail(cols-i);
596 m.block(0, 0, rank(), rank());
597 m.block(0, 0, rank(), rank()).template triangularView<StrictlyLower>().setZero();
598 for(Index i = 0; i < rank(); ++i)
599 m.col(i).swap(m.col(pivots.coeff(i)));
604 m.topLeftCorner(rank(), rank())
605 .template triangularView<Upper>().solveInPlace(
606 m.topRightCorner(rank(), dimker)
610 for(Index i = rank()-1; i >= 0; --i)
611 m.col(i).swap(m.col(pivots.coeff(i)));
614 for(Index i = 0; i < rank(); ++i) dst.row(dec().permutationQ().indices().coeff(i)) = -m.row(i).tail(dimker);
615 for(Index i = rank(); i < cols; ++i) dst.row(dec().permutationQ().indices().coeff(i)).setZero();
616 for(Index k = 0; k < dimker; ++k) dst.coeffRef(dec().permutationQ().indices().coeff(rank()+k), k) = Scalar(1);
622 template<
typename _MatrixType>
623 struct image_retval<FullPivLU<_MatrixType> >
624 : image_retval_base<FullPivLU<_MatrixType> >
626 EIGEN_MAKE_IMAGE_HELPERS(FullPivLU<_MatrixType>)
628 enum { MaxSmallDimAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(
629 MatrixType::MaxColsAtCompileTime,
630 MatrixType::MaxRowsAtCompileTime)
633 template<
typename Dest>
void evalTo(Dest& dst)
const
644 Matrix<Index, Dynamic, 1, 0, MaxSmallDimAtCompileTime, 1> pivots(rank());
645 RealScalar premultiplied_threshold = dec().maxPivot() * dec().threshold();
647 for(Index i = 0; i < dec().nonzeroPivots(); ++i)
648 if(
internal::abs(dec().matrixLU().coeff(i,i)) > premultiplied_threshold)
649 pivots.coeffRef(p++) = i;
650 eigen_internal_assert(p == rank());
652 for(Index i = 0; i < rank(); ++i)
653 dst.col(i) = originalMatrix().col(dec().permutationQ().indices().coeff(pivots.coeff(i)));
659 template<
typename _MatrixType,
typename Rhs>
660 struct solve_retval<FullPivLU<_MatrixType>, Rhs>
661 : solve_retval_base<FullPivLU<_MatrixType>, Rhs>
663 EIGEN_MAKE_SOLVE_HELPERS(FullPivLU<_MatrixType>,Rhs)
665 template<typename Dest>
void evalTo(Dest& dst)
const
675 const Index rows = dec().rows(), cols = dec().cols(),
676 nonzero_pivots = dec().nonzeroPivots();
677 eigen_assert(rhs().rows() == rows);
678 const Index smalldim = (std::min)(rows, cols);
680 if(nonzero_pivots == 0)
686 typename Rhs::PlainObject c(rhs().rows(), rhs().cols());
689 c = dec().permutationP() * rhs();
693 .topLeftCorner(smalldim,smalldim)
694 .template triangularView<UnitLower>()
695 .solveInPlace(c.topRows(smalldim));
698 c.bottomRows(rows-cols)
699 -= dec().matrixLU().bottomRows(rows-cols)
705 .topLeftCorner(nonzero_pivots, nonzero_pivots)
706 .template triangularView<Upper>()
707 .solveInPlace(c.topRows(nonzero_pivots));
710 for(Index i = 0; i < nonzero_pivots; ++i)
711 dst.row(dec().permutationQ().indices().coeff(i)) = c.row(i);
712 for(Index i = nonzero_pivots; i < dec().matrixLU().cols(); ++i)
713 dst.row(dec().permutationQ().indices().coeff(i)).setZero();
727 template<
typename Derived>
728 inline const FullPivLU<typename MatrixBase<Derived>::PlainObject>