10 #ifndef EIGEN_SUITESPARSEQRSUPPORT_H 11 #define EIGEN_SUITESPARSEQRSUPPORT_H 15 template<
typename MatrixType>
class SPQR;
22 typedef typename SPQRType::MatrixType ReturnType;
26 typedef typename SPQRType::MatrixType ReturnType;
30 typedef typename Derived::PlainObject ReturnType;
56 template<
typename _MatrixType>
61 typedef typename _MatrixType::RealScalar RealScalar;
62 typedef SuiteSparse_long Index ;
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);
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");
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;
161 y = matrixQ().transpose() * b;
164 Index rk = this->rank();
166 y.resize((std::max)(cols(),Index(y.rows())),y.cols());
167 y.topRows(rk) = this->matrixR().topLeftCorner(rk, rk).template triangularView<Upper>().solve(y2.topRows(rk));
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;
200 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
201 Index n = m_cR->ncol;
202 PermutationType colsPerm(n);
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;
260 template <
typename SPQRType,
typename Derived>
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);
281 cholmod_l_free_dense(&x_cd, cc);
283 const SPQRType& m_spqr;
284 const Derived& m_other;
288 template<
typename SPQRType>
292 template<
typename Derived>
306 const SPQRType& m_spqr;
309 template<
typename SPQRType>
312 template<
typename Derived>
317 const SPQRType& m_spqr;
322 template<
typename _MatrixType,
typename Rhs>
327 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
329 template<
typename Dest>
void evalTo(Dest& dst)
const 331 dec()._solve(rhs(),dst);
const MatrixType matrixR() const
Definition: SuiteSparseQRSupport.h:183
Definition: ForwardDeclarations.h:124
const internal::solve_retval< SPQR, Rhs > solve(const MatrixBase< Rhs > &B) const
Definition: SuiteSparseQRSupport.h:145
Index rank() const
Gets the rank of the matrix.
Definition: SuiteSparseQRSupport.h:211
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
Index cols() const
Get the number of columns of the input matrix.
Definition: SuiteSparseQRSupport.h:138
Holds information about the various numeric (i.e.
Definition: NumTraits.h:88
RowXpr row(Index i)
Definition: DenseBase.h:750
const internal::permut_matrix_product_retval< PermutationDerived, Derived, OnTheRight > operator*(const MatrixBase< Derived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:539
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SuiteSparseQRSupport.h:234
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:219
cholmod_sparse viewAsCholmod(SparseMatrix< _Scalar, _Options, _Index > &mat)
Wraps the Eigen sparse matrix mat into a Cholmod sparse matrix object.
Definition: CholmodSupport.h:52
Definition: SuiteSparseQRSupport.h:18
The provided data did not satisfy the prerequisites.
Definition: Constants.h:378
SPQRMatrixQReturnType< SPQR > matrixQ() const
Get an expression of the matrix Q.
Definition: SuiteSparseQRSupport.h:193
Definition: SuiteSparseQRSupport.h:16
Computation was successful.
Definition: Constants.h:376
Index rows() const
Get the number of rows of the input matrix and the Q matrix.
Definition: SuiteSparseQRSupport.h:133
Definition: BandTriangularSolver.h:13
PermutationType colsPermutation() const
Get the permutation that was applied to columns of A.
Definition: SuiteSparseQRSupport.h:198
const IndicesType & indices() const
const version of indices().
Definition: PermutationMatrix.h:387
void setSPQROrdering(int ord)
Set the fill-reducing ordering method to be used.
Definition: SuiteSparseQRSupport.h:217
Definition: ForwardDeclarations.h:125
Sparse QR factorization based on SuiteSparseQR library.
Definition: SuiteSparseQRSupport.h:15
Definition: SuiteSparseQRSupport.h:17
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:374
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: ForwardDeclarations.h:17
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48
cholmod_common * cholmodCommon() const
Definition: SuiteSparseQRSupport.h:226