Eigen::HouseholderQR< MatrixType_ > Class Template Reference

Householder QR decomposition of a matrix. More...

+ Inheritance diagram for Eigen::HouseholderQR< MatrixType_ >:

Public Types

enum  {
  MaxRowsAtCompileTime ,
  MaxColsAtCompileTime
}
 
typedef SolverBase< HouseholderQRBase
 
typedef internal::plain_diag_type< MatrixType >::type HCoeffsType
 
typedef HouseholderSequence< MatrixType, internal::remove_all_t< typename HCoeffsType::ConjugateReturnType > > HouseholderSequenceType
 
typedef Matrix< Scalar, RowsAtCompileTime, RowsAtCompileTime,(MatrixType::Flags &RowMajorBit) ? RowMajor :ColMajor, MaxRowsAtCompileTime, MaxRowsAtCompileTimeMatrixQType
 
typedef MatrixType_ MatrixType
 
typedef internal::plain_row_type< MatrixType >::type RowVectorType
 
- Public Types inherited from Eigen::SolverBase< HouseholderQR< MatrixType_ > >
enum  
 
typedef std::conditional_t< NumTraits< Scalar >::IsComplex, CwiseUnaryOp< internal::scalar_conjugate_op< Scalar >, const ConstTransposeReturnType >, const ConstTransposeReturnTypeAdjointReturnType
 
typedef EigenBase< HouseholderQR< MatrixType_ > > Base
 
typedef Scalar CoeffReturnType
 
typedef Transpose< const HouseholderQR< MatrixType_ > > ConstTransposeReturnType
 
typedef internal::traits< HouseholderQR< MatrixType_ > >::Scalar Scalar
 
- Public Types inherited from Eigen::EigenBase< Derived >
typedef Eigen::Index Index
 The interface type of indices. More...
 
typedef internal::traits< Derived >::StorageKind StorageKind
 

Public Member Functions

MatrixType::RealScalar absDeterminant () const
 
Index cols () const
 
template<typename InputType >
HouseholderQRcompute (const EigenBase< InputType > &matrix)
 
MatrixType::Scalar determinant () const
 
const HCoeffsTypehCoeffs () const
 
HouseholderSequenceType householderQ () const
 
 HouseholderQR ()
 Default Constructor. More...
 
template<typename InputType >
 HouseholderQR (const EigenBase< InputType > &matrix)
 Constructs a QR factorization from a given matrix. More...
 
template<typename InputType >
 HouseholderQR (EigenBase< InputType > &matrix)
 Constructs a QR factorization from a given matrix. More...
 
 HouseholderQR (Index rows, Index cols)
 Default Constructor with memory preallocation. More...
 
MatrixType::RealScalar logAbsDeterminant () const
 
const MatrixTypematrixQR () const
 
Index rows () const
 
template<typename Rhs >
const Solve< HouseholderQR, Rhs > solve (const MatrixBase< Rhs > &b) const
 
- Public Member Functions inherited from Eigen::SolverBase< HouseholderQR< MatrixType_ > >
const AdjointReturnType adjoint () const
 
HouseholderQR< MatrixType_ > & derived ()
 
const HouseholderQR< MatrixType_ > & derived () const
 
const Solve< HouseholderQR< MatrixType_ >, Rhs > solve (const MatrixBase< Rhs > &b) const
 
 SolverBase ()
 
const ConstTransposeReturnType transpose () const
 
 ~SolverBase ()
 
- Public Member Functions inherited from Eigen::EigenBase< Derived >
template<typename Dest >
void addTo (Dest &dst) const
 
template<typename Dest >
void applyThisOnTheLeft (Dest &dst) const
 
template<typename Dest >
void applyThisOnTheRight (Dest &dst) const
 
EIGEN_CONSTEXPR Index cols () const EIGEN_NOEXCEPT
 
Derived & const_cast_derived () const
 
const Derived & const_derived () const
 
Derived & derived ()
 
const Derived & derived () const
 
template<typename Dest >
void evalTo (Dest &dst) const
 
EIGEN_CONSTEXPR Index rows () const EIGEN_NOEXCEPT
 
EIGEN_CONSTEXPR Index size () const EIGEN_NOEXCEPT
 
template<typename Dest >
void subTo (Dest &dst) const
 

Protected Member Functions

void computeInPlace ()
 
- Protected Member Functions inherited from Eigen::SolverBase< HouseholderQR< MatrixType_ > >
void _check_solve_assertion (const Rhs &b) const
 

Protected Attributes

HCoeffsType m_hCoeffs
 
bool m_isInitialized
 
MatrixType m_qr
 
RowVectorType m_temp
 

Detailed Description

template<typename MatrixType_>
class Eigen::HouseholderQR< MatrixType_ >

Householder QR decomposition of a matrix.

Template Parameters
MatrixType_the type of the matrix of which we are computing the QR decomposition

This class performs a QR decomposition of a matrix A into matrices Q and R such that

\[ \mathbf{A} = \mathbf{Q} \, \mathbf{R} \]

by using Householder transformations. Here, Q a unitary matrix and R an upper triangular matrix. The result is stored in a compact way compatible with LAPACK.

Note that no pivoting is performed. This is not a rank-revealing decomposition. If you want that feature, use FullPivHouseholderQR or ColPivHouseholderQR instead.

This Householder QR decomposition is faster, but less numerically stable and less feature-full than FullPivHouseholderQR or ColPivHouseholderQR.

This class supports the inplace decomposition mechanism.

See also
MatrixBase::householderQr()

Definition at line 58 of file HouseholderQR.h.

Member Typedef Documentation

◆ Base

template<typename MatrixType_ >
typedef SolverBase<HouseholderQR> Eigen::HouseholderQR< MatrixType_ >::Base

Definition at line 64 of file HouseholderQR.h.

◆ HCoeffsType

template<typename MatrixType_ >
typedef internal::plain_diag_type<MatrixType>::type Eigen::HouseholderQR< MatrixType_ >::HCoeffsType

Definition at line 73 of file HouseholderQR.h.

◆ HouseholderSequenceType

template<typename MatrixType_ >
typedef HouseholderSequence<MatrixType,internal::remove_all_t<typename HCoeffsType::ConjugateReturnType> > Eigen::HouseholderQR< MatrixType_ >::HouseholderSequenceType

Definition at line 75 of file HouseholderQR.h.

◆ MatrixQType

template<typename MatrixType_ >
typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, (MatrixType::Flags&RowMajorBit) ? RowMajor : ColMajor, MaxRowsAtCompileTime, MaxRowsAtCompileTime> Eigen::HouseholderQR< MatrixType_ >::MatrixQType

Definition at line 72 of file HouseholderQR.h.

◆ MatrixType

template<typename MatrixType_ >
typedef MatrixType_ Eigen::HouseholderQR< MatrixType_ >::MatrixType

Definition at line 63 of file HouseholderQR.h.

◆ RowVectorType

template<typename MatrixType_ >
typedef internal::plain_row_type<MatrixType>::type Eigen::HouseholderQR< MatrixType_ >::RowVectorType

Definition at line 74 of file HouseholderQR.h.

Member Enumeration Documentation

◆ anonymous enum

template<typename MatrixType_ >
anonymous enum
Enumerator
MaxRowsAtCompileTime 
MaxColsAtCompileTime 

Definition at line 68 of file HouseholderQR.h.

68  {
69  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
70  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
71  };

Constructor & Destructor Documentation

◆ HouseholderQR() [1/4]

template<typename MatrixType_ >
Eigen::HouseholderQR< MatrixType_ >::HouseholderQR ( )
inline

Default Constructor.

The default constructor is useful in cases in which the user intends to perform decompositions via HouseholderQR::compute(const MatrixType&).

Definition at line 83 of file HouseholderQR.h.

83 : m_qr(), m_hCoeffs(), m_temp(), m_isInitialized(false) {}
RowVectorType m_temp

◆ HouseholderQR() [2/4]

template<typename MatrixType_ >
Eigen::HouseholderQR< MatrixType_ >::HouseholderQR ( Index  rows,
Index  cols 
)
inline

Default Constructor with memory preallocation.

Like the default constructor but with preallocation of the internal data according to the specified problem size.

See also
HouseholderQR()

Definition at line 91 of file HouseholderQR.h.

92  : m_qr(rows, cols),
94  m_temp(cols),
95  m_isInitialized(false) {}
Index rows() const
Index cols() const
bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:684

◆ HouseholderQR() [3/4]

template<typename MatrixType_ >
template<typename InputType >
Eigen::HouseholderQR< MatrixType_ >::HouseholderQR ( const EigenBase< InputType > &  matrix)
inlineexplicit

Constructs a QR factorization from a given matrix.

This constructor computes the QR factorization of the matrix matrix by calling the method compute(). It is a short cut for:

HouseholderQR<MatrixType> qr(matrix.rows(), matrix.cols());
qr.compute(matrix);
HouseholderQR< MatrixXf > qr(A)
See also
compute()

Definition at line 110 of file HouseholderQR.h.

111  : m_qr(matrix.rows(), matrix.cols()),
112  m_hCoeffs((std::min)(matrix.rows(),matrix.cols())),
113  m_temp(matrix.cols()),
114  m_isInitialized(false)
115  {
116  compute(matrix.derived());
117  }
HouseholderQR & compute(const EigenBase< InputType > &matrix)

◆ HouseholderQR() [4/4]

template<typename MatrixType_ >
template<typename InputType >
Eigen::HouseholderQR< MatrixType_ >::HouseholderQR ( EigenBase< InputType > &  matrix)
inlineexplicit

Constructs a QR factorization from a given matrix.

This overloaded constructor is provided for inplace decomposition when MatrixType is a Eigen::Ref.

See also
HouseholderQR(const EigenBase&)

Definition at line 128 of file HouseholderQR.h.

129  : m_qr(matrix.derived()),
130  m_hCoeffs((std::min)(matrix.rows(),matrix.cols())),
131  m_temp(matrix.cols()),
132  m_isInitialized(false)
133  {
134  computeInPlace();
135  }

Member Function Documentation

◆ absDeterminant()

template<typename MatrixType >
MatrixType::RealScalar Eigen::HouseholderQR< MatrixType >::absDeterminant
Returns
the absolute value of the determinant of the matrix of which *this is the QR decomposition. It has only linear complexity (that is, O(n) where n is the dimension of the square matrix) as the QR decomposition has already been computed.
Note
This is only for square matrices.
Warning
a determinant can be very big or small, so for matrices of large enough dimension, there is a risk of overflow/underflow. One way to work around that is to use logAbsDeterminant() instead.
See also
determinant(), logAbsDeterminant(), MatrixBase::determinant()

Definition at line 312 of file HouseholderQR.h.

313 {
314  using std::abs;
315  eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
316  eigen_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!");
317  return abs(m_qr.diagonal().prod());
318 }
const AbsReturnType abs() const
#define eigen_assert(x)
Definition: Macros.h:902
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)

◆ cols()

template<typename MatrixType_ >
Index Eigen::HouseholderQR< MatrixType_ >::cols ( void  ) const
inline

Definition at line 232 of file HouseholderQR.h.

232 { return m_qr.cols(); }

◆ compute()

template<typename MatrixType_ >
template<typename InputType >
HouseholderQR& Eigen::HouseholderQR< MatrixType_ >::compute ( const EigenBase< InputType > &  matrix)
inline

Definition at line 181 of file HouseholderQR.h.

181  {
182  m_qr = matrix.derived();
183  computeInPlace();
184  return *this;
185  }

◆ computeInPlace()

template<typename MatrixType >
void Eigen::HouseholderQR< MatrixType >::computeInPlace
protected

Performs the QR factorization of the given matrix matrix. The result of the factorization is stored into *this, and a reference to *this is returned.

See also
class HouseholderQR, HouseholderQR(const MatrixType&)

Definition at line 506 of file HouseholderQR.h.

507 {
508  Index rows = m_qr.rows();
509  Index cols = m_qr.cols();
510  Index size = (std::min)(rows,cols);
511 
512  m_hCoeffs.resize(size);
513 
514  m_temp.resize(cols);
515 
516  internal::householder_qr_inplace_blocked<MatrixType, HCoeffsType>::run(m_qr, m_hCoeffs, 48, m_temp.data());
517 
518  m_isInitialized = true;
519 }
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition: EigenBase.h:69

◆ determinant()

template<typename MatrixType >
MatrixType::Scalar Eigen::HouseholderQR< MatrixType >::determinant
Returns
the determinant of the matrix of which *this is the QR decomposition. It has only linear complexity (that is, O(n) where n is the dimension of the square matrix) as the QR decomposition has already been computed.
Note
This is only for square matrices.
Warning
a determinant can be very big or small, so for matrices of large enough dimension, there is a risk of overflow/underflow. One way to work around that is to use logAbsDeterminant() instead.
See also
absDeterminant(), logAbsDeterminant(), MatrixBase::determinant()

Definition at line 302 of file HouseholderQR.h.

303 {
304  eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
305  eigen_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!");
306  Scalar detQ;
307  internal::householder_determinant<HCoeffsType, Scalar, NumTraits<Scalar>::IsComplex>::run(m_hCoeffs, detQ);
308  return m_qr.diagonal().prod() * detQ;
309 }
internal::traits< HouseholderQR< MatrixType_ > >::Scalar Scalar
Definition: SolverBase.h:75

◆ hCoeffs()

template<typename MatrixType_ >
const HCoeffsType& Eigen::HouseholderQR< MatrixType_ >::hCoeffs ( ) const
inline
Returns
a const reference to the vector of Householder coefficients used to represent the factor Q.

For advanced uses only.

Definition at line 238 of file HouseholderQR.h.

238 { return m_hCoeffs; }

◆ householderQ()

template<typename MatrixType_ >
HouseholderSequenceType Eigen::HouseholderQR< MatrixType_ >::householderQ ( void  ) const
inline

This method returns an expression of the unitary matrix Q as a sequence of Householder transformations.

The returned expression can directly be used to perform matrix products. It can also be assigned to a dense Matrix object. Here is an example showing how to recover the full or thin matrix Q, as well as how to perform matrix products using operator*:

Example:

A.setRandom();
HouseholderQR<MatrixXf> qr(A);
Q = qr.householderQ();
thinQ = qr.householderQ() * thinQ;
std::cout << "The complete unitary matrix Q is:\n" << Q << "\n\n";
std::cout << "The thin matrix Q is:\n" << thinQ << "\n\n";
MatrixXcf A
MatrixXf Q
static const RandomReturnType Random()
Definition: Random.h:114
static const IdentityReturnType Identity()
Matrix< float, Dynamic, Dynamic > MatrixXf
Dynamic×Dynamic matrix of type float.
Definition: Matrix.h:501

Output:

The complete unitary matrix Q is:
  -0.676   0.0793    0.713  -0.0788   -0.147
  -0.221   -0.322    -0.37   -0.366   -0.759
  -0.353   -0.345   -0.214    0.841  -0.0518
   0.582   -0.462    0.555    0.176   -0.329
  -0.174   -0.747 -0.00907   -0.348    0.539

The thin matrix Q is:
  -0.676   0.0793    0.713
  -0.221   -0.322    -0.37
  -0.353   -0.345   -0.214
   0.582   -0.462    0.555
  -0.174   -0.747 -0.00907

Definition at line 165 of file HouseholderQR.h.

166  {
167  eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
168  return HouseholderSequenceType(m_qr, m_hCoeffs.conjugate());
169  }
HouseholderSequence< MatrixType, internal::remove_all_t< typename HCoeffsType::ConjugateReturnType > > HouseholderSequenceType
Definition: HouseholderQR.h:75

◆ logAbsDeterminant()

template<typename MatrixType >
MatrixType::RealScalar Eigen::HouseholderQR< MatrixType >::logAbsDeterminant
Returns
the natural log of the absolute value of the determinant of the matrix of which *this is the QR decomposition. It has only linear complexity (that is, O(n) where n is the dimension of the square matrix) as the QR decomposition has already been computed.
Note
This is only for square matrices.
This method is useful to work around the risk of overflow/underflow that's inherent to determinant computation.
See also
determinant(), absDeterminant(), MatrixBase::determinant()

Definition at line 321 of file HouseholderQR.h.

322 {
323  eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
324  eigen_assert(m_qr.rows() == m_qr.cols() && "You can't take the determinant of a non-square matrix!");
325  return m_qr.diagonal().cwiseAbs().array().log().sum();
326 }

◆ matrixQR()

template<typename MatrixType_ >
const MatrixType& Eigen::HouseholderQR< MatrixType_ >::matrixQR ( ) const
inline
Returns
a reference to the matrix where the Householder QR decomposition is stored in a LAPACK-compatible way.

Definition at line 174 of file HouseholderQR.h.

175  {
176  eigen_assert(m_isInitialized && "HouseholderQR is not initialized.");
177  return m_qr;
178  }

◆ rows()

template<typename MatrixType_ >
Index Eigen::HouseholderQR< MatrixType_ >::rows ( void  ) const
inline

Definition at line 231 of file HouseholderQR.h.

231 { return m_qr.rows(); }

◆ solve()

template<typename MatrixType_ >
template<typename Rhs >
const Solve<HouseholderQR, Rhs> Eigen::HouseholderQR< MatrixType_ >::solve ( const MatrixBase< Rhs > &  b) const
inline

This method finds a solution x to the equation Ax=b, where A is the matrix of which *this is the QR decomposition, if any exists.

Parameters
bthe right-hand-side of the equation to solve.
Returns
a solution.

This method just tries to find as good a solution as possible. If you want to check whether a solution exists or if it is accurate, just call this function to get a result and then compute the error of this result, or use MatrixBase::isApprox() directly, for instance like this:

bool a_solution_exists = (A*result).isApprox(b, precision);
Array< int, 3, 1 > b
bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())

This method avoids dividing by zero, so that the non-existence of a solution doesn't by itself mean that you'll get inf or nan values.

If there exists more than one solution, this method will arbitrarily choose one.

Example:

typedef Matrix<float,3,3> Matrix3x3;
Matrix3x3 m = Matrix3x3::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is the matrix y:" << endl << y << endl;
x = m.householderQr().solve(y);
assert(y.isApprox(m*x));
cout << "Here is a solution x to the equation mx=y:" << endl << x << endl;
Matrix3f m
Matrix< float, 3, 3 > Matrix3x3
Matrix< float, 3, 3 > Matrix3f
3×3 matrix of type float.
Definition: Matrix.h:501
const Scalar & y

Output:

Here is the matrix m:
  0.68  0.597  -0.33
-0.211  0.823  0.536
 0.566 -0.605 -0.444
Here is the matrix y:
  0.108   -0.27   0.832
-0.0452  0.0268   0.271
  0.258   0.904   0.435
Here is a solution x to the equation mx=y:
 0.609   2.68   1.67
-0.231  -1.57 0.0713
  0.51   3.51   1.05

Member Data Documentation

◆ m_hCoeffs

template<typename MatrixType_ >
HCoeffsType Eigen::HouseholderQR< MatrixType_ >::m_hCoeffs
protected

Definition at line 255 of file HouseholderQR.h.

◆ m_isInitialized

template<typename MatrixType_ >
bool Eigen::HouseholderQR< MatrixType_ >::m_isInitialized
protected

Definition at line 257 of file HouseholderQR.h.

◆ m_qr

template<typename MatrixType_ >
MatrixType Eigen::HouseholderQR< MatrixType_ >::m_qr
protected

Definition at line 254 of file HouseholderQR.h.

◆ m_temp

template<typename MatrixType_ >
RowVectorType Eigen::HouseholderQR< MatrixType_ >::m_temp
protected

Definition at line 256 of file HouseholderQR.h.


The documentation for this class was generated from the following files: