Eigen::SPQR< MatrixType_ > Class Template Reference

Sparse QR factorization based on SuiteSparseQR library. More...

#include <SuiteSparseQRSupport.h>

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

Public Types

enum  { ColsAtCompileTime = Dynamic , MaxColsAtCompileTime = Dynamic }
 
typedef MatrixType_::Scalar Scalar
 
typedef MatrixType_::RealScalar RealScalar
 
typedef SuiteSparse_long StorageIndex
 
typedef SparseMatrix< Scalar, ColMajor, StorageIndexMatrixType
 
typedef Map< PermutationMatrix< Dynamic, Dynamic, StorageIndex > > PermutationType
 

Public Member Functions

 SPQR ()
 
 SPQR (const MatrixType_ &matrix)
 
 ~SPQR ()
 
void SPQR_free ()
 
void compute (const MatrixType_ &matrix)
 
Index rows () const
 
Index cols () const
 
template<typename Rhs , typename Dest >
void _solve_impl (const MatrixBase< Rhs > &b, MatrixBase< Dest > &dest) const
 
const MatrixType matrixR () const
 
SPQRMatrixQReturnType< SPQRmatrixQ () const
 Get an expression of the matrix Q. More...
 
PermutationType colsPermutation () const
 Get the permutation that was applied to columns of A. More...
 
Index rank () const
 
void setSPQROrdering (int ord)
 Set the fill-reducing ordering method to be used. More...
 
void setPivotThreshold (const RealScalar &tol)
 Set the tolerance tol to treat columns with 2-norm < =tol as zero. More...
 
cholmod_common * cholmodCommon () const
 
ComputationInfo info () const
 Reports whether previous computation was successful. More...
 
- Public Member Functions inherited from Eigen::SparseSolverBase< SPQR< MatrixType_ > >
 SparseSolverBase ()
 
 SparseSolverBase (SparseSolverBase &&other)
 
 ~SparseSolverBase ()
 
SPQR< MatrixType_ > & derived ()
 
const SPQR< MatrixType_ > & derived () const
 
const Solve< SPQR< MatrixType_ >, Rhs > solve (const MatrixBase< Rhs > &b) const
 
const Solve< SPQR< MatrixType_ >, Rhs > solve (const SparseMatrixBase< Rhs > &b) const
 
void _solve_impl (const SparseMatrixBase< Rhs > &b, SparseMatrixBase< Dest > &dest) const
 

Protected Types

typedef SparseSolverBase< SPQR< MatrixType_ > > Base
 

Protected Attributes

bool m_analysisIsOk
 
bool m_factorizationIsOk
 
bool m_isRUpToDate
 
ComputationInfo m_info
 
int m_ordering
 
int m_allow_tol
 
RealScalar m_tolerance
 
cholmod_sparse * m_cR = nullptr
 
MatrixType m_R
 
StorageIndexm_E = nullptr
 
cholmod_sparse * m_H = nullptr
 
StorageIndexm_HPinv = nullptr
 
cholmod_dense * m_HTau = nullptr
 
Index m_rank
 
cholmod_common m_cc
 
bool m_useDefaultThreshold
 
Index m_rows
 
- Protected Attributes inherited from Eigen::SparseSolverBase< SPQR< MatrixType_ > >
bool m_isInitialized
 

Friends

template<typename , typename >
struct SPQR_QProduct
 

Detailed Description

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

Sparse QR factorization based on SuiteSparseQR library.

This class is used to perform a multithreaded and multifrontal rank-revealing QR decomposition of sparse matrices. The result is then used to solve linear leasts_square systems. Clearly, a QR factorization is returned such that A*P = Q*R where :

P is the column permutation. Use colsPermutation() to get it.

Q is the orthogonal matrix represented as Householder reflectors. Use matrixQ() to get an expression and matrixQ().transpose() to get the transpose. You can then apply it to a vector.

R is the sparse triangular factor. Use matrixQR() to get it as SparseMatrix. NOTE : The Index type of R is always SuiteSparse_long. You can get it with SPQR::Index

Template Parameters
MatrixType_The type of the sparse matrix A, must be a column-major SparseMatrix<>

\implsparsesolverconcept

Member Typedef Documentation

◆ Base

template<typename MatrixType_ >
typedef SparseSolverBase<SPQR<MatrixType_> > Eigen::SPQR< MatrixType_ >::Base
protected

◆ MatrixType

template<typename MatrixType_ >
typedef SparseMatrix<Scalar, ColMajor, StorageIndex> Eigen::SPQR< MatrixType_ >::MatrixType

◆ PermutationType

template<typename MatrixType_ >
typedef Map<PermutationMatrix<Dynamic, Dynamic, StorageIndex> > Eigen::SPQR< MatrixType_ >::PermutationType

◆ RealScalar

template<typename MatrixType_ >
typedef MatrixType_::RealScalar Eigen::SPQR< MatrixType_ >::RealScalar

◆ Scalar

template<typename MatrixType_ >
typedef MatrixType_::Scalar Eigen::SPQR< MatrixType_ >::Scalar

◆ StorageIndex

template<typename MatrixType_ >
typedef SuiteSparse_long Eigen::SPQR< MatrixType_ >::StorageIndex

Member Enumeration Documentation

◆ anonymous enum

template<typename MatrixType_ >
anonymous enum
Enumerator
ColsAtCompileTime 
MaxColsAtCompileTime 
@ MaxColsAtCompileTime
Definition: SuiteSparseQRSupport.h:78
@ ColsAtCompileTime
Definition: SuiteSparseQRSupport.h:78
const int Dynamic
Definition: Constants.h:25

Constructor & Destructor Documentation

◆ SPQR() [1/2]

template<typename MatrixType_ >
Eigen::SPQR< MatrixType_ >::SPQR ( )
inline
82  : m_analysisIsOk(false),
83  m_factorizationIsOk(false),
84  m_isRUpToDate(false),
85  m_ordering(SPQR_ORDERING_DEFAULT),
86  m_allow_tol(SPQR_DEFAULT_TOL),
88  m_cR(0),
89  m_E(0),
90  m_H(0),
91  m_HPinv(0),
92  m_HTau(0),
93  m_useDefaultThreshold(true) {
94  cholmod_l_start(&m_cc);
95  }
cholmod_dense * m_HTau
Definition: SuiteSparseQRSupport.h:253
bool m_factorizationIsOk
Definition: SuiteSparseQRSupport.h:242
StorageIndex * m_E
Definition: SuiteSparseQRSupport.h:250
cholmod_sparse * m_cR
Definition: SuiteSparseQRSupport.h:248
bool m_isRUpToDate
Definition: SuiteSparseQRSupport.h:243
RealScalar m_tolerance
Definition: SuiteSparseQRSupport.h:247
int m_ordering
Definition: SuiteSparseQRSupport.h:245
cholmod_sparse * m_H
Definition: SuiteSparseQRSupport.h:251
bool m_useDefaultThreshold
Definition: SuiteSparseQRSupport.h:256
StorageIndex * m_HPinv
Definition: SuiteSparseQRSupport.h:252
int m_allow_tol
Definition: SuiteSparseQRSupport.h:246
bool m_analysisIsOk
Definition: SuiteSparseQRSupport.h:241
cholmod_common m_cc
Definition: SuiteSparseQRSupport.h:255
double epsilon
Definition: osc_ring_sarah_asymptotics.h:43

References Eigen::SPQR< MatrixType_ >::m_cc.

◆ SPQR() [2/2]

template<typename MatrixType_ >
Eigen::SPQR< MatrixType_ >::SPQR ( const MatrixType_ &  matrix)
inlineexplicit
98  : m_analysisIsOk(false),
99  m_factorizationIsOk(false),
100  m_isRUpToDate(false),
101  m_ordering(SPQR_ORDERING_DEFAULT),
102  m_allow_tol(SPQR_DEFAULT_TOL),
104  m_cR(0),
105  m_E(0),
106  m_H(0),
107  m_HPinv(0),
108  m_HTau(0),
109  m_useDefaultThreshold(true) {
110  cholmod_l_start(&m_cc);
111  compute(matrix);
112  }
void compute(const MatrixType_ &matrix)
Definition: SuiteSparseQRSupport.h:126
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85

References Eigen::SPQR< MatrixType_ >::compute(), Eigen::SPQR< MatrixType_ >::m_cc, and matrix().

◆ ~SPQR()

template<typename MatrixType_ >
Eigen::SPQR< MatrixType_ >::~SPQR ( )
inline
114  {
115  SPQR_free();
116  cholmod_l_finish(&m_cc);
117  }
void SPQR_free()
Definition: SuiteSparseQRSupport.h:118

References Eigen::SPQR< MatrixType_ >::m_cc, and Eigen::SPQR< MatrixType_ >::SPQR_free().

Member Function Documentation

◆ _solve_impl()

template<typename MatrixType_ >
template<typename Rhs , typename Dest >
void Eigen::SPQR< MatrixType_ >::_solve_impl ( const MatrixBase< Rhs > &  b,
MatrixBase< Dest > &  dest 
) const
inline
168  {
169  eigen_assert(m_isInitialized && " The QR factorization should be computed first, call compute()");
170  eigen_assert(b.cols() == 1 && "This method is for vectors only");
171 
172  // Compute Q^T * b
173  typename Dest::PlainObject y, y2;
174  y = matrixQ().transpose() * b;
175 
176  // Solves with the triangular matrix R
177  Index rk = this->rank();
178  y2 = y;
179  y.resize((std::max)(cols(), Index(y.rows())), y.cols());
180  y.topRows(rk) = this->matrixR().topLeftCorner(rk, rk).template triangularView<Upper>().solve(y2.topRows(rk));
181 
182  // Apply the column permutation
183  // colsPermutation() performs a copy of the permutation,
184  // so let's apply it manually:
185  for (Index i = 0; i < rk; ++i) dest.row(m_E[i]) = y.row(i);
186  for (Index i = rk; i < cols(); ++i) dest.row(m_E[i]).setZero();
187 
188  // y.bottomRows(y.rows()-rk).setZero();
189  // dest = colsPermutation() * y.topRows(cols());
190 
191  m_info = Success;
192  }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define eigen_assert(x)
Definition: Macros.h:910
Scalar * b
Definition: benchVecAdd.cpp:17
Index rank() const
Definition: SuiteSparseQRSupport.h:215
const MatrixType matrixR() const
Definition: SuiteSparseQRSupport.h:196
ComputationInfo m_info
Definition: SuiteSparseQRSupport.h:244
Index cols() const
Definition: SuiteSparseQRSupport.h:165
SPQRMatrixQReturnType< SPQR > matrixQ() const
Get an expression of the matrix Q.
Definition: SuiteSparseQRSupport.h:205
bool m_isInitialized
Definition: SparseSolverBase.h:110
#define max(a, b)
Definition: datatypes.h:23
@ Success
Definition: Constants.h:440
Scalar * y
Definition: level1_cplx_impl.h:128
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83

References b, Eigen::SPQR< MatrixType_ >::cols(), eigen_assert, i, Eigen::SPQR< MatrixType_ >::m_E, Eigen::SPQR< MatrixType_ >::m_info, Eigen::SparseSolverBase< SPQR< MatrixType_ > >::m_isInitialized, Eigen::SPQR< MatrixType_ >::matrixQ(), Eigen::SPQR< MatrixType_ >::matrixR(), max, Eigen::SPQR< MatrixType_ >::rank(), Eigen::DenseBase< Derived >::setZero(), Eigen::Success, and y.

◆ cholmodCommon()

template<typename MatrixType_ >
cholmod_common* Eigen::SPQR< MatrixType_ >::cholmodCommon ( ) const
inline
Returns
a pointer to the SPQR workspace
228 { return &m_cc; }

References Eigen::SPQR< MatrixType_ >::m_cc.

◆ cols()

◆ colsPermutation()

template<typename MatrixType_ >
PermutationType Eigen::SPQR< MatrixType_ >::colsPermutation ( ) const
inline

Get the permutation that was applied to columns of A.

207  {
208  eigen_assert(m_isInitialized && "Decomposition is not initialized.");
209  return PermutationType(m_E, m_cR->ncol);
210  }
Map< PermutationMatrix< Dynamic, Dynamic, StorageIndex > > PermutationType
Definition: SuiteSparseQRSupport.h:77

References eigen_assert, Eigen::SPQR< MatrixType_ >::m_cR, Eigen::SPQR< MatrixType_ >::m_E, and Eigen::SparseSolverBase< SPQR< MatrixType_ > >::m_isInitialized.

◆ compute()

template<typename MatrixType_ >
void Eigen::SPQR< MatrixType_ >::compute ( const MatrixType_ &  matrix)
inline
126  {
127  if (m_isInitialized) SPQR_free();
128 
130 
131  /* Compute the default threshold as in MatLab, see:
132  * Tim Davis, "Algorithm 915, SuiteSparseQR: Multifrontal Multithreaded Rank-Revealing
133  * Sparse QR Factorization, ACM Trans. on Math. Soft. 38(1), 2011, Page 8:3
134  */
135  RealScalar pivotThreshold = m_tolerance;
136  if (m_useDefaultThreshold) {
137  RealScalar max2Norm = 0.0;
138  for (int j = 0; j < mat.cols(); j++) max2Norm = numext::maxi(max2Norm, mat.col(j).norm());
139  if (numext::is_exactly_zero(max2Norm)) max2Norm = RealScalar(1);
140  pivotThreshold = 20 * (mat.rows() + mat.cols()) * max2Norm * NumTraits<RealScalar>::epsilon();
141  }
142  cholmod_sparse A;
143  A = viewAsCholmod(mat);
144  m_rows = matrix.rows();
145  m_rank = SuiteSparseQR<Scalar>(m_ordering, pivotThreshold, internal::convert_index<StorageIndex>(matrix.cols()), &A,
146  &m_cR, &m_E, &m_H, &m_HPinv, &m_HTau, &m_cc);
147 
148  if (!m_cR) {
150  m_isInitialized = false;
151  return;
152  }
153  m_info = Success;
154  m_isInitialized = true;
155  m_isRUpToDate = false;
156  }
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Index m_rows
Definition: SuiteSparseQRSupport.h:257
MatrixType_::RealScalar RealScalar
Definition: SuiteSparseQRSupport.h:74
Index m_rank
Definition: SuiteSparseQRSupport.h:254
RealScalar norm() const
Definition: SparseDot.h:88
Index cols() const
Definition: SparseMatrix.h:161
Index rows() const
Definition: SparseMatrix.h:159
@ NumericalIssue
Definition: Constants.h:442
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:926
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool is_exactly_zero(const X &x)
Definition: Meta.h:592
cholmod_sparse viewAsCholmod(Ref< SparseMatrix< Scalar_, Options_, StorageIndex_ > > mat)
Definition: CholmodSupport.h:64
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::cols(), Eigen::numext::is_exactly_zero(), j, Eigen::SPQR< MatrixType_ >::m_cc, Eigen::SPQR< MatrixType_ >::m_cR, Eigen::SPQR< MatrixType_ >::m_E, Eigen::SPQR< MatrixType_ >::m_H, Eigen::SPQR< MatrixType_ >::m_HPinv, Eigen::SPQR< MatrixType_ >::m_HTau, Eigen::SPQR< MatrixType_ >::m_info, Eigen::SparseSolverBase< SPQR< MatrixType_ > >::m_isInitialized, Eigen::SPQR< MatrixType_ >::m_isRUpToDate, Eigen::SPQR< MatrixType_ >::m_ordering, Eigen::SPQR< MatrixType_ >::m_rank, Eigen::SPQR< MatrixType_ >::m_rows, Eigen::SPQR< MatrixType_ >::m_tolerance, Eigen::SPQR< MatrixType_ >::m_useDefaultThreshold, matrix(), Eigen::numext::maxi(), Eigen::SparseMatrixBase< Derived >::norm(), Eigen::NumericalIssue, Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::rows(), Eigen::SPQR< MatrixType_ >::SPQR_free(), Eigen::Success, and Eigen::viewAsCholmod().

Referenced by Eigen::SPQR< MatrixType_ >::SPQR().

◆ info()

template<typename MatrixType_ >
ComputationInfo Eigen::SPQR< MatrixType_ >::info ( ) const
inline

Reports whether previous computation was successful.

Returns
Success if computation was successful, NumericalIssue if the sparse QR can not be computed
235  {
236  eigen_assert(m_isInitialized && "Decomposition is not initialized.");
237  return m_info;
238  }

References eigen_assert, Eigen::SPQR< MatrixType_ >::m_info, and Eigen::SparseSolverBase< SPQR< MatrixType_ > >::m_isInitialized.

◆ matrixQ()

template<typename MatrixType_ >
SPQRMatrixQReturnType<SPQR> Eigen::SPQR< MatrixType_ >::matrixQ ( void  ) const
inline

Get an expression of the matrix Q.

205 { return SPQRMatrixQReturnType<SPQR>(*this); }

Referenced by Eigen::SPQR< MatrixType_ >::_solve_impl().

◆ matrixR()

template<typename MatrixType_ >
const MatrixType Eigen::SPQR< MatrixType_ >::matrixR ( ) const
inline
Returns
the sparse triangular factor R. It is a sparse matrix
196  {
197  eigen_assert(m_isInitialized && " The QR factorization should be computed first, call compute()");
198  if (!m_isRUpToDate) {
199  m_R = viewAsEigen<Scalar, StorageIndex>(*m_cR);
200  m_isRUpToDate = true;
201  }
202  return m_R;
203  }
MatrixType m_R
Definition: SuiteSparseQRSupport.h:249

References eigen_assert, Eigen::SPQR< MatrixType_ >::m_cR, Eigen::SparseSolverBase< SPQR< MatrixType_ > >::m_isInitialized, Eigen::SPQR< MatrixType_ >::m_isRUpToDate, and Eigen::SPQR< MatrixType_ >::m_R.

Referenced by Eigen::SPQR< MatrixType_ >::_solve_impl().

◆ rank()

template<typename MatrixType_ >
Index Eigen::SPQR< MatrixType_ >::rank ( ) const
inline

Gets the rank of the matrix. It should be equal to matrixQR().cols if the matrix is full-rank

215  {
216  eigen_assert(m_isInitialized && "Decomposition is not initialized.");
217  return m_cc.SPQR_istat[4];
218  }

References eigen_assert, Eigen::SPQR< MatrixType_ >::m_cc, and Eigen::SparseSolverBase< SPQR< MatrixType_ > >::m_isInitialized.

Referenced by Eigen::SPQR< MatrixType_ >::_solve_impl().

◆ rows()

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

◆ setPivotThreshold()

template<typename MatrixType_ >
void Eigen::SPQR< MatrixType_ >::setPivotThreshold ( const RealScalar tol)
inline

Set the tolerance tol to treat columns with 2-norm < =tol as zero.

222  {
223  m_useDefaultThreshold = false;
224  m_tolerance = tol;
225  }

References Eigen::SPQR< MatrixType_ >::m_tolerance, and Eigen::SPQR< MatrixType_ >::m_useDefaultThreshold.

◆ setSPQROrdering()

template<typename MatrixType_ >
void Eigen::SPQR< MatrixType_ >::setSPQROrdering ( int  ord)
inline

Set the fill-reducing ordering method to be used.

220 { m_ordering = ord; }

References Eigen::SPQR< MatrixType_ >::m_ordering.

◆ SPQR_free()

template<typename MatrixType_ >
void Eigen::SPQR< MatrixType_ >::SPQR_free ( )
inline
118  {
119  cholmod_l_free_sparse(&m_H, &m_cc);
120  cholmod_l_free_sparse(&m_cR, &m_cc);
121  cholmod_l_free_dense(&m_HTau, &m_cc);
122  std::free(m_E);
123  std::free(m_HPinv);
124  }

References Eigen::SPQR< MatrixType_ >::m_cc, Eigen::SPQR< MatrixType_ >::m_cR, Eigen::SPQR< MatrixType_ >::m_E, Eigen::SPQR< MatrixType_ >::m_H, Eigen::SPQR< MatrixType_ >::m_HPinv, and Eigen::SPQR< MatrixType_ >::m_HTau.

Referenced by Eigen::SPQR< MatrixType_ >::compute(), and Eigen::SPQR< MatrixType_ >::~SPQR().

Friends And Related Function Documentation

◆ SPQR_QProduct

template<typename MatrixType_ >
template<typename , typename >
friend struct SPQR_QProduct
friend

Member Data Documentation

◆ m_allow_tol

template<typename MatrixType_ >
int Eigen::SPQR< MatrixType_ >::m_allow_tol
protected

◆ m_analysisIsOk

template<typename MatrixType_ >
bool Eigen::SPQR< MatrixType_ >::m_analysisIsOk
protected

◆ m_cc

◆ m_cR

◆ m_E

◆ m_factorizationIsOk

template<typename MatrixType_ >
bool Eigen::SPQR< MatrixType_ >::m_factorizationIsOk
protected

◆ m_H

template<typename MatrixType_ >
cholmod_sparse* Eigen::SPQR< MatrixType_ >::m_H = nullptr
mutableprotected

◆ m_HPinv

template<typename MatrixType_ >
StorageIndex* Eigen::SPQR< MatrixType_ >::m_HPinv = nullptr
mutableprotected

◆ m_HTau

template<typename MatrixType_ >
cholmod_dense* Eigen::SPQR< MatrixType_ >::m_HTau = nullptr
mutableprotected

◆ m_info

template<typename MatrixType_ >
ComputationInfo Eigen::SPQR< MatrixType_ >::m_info
mutableprotected

◆ m_isRUpToDate

template<typename MatrixType_ >
bool Eigen::SPQR< MatrixType_ >::m_isRUpToDate
mutableprotected

◆ m_ordering

template<typename MatrixType_ >
int Eigen::SPQR< MatrixType_ >::m_ordering
protected

◆ m_R

template<typename MatrixType_ >
MatrixType Eigen::SPQR< MatrixType_ >::m_R
mutableprotected

◆ m_rank

template<typename MatrixType_ >
Index Eigen::SPQR< MatrixType_ >::m_rank
mutableprotected

◆ m_rows

template<typename MatrixType_ >
Index Eigen::SPQR< MatrixType_ >::m_rows
protected

◆ m_tolerance

template<typename MatrixType_ >
RealScalar Eigen::SPQR< MatrixType_ >::m_tolerance
protected

◆ m_useDefaultThreshold

template<typename MatrixType_ >
bool Eigen::SPQR< MatrixType_ >::m_useDefaultThreshold
protected

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