44 template<
typename _MatrixType>
class HouseholderQR
48 typedef _MatrixType MatrixType;
50 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
51 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
52 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
53 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
55 typedef typename MatrixType::Scalar Scalar;
56 typedef typename MatrixType::RealScalar RealScalar;
58 typedef typename MatrixType::StorageIndex StorageIndex;
59 typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, (MatrixType::Flags&RowMajorBit) ? RowMajor : ColMajor, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType;
60 typedef typename internal::plain_diag_type<MatrixType>::type HCoeffsType;
61 typedef typename internal::plain_row_type<MatrixType>::type RowVectorType;
62 typedef HouseholderSequence<MatrixType,typename internal::remove_all<typename HCoeffsType::ConjugateReturnType>::type> HouseholderSequenceType;
70 HouseholderQR() : m_qr(), m_hCoeffs(), m_temp(), m_isInitialized(false) {}
80 m_hCoeffs((
std::min)(rows,cols)),
82 m_isInitialized(false) {}
96 template<
typename InputType>
98 : m_qr(matrix.rows(), matrix.cols()),
99 m_hCoeffs((
std::min)(matrix.rows(),matrix.cols())),
100 m_temp(matrix.cols()),
101 m_isInitialized(false)
114 template<
typename InputType>
116 : m_qr(matrix.derived()),
117 m_hCoeffs((
std::min)(matrix.rows(),matrix.cols())),
118 m_temp(matrix.cols()),
119 m_isInitialized(false)
141 template<
typename Rhs>
145 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
159 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
160 return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate());
168 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
172 template<
typename InputType>
208 inline Index rows()
const {
return m_qr.rows(); }
209 inline Index cols()
const {
return m_qr.cols(); }
215 const HCoeffsType&
hCoeffs()
const {
return m_hCoeffs; }
217 #ifndef EIGEN_PARSED_BY_DOXYGEN 218 template<
typename RhsType,
typename DstType>
220 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
225 static void check_template_parameters()
227 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
233 HCoeffsType m_hCoeffs;
234 RowVectorType m_temp;
235 bool m_isInitialized;
238 template<
typename MatrixType>
242 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
243 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
244 return abs(m_qr.diagonal().prod());
247 template<
typename MatrixType>
250 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
251 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
252 return m_qr.diagonal().cwiseAbs().array().log().sum();
258 template<
typename MatrixQR,
typename HCoeffs>
259 void householder_qr_inplace_unblocked(MatrixQR& mat, HCoeffs&
hCoeffs,
typename MatrixQR::Scalar* tempData = 0)
261 typedef typename MatrixQR::Scalar Scalar;
262 typedef typename MatrixQR::RealScalar RealScalar;
263 Index rows = mat.rows();
264 Index cols = mat.cols();
265 Index size = (std::min)(rows,cols);
267 eigen_assert(hCoeffs.size() == size);
274 tempData = tempVector.data();
277 for(
Index k = 0; k < size; ++k)
279 Index remainingRows = rows - k;
280 Index remainingCols = cols - k - 1;
283 mat.col(k).tail(remainingRows).makeHouseholderInPlace(hCoeffs.coeffRef(k), beta);
284 mat.coeffRef(k,k) = beta;
287 mat.bottomRightCorner(remainingRows, remainingCols)
288 .applyHouseholderOnTheLeft(mat.col(k).tail(remainingRows-1), hCoeffs.coeffRef(k), tempData+k+1);
293 template<
typename MatrixQR,
typename HCoeffs,
294 typename MatrixQRScalar =
typename MatrixQR::Scalar,
295 bool InnerStrideIsOne = (MatrixQR::InnerStrideAtCompileTime == 1 && HCoeffs::InnerStrideAtCompileTime == 1)>
296 struct householder_qr_inplace_blocked
299 static void run(MatrixQR& mat, HCoeffs& hCoeffs,
Index maxBlockSize=32,
300 typename MatrixQR::Scalar* tempData = 0)
302 typedef typename MatrixQR::Scalar Scalar;
305 Index rows = mat.rows();
306 Index cols = mat.cols();
307 Index size = (std::min)(rows, cols);
314 tempData = tempVector.data();
317 Index blockSize = (std::min)(maxBlockSize,size);
320 for (k = 0; k < size; k += blockSize)
322 Index bs = (std::min)(size-k,blockSize);
323 Index tcols = cols - k - bs;
324 Index brows = rows-k;
334 BlockType A11_21 = mat.block(k,k,brows,bs);
337 householder_qr_inplace_unblocked(A11_21, hCoeffsSegment, tempData);
341 BlockType A21_22 = mat.block(k,k+bs,brows,tcols);
342 apply_block_householder_on_the_left(A21_22,A11_21,hCoeffsSegment,
false);
350 #ifndef EIGEN_PARSED_BY_DOXYGEN 351 template<
typename _MatrixType>
352 template<
typename RhsType,
typename DstType>
355 const Index rank = (std::min)(rows(), cols());
356 eigen_assert(rhs.rows() == rows());
358 typename RhsType::PlainObject c(rhs);
363 m_hCoeffs.head(rank)).transpose()
366 m_qr.topLeftCorner(rank, rank)
367 .template triangularView<Upper>()
368 .solveInPlace(c.topRows(rank));
370 dst.topRows(rank) = c.topRows(rank);
371 dst.bottomRows(cols()-rank).setZero();
381 template<
typename MatrixType>
384 check_template_parameters();
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 internal::householder_qr_inplace_blocked<MatrixType, HCoeffsType>::run(m_qr, m_hCoeffs, 48, m_temp.data());
396 m_isInitialized =
true;
404 template<
typename Derived>
HouseholderQR(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition: HouseholderQR.h:78
const MatrixType & matrixQR() const
Definition: HouseholderQR.h:166
MatrixType::RealScalar absDeterminant() const
Definition: HouseholderQR.h:239
Namespace containing all symbols from the Eigen library.
Definition: Core:271
Derived & derived()
Definition: EigenBase.h:44
const Solve< HouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: HouseholderQR.h:143
HouseholderSequence< VectorsType, CoeffsType > householderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition: HouseholderSequence.h:451
void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:273
Definition: EigenBase.h:28
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
HouseholderQR(EigenBase< InputType > &matrix)
Constructs a QR factorization from a given matrix.
Definition: HouseholderQR.h:115
MatrixType::RealScalar logAbsDeterminant() const
Definition: HouseholderQR.h:248
const HouseholderQR< PlainObject > householderQr() const
Definition: HouseholderQR.h:406
HouseholderSequenceType householderQ() const
Definition: HouseholderQR.h:157
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
Definition: Eigen_Colamd.h:50
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
Householder QR decomposition of a matrix.
Definition: ForwardDeclarations.h:254
void computeInPlace()
Definition: HouseholderQR.h:382
Pseudo expression representing a solving operation.
Definition: Solve.h:62
HouseholderQR()
Default Constructor.
Definition: HouseholderQR.h:70
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
HouseholderQR(const EigenBase< InputType > &matrix)
Constructs a QR factorization from a given matrix.
Definition: HouseholderQR.h:97
const HCoeffsType & hCoeffs() const
Definition: HouseholderQR.h:215