79 using MatrixType = MatrixType_;
85 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
86 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
90 MaxRowsAtCompileTime, MaxRowsAtCompileTime>;
91 using HCoeffsType =
typename internal::plain_diag_type<MatrixType>::type;
92 using RowVectorType =
typename internal::plain_row_type<MatrixType>::type;
93 using HouseholderSequenceType =
103 eigen_assert(m_isInitialized &&
"HouseHolderQR is not initialized.");
122 : m_qr(rows, cols), m_hCoeffs((std::min)(rows, cols)), m_temp(cols), m_isInitialized(false) {}
136 template <
typename InputType>
138 : m_qr(matrix.rows(), matrix.cols()),
139 m_hCoeffs((std::min)(matrix.rows(), matrix.cols())),
140 m_temp(matrix.cols()),
141 m_isInitialized(false) {
152 template <
typename InputType>
155 m_hCoeffs((std::min)(matrix.rows(), matrix.cols())),
156 m_temp(matrix.cols()),
157 m_isInitialized(false) {
161#ifdef EIGEN_PARSED_BY_DOXYGEN
176 template <
typename Rhs>
190 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
191 return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate());
198 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
202 template <
typename InputType>
204 m_qr.resize(matrix.
rows(), matrix.
cols());
278 inline Index rows()
const {
return m_qr.rows(); }
279 inline Index cols()
const {
return m_qr.cols(); }
285 const HCoeffsType&
hCoeffs()
const {
return m_hCoeffs; }
287#ifndef EIGEN_PARSED_BY_DOXYGEN
288 template <
typename RhsType,
typename DstType>
289 void _solve_impl(
const RhsType& rhs, DstType& dst)
const;
291 template <
bool Conjugate,
typename RhsType,
typename DstType>
292 void _solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const;
296 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
301 HCoeffsType m_hCoeffs;
302 RowVectorType m_temp;
303 bool m_isInitialized;
376template <
typename MatrixQR,
typename HCoeffs>
377void householder_qr_inplace_unblocked(MatrixQR& mat, HCoeffs& hCoeffs,
typename MatrixQR::Scalar* tempData = 0) {
378 using Scalar =
typename MatrixQR::Scalar;
379 using RealScalar =
typename MatrixQR::RealScalar;
380 Index rows = mat.rows();
381 Index cols = mat.cols();
382 Index size = (std::min)(rows, cols);
384 eigen_assert(hCoeffs.size() == size);
389 tempVector.resize(cols);
390 tempData = tempVector.data();
393 for (Index k = 0; k < size; ++k) {
394 Index remainingRows = rows - k;
395 Index remainingCols = cols - k - 1;
398 mat.col(k).tail(remainingRows).makeHouseholderInPlace(hCoeffs.coeffRef(k), beta);
399 mat.coeffRef(k, k) = beta;
402 mat.bottomRightCorner(remainingRows, remainingCols)
403 .applyHouseholderOnTheLeft(mat.col(k).tail(remainingRows - 1), hCoeffs.coeffRef(k), tempData + k + 1);
420template <
typename MatrixQR,
typename HCoeffs,
typename VectorQR>
421void householder_qr_inplace_update(MatrixQR& mat, HCoeffs& hCoeffs,
const VectorQR& newColumn,
422 typename MatrixQR::Index k,
typename MatrixQR::Scalar* tempData) {
423 using Index =
typename MatrixQR::Index;
424 using RealScalar =
typename MatrixQR::RealScalar;
425 Index rows = mat.rows();
427 eigen_assert(k < mat.cols());
428 eigen_assert(k < rows);
429 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_COLS(hCoeffs, mat);
430 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_ROWS(newColumn, mat);
431 eigen_assert(tempData);
434 mat.col(k) = newColumn;
436 for (Index i = 0; i < k; ++i) {
437 Index remainingRows = rows - i;
440 .applyHouseholderOnTheLeft(mat.col(i).tail(remainingRows - 1), hCoeffs.coeffRef(i), tempData + i + 1);
444 mat.col(k).tail(rows - k).makeHouseholderInPlace(hCoeffs.coeffRef(k), beta);
445 mat.coeffRef(k, k) = beta;
449template <
typename MatrixQR,
typename HCoeffs,
typename MatrixQRScalar =
typename MatrixQR::Scalar,
450 bool InnerStrideIsOne = (MatrixQR::InnerStrideAtCompileTime == 1 && HCoeffs::InnerStrideAtCompileTime == 1)>
451struct householder_qr_inplace_blocked {
453 static void run(MatrixQR& mat, HCoeffs& hCoeffs, Index maxBlockSize = 32,
typename MatrixQR::Scalar* tempData = 0) {
454 using Scalar =
typename MatrixQR::Scalar;
457 Index rows = mat.rows();
458 Index cols = mat.cols();
459 Index size = (std::min)(rows, cols);
464 tempVector.resize(cols);
465 tempData = tempVector.data();
468 Index blockSize = (std::min)(maxBlockSize, size);
471 for (k = 0; k < size; k += blockSize) {
472 Index bs = (std::min)(size - k, blockSize);
473 Index tcols = cols - k - bs;
474 Index brows = rows - k;
484 BlockType A11_21 = mat.block(k, k, brows, bs);
487 householder_qr_inplace_unblocked(A11_21, hCoeffsSegment, tempData);
490 BlockType A21_22 = mat.block(k, k + bs, brows, tcols);
491 apply_block_householder_on_the_left(A21_22, A11_21, hCoeffsSegment,
false);
509template <
typename Scalar>
510Index householder_qr_panel_width(Index rows, Index cols) {
511 const Index size = numext::mini(rows, cols);
512 const double m = double(rows), n = double(cols), s = double(size);
513 if (m * n * s <= 64.0 * 64.0 * 64.0)
return size;
514 const double P = m * s - s * s / 2;
515 const double Q = m * n * s - (m + n) * s * s / 2 + s * s * s / 3;
516 const double target = numext::sqrt(3 * Q / P);
517 constexpr Index kPacketSize = packet_traits<Scalar>::size;
518 const Index kWidths[] = {8, 16, 24, 32, 48};
519 Index width = kPacketSize;
520 double distance = NumTraits<double>::highest();
521 for (Index w : kWidths) {
522 const double d = numext::maxi(
double(w) / target, target /
double(w));
523 if (w % kPacketSize == 0 && d < distance) {
533#ifndef EIGEN_PARSED_BY_DOXYGEN
534template <
typename MatrixType_>
535template <
typename RhsType,
typename DstType>
537 const Index rank = (std::min)(rows(), cols());
539 typename RhsType::PlainObject c(rhs);
543 m_qr.topLeftCorner(rank, rank).template triangularView<Upper>().solveInPlace(c.topRows(rank));
545 dst.topRows(rank) = c.topRows(rank);
546 dst.bottomRows(cols() - rank).setZero();
549template <
typename MatrixType_>
550template <
bool Conjugate,
typename RhsType,
typename DstType>
551void HouseholderQR<MatrixType_>::_solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const {
552 const Index rank = (std::min)(rows(), cols());
554 typename RhsType::PlainObject c(rhs);
556 m_qr.topLeftCorner(rank, rank)
557 .template triangularView<Upper>()
559 .template conjugateIf<Conjugate>()
560 .solveInPlace(c.topRows(rank));
562 dst.topRows(rank) = c.topRows(rank);
563 dst.bottomRows(rows() - rank).setZero();
565 dst.applyOnTheLeft(
householderQ().setLength(rank).
template conjugateIf<!Conjugate>());
575template <
typename MatrixType>
577 Index rows = m_qr.rows();
578 Index cols = m_qr.cols();
579 Index
size = (std::min)(rows, cols);
581 m_hCoeffs.resize(
size);
585 const Index maxBlockSize = internal::householder_qr_panel_width<Scalar>(rows, cols);
586 internal::householder_qr_inplace_blocked<MatrixType, HCoeffsType>::run(m_qr, m_hCoeffs, maxBlockSize, m_temp.data());
588 m_isInitialized =
true;
595template <
typename Derived>