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) {}
95 template<
typename InputType>
97 : m_qr(matrix.rows(), matrix.cols()),
98 m_hCoeffs((
std::min)(matrix.rows(),matrix.cols())),
99 m_temp(matrix.cols()),
100 m_isInitialized(false)
122 template<
typename Rhs>
126 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
140 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
141 return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate());
149 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
153 template<
typename InputType>
185 inline Index rows()
const {
return m_qr.rows(); }
186 inline Index cols()
const {
return m_qr.cols(); }
192 const HCoeffsType&
hCoeffs()
const {
return m_hCoeffs; }
194 #ifndef EIGEN_PARSED_BY_DOXYGEN
195 template<
typename RhsType,
typename DstType>
197 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
202 static void check_template_parameters()
204 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
208 HCoeffsType m_hCoeffs;
209 RowVectorType m_temp;
210 bool m_isInitialized;
213 template<
typename MatrixType>
217 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
218 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
219 return abs(m_qr.diagonal().prod());
222 template<
typename MatrixType>
225 eigen_assert(m_isInitialized &&
"HouseholderQR is not initialized.");
226 eigen_assert(m_qr.rows() == m_qr.cols() &&
"You can't take the determinant of a non-square matrix!");
227 return m_qr.diagonal().cwiseAbs().array().log().sum();
233 template<
typename MatrixQR,
typename HCoeffs>
234 void householder_qr_inplace_unblocked(MatrixQR& mat, HCoeffs& hCoeffs,
typename MatrixQR::Scalar* tempData = 0)
236 typedef typename MatrixQR::Scalar Scalar;
237 typedef typename MatrixQR::RealScalar RealScalar;
238 Index rows = mat.rows();
239 Index cols = mat.cols();
240 Index size = (std::min)(rows,cols);
242 eigen_assert(hCoeffs.size() == size);
249 tempData = tempVector.data();
252 for(Index k = 0; k < size; ++k)
254 Index remainingRows = rows - k;
255 Index remainingCols = cols - k - 1;
258 mat.col(k).tail(remainingRows).makeHouseholderInPlace(hCoeffs.coeffRef(k), beta);
259 mat.coeffRef(k,k) = beta;
262 mat.bottomRightCorner(remainingRows, remainingCols)
263 .applyHouseholderOnTheLeft(mat.col(k).tail(remainingRows-1), hCoeffs.coeffRef(k), tempData+k+1);
268 template<
typename MatrixQR,
typename HCoeffs,
269 typename MatrixQRScalar =
typename MatrixQR::Scalar,
270 bool InnerStrideIsOne = (MatrixQR::InnerStrideAtCompileTime == 1 && HCoeffs::InnerStrideAtCompileTime == 1)>
271 struct householder_qr_inplace_blocked
274 static void run(MatrixQR& mat, HCoeffs& hCoeffs, Index maxBlockSize=32,
275 typename MatrixQR::Scalar* tempData = 0)
277 typedef typename MatrixQR::Scalar Scalar;
278 typedef Block<MatrixQR,Dynamic,Dynamic> BlockType;
280 Index rows = mat.rows();
281 Index cols = mat.cols();
282 Index size = (std::min)(rows, cols);
284 typedef Matrix<Scalar,Dynamic,1,ColMajor,MatrixQR::MaxColsAtCompileTime,1> TempType;
288 tempVector.resize(cols);
289 tempData = tempVector.data();
292 Index blockSize = (std::min)(maxBlockSize,size);
295 for (k = 0; k < size; k += blockSize)
297 Index bs = (std::min)(size-k,blockSize);
298 Index tcols = cols - k - bs;
299 Index brows = rows-k;
309 BlockType A11_21 = mat.block(k,k,brows,bs);
310 Block<HCoeffs,Dynamic,1> hCoeffsSegment = hCoeffs.segment(k,bs);
312 householder_qr_inplace_unblocked(A11_21, hCoeffsSegment, tempData);
316 BlockType A21_22 = mat.block(k,k+bs,brows,tcols);
317 apply_block_householder_on_the_left(A21_22,A11_21,hCoeffsSegment,
false);
325 #ifndef EIGEN_PARSED_BY_DOXYGEN
326 template<
typename _MatrixType>
327 template<
typename RhsType,
typename DstType>
328 void HouseholderQR<_MatrixType>::_solve_impl(
const RhsType &rhs, DstType &dst)
const
330 const Index rank = (std::min)(rows(), cols());
331 eigen_assert(rhs.rows() == rows());
333 typename RhsType::PlainObject c(rhs);
338 m_hCoeffs.head(rank)).transpose()
341 m_qr.topLeftCorner(rank, rank)
342 .template triangularView<Upper>()
343 .solveInPlace(c.topRows(rank));
345 dst.topRows(rank) = c.topRows(rank);
346 dst.bottomRows(cols()-rank).setZero();
356 template<
typename MatrixType>
357 template<
typename InputType>
360 check_template_parameters();
362 Index rows = matrix.
rows();
363 Index cols = matrix.
cols();
364 Index size = (std::min)(rows,cols);
367 m_hCoeffs.resize(size);
371 internal::householder_qr_inplace_blocked<MatrixType, HCoeffsType>::run(m_qr, m_hCoeffs, 48, m_temp.data());
373 m_isInitialized =
true;
382 template<
typename Derived>
HouseholderSequenceType householderQ() const
Definition: HouseholderQR.h:138
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:192
Derived & derived()
Definition: EigenBase.h:44
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
Index rows() const
Definition: EigenBase.h:58
Definition: EigenBase.h:28
Definition: Eigen_Colamd.h:54
Index cols() const
Definition: EigenBase.h:61
Householder QR decomposition of a matrix.
Definition: ForwardDeclarations.h:252
const Solve< HouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: HouseholderQR.h:124
MatrixType::RealScalar logAbsDeterminant() const
Definition: HouseholderQR.h:223
Pseudo expression representing a solving operation.
Definition: Solve.h:62
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:214
const HouseholderQR< PlainObject > householderQr() const
Definition: HouseholderQR.h:384
HouseholderQR(const EigenBase< InputType > &matrix)
Constructs a QR factorization from a given matrix.
Definition: HouseholderQR.h:96
const MatrixType & matrixQR() const
Definition: HouseholderQR.h:147