$treeview $search $mathjax
|
Eigen
3.2.3
$projectbrief
|
$projectbrief
|
$searchbox |
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com> 00006 // 00007 // This Source Code Form is subject to the terms of the Mozilla 00008 // Public License v. 2.0. If a copy of the MPL was not distributed 00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00010 00011 #ifndef EIGEN_COLPIVOTINGHOUSEHOLDERQR_H 00012 #define EIGEN_COLPIVOTINGHOUSEHOLDERQR_H 00013 00014 namespace Eigen { 00015 00037 template<typename _MatrixType> class ColPivHouseholderQR 00038 { 00039 public: 00040 00041 typedef _MatrixType MatrixType; 00042 enum { 00043 RowsAtCompileTime = MatrixType::RowsAtCompileTime, 00044 ColsAtCompileTime = MatrixType::ColsAtCompileTime, 00045 Options = MatrixType::Options, 00046 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime, 00047 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime 00048 }; 00049 typedef typename MatrixType::Scalar Scalar; 00050 typedef typename MatrixType::RealScalar RealScalar; 00051 typedef typename MatrixType::Index Index; 00052 typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, Options, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType; 00053 typedef typename internal::plain_diag_type<MatrixType>::type HCoeffsType; 00054 typedef PermutationMatrix<ColsAtCompileTime, MaxColsAtCompileTime> PermutationType; 00055 typedef typename internal::plain_row_type<MatrixType, Index>::type IntRowVectorType; 00056 typedef typename internal::plain_row_type<MatrixType>::type RowVectorType; 00057 typedef typename internal::plain_row_type<MatrixType, RealScalar>::type RealRowVectorType; 00058 typedef HouseholderSequence<MatrixType,typename internal::remove_all<typename HCoeffsType::ConjugateReturnType>::type> HouseholderSequenceType; 00059 00060 private: 00061 00062 typedef typename PermutationType::Index PermIndexType; 00063 00064 public: 00065 00072 ColPivHouseholderQR() 00073 : m_qr(), 00074 m_hCoeffs(), 00075 m_colsPermutation(), 00076 m_colsTranspositions(), 00077 m_temp(), 00078 m_colSqNorms(), 00079 m_isInitialized(false), 00080 m_usePrescribedThreshold(false) {} 00081 00088 ColPivHouseholderQR(Index rows, Index cols) 00089 : m_qr(rows, cols), 00090 m_hCoeffs((std::min)(rows,cols)), 00091 m_colsPermutation(PermIndexType(cols)), 00092 m_colsTranspositions(cols), 00093 m_temp(cols), 00094 m_colSqNorms(cols), 00095 m_isInitialized(false), 00096 m_usePrescribedThreshold(false) {} 00097 00110 ColPivHouseholderQR(const MatrixType& matrix) 00111 : m_qr(matrix.rows(), matrix.cols()), 00112 m_hCoeffs((std::min)(matrix.rows(),matrix.cols())), 00113 m_colsPermutation(PermIndexType(matrix.cols())), 00114 m_colsTranspositions(matrix.cols()), 00115 m_temp(matrix.cols()), 00116 m_colSqNorms(matrix.cols()), 00117 m_isInitialized(false), 00118 m_usePrescribedThreshold(false) 00119 { 00120 compute(matrix); 00121 } 00122 00140 template<typename Rhs> 00141 inline const internal::solve_retval<ColPivHouseholderQR, Rhs> 00142 solve(const MatrixBase<Rhs>& b) const 00143 { 00144 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00145 return internal::solve_retval<ColPivHouseholderQR, Rhs>(*this, b.derived()); 00146 } 00147 00148 HouseholderSequenceType householderQ(void) const; 00149 HouseholderSequenceType matrixQ(void) const 00150 { 00151 return householderQ(); 00152 } 00153 00156 const MatrixType& matrixQR() const 00157 { 00158 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00159 return m_qr; 00160 } 00161 00171 const MatrixType& matrixR() const 00172 { 00173 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00174 return m_qr; 00175 } 00176 00177 ColPivHouseholderQR& compute(const MatrixType& matrix); 00178 00180 const PermutationType& colsPermutation() const 00181 { 00182 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00183 return m_colsPermutation; 00184 } 00185 00199 typename MatrixType::RealScalar absDeterminant() const; 00200 00213 typename MatrixType::RealScalar logAbsDeterminant() const; 00214 00221 inline Index rank() const 00222 { 00223 using std::abs; 00224 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00225 RealScalar premultiplied_threshold = abs(m_maxpivot) * threshold(); 00226 Index result = 0; 00227 for(Index i = 0; i < m_nonzero_pivots; ++i) 00228 result += (abs(m_qr.coeff(i,i)) > premultiplied_threshold); 00229 return result; 00230 } 00231 00238 inline Index dimensionOfKernel() const 00239 { 00240 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00241 return cols() - rank(); 00242 } 00243 00251 inline bool isInjective() const 00252 { 00253 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00254 return rank() == cols(); 00255 } 00256 00264 inline bool isSurjective() const 00265 { 00266 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00267 return rank() == rows(); 00268 } 00269 00276 inline bool isInvertible() const 00277 { 00278 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00279 return isInjective() && isSurjective(); 00280 } 00281 00287 inline const 00288 internal::solve_retval<ColPivHouseholderQR, typename MatrixType::IdentityReturnType> 00289 inverse() const 00290 { 00291 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00292 return internal::solve_retval<ColPivHouseholderQR,typename MatrixType::IdentityReturnType> 00293 (*this, MatrixType::Identity(m_qr.rows(), m_qr.cols())); 00294 } 00295 00296 inline Index rows() const { return m_qr.rows(); } 00297 inline Index cols() const { return m_qr.cols(); } 00298 00303 const HCoeffsType& hCoeffs() const { return m_hCoeffs; } 00304 00322 ColPivHouseholderQR& setThreshold(const RealScalar& threshold) 00323 { 00324 m_usePrescribedThreshold = true; 00325 m_prescribedThreshold = threshold; 00326 return *this; 00327 } 00328 00337 ColPivHouseholderQR& setThreshold(Default_t) 00338 { 00339 m_usePrescribedThreshold = false; 00340 return *this; 00341 } 00342 00347 RealScalar threshold() const 00348 { 00349 eigen_assert(m_isInitialized || m_usePrescribedThreshold); 00350 return m_usePrescribedThreshold ? m_prescribedThreshold 00351 // this formula comes from experimenting (see "LU precision tuning" thread on the list) 00352 // and turns out to be identical to Higham's formula used already in LDLt. 00353 : NumTraits<Scalar>::epsilon() * RealScalar(m_qr.diagonalSize()); 00354 } 00355 00363 inline Index nonzeroPivots() const 00364 { 00365 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00366 return m_nonzero_pivots; 00367 } 00368 00372 RealScalar maxPivot() const { return m_maxpivot; } 00373 00380 ComputationInfo info() const 00381 { 00382 eigen_assert(m_isInitialized && "Decomposition is not initialized."); 00383 return Success; 00384 } 00385 00386 protected: 00387 MatrixType m_qr; 00388 HCoeffsType m_hCoeffs; 00389 PermutationType m_colsPermutation; 00390 IntRowVectorType m_colsTranspositions; 00391 RowVectorType m_temp; 00392 RealRowVectorType m_colSqNorms; 00393 bool m_isInitialized, m_usePrescribedThreshold; 00394 RealScalar m_prescribedThreshold, m_maxpivot; 00395 Index m_nonzero_pivots; 00396 Index m_det_pq; 00397 }; 00398 00399 template<typename MatrixType> 00400 typename MatrixType::RealScalar ColPivHouseholderQR<MatrixType>::absDeterminant() const 00401 { 00402 using std::abs; 00403 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00404 eigen_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!"); 00405 return abs(m_qr.diagonal().prod()); 00406 } 00407 00408 template<typename MatrixType> 00409 typename MatrixType::RealScalar ColPivHouseholderQR<MatrixType>::logAbsDeterminant() const 00410 { 00411 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00412 eigen_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!"); 00413 return m_qr.diagonal().cwiseAbs().array().log().sum(); 00414 } 00415 00422 template<typename MatrixType> 00423 ColPivHouseholderQR<MatrixType>& ColPivHouseholderQR<MatrixType>::compute(const MatrixType& matrix) 00424 { 00425 using std::abs; 00426 Index rows = matrix.rows(); 00427 Index cols = matrix.cols(); 00428 Index size = matrix.diagonalSize(); 00429 00430 // the column permutation is stored as int indices, so just to be sure: 00431 eigen_assert(cols<=NumTraits<int>::highest()); 00432 00433 m_qr = matrix; 00434 m_hCoeffs.resize(size); 00435 00436 m_temp.resize(cols); 00437 00438 m_colsTranspositions.resize(matrix.cols()); 00439 Index number_of_transpositions = 0; 00440 00441 m_colSqNorms.resize(cols); 00442 for(Index k = 0; k < cols; ++k) 00443 m_colSqNorms.coeffRef(k) = m_qr.col(k).squaredNorm(); 00444 00445 RealScalar threshold_helper = m_colSqNorms.maxCoeff() * numext::abs2(NumTraits<Scalar>::epsilon()) / RealScalar(rows); 00446 00447 m_nonzero_pivots = size; // the generic case is that in which all pivots are nonzero (invertible case) 00448 m_maxpivot = RealScalar(0); 00449 00450 for(Index k = 0; k < size; ++k) 00451 { 00452 // first, we look up in our table m_colSqNorms which column has the biggest squared norm 00453 Index biggest_col_index; 00454 RealScalar biggest_col_sq_norm = m_colSqNorms.tail(cols-k).maxCoeff(&biggest_col_index); 00455 biggest_col_index += k; 00456 00457 // since our table m_colSqNorms accumulates imprecision at every step, we must now recompute 00458 // the actual squared norm of the selected column. 00459 // Note that not doing so does result in solve() sometimes returning inf/nan values 00460 // when running the unit test with 1000 repetitions. 00461 biggest_col_sq_norm = m_qr.col(biggest_col_index).tail(rows-k).squaredNorm(); 00462 00463 // we store that back into our table: it can't hurt to correct our table. 00464 m_colSqNorms.coeffRef(biggest_col_index) = biggest_col_sq_norm; 00465 00466 // if the current biggest column is smaller than epsilon times the initial biggest column, 00467 // terminate to avoid generating nan/inf values. 00468 // Note that here, if we test instead for "biggest == 0", we get a failure every 1000 (or so) 00469 // repetitions of the unit test, with the result of solve() filled with large values of the order 00470 // of 1/(size*epsilon). 00471 if(biggest_col_sq_norm < threshold_helper * RealScalar(rows-k)) 00472 { 00473 m_nonzero_pivots = k; 00474 m_hCoeffs.tail(size-k).setZero(); 00475 m_qr.bottomRightCorner(rows-k,cols-k) 00476 .template triangularView<StrictlyLower>() 00477 .setZero(); 00478 break; 00479 } 00480 00481 // apply the transposition to the columns 00482 m_colsTranspositions.coeffRef(k) = biggest_col_index; 00483 if(k != biggest_col_index) { 00484 m_qr.col(k).swap(m_qr.col(biggest_col_index)); 00485 std::swap(m_colSqNorms.coeffRef(k), m_colSqNorms.coeffRef(biggest_col_index)); 00486 ++number_of_transpositions; 00487 } 00488 00489 // generate the householder vector, store it below the diagonal 00490 RealScalar beta; 00491 m_qr.col(k).tail(rows-k).makeHouseholderInPlace(m_hCoeffs.coeffRef(k), beta); 00492 00493 // apply the householder transformation to the diagonal coefficient 00494 m_qr.coeffRef(k,k) = beta; 00495 00496 // remember the maximum absolute value of diagonal coefficients 00497 if(abs(beta) > m_maxpivot) m_maxpivot = abs(beta); 00498 00499 // apply the householder transformation 00500 m_qr.bottomRightCorner(rows-k, cols-k-1) 00501 .applyHouseholderOnTheLeft(m_qr.col(k).tail(rows-k-1), m_hCoeffs.coeffRef(k), &m_temp.coeffRef(k+1)); 00502 00503 // update our table of squared norms of the columns 00504 m_colSqNorms.tail(cols-k-1) -= m_qr.row(k).tail(cols-k-1).cwiseAbs2(); 00505 } 00506 00507 m_colsPermutation.setIdentity(PermIndexType(cols)); 00508 for(PermIndexType k = 0; k < m_nonzero_pivots; ++k) 00509 m_colsPermutation.applyTranspositionOnTheRight(k, PermIndexType(m_colsTranspositions.coeff(k))); 00510 00511 m_det_pq = (number_of_transpositions%2) ? -1 : 1; 00512 m_isInitialized = true; 00513 00514 return *this; 00515 } 00516 00517 namespace internal { 00518 00519 template<typename _MatrixType, typename Rhs> 00520 struct solve_retval<ColPivHouseholderQR<_MatrixType>, Rhs> 00521 : solve_retval_base<ColPivHouseholderQR<_MatrixType>, Rhs> 00522 { 00523 EIGEN_MAKE_SOLVE_HELPERS(ColPivHouseholderQR<_MatrixType>,Rhs) 00524 00525 template<typename Dest> void evalTo(Dest& dst) const 00526 { 00527 eigen_assert(rhs().rows() == dec().rows()); 00528 00529 const Index cols = dec().cols(), 00530 nonzero_pivots = dec().nonzeroPivots(); 00531 00532 if(nonzero_pivots == 0) 00533 { 00534 dst.setZero(); 00535 return; 00536 } 00537 00538 typename Rhs::PlainObject c(rhs()); 00539 00540 // Note that the matrix Q = H_0^* H_1^*... so its inverse is Q^* = (H_0 H_1 ...)^T 00541 c.applyOnTheLeft(householderSequence(dec().matrixQR(), dec().hCoeffs()) 00542 .setLength(dec().nonzeroPivots()) 00543 .transpose() 00544 ); 00545 00546 dec().matrixR() 00547 .topLeftCorner(nonzero_pivots, nonzero_pivots) 00548 .template triangularView<Upper>() 00549 .solveInPlace(c.topRows(nonzero_pivots)); 00550 00551 for(Index i = 0; i < nonzero_pivots; ++i) dst.row(dec().colsPermutation().indices().coeff(i)) = c.row(i); 00552 for(Index i = nonzero_pivots; i < cols; ++i) dst.row(dec().colsPermutation().indices().coeff(i)).setZero(); 00553 } 00554 }; 00555 00556 } // end namespace internal 00557 00559 template<typename MatrixType> 00560 typename ColPivHouseholderQR<MatrixType>::HouseholderSequenceType ColPivHouseholderQR<MatrixType> 00561 ::householderQ() const 00562 { 00563 eigen_assert(m_isInitialized && "ColPivHouseholderQR is not initialized."); 00564 return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate()).setLength(m_nonzero_pivots); 00565 } 00566 00571 template<typename Derived> 00572 const ColPivHouseholderQR<typename MatrixBase<Derived>::PlainObject> 00573 MatrixBase<Derived>::colPivHouseholderQr() const 00574 { 00575 return ColPivHouseholderQR<PlainObject>(eval()); 00576 } 00577 00578 } // end namespace Eigen 00579 00580 #endif // EIGEN_COLPIVOTINGHOUSEHOLDERQR_H
1.6.1