12#ifndef EIGEN_FULLPIVOTINGHOUSEHOLDERQR_H
13#define EIGEN_FULLPIVOTINGHOUSEHOLDERQR_H
16#include "./InternalHeaderCheck.h"
22template <
typename MatrixType_,
typename PermutationIndex_>
23struct traits<FullPivHouseholderQR<MatrixType_, PermutationIndex_> > : traits<MatrixType_> {
24 using XprKind = MatrixXpr;
25 using StorageKind = SolverStorage;
26 using PermutationIndex = PermutationIndex_;
30template <
typename MatrixType,
typename PermutationIndex>
33template <
typename MatrixType,
typename PermutationIndex>
35 using ReturnType =
typename MatrixType::PlainObject;
64template <
typename MatrixType_,
typename PermutationIndex_>
66 public RankRevealingBase<FullPivHouseholderQR<MatrixType_, PermutationIndex_> > {
68 using MatrixType = MatrixType_;
70 using RankRevealingBase_ = RankRevealingBase<FullPivHouseholderQR>;
82 using PermutationIndex = PermutationIndex_;
86 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
87 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
90 using HCoeffsType =
typename internal::plain_diag_type<MatrixType>::type;
91 using IntDiagSizeVectorType =
92 Matrix<PermutationIndex, 1, internal::min_size_prefer_dynamic(ColsAtCompileTime, RowsAtCompileTime),
RowMajor, 1,
93 internal::min_size_prefer_fixed(MaxColsAtCompileTime, MaxRowsAtCompileTime)>;
95 using RowVectorType =
typename internal::plain_row_type<MatrixType>::type;
96 using ColVectorType =
typename internal::plain_col_type<MatrixType>::type;
97 using PlainObject =
typename MatrixType::PlainObject;
106 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
118 m_rows_transpositions(),
119 m_cols_transpositions(),
120 m_cols_permutation(),
122 m_isInitialized(false) {}
132 m_hCoeffs((std::min)(rows, cols)),
133 m_rows_transpositions((std::min)(rows, cols)),
134 m_cols_transpositions((std::min)(rows, cols)),
135 m_cols_permutation(cols),
137 m_isInitialized(false) {}
151 template <
typename InputType>
153 : m_qr(matrix.rows(), matrix.cols()),
154 m_hCoeffs((std::min)(matrix.rows(), matrix.cols())),
155 m_rows_transpositions((std::min)(matrix.rows(), matrix.cols())),
156 m_cols_transpositions((std::min)(matrix.rows(), matrix.cols())),
157 m_cols_permutation(matrix.cols()),
158 m_temp(matrix.cols()),
159 m_isInitialized(false) {
170 template <
typename InputType>
173 m_hCoeffs((std::min)(matrix.rows(), matrix.cols())),
174 m_rows_transpositions((std::min)(matrix.rows(), matrix.cols())),
175 m_cols_transpositions((std::min)(matrix.rows(), matrix.cols())),
176 m_cols_permutation(matrix.cols()),
177 m_temp(matrix.cols()),
178 m_isInitialized(false) {
182#ifdef EIGEN_PARSED_BY_DOXYGEN
201 template <
typename Rhs>
212 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
216 template <
typename InputType>
221 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
222 return m_cols_permutation;
227 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
228 return m_rows_transpositions;
298 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
302 inline Index rows()
const {
return m_qr.rows(); }
303 inline Index cols()
const {
return m_qr.cols(); }
309 const HCoeffsType&
hCoeffs()
const {
return m_hCoeffs; }
311#ifndef EIGEN_PARSED_BY_DOXYGEN
312 template <
typename RhsType,
typename DstType>
313 void _solve_impl(
const RhsType& rhs, DstType& dst)
const;
315 template <
bool Conjugate,
typename RhsType,
typename DstType>
316 void _solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const;
320 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
322 void computeInPlace();
325 HCoeffsType m_hCoeffs;
326 IntDiagSizeVectorType m_rows_transpositions;
327 IntDiagSizeVectorType m_cols_transpositions;
328 PermutationType m_cols_permutation;
329 RowVectorType m_temp;
330 bool m_isInitialized;
331 RealScalar m_precision;
335template <
typename MatrixType,
typename PermutationIndex>
337 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
338 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_qr,
"You can't take the determinant of a non-square matrix!");
340 internal::householder_determinant<HCoeffsType, Scalar, NumTraits<Scalar>::IsComplex>::run(m_hCoeffs, detQ);
341 return isInjective() ? (detQ * Scalar(m_det_p)) * internal::scaled_prod(m_qr.diagonal()).value() : Scalar(0);
344template <
typename MatrixType,
typename PermutationIndex>
346 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
347 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_qr,
"You can't take the determinant of a non-square matrix!");
348 return isInjective() ? internal::scaled_prod(m_qr.diagonal()).abs() : RealScalar(0);
351template <
typename MatrixType,
typename PermutationIndex>
353 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
354 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_qr,
"You can't take the determinant of a non-square matrix!");
355 return isInjective() ? internal::log_abs_prod(m_qr.diagonal()) : -NumTraits<RealScalar>::infinity();
358template <
typename MatrixType,
typename PermutationIndex>
360 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
361 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_qr,
"You can't take the determinant of a non-square matrix!");
363 internal::householder_determinant<HCoeffsType, Scalar, NumTraits<Scalar>::IsComplex>::run(m_hCoeffs, detQ);
364 return isInjective() ? (detQ * Scalar(m_det_p)) * m_qr.diagonal().array().sign().prod() : Scalar(0);
373template <
typename MatrixType,
typename PermutationIndex>
374template <
typename InputType>
377 m_qr.resize(matrix.
rows(), matrix.
cols());
383template <
typename MatrixType,
typename PermutationIndex>
384void FullPivHouseholderQR<MatrixType, PermutationIndex>::computeInPlace() {
385 eigen_assert(m_qr.cols() <= NumTraits<PermutationIndex>::highest());
386 Index rows = m_qr.rows();
387 Index cols = m_qr.cols();
388 Index size = (std::min)(rows, cols);
390 m_hCoeffs.resize(size);
394 m_precision = NumTraits<Scalar>::epsilon() * RealScalar(size);
396 m_rows_transpositions.resize(size);
397 m_cols_transpositions.resize(size);
398 Index number_of_transpositions = 0;
400 RealScalar biggest(0);
402 this->m_nonzero_pivots = size;
403 this->m_maxpivot = RealScalar(0);
405 for (Index k = 0; k < size; ++k) {
406 Index row_of_biggest_in_corner, col_of_biggest_in_corner;
407 using Scoring = internal::scalar_score_coeff_op<Scalar>;
408 using Score =
typename Scoring::result_type;
410 Score score = m_qr.bottomRightCorner(rows - k, cols - k)
411 .unaryExpr(Scoring())
412 .maxCoeff(&row_of_biggest_in_corner, &col_of_biggest_in_corner);
413 row_of_biggest_in_corner += k;
414 col_of_biggest_in_corner += k;
415 RealScalar biggest_in_corner =
416 internal::abs_knowing_score<Scalar>()(m_qr(row_of_biggest_in_corner, col_of_biggest_in_corner), score);
417 if (k == 0) biggest = biggest_in_corner;
420 if (internal::isMuchSmallerThan(biggest_in_corner, biggest, m_precision)) {
421 this->m_nonzero_pivots = k;
422 for (Index i = k; i < size; i++) {
423 m_rows_transpositions.coeffRef(i) = internal::convert_index<PermutationIndex>(i);
424 m_cols_transpositions.coeffRef(i) = internal::convert_index<PermutationIndex>(i);
425 m_hCoeffs.coeffRef(i) = Scalar(0);
430 m_rows_transpositions.coeffRef(k) = internal::convert_index<PermutationIndex>(row_of_biggest_in_corner);
431 m_cols_transpositions.coeffRef(k) = internal::convert_index<PermutationIndex>(col_of_biggest_in_corner);
432 if (k != row_of_biggest_in_corner) {
433 m_qr.row(k).tail(cols - k).swap(m_qr.row(row_of_biggest_in_corner).tail(cols - k));
434 ++number_of_transpositions;
436 if (k != col_of_biggest_in_corner) {
437 m_qr.col(k).swap(m_qr.col(col_of_biggest_in_corner));
438 ++number_of_transpositions;
442 m_qr.col(k).tail(rows - k).makeHouseholderInPlace(m_hCoeffs.coeffRef(k), beta);
443 m_qr.coeffRef(k, k) = beta;
446 if (numext::abs(beta) > this->m_maxpivot) this->m_maxpivot = numext::abs(beta);
448 m_qr.bottomRightCorner(rows - k, cols - k - 1)
449 .applyHouseholderOnTheLeft(m_qr.col(k).tail(rows - k - 1), m_hCoeffs.coeffRef(k), &m_temp.coeffRef(k + 1));
452 m_cols_permutation.setIdentity(cols);
453 for (Index k = 0; k < size; ++k) m_cols_permutation.applyTranspositionOnTheRight(k, m_cols_transpositions.coeff(k));
455 m_det_p = (number_of_transpositions % 2) ? -1 : 1;
456 m_isInitialized =
true;
459#ifndef EIGEN_PARSED_BY_DOXYGEN
460template <
typename MatrixType_,
typename PermutationIndex_>
461template <
typename RhsType,
typename DstType>
463 const Index l_rank = rank();
471 typename RhsType::PlainObject c(rhs);
474 for (Index k = 0; k < l_rank; ++k) {
475 Index remainingSize = rows() - k;
476 c.row(k).swap(c.row(m_rows_transpositions.coeff(k)));
477 c.bottomRightCorner(remainingSize, rhs.cols())
478 .applyHouseholderOnTheLeft(m_qr.col(k).tail(remainingSize - 1), m_hCoeffs.coeff(k), temp.data());
481 m_qr.topLeftCorner(l_rank, l_rank).template triangularView<Upper>().solveInPlace(c.topRows(l_rank));
483 for (Index i = 0; i < l_rank; ++i) dst.row(m_cols_permutation.indices().coeff(i)) = c.row(i);
484 for (Index i = l_rank; i < cols(); ++i) dst.row(m_cols_permutation.indices().coeff(i)).setZero();
487template <
typename MatrixType_,
typename PermutationIndex_>
488template <
bool Conjugate,
typename RhsType,
typename DstType>
490 DstType& dst)
const {
491 const Index l_rank = rank();
498 typename RhsType::PlainObject c(m_cols_permutation.transpose() * rhs);
500 m_qr.topLeftCorner(l_rank, l_rank)
501 .template triangularView<Upper>()
503 .template conjugateIf<Conjugate>()
504 .solveInPlace(c.topRows(l_rank));
506 dst.topRows(l_rank) = c.topRows(l_rank);
507 dst.bottomRows(rows() - l_rank).setZero();
510 const Index size = (std::min)(rows(), cols());
511 for (Index k = size - 1; k >= 0; --k) {
512 Index remainingSize = rows() - k;
514 dst.bottomRightCorner(remainingSize, dst.cols())
515 .applyHouseholderOnTheLeft(m_qr.col(k).tail(remainingSize - 1).template conjugateIf<!Conjugate>(),
516 m_hCoeffs.template conjugateIf<Conjugate>().coeff(k), temp.data());
518 dst.row(k).swap(dst.row(m_rows_transpositions.coeff(k)));
525template <
typename DstXprType,
typename MatrixType,
typename PermutationIndex>
526struct Assignment<DstXprType, Inverse<FullPivHouseholderQR<MatrixType, PermutationIndex> >,
527 internal::assign_op<typename DstXprType::Scalar,
528 typename FullPivHouseholderQR<MatrixType, PermutationIndex>::Scalar>,
530 using QrType = FullPivHouseholderQR<MatrixType, PermutationIndex>;
531 using SrcXprType = Inverse<QrType>;
532 static void run(DstXprType& dst,
const SrcXprType& src,
533 const internal::assign_op<typename DstXprType::Scalar, typename QrType::Scalar>&) {
534 dst = src.nestedExpression().solve(MatrixType::Identity(src.rows(), src.cols()));
544template <
typename MatrixType,
typename PermutationIndex>
545struct FullPivHouseholderQRMatrixQReturnType
546 :
public ReturnByValue<FullPivHouseholderQRMatrixQReturnType<MatrixType, PermutationIndex> > {
548 using IntDiagSizeVectorType =
typename FullPivHouseholderQR<MatrixType, PermutationIndex>::IntDiagSizeVectorType;
549 using HCoeffsType =
typename internal::plain_diag_type<MatrixType>::type;
550 using WorkVectorType =
Matrix<
typename MatrixType::Scalar, 1, MatrixType::RowsAtCompileTime,
RowMajor, 1,
551 MatrixType::MaxRowsAtCompileTime>;
553 FullPivHouseholderQRMatrixQReturnType(
const MatrixType& qr,
const HCoeffsType& hCoeffs,
554 const IntDiagSizeVectorType& rowsTranspositions)
555 : m_qr(qr), m_hCoeffs(hCoeffs), m_rowsTranspositions(rowsTranspositions) {}
557 template <
typename ResultType>
558 void evalTo(ResultType& result)
const {
559 const Index rows = m_qr.rows();
560 WorkVectorType workspace(rows);
561 evalTo(result, workspace);
564 template <
typename ResultType>
565 void evalTo(ResultType& result, WorkVectorType& workspace)
const {
569 const Index rows = m_qr.rows();
570 const Index cols = m_qr.cols();
571 const Index size = (std::min)(rows, cols);
573 result.setIdentity(rows, rows);
574 for (Index k = size - 1; k >= 0; k--) {
575 result.block(k, k, rows - k, rows - k)
576 .applyHouseholderOnTheLeft(m_qr.col(k).tail(rows - k - 1), numext::conj(m_hCoeffs.coeff(k)),
578 result.row(k).swap(result.row(m_rowsTranspositions.coeff(k)));
582 Index rows()
const {
return m_qr.rows(); }
583 Index cols()
const {
return m_qr.rows(); }
586 typename MatrixType::Nested m_qr;
587 typename HCoeffsType::Nested m_hCoeffs;
588 typename IntDiagSizeVectorType::Nested m_rowsTranspositions;
593template <
typename MatrixType,
typename PermutationIndex>
594inline typename FullPivHouseholderQR<MatrixType, PermutationIndex>::MatrixQReturnType
596 eigen_assert(m_isInitialized &&
"FullPivHouseholderQR is not initialized.");
597 return MatrixQReturnType(m_qr, m_hCoeffs, m_rows_transpositions);
604template <
typename Derived>
605template <
typename PermutationIndex>
607MatrixBase<Derived>::fullPivHouseholderQr()
const {
EvalReturnType eval() const
Definition DenseBase.h:389
Householder rank-revealing QR decomposition of a matrix with full pivoting.
Definition FullPivHouseholderQR.h:66
const MatrixType & matrixQR() const
Definition FullPivHouseholderQR.h:211
ComputationInfo info() const
Reports whether the QR factorization was successful.
Definition FullPivHouseholderQR.h:105
MatrixQReturnType matrixQ(void) const
Definition FullPivHouseholderQR.h:595
Inverse< FullPivHouseholderQR > inverse() const
Definition FullPivHouseholderQR.h:297
Solve< FullPivHouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
MatrixType::RealScalar logAbsDeterminant() const
Definition FullPivHouseholderQR.h:352
const PermutationType & colsPermutation() const
Definition FullPivHouseholderQR.h:220
const IntDiagSizeVectorType & rowsTranspositions() const
Definition FullPivHouseholderQR.h:226
MatrixType::Scalar determinant() const
Definition FullPivHouseholderQR.h:336
FullPivHouseholderQR()
Default Constructor.
Definition FullPivHouseholderQR.h:115
const HCoeffsType & hCoeffs() const
Definition FullPivHouseholderQR.h:309
MatrixType::RealScalar absDeterminant() const
Definition FullPivHouseholderQR.h:345
FullPivHouseholderQR(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition FullPivHouseholderQR.h:130
bool isInjective() const
Definition RankRevealingBase.h:151
FullPivHouseholderQR(const EigenBase< InputType > &matrix)
Constructs a QR factorization from a given matrix.
Definition FullPivHouseholderQR.h:152
RealScalar pivotCoeff(Index i) const
Definition FullPivHouseholderQR.h:290
FullPivHouseholderQR(EigenBase< InputType > &matrix)
Constructs a QR factorization from a given matrix.
Definition FullPivHouseholderQR.h:171
MatrixType::Scalar signDeterminant() const
Definition FullPivHouseholderQR.h:359
Expression of the inverse of another expression.
Definition Inverse.h:44
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition PlainObjectBase.h:205
Permutation matrix.
Definition PermutationMatrix.h:346
constexpr void resize(Index rows, Index cols)
Definition PlainObjectBase.h:282
Index dimensionOfKernel() const
Definition RankRevealingBase.h:139
bool isInjective() const
Definition RankRevealingBase.h:151
bool isSurjective() const
Definition RankRevealingBase.h:163
FullPivHouseholderQR & setThreshold(const RealScalar &threshold)
Definition RankRevealingBase.h:87
Index nonzeroPivots() const
Definition RankRevealingBase.h:186
RealScalar threshold() const
Definition RankRevealingBase.h:110
RealScalar maxPivot() const
Definition RankRevealingBase.h:194
Index rank() const
Definition RankRevealingBase.h:125
bool isInvertible() const
Definition RankRevealingBase.h:174
Pseudo expression representing a solving operation.
Definition Solve.h:63
constexpr Derived & derived()
Definition EigenBase.h:50
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457
@ RowMajor
Definition Constants.h:321
Definition EigenBase.h:34
constexpr Index cols() const noexcept
Definition EigenBase.h:62
constexpr Derived & derived()
Definition EigenBase.h:50
constexpr Index rows() const noexcept
Definition EigenBase.h:60
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44
Expression type for return value of FullPivHouseholderQR::matrixQ()
Definition FullPivHouseholderQR.h:546