42 template<
typename _MatrixType>
class HouseholderQR
46 typedef _MatrixType MatrixType;
48 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
49 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
50 Options = MatrixType::Options,
51 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
52 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
54 typedef typename MatrixType::Scalar Scalar;
55 typedef typename MatrixType::RealScalar RealScalar;
57 typedef typename MatrixType::StorageIndex StorageIndex;
58 typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, (MatrixType::Flags&RowMajorBit) ? RowMajor : ColMajor, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType;
59 typedef typename internal::plain_diag_type<MatrixType>::type HCoeffsType;
60 typedef typename internal::plain_row_type<MatrixType>::type RowVectorType;
61 typedef HouseholderSequence<MatrixType,typename internal::remove_all<typename HCoeffsType::ConjugateReturnType>::type> HouseholderSequenceType;
69 HouseholderQR() : m_qr(), m_hCoeffs(), m_temp(), m_isInitialized(false) {}
79 m_hCoeffs((
std::min)(rows,cols)),
81 m_isInitialized(false) {}
96 : m_qr(matrix.rows(), matrix.cols()),
97 m_hCoeffs((
std::min)(matrix.rows(),matrix.cols())),
98 m_temp(matrix.cols()),
99 m_isInitialized(false)
121 template<
typename Rhs>
125 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
139 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
140 return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate());
148 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
183 inline Index rows()
const {
return m_qr.rows(); }
184 inline Index cols()
const {
return m_qr.cols(); }
190 const HCoeffsType&
hCoeffs()
const {
return m_hCoeffs; }
192 #ifndef EIGEN_PARSED_BY_DOXYGEN
193 template<
typename RhsType,
typename DstType>
195 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
200 static void check_template_parameters()
202 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
206 HCoeffsType m_hCoeffs;
207 RowVectorType m_temp;
208 bool m_isInitialized;
211 template<
typename MatrixType>
215 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
216 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
217 return abs(m_qr.diagonal().prod());
220 template<
typename MatrixType>
223 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
224 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
225 return m_qr.diagonal().cwiseAbs().array().log().sum();
231 template<
typename MatrixQR,
typename HCoeffs>
232 void householder_qr_inplace_unblocked(MatrixQR& mat, HCoeffs& hCoeffs,
typename MatrixQR::Scalar* tempData = 0)
234 typedef typename MatrixQR::Scalar Scalar;
235 typedef typename MatrixQR::RealScalar RealScalar;
236 Index rows = mat.rows();
237 Index cols = mat.cols();
238 Index size = (std::min)(rows,cols);
240 eigen_assert(hCoeffs.size() == size);
247 tempData = tempVector.data();
250 for(Index k = 0; k < size; ++k)
252 Index remainingRows = rows - k;
253 Index remainingCols = cols - k - 1;
256 mat.col(k).tail(remainingRows).makeHouseholderInPlace(hCoeffs.coeffRef(k), beta);
257 mat.coeffRef(k,k) = beta;
260 mat.bottomRightCorner(remainingRows, remainingCols)
261 .applyHouseholderOnTheLeft(mat.col(k).tail(remainingRows-1), hCoeffs.coeffRef(k), tempData+k+1);
266 template<
typename MatrixQR,
typename HCoeffs,
267 typename MatrixQRScalar =
typename MatrixQR::Scalar,
268 bool InnerStrideIsOne = (MatrixQR::InnerStrideAtCompileTime == 1 && HCoeffs::InnerStrideAtCompileTime == 1)>
269 struct householder_qr_inplace_blocked
272 static void run(MatrixQR& mat, HCoeffs& hCoeffs, Index maxBlockSize=32,
273 typename MatrixQR::Scalar* tempData = 0)
275 typedef typename MatrixQR::Scalar Scalar;
276 typedef Block<MatrixQR,Dynamic,Dynamic> BlockType;
278 Index rows = mat.rows();
279 Index cols = mat.cols();
280 Index size = (std::min)(rows, cols);
282 typedef Matrix<Scalar,Dynamic,1,ColMajor,MatrixQR::MaxColsAtCompileTime,1> TempType;
286 tempVector.resize(cols);
287 tempData = tempVector.data();
290 Index blockSize = (std::min)(maxBlockSize,size);
293 for (k = 0; k < size; k += blockSize)
295 Index bs = (std::min)(size-k,blockSize);
296 Index tcols = cols - k - bs;
297 Index brows = rows-k;
307 BlockType A11_21 = mat.block(k,k,brows,bs);
308 Block<HCoeffs,Dynamic,1> hCoeffsSegment = hCoeffs.segment(k,bs);
310 householder_qr_inplace_unblocked(A11_21, hCoeffsSegment, tempData);
314 BlockType A21_22 = mat.block(k,k+bs,brows,tcols);
315 apply_block_householder_on_the_left(A21_22,A11_21,hCoeffsSegment,
false);
323 #ifndef EIGEN_PARSED_BY_DOXYGEN
324 template<
typename _MatrixType>
325 template<
typename RhsType,
typename DstType>
326 void HouseholderQR<_MatrixType>::_solve_impl(
const RhsType &rhs, DstType &dst)
const
328 const Index rank = (std::min)(rows(), cols());
329 eigen_assert(rhs.rows() == rows());
331 typename RhsType::PlainObject c(rhs);
336 m_hCoeffs.head(rank)).transpose()
339 m_qr.topLeftCorner(rank, rank)
340 .template triangularView<Upper>()
341 .solveInPlace(c.topRows(rank));
343 dst.topRows(rank) = c.topRows(rank);
344 dst.bottomRows(cols()-rank).setZero();
354 template<
typename MatrixType>
357 check_template_parameters();
359 Index rows = matrix.rows();
360 Index cols = matrix.cols();
361 Index size = (std::min)(rows,cols);
364 m_hCoeffs.resize(size);
368 internal::householder_qr_inplace_blocked<MatrixType, HCoeffsType>::run(m_qr, m_hCoeffs, 48, m_temp.data());
370 m_isInitialized =
true;
379 template<
typename Derived>
HouseholderQR(const MatrixType &matrix)
Constructs a QR factorization from a given matrix.
Definition: HouseholderQR.h:95
HouseholderSequenceType householderQ() const
Definition: HouseholderQR.h:137
HouseholderQR(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition: HouseholderQR.h:77
Definition: StdDeque.h:58
const HCoeffsType & hCoeffs() const
Definition: HouseholderQR.h:190
HouseholderSequence< VectorsType, CoeffsType > householderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition: HouseholderSequence.h:452
void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:252
Definition: Eigen_Colamd.h:54
Householder QR decomposition of a matrix.
Definition: ForwardDeclarations.h:251
HouseholderQR & compute(const MatrixType &matrix)
Definition: HouseholderQR.h:355
const Solve< HouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: HouseholderQR.h:123
MatrixType::RealScalar logAbsDeterminant() const
Definition: HouseholderQR.h:221
Pseudo expression representing a solving operation.
Definition: Solve.h:63
HouseholderQR()
Default Constructor.
Definition: HouseholderQR.h:69
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
MatrixType::RealScalar absDeterminant() const
Definition: HouseholderQR.h:212
const HouseholderQR< PlainObject > householderQr() const
Definition: HouseholderQR.h:381
const MatrixType & matrixQR() const
Definition: HouseholderQR.h:146