11#ifndef EIGEN_COMPLETEORTHOGONALDECOMPOSITION_H
12#define EIGEN_COMPLETEORTHOGONALDECOMPOSITION_H
15#include "./InternalHeaderCheck.h"
21template <
typename MatrixType_,
typename PermutationIndex_,
template <
typename,
typename>
class RankRevealingQR_>
22class CompleteOrthogonalDecompositionImpl;
24template <
typename MatrixType_,
typename PermutationIndex_,
template <
typename,
typename>
class RankRevealingQR_>
25struct traits<CompleteOrthogonalDecompositionImpl<MatrixType_, PermutationIndex_, RankRevealingQR_>>
26 : traits<MatrixType_> {
27 using XprKind = MatrixXpr;
28 using StorageKind = SolverStorage;
29 using PermutationIndex = PermutationIndex_;
33template <
typename MatrixType_,
typename PermutationIndex_>
34struct traits<CompleteOrthogonalDecomposition<MatrixType_, PermutationIndex_>> : traits<MatrixType_> {
35 using XprKind = MatrixXpr;
36 using StorageKind = SolverStorage;
37 using PermutationIndex = PermutationIndex_;
41template <
typename MatrixType_,
typename PermutationIndex_>
42struct traits<RandCompleteOrthogonalDecomposition<MatrixType_, PermutationIndex_>> : traits<MatrixType_> {
43 using XprKind = MatrixXpr;
44 using StorageKind = SolverStorage;
45 using PermutationIndex = PermutationIndex_;
66template <
typename MatrixType_,
typename PermutationIndex_,
template <
typename,
typename>
class RankRevealingQR_>
67class CompleteOrthogonalDecompositionImpl
68 :
public SolverBase<CompleteOrthogonalDecompositionImpl<MatrixType_, PermutationIndex_, RankRevealingQR_>> {
70 using MatrixType = MatrixType_;
73 template <
typename Derived>
74 friend struct internal::solve_assertion;
75 using PermutationIndex = PermutationIndex_;
76 EIGEN_GENERIC_PUBLIC_INTERFACE(CompleteOrthogonalDecompositionImpl)
78 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
79 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
81 using HCoeffsType =
typename internal::plain_diag_type<MatrixType>::type;
82 using PermutationType = PermutationMatrix<ColsAtCompileTime, MaxColsAtCompileTime, PermutationIndex>;
83 using IntRowVectorType =
typename internal::plain_row_type<MatrixType, Index>::type;
84 using RowVectorType =
typename internal::plain_row_type<MatrixType>::type;
85 using RealRowVectorType =
typename internal::plain_row_type<MatrixType, RealScalar>::type;
86 using HouseholderSequenceType =
87 HouseholderSequence<MatrixType, internal::remove_all_t<typename HCoeffsType::ConjugateReturnType>>;
88 using PlainObject =
typename MatrixType::PlainObject;
89 using RankRevealingQRType = RankRevealingQR_<MatrixType, PermutationIndex>;
92 CompleteOrthogonalDecompositionImpl() : m_cpqr(), m_zCoeffs(), m_temp() {}
94 CompleteOrthogonalDecompositionImpl(
Index rows,
Index cols)
95 : m_cpqr(rows, cols), m_zCoeffs((std::min)(rows, cols)), m_temp(cols) {}
97 template <
typename InputType>
98 explicit CompleteOrthogonalDecompositionImpl(
const EigenBase<InputType>& matrix)
99 : m_cpqr(matrix.rows(), matrix.cols()),
100 m_zCoeffs((std::min)(matrix.rows(), matrix.cols())),
101 m_temp(matrix.cols()) {
102 compute(matrix.derived());
105 template <
typename InputType>
106 explicit CompleteOrthogonalDecompositionImpl(EigenBase<InputType>& matrix)
107 : m_cpqr(matrix.
derived()), m_zCoeffs((std::min)(matrix.rows(), matrix.cols())), m_temp(matrix.cols()) {
111 HouseholderSequenceType householderQ()
const;
112 HouseholderSequenceType matrixQ()
const {
return m_cpqr.householderQ(); }
114 using MatrixZType = Matrix<Scalar, ColsAtCompileTime, ColsAtCompileTime, plain_object_options<MatrixType>::value,
115 MaxColsAtCompileTime, MaxColsAtCompileTime>;
117 MatrixZType matrixZ()
const {
118 MatrixZType Z = MatrixZType::Identity(m_cpqr.cols(), m_cpqr.cols());
119 if (rank() < cols()) applyZOnTheLeftInPlace<false>(Z);
123 const MatrixType& matrixQTZ()
const {
return m_cpqr.matrixQR(); }
124 const MatrixType& matrixT()
const {
return m_cpqr.matrixQR(); }
126 template <
typename InputType>
127 CompleteOrthogonalDecompositionImpl& compute(
const EigenBase<InputType>& matrix) {
128 m_cpqr.compute(matrix);
133 const PermutationType& colsPermutation()
const {
return m_cpqr.colsPermutation(); }
135 typename MatrixType::Scalar determinant()
const;
136 typename MatrixType::RealScalar absDeterminant()
const;
137 typename MatrixType::RealScalar logAbsDeterminant()
const;
138 typename MatrixType::Scalar signDeterminant()
const;
140 inline Index rank()
const {
141 eigen_assert(m_cpqr.m_isInitialized &&
"Decomposition is not initialized.");
144 inline Index dimensionOfKernel()
const {
return cols() - rank(); }
145 inline bool isInjective()
const {
return rank() == cols(); }
146 inline bool isSurjective()
const {
return rank() == rows(); }
147 inline bool isInvertible()
const {
return isInjective() && isSurjective(); }
149 inline Index rows()
const {
return m_cpqr.rows(); }
150 inline Index cols()
const {
return m_cpqr.cols(); }
152 inline const HCoeffsType& hCoeffs()
const {
return m_cpqr.hCoeffs(); }
153 const HCoeffsType& zCoeffs()
const {
return m_zCoeffs; }
155 CompleteOrthogonalDecompositionImpl& setThreshold(
const RealScalar& threshold) {
156 m_cpqr.setThreshold(threshold);
160 CompleteOrthogonalDecompositionImpl& setThreshold(Default_t) {
161 m_cpqr.setThreshold(Default);
170 RealScalar threshold()
const {
return m_cpqr.threshold(); }
172 inline Index nonzeroPivots()
const {
return m_cpqr.nonzeroPivots(); }
173 inline RealScalar maxPivot()
const {
return m_cpqr.maxPivot(); }
176 eigen_assert(m_cpqr.m_isInitialized &&
"Decomposition is not initialized.");
181 void check_initialized()
const {
182 eigen_assert(m_cpqr.m_isInitialized &&
"CompleteOrthogonalDecomposition is not initialized.");
185#ifndef EIGEN_PARSED_BY_DOXYGEN
186 template <
typename RhsType,
typename DstType>
187 void _solve_impl(
const RhsType& rhs, DstType& dst)
const;
189 template <
bool Conjugate,
typename RhsType,
typename DstType>
190 void _solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const;
194 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
196 template <
bool Transpose_,
typename Rhs>
197 void _check_solve_assertion(
const Rhs& b)
const {
198 eigen_assert(m_cpqr.m_isInitialized &&
"CompleteOrthogonalDecomposition is not initialized.");
199 EIGEN_INTERNAL_ASSERT_SOLVER_RHS_MSG(
200 *
this, b, Transpose_,
201 "CompleteOrthogonalDecomposition::solve(): invalid number of rows of the right hand side matrix b");
204 void computeInPlace();
206 template <
bool Conjugate,
typename Rhs>
207 void applyZOnTheLeftInPlace(Rhs& rhs)
const;
209 template <
typename Rhs>
210 void applyZAdjointOnTheLeftInPlace(Rhs& rhs)
const;
212 RankRevealingQRType m_cpqr;
213 HCoeffsType m_zCoeffs;
214 RowVectorType m_temp;
219template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
220typename MatrixType::Scalar
221CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::determinant()
const {
222 return m_cpqr.determinant();
225template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
226typename MatrixType::RealScalar
227CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::absDeterminant()
const {
228 return m_cpqr.absDeterminant();
231template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
232typename MatrixType::RealScalar
233CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::logAbsDeterminant()
const {
234 return m_cpqr.logAbsDeterminant();
237template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
238typename MatrixType::Scalar
239CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::signDeterminant()
const {
240 return m_cpqr.signDeterminant();
243template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
244void CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::computeInPlace() {
245 eigen_assert(m_cpqr.cols() <= NumTraits<PermutationIndex>::highest());
247 const Index rank = m_rank = m_cpqr.rank();
248 const Index cols = m_cpqr.cols();
249 const Index rows = m_cpqr.rows();
250 m_zCoeffs.resize((std::min)(rows, cols));
265 for (Index k = rank - 1; k >= 0; --k) {
270 m_cpqr.m_qr.col(k).head(k + 1).swap(m_cpqr.m_qr.col(rank - 1).head(k + 1));
276 m_cpqr.m_qr.row(k).tail(cols - rank + 1).makeHouseholderInPlace(m_zCoeffs(k), beta);
277 m_cpqr.m_qr(k, rank - 1) = beta;
280 m_cpqr.m_qr.topRightCorner(k, cols - rank + 1)
281 .applyHouseholderOnTheRight(m_cpqr.m_qr.row(k).tail(cols - rank).adjoint(), m_zCoeffs(k), &m_temp(0));
285 m_cpqr.m_qr.col(k).head(k + 1).swap(m_cpqr.m_qr.col(rank - 1).head(k + 1));
291template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
292template <
bool Conjugate,
typename Rhs>
293void CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::applyZOnTheLeftInPlace(
295 const Index cols = this->cols();
296 const Index nrhs = rhs.cols();
297 const Index rank = this->rank();
298 Matrix<typename Rhs::Scalar, Dynamic, 1> temp((std::max)(cols, nrhs));
299 for (Index k = rank - 1; k >= 0; --k) {
301 rhs.row(k).swap(rhs.row(rank - 1));
303 rhs.middleRows(rank - 1, cols - rank + 1)
304 .applyHouseholderOnTheLeft(matrixQTZ().row(k).tail(cols - rank).transpose().
template conjugateIf<!Conjugate>(),
305 zCoeffs().
template conjugateIf<Conjugate>()(k), &temp(0));
307 rhs.row(k).swap(rhs.row(rank - 1));
312template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
313template <
typename Rhs>
314void CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::applyZAdjointOnTheLeftInPlace(
316 const Index cols = this->cols();
317 const Index nrhs = rhs.cols();
318 const Index rank = this->rank();
319 Matrix<typename Rhs::Scalar, Dynamic, 1> temp((std::max)(cols, nrhs));
320 for (Index k = 0; k < rank; ++k) {
322 rhs.row(k).swap(rhs.row(rank - 1));
324 rhs.middleRows(rank - 1, cols - rank + 1)
325 .applyHouseholderOnTheLeft(matrixQTZ().row(k).tail(cols - rank).adjoint(), zCoeffs()(k), &temp(0));
327 rhs.row(k).swap(rhs.row(rank - 1));
332#ifndef EIGEN_PARSED_BY_DOXYGEN
333template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
334template <
typename RhsType,
typename DstType>
335void CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::_solve_impl(
336 const RhsType& rhs, DstType& dst)
const {
337 const Index rank = this->rank();
344 typename RhsType::PlainObject c(rhs);
345 c.applyOnTheLeft(matrixQ().setLength(rank).adjoint());
348 dst.topRows(rank) = matrixT().topLeftCorner(rank, rank).template triangularView<Upper>().solve(c.topRows(rank));
350 const Index cols = this->cols();
354 dst.bottomRows(cols - rank).setZero();
355 applyZAdjointOnTheLeftInPlace(dst);
359 dst = colsPermutation() * dst;
362template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
363template <
bool Conjugate,
typename RhsType,
typename DstType>
364void CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::_solve_impl_transposed(
365 const RhsType& rhs, DstType& dst)
const {
366 const Index rank = this->rank();
373 typename RhsType::PlainObject c(colsPermutation().transpose() * rhs);
376 applyZOnTheLeftInPlace<!Conjugate>(c);
380 .topLeftCorner(rank, rank)
381 .template triangularView<Upper>()
383 .template conjugateIf<Conjugate>()
384 .solveInPlace(c.topRows(rank));
386 dst.topRows(rank) = c.topRows(rank);
387 dst.bottomRows(rows() - rank).setZero();
389 dst.applyOnTheLeft(householderQ().setLength(rank).
template conjugateIf<!Conjugate>());
393template <
typename MatrixType,
typename PermutationIndex,
template <
typename,
typename>
class RankRevealingQR_>
394typename CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::HouseholderSequenceType
395CompleteOrthogonalDecompositionImpl<MatrixType, PermutationIndex, RankRevealingQR_>::householderQ()
const {
396 return m_cpqr.householderQ();
428template <
typename MatrixType_,
typename PermutationIndex_>
429class CompleteOrthogonalDecomposition
430 :
public internal::CompleteOrthogonalDecompositionImpl<MatrixType_, PermutationIndex_, ColPivHouseholderQR> {
433 internal::CompleteOrthogonalDecompositionImpl<MatrixType_, PermutationIndex_, Eigen::ColPivHouseholderQR>;
434 using typename Base::RealScalar;
436 CompleteOrthogonalDecomposition() : Base() {}
437 CompleteOrthogonalDecomposition(Index rows, Index cols) : Base(rows, cols) {}
439 template <
typename InputType>
442 template <
typename InputType>
446 template <
typename InputType>
448 Base::compute(matrix);
460 CompleteOrthogonalDecomposition&
setThreshold(
const RealScalar& threshold) {
461 Base::setThreshold(threshold);
467 Base::setThreshold(Default);
477 this->check_initialized();
505template <
typename MatrixType_,
typename PermutationIndex_>
506class RandCompleteOrthogonalDecomposition
507 :
public internal::CompleteOrthogonalDecompositionImpl<MatrixType_, PermutationIndex_, RandColPivHouseholderQR> {
510 internal::CompleteOrthogonalDecompositionImpl<MatrixType_, PermutationIndex_, Eigen::RandColPivHouseholderQR>;
511 using typename Base::RealScalar;
513 RandCompleteOrthogonalDecomposition() : Base() {}
514 RandCompleteOrthogonalDecomposition(Index rows, Index cols) : Base(rows, cols) {}
516 template <
typename InputType>
519 template <
typename InputType>
522 template <
typename InputType>
524 Base::compute(matrix);
536 RandCompleteOrthogonalDecomposition&
setThreshold(
const RealScalar& threshold) {
537 Base::setThreshold(threshold);
543 Base::setThreshold(Default);
558 RandCompleteOrthogonalDecomposition&
setSeed(uint64_t seed) {
564 this->check_initialized();
571template <
typename MatrixType,
typename PermutationIndex>
572struct traits<Inverse<CompleteOrthogonalDecomposition<MatrixType, PermutationIndex>>>
573 : traits<typename Transpose<typename MatrixType::PlainObject>::PlainObject> {
577template <
typename DstXprType,
typename MatrixType,
typename PermutationIndex>
578struct Assignment<DstXprType, Inverse<CompleteOrthogonalDecomposition<MatrixType, PermutationIndex>>,
579 internal::assign_op<typename DstXprType::Scalar,
580 typename CompleteOrthogonalDecomposition<MatrixType, PermutationIndex>::Scalar>,
582 using CodType = CompleteOrthogonalDecomposition<MatrixType, PermutationIndex>;
583 using SrcXprType = Inverse<CodType>;
584 static void run(DstXprType& dst,
const SrcXprType& src,
585 const internal::assign_op<typename DstXprType::Scalar, typename CodType::Scalar>&) {
586 using IdentityMatrixType = Matrix<
typename CodType::Scalar, CodType::RowsAtCompileTime, CodType::RowsAtCompileTime,
587 0, CodType::MaxRowsAtCompileTime, CodType::MaxRowsAtCompileTime>;
588 dst = src.nestedExpression().solve(IdentityMatrixType::Identity(src.cols(), src.cols()));
592template <
typename MatrixType,
typename PermutationIndex>
593struct traits<Inverse<RandCompleteOrthogonalDecomposition<MatrixType, PermutationIndex>>>
594 : traits<typename Transpose<typename MatrixType::PlainObject>::PlainObject> {
598template <
typename DstXprType,
typename MatrixType,
typename PermutationIndex>
599struct Assignment<DstXprType, Inverse<RandCompleteOrthogonalDecomposition<MatrixType, PermutationIndex>>,
600 internal::assign_op<typename DstXprType::Scalar, typename RandCompleteOrthogonalDecomposition<
601 MatrixType, PermutationIndex>::Scalar>,
603 using CodType = RandCompleteOrthogonalDecomposition<MatrixType, PermutationIndex>;
604 using SrcXprType = Inverse<CodType>;
605 static void run(DstXprType& dst,
const SrcXprType& src,
606 const internal::assign_op<typename DstXprType::Scalar, typename CodType::Scalar>&) {
607 using IdentityMatrixType = Matrix<
typename CodType::Scalar, CodType::RowsAtCompileTime, CodType::RowsAtCompileTime,
608 0, CodType::MaxRowsAtCompileTime, CodType::MaxRowsAtCompileTime>;
609 dst = src.nestedExpression().solve(IdentityMatrixType::Identity(src.cols(), src.cols()));
619template <
typename Derived>
620template <
typename PermutationIndex>
622MatrixBase<Derived>::completeOrthogonalDecomposition()
const {
630template <
typename Derived>
631template <
typename PermutationIndex>
633MatrixBase<Derived>::randCompleteOrthogonalDecomposition()
const {
Complete orthogonal decomposition (COD) of a matrix.
Definition CompleteOrthogonalDecomposition.h:430
CompleteOrthogonalDecomposition & setThreshold(const RealScalar &threshold)
Definition CompleteOrthogonalDecomposition.h:460
CompleteOrthogonalDecomposition & compute(const EigenBase< InputType > &matrix)
Computes the COD of matrix.
Definition CompleteOrthogonalDecomposition.h:447
Inverse< CompleteOrthogonalDecomposition > pseudoInverse() const
Definition CompleteOrthogonalDecomposition.h:476
CompleteOrthogonalDecomposition & setThreshold(Default_t)
Definition CompleteOrthogonalDecomposition.h:466
EvalReturnType eval() const
Definition DenseBase.h:389
Expression of the inverse of another expression.
Definition Inverse.h:44
Complete orthogonal decomposition (COD) of a matrix, backed by RandColPivHouseholderQR.
Definition CompleteOrthogonalDecomposition.h:507
RandCompleteOrthogonalDecomposition & setThreshold(Default_t)
Definition CompleteOrthogonalDecomposition.h:542
RandCompleteOrthogonalDecomposition & setBlockSize(Index b)
Sets the panel block size of the underlying randomized QR.
Definition CompleteOrthogonalDecomposition.h:550
RandCompleteOrthogonalDecomposition & setThreshold(const RealScalar &threshold)
Definition CompleteOrthogonalDecomposition.h:536
RandCompleteOrthogonalDecomposition & setSeed(uint64_t seed)
Fixes the RNG seed of the underlying randomized QR.
Definition CompleteOrthogonalDecomposition.h:558
constexpr CompleteOrthogonalDecompositionImpl< MatrixType_, PermutationIndex_, RankRevealingQR_ > & derived()
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50
Eigen::Index Index
Definition EigenBase.h:44