11 #ifndef EIGEN_SUITESPARSEQRSUPPORT_H 12 #define EIGEN_SUITESPARSEQRSUPPORT_H 16 template<
typename MatrixType>
class SPQR;
23 typedef typename SPQRType::MatrixType ReturnType;
27 typedef typename SPQRType::MatrixType ReturnType;
31 typedef typename Derived::PlainObject ReturnType;
59 template<
typename _MatrixType>
64 using Base::m_isInitialized;
66 typedef typename _MatrixType::Scalar Scalar;
67 typedef typename _MatrixType::RealScalar RealScalar;
68 typedef SuiteSparse_long StorageIndex ;
77 : m_ordering(SPQR_ORDERING_DEFAULT), m_allow_tol(SPQR_DEFAULT_TOL), m_tolerance (
NumTraits<Scalar>::epsilon()), m_useDefaultThreshold(
true)
79 cholmod_l_start(&m_cc);
82 explicit SPQR(
const _MatrixType& matrix)
83 : m_ordering(SPQR_ORDERING_DEFAULT), m_allow_tol(SPQR_DEFAULT_TOL), m_tolerance (
NumTraits<Scalar>::epsilon()), m_useDefaultThreshold(
true)
85 cholmod_l_start(&m_cc);
92 cholmod_l_finish(&m_cc);
96 cholmod_l_free_sparse(&m_H, &m_cc);
97 cholmod_l_free_sparse(&m_cR, &m_cc);
98 cholmod_l_free_dense(&m_HTau, &m_cc);
103 void compute(
const _MatrixType& matrix)
105 if(m_isInitialized) SPQR_free();
107 MatrixType mat(matrix);
113 RealScalar pivotThreshold = m_tolerance;
114 if(m_useDefaultThreshold)
116 RealScalar max2Norm = 0.0;
117 for (
int j = 0; j < mat.cols(); j++) max2Norm = numext::maxi(max2Norm, mat.col(j).norm());
118 if(max2Norm==RealScalar(0))
119 max2Norm = RealScalar(1);
124 m_rows = matrix.rows();
125 Index col = matrix.cols();
126 m_rank = SuiteSparseQR<Scalar>(m_ordering, pivotThreshold, col, &A,
127 &m_cR, &m_E, &m_H, &m_HPinv, &m_HTau, &m_cc);
132 m_isInitialized =
false;
136 m_isInitialized =
true;
137 m_isRUpToDate =
false;
149 template<
typename Rhs,
typename Dest>
152 eigen_assert(m_isInitialized &&
" The QR factorization should be computed first, call compute()");
153 eigen_assert(b.cols()==1 &&
"This method is for vectors only");
156 typename Dest::PlainObject y, y2;
157 y = matrixQ().transpose() * b;
160 Index rk = this->rank();
162 y.resize((std::max)(cols(),
Index(y.rows())),y.cols());
163 y.topRows(rk) = this->matrixR().topLeftCorner(rk, rk).template triangularView<Upper>().solve(y2.topRows(rk));
168 for(
Index i = 0; i < rk; ++i) dest.
row(m_E[i]) = y.row(i);
169 for(
Index i = rk; i < cols(); ++i) dest.
row(m_E[i]).setZero();
181 eigen_assert(m_isInitialized &&
" The QR factorization should be computed first, call compute()");
183 m_R = viewAsEigen<Scalar,ColMajor, typename MatrixType::StorageIndex>(*m_cR);
184 m_isRUpToDate =
true;
196 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
197 return PermutationType(m_E, m_cR->ncol);
205 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
206 return m_cc.SPQR_istat[4];
213 m_useDefaultThreshold =
false;
228 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
233 bool m_factorizationIsOk;
234 mutable bool m_isRUpToDate;
238 RealScalar m_tolerance;
239 mutable cholmod_sparse *m_cR;
240 mutable MatrixType m_R;
241 mutable StorageIndex *m_E;
242 mutable cholmod_sparse *m_H;
243 mutable StorageIndex *m_HPinv;
244 mutable cholmod_dense *m_HTau;
245 mutable Index m_rank;
246 mutable cholmod_common m_cc;
247 bool m_useDefaultThreshold;
252 template <
typename SPQRType,
typename Derived>
255 typedef typename SPQRType::Scalar Scalar;
256 typedef typename SPQRType::StorageIndex StorageIndex;
258 SPQR_QProduct(
const SPQRType& spqr,
const Derived& other,
bool transpose) : m_spqr(spqr),m_other(other),m_transpose(transpose) {}
260 inline Index rows()
const {
return m_transpose ? m_spqr.rows() : m_spqr.cols(); }
261 inline Index cols()
const {
return m_other.cols(); }
263 template<
typename ResType>
264 void evalTo(ResType& res)
const 268 int method = m_transpose ? SPQR_QTX : SPQR_QX;
269 cholmod_common *cc = m_spqr.cholmodCommon();
271 x_cd = SuiteSparseQR_qmult<Scalar>(method, m_spqr.m_H, m_spqr.m_HTau, m_spqr.m_HPinv, &y_cd, cc);
273 cholmod_l_free_dense(&x_cd, cc);
275 const SPQRType& m_spqr;
276 const Derived& m_other;
280 template<
typename SPQRType>
284 template<
typename Derived>
298 const SPQRType& m_spqr;
301 template<
typename SPQRType>
304 template<
typename Derived>
309 const SPQRType& m_spqr;
const MatrixType matrixR() const
Definition: SuiteSparseQRSupport.h:179
Index rank() const
Gets the rank of the matrix.
Definition: SuiteSparseQRSupport.h:203
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Index cols() const
Get the number of columns of the input matrix.
Definition: SuiteSparseQRSupport.h:147
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SuiteSparseQRSupport.h:226
Definition: ReturnByValue.h:50
void setPivotThreshold(const RealScalar &tol)
Set the tolerance tol to treat columns with 2-norm < =tol as zero.
Definition: SuiteSparseQRSupport.h:211
Definition: SuiteSparseQRSupport.h:19
The provided data did not satisfy the prerequisites.
Definition: Constants.h:434
SPQRMatrixQReturnType< SPQR > matrixQ() const
Get an expression of the matrix Q.
Definition: SuiteSparseQRSupport.h:189
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: SuiteSparseQRSupport.h:17
cholmod_sparse viewAsCholmod(Ref< SparseMatrix< _Scalar, _Options, _StorageIndex > > mat)
Wraps the Eigen sparse matrix mat into a Cholmod sparse matrix object.
Definition: CholmodSupport.h:58
Computation was successful.
Definition: Constants.h:432
Index rows() const
Get the number of rows of the input matrix and the Q matrix.
Definition: SuiteSparseQRSupport.h:142
Definition: BandTriangularSolver.h:13
PermutationType colsPermutation() const
Get the permutation that was applied to columns of A.
Definition: SuiteSparseQRSupport.h:194
void setSPQROrdering(int ord)
Set the fill-reducing ordering method to be used.
Definition: SuiteSparseQRSupport.h:209
Sparse QR factorization based on SuiteSparseQR library.
Definition: SuiteSparseQRSupport.h:16
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
EIGEN_DEVICE_FUNC RowXpr row(Index i)
Definition: DenseBase.h:860
Definition: SuiteSparseQRSupport.h:18
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:430
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: ForwardDeclarations.h:17
cholmod_common * cholmodCommon() const
Definition: SuiteSparseQRSupport.h:218