11 #ifndef EIGEN_COLPIVOTINGHOUSEHOLDERQR_H
12 #define EIGEN_COLPIVOTINGHOUSEHOLDERQR_H
41 typedef _MatrixType MatrixType;
43 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
44 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
45 Options = MatrixType::Options,
46 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
47 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
49 typedef typename MatrixType::Scalar Scalar;
50 typedef typename MatrixType::RealScalar RealScalar;
51 typedef typename MatrixType::Index Index;
53 typedef typename internal::plain_diag_type<MatrixType>::type HCoeffsType;
55 typedef typename internal::plain_row_type<MatrixType, Index>::type IntRowVectorType;
56 typedef typename internal::plain_row_type<MatrixType>::type RowVectorType;
57 typedef typename internal::plain_row_type<MatrixType, RealScalar>::type RealRowVectorType;
62 typedef typename PermutationType::Index PermIndexType;
76 m_colsTranspositions(),
79 m_isInitialized(false) {}
89 m_hCoeffs((std::min)(rows,cols)),
90 m_colsPermutation(PermIndexType(cols)),
91 m_colsTranspositions(cols),
94 m_isInitialized(false),
95 m_usePrescribedThreshold(false) {}
98 : m_qr(matrix.rows(), matrix.cols()),
99 m_hCoeffs((std::min)(matrix.rows(),matrix.cols())),
100 m_colsPermutation(PermIndexType(matrix.cols())),
101 m_colsTranspositions(matrix.cols()),
102 m_temp(matrix.cols()),
103 m_colSqNorms(matrix.cols()),
104 m_isInitialized(false),
105 m_usePrescribedThreshold(false)
127 template<
typename Rhs>
128 inline const internal::solve_retval<ColPivHouseholderQR, Rhs>
131 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
132 return internal::solve_retval<ColPivHouseholderQR, Rhs>(*
this, b.derived());
141 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
147 const PermutationType& colsPermutation()
const
149 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
150 return m_colsPermutation;
190 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
193 for(Index i = 0; i < m_nonzero_pivots; ++i)
194 result += (
internal::abs(m_qr.coeff(i,i)) > premultiplied_threshold);
206 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
207 return cols() -
rank();
219 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
220 return rank() == cols();
232 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
233 return rank() == rows();
244 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
254 internal::solve_retval<ColPivHouseholderQR, typename MatrixType::IdentityReturnType>
257 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
258 return internal::solve_retval<ColPivHouseholderQR,typename MatrixType::IdentityReturnType>
259 (*
this, MatrixType::Identity(m_qr.rows(), m_qr.cols()));
262 inline Index rows()
const {
return m_qr.rows(); }
263 inline Index cols()
const {
return m_qr.cols(); }
264 const HCoeffsType& hCoeffs()
const {
return m_hCoeffs; }
285 m_usePrescribedThreshold =
true;
300 m_usePrescribedThreshold =
false;
310 eigen_assert(m_isInitialized || m_usePrescribedThreshold);
311 return m_usePrescribedThreshold ? m_prescribedThreshold
326 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
327 return m_nonzero_pivots;
337 HCoeffsType m_hCoeffs;
338 PermutationType m_colsPermutation;
339 IntRowVectorType m_colsTranspositions;
340 RowVectorType m_temp;
341 RealRowVectorType m_colSqNorms;
342 bool m_isInitialized, m_usePrescribedThreshold;
343 RealScalar m_prescribedThreshold, m_maxpivot;
344 Index m_nonzero_pivots;
348 template<
typename MatrixType>
351 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
352 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
356 template<
typename MatrixType>
359 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
360 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
361 return m_qr.diagonal().cwiseAbs().array().log().sum();
364 template<
typename MatrixType>
367 Index rows = matrix.rows();
368 Index cols = matrix.cols();
369 Index size = matrix.diagonalSize();
372 m_hCoeffs.resize(size);
376 m_colsTranspositions.resize(matrix.cols());
377 Index number_of_transpositions = 0;
379 m_colSqNorms.resize(cols);
380 for(Index k = 0; k < cols; ++k)
381 m_colSqNorms.coeffRef(k) = m_qr.col(k).squaredNorm();
385 m_nonzero_pivots = size;
386 m_maxpivot = RealScalar(0);
388 for(Index k = 0; k < size; ++k)
391 Index biggest_col_index;
392 RealScalar biggest_col_sq_norm = m_colSqNorms.tail(cols-k).maxCoeff(&biggest_col_index);
393 biggest_col_index += k;
399 biggest_col_sq_norm = m_qr.col(biggest_col_index).tail(rows-k).squaredNorm();
402 m_colSqNorms.coeffRef(biggest_col_index) = biggest_col_sq_norm;
409 if(biggest_col_sq_norm < threshold_helper * RealScalar(rows-k))
411 m_nonzero_pivots = k;
412 m_hCoeffs.tail(size-k).setZero();
413 m_qr.bottomRightCorner(rows-k,cols-k)
414 .template triangularView<StrictlyLower>()
420 m_colsTranspositions.coeffRef(k) = biggest_col_index;
421 if(k != biggest_col_index) {
422 m_qr.col(k).swap(m_qr.col(biggest_col_index));
423 std::swap(m_colSqNorms.coeffRef(k), m_colSqNorms.coeffRef(biggest_col_index));
424 ++number_of_transpositions;
429 m_qr.col(k).tail(rows-k).makeHouseholderInPlace(m_hCoeffs.coeffRef(k), beta);
432 m_qr.coeffRef(k,k) = beta;
438 m_qr.bottomRightCorner(rows-k, cols-k-1)
439 .applyHouseholderOnTheLeft(m_qr.col(k).tail(rows-k-1), m_hCoeffs.coeffRef(k), &m_temp.coeffRef(k+1));
442 m_colSqNorms.tail(cols-k-1) -= m_qr.row(k).tail(cols-k-1).cwiseAbs2();
445 m_colsPermutation.setIdentity(PermIndexType(cols));
446 for(PermIndexType k = 0; k < m_nonzero_pivots; ++k)
447 m_colsPermutation.applyTranspositionOnTheRight(PermIndexType(k), PermIndexType(m_colsTranspositions.coeff(k)));
449 m_det_pq = (number_of_transpositions%2) ? -1 : 1;
450 m_isInitialized =
true;
457 template<
typename _MatrixType,
typename Rhs>
458 struct solve_retval<ColPivHouseholderQR<_MatrixType>, Rhs>
459 : solve_retval_base<ColPivHouseholderQR<_MatrixType>, Rhs>
461 EIGEN_MAKE_SOLVE_HELPERS(ColPivHouseholderQR<_MatrixType>,Rhs)
463 template<typename Dest>
void evalTo(Dest& dst)
const
465 eigen_assert(rhs().rows() == dec().rows());
467 const int cols = dec().cols(),
468 nonzero_pivots = dec().nonzeroPivots();
470 if(nonzero_pivots == 0)
476 typename Rhs::PlainObject c(rhs());
480 .setLength(dec().nonzeroPivots())
485 .topLeftCorner(nonzero_pivots, nonzero_pivots)
486 .template triangularView<Upper>()
487 .solveInPlace(c.topRows(nonzero_pivots));
490 typename Rhs::PlainObject d(c);
491 d.topRows(nonzero_pivots)
493 .topLeftCorner(nonzero_pivots, nonzero_pivots)
494 .template triangularView<Upper>()
495 * c.topRows(nonzero_pivots);
497 for(Index i = 0; i < nonzero_pivots; ++i) dst.row(dec().colsPermutation().indices().coeff(i)) = c.row(i);
498 for(Index i = nonzero_pivots; i < cols; ++i) dst.row(dec().colsPermutation().indices().coeff(i)).setZero();
505 template<
typename MatrixType>
509 eigen_assert(m_isInitialized &&
"ColPivHouseholderQR is not initialized.");
517 template<
typename Derived>
526 #endif // EIGEN_COLPIVOTINGHOUSEHOLDERQR_H