10 #ifndef EIGEN_SUITESPARSEQRSUPPORT_H
11 #define EIGEN_SUITESPARSEQRSUPPORT_H
15 template<
typename MatrixType>
class SPQR;
16 template<
typename SPQRType>
struct SPQRMatrixQReturnType;
17 template<
typename SPQRType>
struct SPQRMatrixQTransposeReturnType;
18 template <
typename SPQRType,
typename Derived>
struct SPQR_QProduct;
20 template <
typename SPQRType>
struct traits<SPQRMatrixQReturnType<SPQRType> >
22 typedef typename SPQRType::MatrixType ReturnType;
24 template <
typename SPQRType>
struct traits<SPQRMatrixQTransposeReturnType<SPQRType> >
26 typedef typename SPQRType::MatrixType ReturnType;
28 template <
typename SPQRType,
typename Derived>
struct traits<SPQR_QProduct<SPQRType, Derived> >
30 typedef typename Derived::PlainObject ReturnType;
56 template<
typename _MatrixType>
60 typedef typename _MatrixType::Scalar Scalar;
61 typedef typename _MatrixType::RealScalar RealScalar;
62 typedef SuiteSparse_long Index ;
63 typedef SparseMatrix<Scalar, ColMajor, Index> MatrixType;
64 typedef PermutationMatrix<Dynamic, Dynamic> PermutationType;
67 : m_isInitialized(false), m_ordering(SPQR_ORDERING_DEFAULT), m_allow_tol(SPQR_DEFAULT_TOL), m_tolerance (NumTraits<Scalar>::epsilon()), m_useDefaultThreshold(true)
69 cholmod_l_start(&m_cc);
72 SPQR(
const _MatrixType& matrix)
73 : m_isInitialized(false), m_ordering(SPQR_ORDERING_DEFAULT), m_allow_tol(SPQR_DEFAULT_TOL), m_tolerance (NumTraits<Scalar>::epsilon()), m_useDefaultThreshold(true)
75 cholmod_l_start(&m_cc);
82 cholmod_l_finish(&m_cc);
86 cholmod_l_free_sparse(&m_H, &m_cc);
87 cholmod_l_free_sparse(&m_cR, &m_cc);
88 cholmod_l_free_dense(&m_HTau, &m_cc);
93 void compute(
const _MatrixType& matrix)
95 if(m_isInitialized) SPQR_free();
97 MatrixType mat(matrix);
103 RealScalar pivotThreshold = m_tolerance;
104 if(m_useDefaultThreshold)
107 RealScalar max2Norm = 0.0;
108 for (
int j = 0; j < mat.cols(); j++) max2Norm = (max)(max2Norm, mat.col(j).norm());
109 if(max2Norm==RealScalar(0))
110 max2Norm = RealScalar(1);
111 pivotThreshold = 20 * (mat.rows() + mat.cols()) * max2Norm * NumTraits<RealScalar>::epsilon();
116 Index col = matrix.cols();
117 m_rank = SuiteSparseQR<Scalar>(m_ordering, pivotThreshold, col, &A,
118 &m_cR, &m_E, &m_H, &m_HPinv, &m_HTau, &m_cc);
123 m_isInitialized =
false;
127 m_isInitialized =
true;
128 m_isRUpToDate =
false;
133 inline Index
rows()
const {
return m_cR->nrow; }
138 inline Index
cols()
const {
return m_cR->ncol; }
144 template<
typename Rhs>
147 eigen_assert(m_isInitialized &&
" The QR factorization should be computed first, call compute()");
148 eigen_assert(this->
rows()==B.rows()
149 &&
"SPQR::solve(): invalid number of rows of the right hand side matrix B");
150 return internal::solve_retval<SPQR, Rhs>(*
this, B.derived());
153 template<
typename Rhs,
typename Dest>
156 eigen_assert(m_isInitialized &&
" The QR factorization should be computed first, call compute()");
157 eigen_assert(b.cols()==1 &&
"This method is for vectors only");
160 typename Dest::PlainObject y, y2;
164 Index rk = this->
rank();
166 y.resize((std::max)(
cols(),Index(y.rows())),y.cols());
172 for(Index i = 0; i < rk; ++i) dest.
row(m_E[i]) = y.row(i);
173 for(Index i = rk; i <
cols(); ++i) dest.
row(m_E[i]).setZero();
185 eigen_assert(m_isInitialized &&
" The QR factorization should be computed first, call compute()");
187 m_R = viewAsEigen<Scalar,ColMajor, typename MatrixType::Index>(*m_cR);
188 m_isRUpToDate =
true;
195 return SPQRMatrixQReturnType<SPQR>(*this);
200 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
201 Index n = m_cR->ncol;
203 for(Index j = 0; j <n; j++) colsPerm.
indices()(j) = m_E[j];
213 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
214 return m_cc.SPQR_istat[4];
221 m_useDefaultThreshold =
false;
236 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
240 bool m_isInitialized;
242 bool m_factorizationIsOk;
243 mutable bool m_isRUpToDate;
247 RealScalar m_tolerance;
248 mutable cholmod_sparse *m_cR;
249 mutable MatrixType m_R;
251 mutable cholmod_sparse *m_H;
252 mutable Index *m_HPinv;
253 mutable cholmod_dense *m_HTau;
254 mutable Index m_rank;
255 mutable cholmod_common m_cc;
256 bool m_useDefaultThreshold;
257 template<
typename ,
typename >
friend struct SPQR_QProduct;
260 template <
typename SPQRType,
typename Derived>
261 struct SPQR_QProduct : ReturnByValue<SPQR_QProduct<SPQRType,Derived> >
263 typedef typename SPQRType::Scalar Scalar;
264 typedef typename SPQRType::Index Index;
266 SPQR_QProduct(
const SPQRType& spqr,
const Derived& other,
bool transpose) : m_spqr(spqr),m_other(other),m_transpose(transpose) {}
268 inline Index rows()
const {
return m_transpose ? m_spqr.rows() : m_spqr.cols(); }
269 inline Index cols()
const {
return m_other.cols(); }
271 template<
typename ResType>
272 void evalTo(ResType& res)
const
276 int method = m_transpose ? SPQR_QTX : SPQR_QX;
277 cholmod_common *cc = m_spqr.cholmodCommon();
279 x_cd = SuiteSparseQR_qmult<Scalar>(method, m_spqr.m_H, m_spqr.m_HTau, m_spqr.m_HPinv, &y_cd, cc);
280 res = Matrix<Scalar,ResType::RowsAtCompileTime,ResType::ColsAtCompileTime>::Map(reinterpret_cast<Scalar*>(x_cd->x), x_cd->nrow, x_cd->ncol);
281 cholmod_l_free_dense(&x_cd, cc);
283 const SPQRType& m_spqr;
284 const Derived& m_other;
288 template<
typename SPQRType>
289 struct SPQRMatrixQReturnType{
291 SPQRMatrixQReturnType(
const SPQRType& spqr) : m_spqr(spqr) {}
292 template<
typename Derived>
293 SPQR_QProduct<SPQRType, Derived>
operator*(
const MatrixBase<Derived>& other)
295 return SPQR_QProduct<SPQRType,Derived>(m_spqr,other.derived(),
false);
297 SPQRMatrixQTransposeReturnType<SPQRType> adjoint()
const
299 return SPQRMatrixQTransposeReturnType<SPQRType>(m_spqr);
302 SPQRMatrixQTransposeReturnType<SPQRType> transpose()
const
304 return SPQRMatrixQTransposeReturnType<SPQRType>(m_spqr);
306 const SPQRType& m_spqr;
309 template<
typename SPQRType>
310 struct SPQRMatrixQTransposeReturnType{
311 SPQRMatrixQTransposeReturnType(
const SPQRType& spqr) : m_spqr(spqr) {}
312 template<
typename Derived>
313 SPQR_QProduct<SPQRType,Derived>
operator*(
const MatrixBase<Derived>& other)
315 return SPQR_QProduct<SPQRType,Derived>(m_spqr,other.derived(),
true);
317 const SPQRType& m_spqr;
322 template<
typename _MatrixType,
typename Rhs>
323 struct solve_retval<SPQR<_MatrixType>, Rhs>
324 : solve_retval_base<SPQR<_MatrixType>, Rhs>
326 typedef SPQR<_MatrixType> Dec;
327 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
329 template<typename Dest>
void evalTo(Dest& dst)
const
331 dec()._solve(rhs(),dst);
RowXpr row(Index i)
Definition: DenseBase.h:750
const IndicesType & indices() const
Definition: PermutationMatrix.h:387
const internal::solve_retval< SPQR, Rhs > solve(const MatrixBase< Rhs > &B) const
Definition: SuiteSparseQRSupport.h:145
Definition: Constants.h:378
Index rows() const
Definition: SuiteSparseQRSupport.h:133
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SuiteSparseQRSupport.h:234
const internal::permut_matrix_product_retval< PermutationDerived, Derived, OnTheRight > operator*(const MatrixBase< Derived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:539
SPQRMatrixQReturnType< SPQR > matrixQ() const
Get an expression of the matrix Q.
Definition: SuiteSparseQRSupport.h:193
cholmod_common * cholmodCommon() const
Definition: SuiteSparseQRSupport.h:226
Permutation matrix.
Definition: PermutationMatrix.h:312
cholmod_sparse viewAsCholmod(SparseMatrix< _Scalar, _Options, _Index > &mat)
Definition: CholmodSupport.h:52
void setSPQROrdering(int ord)
Set the fill-reducing ordering method to be used.
Definition: SuiteSparseQRSupport.h:217
void setPivotThreshold(const RealScalar &tol)
Set the tolerance tol to treat columns with 2-norm < =tol as zero.
Definition: SuiteSparseQRSupport.h:219
Index cols() const
Definition: SuiteSparseQRSupport.h:138
const MatrixType matrixR() const
Definition: SuiteSparseQRSupport.h:183
Sparse QR factorization based on SuiteSparseQR library.
Definition: SuiteSparseQRSupport.h:15
Index rank() const
Definition: SuiteSparseQRSupport.h:211
Definition: Constants.h:376
Block< Derived > topLeftCorner(Index cRows, Index cCols)
Definition: SparseMatrixBase.h:157
ComputationInfo
Definition: Constants.h:374
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
PermutationType colsPermutation() const
Get the permutation that was applied to columns of A.
Definition: SuiteSparseQRSupport.h:198