21 typedef int StorageIndex;
64 typedef _MatrixType MatrixType;
70 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
71 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
77 typedef typename MatrixType::PlainObject PlainObject;
100 template<
typename InputType>
109 template<
typename InputType>
119 template<
typename InputType>
134 eigen_assert(m_isInitialized &&
"LU is not initialized.");
147 eigen_assert(m_isInitialized &&
"LU is not initialized.");
148 return m_nonzero_pivots;
162 eigen_assert(m_isInitialized &&
"LU is not initialized.");
172 eigen_assert(m_isInitialized &&
"LU is not initialized.");
192 eigen_assert(m_isInitialized &&
"LU is not initialized.");
216 image(
const MatrixType& originalMatrix)
const
218 eigen_assert(m_isInitialized &&
"LU is not initialized.");
222 #ifdef EIGEN_PARSED_BY_DOXYGEN
242 template<
typename Rhs>
252 eigen_assert(m_isInitialized &&
"PartialPivLU is not initialized.");
253 return internal::rcond_estimate_helper(m_l1_norm, *
this);
292 m_usePrescribedThreshold =
true;
307 m_usePrescribedThreshold =
false;
317 eigen_assert(m_isInitialized || m_usePrescribedThreshold);
318 return m_usePrescribedThreshold ? m_prescribedThreshold
333 eigen_assert(m_isInitialized &&
"LU is not initialized.");
334 RealScalar premultiplied_threshold = abs(m_maxpivot) *
threshold();
336 for(
Index i = 0; i < m_nonzero_pivots; ++i)
337 result += (abs(m_lu.coeff(i,i)) > premultiplied_threshold);
349 eigen_assert(m_isInitialized &&
"LU is not initialized.");
350 return cols() -
rank();
362 eigen_assert(m_isInitialized &&
"LU is not initialized.");
363 return rank() == cols();
375 eigen_assert(m_isInitialized &&
"LU is not initialized.");
376 return rank() == rows();
387 eigen_assert(m_isInitialized &&
"LU is not initialized.");
388 return isInjective() && (m_lu.rows() == m_lu.cols());
400 eigen_assert(m_isInitialized &&
"LU is not initialized.");
401 eigen_assert(m_lu.rows() == m_lu.cols() &&
"You can't take the inverse of a non-square matrix!");
407 EIGEN_DEVICE_FUNC
inline Index rows()
const {
return m_lu.rows(); }
408 EIGEN_DEVICE_FUNC
inline Index cols()
const {
return m_lu.cols(); }
410 #ifndef EIGEN_PARSED_BY_DOXYGEN
411 template<
typename RhsType,
typename DstType>
412 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
414 template<
bool Conjugate,
typename RhsType,
typename DstType>
415 void _solve_impl_transposed(
const RhsType &rhs, DstType &dst)
const;
420 static void check_template_parameters()
422 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
425 void computeInPlace();
428 PermutationPType m_p;
429 PermutationQType m_q;
430 IntColVectorType m_rowsTranspositions;
431 IntRowVectorType m_colsTranspositions;
432 Index m_nonzero_pivots;
433 RealScalar m_l1_norm;
434 RealScalar m_maxpivot, m_prescribedThreshold;
435 signed char m_det_pq;
436 bool m_isInitialized, m_usePrescribedThreshold;
439 template<
typename MatrixType>
441 : m_isInitialized(false), m_usePrescribedThreshold(false)
445 template<
typename MatrixType>
450 m_rowsTranspositions(rows),
451 m_colsTranspositions(cols),
452 m_isInitialized(false),
453 m_usePrescribedThreshold(false)
457 template<
typename MatrixType>
458 template<
typename InputType>
460 : m_lu(matrix.rows(), matrix.cols()),
463 m_rowsTranspositions(matrix.rows()),
464 m_colsTranspositions(matrix.cols()),
465 m_isInitialized(false),
466 m_usePrescribedThreshold(false)
471 template<
typename MatrixType>
472 template<
typename InputType>
474 : m_lu(matrix.derived()),
477 m_rowsTranspositions(matrix.rows()),
478 m_colsTranspositions(matrix.cols()),
479 m_isInitialized(false),
480 m_usePrescribedThreshold(false)
485 template<
typename MatrixType>
488 check_template_parameters();
493 m_l1_norm = m_lu.cwiseAbs().colwise().sum().maxCoeff();
495 const Index size = m_lu.diagonalSize();
496 const Index rows = m_lu.rows();
497 const Index cols = m_lu.cols();
501 m_rowsTranspositions.resize(m_lu.rows());
502 m_colsTranspositions.resize(m_lu.cols());
503 Index number_of_transpositions = 0;
505 m_nonzero_pivots = size;
506 m_maxpivot = RealScalar(0);
508 for(
Index k = 0; k < size; ++k)
513 Index row_of_biggest_in_corner, col_of_biggest_in_corner;
515 typedef typename Scoring::result_type Score;
516 Score biggest_in_corner;
517 biggest_in_corner = m_lu.bottomRightCorner(rows-k, cols-k)
518 .unaryExpr(Scoring())
519 .maxCoeff(&row_of_biggest_in_corner, &col_of_biggest_in_corner);
520 row_of_biggest_in_corner += k;
521 col_of_biggest_in_corner += k;
523 if(biggest_in_corner==Score(0))
527 m_nonzero_pivots = k;
528 for(
Index i = k; i < size; ++i)
530 m_rowsTranspositions.coeffRef(i) = internal::convert_index<StorageIndex>(i);
531 m_colsTranspositions.coeffRef(i) = internal::convert_index<StorageIndex>(i);
536 RealScalar abs_pivot = internal::abs_knowing_score<Scalar>()(m_lu(row_of_biggest_in_corner, col_of_biggest_in_corner), biggest_in_corner);
537 if(abs_pivot > m_maxpivot) m_maxpivot = abs_pivot;
542 m_rowsTranspositions.coeffRef(k) = internal::convert_index<StorageIndex>(row_of_biggest_in_corner);
543 m_colsTranspositions.coeffRef(k) = internal::convert_index<StorageIndex>(col_of_biggest_in_corner);
544 if(k != row_of_biggest_in_corner) {
545 m_lu.row(k).swap(m_lu.row(row_of_biggest_in_corner));
546 ++number_of_transpositions;
548 if(k != col_of_biggest_in_corner) {
549 m_lu.col(k).swap(m_lu.col(col_of_biggest_in_corner));
550 ++number_of_transpositions;
557 m_lu.col(k).tail(rows-k-1) /= m_lu.coeff(k,k);
559 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);
565 m_p.setIdentity(rows);
566 for(
Index k = size-1; k >= 0; --k)
567 m_p.applyTranspositionOnTheRight(k, m_rowsTranspositions.coeff(k));
569 m_q.setIdentity(cols);
570 for(
Index k = 0; k < size; ++k)
571 m_q.applyTranspositionOnTheRight(k, m_colsTranspositions.coeff(k));
573 m_det_pq = (number_of_transpositions%2) ? -1 : 1;
575 m_isInitialized =
true;
578 template<
typename MatrixType>
581 eigen_assert(m_isInitialized &&
"LU is not initialized.");
582 eigen_assert(m_lu.rows() == m_lu.cols() &&
"You can't take the determinant of a non-square matrix!");
583 return Scalar(m_det_pq) * Scalar(m_lu.diagonal().prod());
589 template<
typename MatrixType>
592 eigen_assert(m_isInitialized &&
"LU is not initialized.");
593 const Index smalldim = (std::min)(m_lu.rows(), m_lu.cols());
595 MatrixType res(m_lu.rows(),m_lu.cols());
597 res = m_lu.leftCols(smalldim)
598 .template triangularView<UnitLower>().toDenseMatrix()
599 * m_lu.topRows(smalldim)
600 .template triangularView<Upper>().toDenseMatrix();
603 res = m_p.inverse() * res;
606 res = res * m_q.inverse();
614 template<
typename _MatrixType>
620 enum { MaxSmallDimAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(
621 MatrixType::MaxColsAtCompileTime,
622 MatrixType::MaxRowsAtCompileTime)
625 template<
typename Dest>
void evalTo(Dest& dst)
const
628 const Index cols = dec().matrixLU().cols(), dimker = cols - rank();
655 RealScalar premultiplied_threshold = dec().maxPivot() * dec().threshold();
657 for(
Index i = 0; i < dec().nonzeroPivots(); ++i)
658 if(abs(dec().matrixLU().coeff(i,i)) > premultiplied_threshold)
660 eigen_internal_assert(p == rank());
667 MaxSmallDimAtCompileTime, MatrixType::MaxColsAtCompileTime>
668 m(dec().matrixLU().block(0, 0, rank(), cols));
669 for(
Index i = 0; i < rank(); ++i)
671 if(i) m.row(i).head(i).setZero();
672 m.row(i).tail(cols-i) = dec().matrixLU().row(pivots.
coeff(i)).tail(cols-i);
674 m.block(0, 0, rank(), rank());
675 m.block(0, 0, rank(), rank()).template triangularView<StrictlyLower>().setZero();
676 for(
Index i = 0; i < rank(); ++i)
677 m.col(i).swap(m.col(pivots.
coeff(i)));
682 m.topLeftCorner(rank(), rank())
683 .
template triangularView<Upper>().solveInPlace(
684 m.topRightCorner(rank(), dimker)
688 for(
Index i = rank()-1; i >= 0; --i)
689 m.col(i).swap(m.col(pivots.
coeff(i)));
692 for(
Index i = 0; i < rank(); ++i) dst.row(dec().permutationQ().indices().coeff(i)) = -m.row(i).tail(dimker);
693 for(
Index i = rank(); i < cols; ++i) dst.row(dec().permutationQ().indices().coeff(i)).setZero();
694 for(
Index k = 0; k < dimker; ++k) dst.coeffRef(dec().permutationQ().indices().coeff(rank()+k), k) = Scalar(1);
700 template<
typename _MatrixType>
706 enum { MaxSmallDimAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(
707 MatrixType::MaxColsAtCompileTime,
708 MatrixType::MaxRowsAtCompileTime)
711 template<
typename Dest>
void evalTo(Dest& dst)
const
724 RealScalar premultiplied_threshold = dec().maxPivot() * dec().threshold();
726 for(
Index i = 0; i < dec().nonzeroPivots(); ++i)
727 if(abs(dec().matrixLU().coeff(i,i)) > premultiplied_threshold)
729 eigen_internal_assert(p == rank());
731 for(
Index i = 0; i < rank(); ++i)
732 dst.col(i) = originalMatrix().col(dec().permutationQ().indices().coeff(pivots.
coeff(i)));
740 #ifndef EIGEN_PARSED_BY_DOXYGEN
741 template<
typename _MatrixType>
742 template<
typename RhsType,
typename DstType>
753 const Index rows = this->rows(),
755 nonzero_pivots = this->rank();
756 const Index smalldim = (std::min)(rows, cols);
758 if(nonzero_pivots == 0)
764 typename RhsType::PlainObject c(rhs.rows(), rhs.cols());
767 c = permutationP() * rhs;
770 m_lu.topLeftCorner(smalldim,smalldim)
771 .template triangularView<UnitLower>()
772 .solveInPlace(c.topRows(smalldim));
774 c.bottomRows(rows-cols) -= m_lu.bottomRows(rows-cols) * c.topRows(cols);
777 m_lu.topLeftCorner(nonzero_pivots, nonzero_pivots)
778 .template triangularView<Upper>()
779 .solveInPlace(c.topRows(nonzero_pivots));
782 for(
Index i = 0; i < nonzero_pivots; ++i)
783 dst.row(permutationQ().indices().coeff(i)) = c.row(i);
784 for(
Index i = nonzero_pivots; i < m_lu.cols(); ++i)
785 dst.row(permutationQ().indices().coeff(i)).setZero();
788 template<
typename _MatrixType>
789 template<
bool Conjugate,
typename RhsType,
typename DstType>
790 void FullPivLU<_MatrixType>::_solve_impl_transposed(
const RhsType &rhs, DstType &dst)
const
803 const Index rows = this->rows(), cols = this->cols(),
804 nonzero_pivots = this->rank();
805 const Index smalldim = (std::min)(rows, cols);
807 if(nonzero_pivots == 0)
813 typename RhsType::PlainObject c(rhs.rows(), rhs.cols());
816 c = permutationQ().inverse() * rhs;
819 m_lu.topLeftCorner(nonzero_pivots, nonzero_pivots)
820 .template triangularView<Upper>()
822 .template conjugateIf<Conjugate>()
823 .solveInPlace(c.topRows(nonzero_pivots));
826 m_lu.topLeftCorner(smalldim, smalldim)
827 .template triangularView<UnitLower>()
829 .template conjugateIf<Conjugate>()
830 .solveInPlace(c.topRows(smalldim));
833 PermutationPType invp = permutationP().inverse().eval();
834 for(
Index i = 0; i < smalldim; ++i)
835 dst.row(invp.indices().coeff(i)) = c.row(i);
836 for(
Index i = smalldim; i < rows; ++i)
837 dst.row(invp.indices().coeff(i)).setZero();
846 template<
typename DstXprType,
typename MatrixType>
853 dst = src.nestedExpression().solve(MatrixType::Identity(src.rows(), src.cols()));
866 template<
typename Derived>