11#ifndef EIGEN_SUITESPARSEQRSUPPORT_H
12#define EIGEN_SUITESPARSEQRSUPPORT_H
15#include "./InternalHeaderCheck.h"
19template <
typename MatrixType>
21template <
typename SPQRType>
22struct SPQRMatrixQReturnType;
23template <
typename SPQRType>
24struct SPQRMatrixQTransposeReturnType;
25template <
typename SPQRType,
typename Derived>
28template <
typename SPQRType>
29struct traits<SPQRMatrixQReturnType<SPQRType> > {
30 typedef typename SPQRType::MatrixType ReturnType;
32template <
typename SPQRType>
33struct traits<SPQRMatrixQTransposeReturnType<SPQRType> > {
34 typedef typename SPQRType::MatrixType ReturnType;
36template <
typename SPQRType,
typename Derived>
37struct traits<SPQR_QProduct<SPQRType, Derived> > {
38 typedef typename Derived::PlainObject ReturnType;
66template <
typename MatrixType_>
70 using Base::m_isInitialized;
73 typedef typename MatrixType_::Scalar Scalar;
74 typedef typename MatrixType_::RealScalar RealScalar;
75 typedef SuiteSparse_long StorageIndex;
78 enum { ColsAtCompileTime =
Dynamic, MaxColsAtCompileTime =
Dynamic };
82 : m_analysisIsOk(
false),
83 m_factorizationIsOk(
false),
85 m_ordering(SPQR_ORDERING_DEFAULT),
86 m_allow_tol(SPQR_DEFAULT_TOL),
93 m_useDefaultThreshold(
true) {
94 cholmod_l_start(&m_cc);
97 explicit SPQR(
const MatrixType_& matrix)
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),
109 m_useDefaultThreshold(
true) {
110 cholmod_l_start(&m_cc);
116 cholmod_l_finish(&m_cc);
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);
126 void compute(
const MatrixType_& matrix) {
127 if (m_isInitialized) SPQR_free();
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);
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);
150 m_isInitialized =
false;
154 m_isInitialized =
true;
155 m_isRUpToDate =
false;
167 template <
typename Rhs,
typename Dest>
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");
173 typename Dest::PlainObject y, y2;
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));
185 for (
Index i = 0; i < rk; ++i) dest.row(m_E[i]) = y.row(i);
197 eigen_assert(m_isInitialized &&
" The QR factorization should be computed first, call compute()");
198 if (!m_isRUpToDate) {
200 m_isRUpToDate =
true;
205 SPQRMatrixQReturnType<SPQR>
matrixQ()
const {
return SPQRMatrixQReturnType<SPQR>(*
this); }
208 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
216 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
217 return m_cc.SPQR_istat[4];
223 m_useDefaultThreshold =
false;
236 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
242 bool m_factorizationIsOk;
243 mutable bool m_isRUpToDate;
247 RealScalar m_tolerance;
248 mutable cholmod_sparse* m_cR =
nullptr;
249 mutable MatrixType m_R;
250 mutable StorageIndex* m_E =
nullptr;
251 mutable cholmod_sparse* m_H =
nullptr;
252 mutable StorageIndex* m_HPinv =
nullptr;
253 mutable cholmod_dense* m_HTau =
nullptr;
254 mutable Index m_rank;
255 mutable cholmod_common m_cc;
256 bool m_useDefaultThreshold;
258 template <
typename,
typename>
259 friend struct SPQR_QProduct;
262template <
typename SPQRType,
typename Derived>
263struct SPQR_QProduct : ReturnByValue<SPQR_QProduct<SPQRType, Derived> > {
264 typedef typename SPQRType::Scalar Scalar;
265 typedef typename SPQRType::StorageIndex StorageIndex;
267 SPQR_QProduct(
const SPQRType& spqr,
const Derived& other,
bool transpose)
268 : m_spqr(spqr), m_other(other), m_transpose(transpose) {}
270 inline Index rows()
const {
return m_transpose ? m_spqr.rows() : m_spqr.cols(); }
271 inline Index cols()
const {
return m_other.cols(); }
273 template <
typename ResType>
274 void evalTo(ResType& res)
const {
277 int method = m_transpose ? SPQR_QTX : SPQR_QX;
278 cholmod_common* cc = m_spqr.cholmodCommon();
280 x_cd = SuiteSparseQR_qmult<Scalar>(method, m_spqr.m_H, m_spqr.m_HTau, m_spqr.m_HPinv, &y_cd, cc);
281 res = Matrix<Scalar, ResType::RowsAtCompileTime, ResType::ColsAtCompileTime>::Map(
282 reinterpret_cast<Scalar*
>(x_cd->x), x_cd->nrow, x_cd->ncol);
283 cholmod_l_free_dense(&x_cd, cc);
285 const SPQRType& m_spqr;
286 const Derived& m_other;
289template <
typename SPQRType>
290struct SPQRMatrixQReturnType {
291 SPQRMatrixQReturnType(
const SPQRType& spqr) : m_spqr(spqr) {}
292 template <
typename Derived>
293 SPQR_QProduct<SPQRType, Derived> operator*(
const MatrixBase<Derived>& other) {
294 return SPQR_QProduct<SPQRType, Derived>(m_spqr, other.derived(),
false);
296 SPQRMatrixQTransposeReturnType<SPQRType> adjoint()
const {
return SPQRMatrixQTransposeReturnType<SPQRType>(m_spqr); }
298 SPQRMatrixQTransposeReturnType<SPQRType> transpose()
const {
299 return SPQRMatrixQTransposeReturnType<SPQRType>(m_spqr);
301 const SPQRType& m_spqr;
304template <
typename SPQRType>
305struct SPQRMatrixQTransposeReturnType {
306 SPQRMatrixQTransposeReturnType(
const SPQRType& spqr) : m_spqr(spqr) {}
307 template <
typename Derived>
308 SPQR_QProduct<SPQRType, Derived> operator*(
const MatrixBase<Derived>& other) {
309 return SPQR_QProduct<SPQRType, Derived>(m_spqr, other.derived(),
true);
311 const SPQRType& m_spqr;
Derived & setZero()
Definition CwiseNullaryOp.h:549
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition EigenBase.h:61
A matrix or vector expression mapping an existing array of data.
Definition Map.h:96
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
Sparse QR factorization based on SuiteSparseQR library.
Definition SuiteSparseQRSupport.h:67
void setPivotThreshold(const RealScalar &tol)
Set the tolerance tol to treat columns with 2-norm < =tol as zero.
Definition SuiteSparseQRSupport.h:222
Index rank() const
Definition SuiteSparseQRSupport.h:215
cholmod_common * cholmodCommon() const
Definition SuiteSparseQRSupport.h:228
SPQRMatrixQReturnType< SPQR > matrixQ() const
Get an expression of the matrix Q.
Definition SuiteSparseQRSupport.h:205
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SuiteSparseQRSupport.h:235
Index rows() const
Definition SuiteSparseQRSupport.h:160
const MatrixType matrixR() const
Definition SuiteSparseQRSupport.h:196
Index cols() const
Definition SuiteSparseQRSupport.h:165
PermutationType colsPermutation() const
Get the permutation that was applied to columns of A.
Definition SuiteSparseQRSupport.h:207
void setSPQROrdering(int ord)
Set the fill-reducing ordering method to be used.
Definition SuiteSparseQRSupport.h:220
A versatible sparse matrix representation.
Definition SparseUtil.h:47
Index cols() const
Definition SparseMatrix.h:161
Index rows() const
Definition SparseMatrix.h:159
A base class for sparse solvers.
Definition SparseSolverBase.h:67
ComputationInfo
Definition Constants.h:438
@ NumericalIssue
Definition Constants.h:442
@ Success
Definition Constants.h:440
Namespace containing all symbols from the Eigen library.
Definition Core:137
Map< const SparseMatrix< Scalar, ColMajor, StorageIndex > > viewAsEigen(cholmod_sparse &cm)
Definition CholmodSupport.h:153
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
cholmod_sparse viewAsCholmod(Ref< SparseMatrix< Scalar_, Options_, StorageIndex_ > > mat)
Definition CholmodSupport.h:64
const int Dynamic
Definition Constants.h:25
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523