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;
56 typedef typename MatrixType::Index Index;
57 typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, (MatrixType::Flags&RowMajorBit) ? RowMajor : ColMajor, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixQType;
58 typedef typename internal::plain_diag_type<MatrixType>::type HCoeffsType;
59 typedef typename internal::plain_row_type<MatrixType>::type RowVectorType;
60 typedef HouseholderSequence<MatrixType,typename internal::remove_all<typename HCoeffsType::ConjugateReturnType>::type> HouseholderSequenceType;
68 HouseholderQR() : m_qr(), m_hCoeffs(), m_temp(), m_isInitialized(false) {}
78 m_hCoeffs((
std::min)(rows,cols)),
80 m_isInitialized(false) {}
95 : m_qr(matrix.rows(), matrix.cols()),
96 m_hCoeffs((
std::min)(matrix.rows(),matrix.cols())),
97 m_temp(matrix.cols()),
98 m_isInitialized(false)
117 template<
typename Rhs>
118 inline const internal::solve_retval<HouseholderQR, Rhs>
121 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
122 return internal::solve_retval<HouseholderQR, Rhs>(*
this, b.derived());
135 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
136 return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate());
144 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
179 inline Index rows()
const {
return m_qr.rows(); }
180 inline Index cols()
const {
return m_qr.cols(); }
186 const HCoeffsType&
hCoeffs()
const {
return m_hCoeffs; }
190 static void check_template_parameters()
192 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
196 HCoeffsType m_hCoeffs;
197 RowVectorType m_temp;
198 bool m_isInitialized;
201 template<
typename MatrixType>
205 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
206 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
207 return abs(m_qr.diagonal().prod());
210 template<
typename MatrixType>
213 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
214 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
215 return m_qr.diagonal().cwiseAbs().array().log().sum();
221 template<
typename MatrixQR,
typename HCoeffs>
222 void householder_qr_inplace_unblocked(MatrixQR& mat, HCoeffs&
hCoeffs,
typename MatrixQR::Scalar* tempData = 0)
224 typedef typename MatrixQR::Index Index;
225 typedef typename MatrixQR::Scalar Scalar;
226 typedef typename MatrixQR::RealScalar RealScalar;
227 Index rows = mat.rows();
228 Index cols = mat.cols();
229 Index size = (std::min)(rows,cols);
231 eigen_assert(hCoeffs.size() == size);
238 tempData = tempVector.data();
241 for(Index k = 0; k < size; ++k)
243 Index remainingRows = rows - k;
244 Index remainingCols = cols - k - 1;
247 mat.col(k).tail(remainingRows).makeHouseholderInPlace(hCoeffs.coeffRef(k), beta);
248 mat.coeffRef(k,k) = beta;
251 mat.bottomRightCorner(remainingRows, remainingCols)
252 .applyHouseholderOnTheLeft(mat.col(k).tail(remainingRows-1), hCoeffs.coeffRef(k), tempData+k+1);
257 template<
typename MatrixQR,
typename HCoeffs,
258 typename MatrixQRScalar =
typename MatrixQR::Scalar,
259 bool InnerStrideIsOne = (MatrixQR::InnerStrideAtCompileTime == 1 && HCoeffs::InnerStrideAtCompileTime == 1)>
260 struct householder_qr_inplace_blocked
263 static void run(MatrixQR& mat, HCoeffs& hCoeffs,
264 typename MatrixQR::Index maxBlockSize=32,
265 typename MatrixQR::Scalar* tempData = 0)
267 typedef typename MatrixQR::Index Index;
268 typedef typename MatrixQR::Scalar Scalar;
271 Index rows = mat.rows();
272 Index cols = mat.cols();
273 Index size = (std::min)(rows, cols);
280 tempData = tempVector.data();
283 Index blockSize = (std::min)(maxBlockSize,size);
286 for (k = 0; k < size; k += blockSize)
288 Index bs = (std::min)(size-k,blockSize);
289 Index tcols = cols - k - bs;
290 Index brows = rows-k;
300 BlockType A11_21 = mat.block(k,k,brows,bs);
303 householder_qr_inplace_unblocked(A11_21, hCoeffsSegment, tempData);
307 BlockType A21_22 = mat.block(k,k+bs,brows,tcols);
308 apply_block_householder_on_the_left(A21_22,A11_21,hCoeffsSegment.adjoint());
314 template<
typename _MatrixType,
typename Rhs>
315 struct solve_retval<HouseholderQR<_MatrixType>, Rhs>
316 : solve_retval_base<HouseholderQR<_MatrixType>, Rhs>
320 template<
typename Dest>
void evalTo(Dest& dst)
const 322 const Index rows = dec().rows(), cols = dec().cols();
323 const Index rank = (std::min)(rows, cols);
324 eigen_assert(rhs().rows() == rows);
326 typename Rhs::PlainObject c(rhs());
331 dec().
hCoeffs().head(rank)).transpose()
335 .topLeftCorner(rank, rank)
336 .template triangularView<Upper>()
337 .solveInPlace(c.topRows(rank));
339 dst.topRows(rank) = c.topRows(rank);
340 dst.bottomRows(cols-rank).setZero();
352 template<
typename MatrixType>
355 check_template_parameters();
357 Index rows = matrix.rows();
358 Index cols = matrix.cols();
359 Index size = (std::min)(rows,cols);
362 m_hCoeffs.resize(size);
366 internal::householder_qr_inplace_blocked<MatrixType, HCoeffsType>::run(m_qr, m_hCoeffs, 48, m_temp.data());
368 m_isInitialized =
true;
376 template<
typename Derived>
HouseholderQR(const MatrixType &matrix)
Constructs a QR factorization from a given matrix.
Definition: HouseholderQR.h:94
const MatrixType & matrixQR() const
Definition: HouseholderQR.h:142
Definition: StdDeque.h:50
const internal::solve_retval< HouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: HouseholderQR.h:119
HouseholderSequence< VectorsType, CoeffsType > householderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition: HouseholderSequence.h:423
HouseholderSequenceType householderQ() const
Definition: HouseholderQR.h:133
MatrixType::RealScalar absDeterminant() const
Definition: HouseholderQR.h:202
HouseholderQR()
Default Constructor.
Definition: HouseholderQR.h:68
const HCoeffsType & hCoeffs() const
Definition: HouseholderQR.h:186
HouseholderQR & compute(const MatrixType &matrix)
Definition: HouseholderQR.h:353
Definition: Eigen_Colamd.h:50
MatrixType::RealScalar logAbsDeterminant() const
Definition: HouseholderQR.h:211
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:102
const HouseholderQR< PlainObject > householderQr() const
Definition: HouseholderQR.h:378
Householder QR decomposition of a matrix.
Definition: ForwardDeclarations.h:221
HouseholderQR(Index rows, Index cols)
Default Constructor with memory preallocation.
Definition: HouseholderQR.h:76
void resize(Index nbRows, Index nbCols)
Definition: PlainObjectBase.h:235
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48