12#ifndef EIGEN_SUITESPARSEQRSUPPORT_H
13#define EIGEN_SUITESPARSEQRSUPPORT_H
16#include "./InternalHeaderCheck.h"
20template <
typename MatrixType>
22template <
typename SPQRType>
23struct SPQRMatrixQReturnType;
24template <
typename SPQRType>
25struct SPQRMatrixQTransposeReturnType;
26template <
typename SPQRType,
typename Derived>
29template <
typename SPQRType>
30struct traits<SPQRMatrixQReturnType<SPQRType> > {
31 using ReturnType =
typename SPQRType::MatrixType;
32 using Scalar =
typename ReturnType::Scalar;
33 using StorageIndex =
typename ReturnType::StorageIndex;
34 using StorageKind =
typename ReturnType::StorageKind;
35 static constexpr int RowsAtCompileTime = Dynamic;
36 static constexpr int ColsAtCompileTime = Dynamic;
38template <
typename SPQRType>
39struct traits<SPQRMatrixQTransposeReturnType<SPQRType> > {
40 using ReturnType =
typename SPQRType::MatrixType;
42template <
typename SPQRType,
typename Derived>
43struct traits<SPQR_QProduct<SPQRType, Derived> > {
44 using ReturnType =
typename Derived::PlainObject;
72template <
typename MatrixType_>
76 using Base::m_isInitialized;
79 using Scalar =
typename MatrixType_::Scalar;
80 using RealScalar =
typename MatrixType_::RealScalar;
81 using StorageIndex = SuiteSparse_long;
84 enum { ColsAtCompileTime = Dynamic, MaxColsAtCompileTime = Dynamic };
88 : m_analysisIsOk(
false),
89 m_factorizationIsOk(
false),
91 m_ordering(SPQR_ORDERING_DEFAULT),
92 m_allow_tol(SPQR_DEFAULT_TOL),
93 m_tolerance(NumTraits<Scalar>::epsilon()),
99 m_useDefaultThreshold(
true) {
100 cholmod_l_start(&m_cc);
103 explicit SPQR(
const MatrixType_& matrix)
104 : m_analysisIsOk(
false),
105 m_factorizationIsOk(
false),
106 m_isRUpToDate(
false),
107 m_ordering(SPQR_ORDERING_DEFAULT),
108 m_allow_tol(SPQR_DEFAULT_TOL),
109 m_tolerance(NumTraits<Scalar>::epsilon()),
115 m_useDefaultThreshold(
true) {
116 cholmod_l_start(&m_cc);
122 cholmod_l_finish(&m_cc);
125 cholmod_l_free_sparse(&m_H, &m_cc);
126 cholmod_l_free_sparse(&m_cR, &m_cc);
127 cholmod_l_free_dense(&m_HTau, &m_cc);
134 void compute(
const MatrixType_& matrix) {
135 if (m_isInitialized) SPQR_free();
137 MatrixType mat(matrix);
143 RealScalar pivotThreshold = m_tolerance;
144 if (m_useDefaultThreshold) {
145 RealScalar max2Norm = 0.0;
146 for (
int j = 0; j < mat.
cols(); j++) max2Norm = numext::maxi(max2Norm, mat.
col(j).norm());
147 if (numext::is_exactly_zero(max2Norm)) max2Norm = RealScalar(1);
148 pivotThreshold = 20 * (mat.
rows() + mat.
cols()) * max2Norm * NumTraits<RealScalar>::epsilon();
151 A = viewAsCholmod(mat);
152 m_rows = matrix.rows();
153 m_rank = SuiteSparseQR<Scalar>(m_ordering, pivotThreshold, internal::convert_index<StorageIndex>(matrix.cols()), &A,
154 &m_cR, &m_E, &m_H, &m_HPinv, &m_HTau, &m_cc);
158 m_isInitialized =
false;
161 if (!m_E && !initIdentityPermutation(m_cR->ncol)) {
164 m_isInitialized =
false;
168 m_isInitialized =
true;
169 m_isRUpToDate =
false;
174 inline Index
rows()
const {
return m_rows; }
179 inline Index
cols()
const {
return m_cR->ncol; }
181 template <
typename Rhs,
typename Dest>
183 eigen_assert(m_isInitialized &&
" The QR factorization should be computed first, call compute()");
184 eigen_assert(b.cols() == 1 &&
"This method is for vectors only");
187 typename Dest::PlainObject y, y2;
191 Index rk = this->
rank();
193 y.resize((std::max)(
cols(), Index(y.rows())), y.cols());
194 y.topRows(rk) = this->
matrixR().
topLeftCorner(rk, rk).template triangularView<Upper>().solve(y2.topRows(rk));
199 for (Index i = 0; i < rk; ++i) dest.
row(m_E[i]) = y.row(i);
200 for (Index i = rk; i <
cols(); ++i) dest.
row(m_E[i]).setZero();
208 eigen_assert(m_isInitialized &&
" The QR factorization should be computed first, call compute()");
209 if (!m_isRUpToDate) {
210 m_R = viewAsEigen<Scalar, StorageIndex>(*m_cR);
211 m_isRUpToDate =
true;
216 SPQRMatrixQReturnType<SPQR>
matrixQ()
const {
return SPQRMatrixQReturnType<SPQR>(*
this); }
219 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
220 return PermutationType(m_E, m_cR->ncol);
228 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
229 return m_cc.SPQR_istat[4];
235 m_useDefaultThreshold =
false;
248 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
254 bool m_factorizationIsOk;
255 mutable bool m_isRUpToDate;
259 RealScalar m_tolerance;
260 mutable cholmod_sparse* m_cR =
nullptr;
261 mutable MatrixType m_R;
262 mutable StorageIndex* m_E =
nullptr;
263 mutable cholmod_sparse* m_H =
nullptr;
264 mutable StorageIndex* m_HPinv =
nullptr;
265 mutable cholmod_dense* m_HTau =
nullptr;
266 mutable Index m_rank;
267 mutable cholmod_common m_cc;
268 bool m_useDefaultThreshold;
271 bool initIdentityPermutation(StorageIndex size) {
272 if (m_E || size == 0)
return true;
274 m_E =
static_cast<StorageIndex*
>(std::malloc(
sizeof(StorageIndex) * size));
275 if (!m_E)
return false;
276 for (StorageIndex i = 0; i < size; ++i) m_E[i] = i;
280 template <
typename,
typename>
281 friend struct SPQR_QProduct;
284template <
typename SPQRType,
typename Derived>
285struct SPQR_QProduct : ReturnByValue<SPQR_QProduct<SPQRType, Derived> > {
286 using Scalar =
typename SPQRType::Scalar;
287 using StorageIndex =
typename SPQRType::StorageIndex;
289 SPQR_QProduct(
const SPQRType& spqr,
const Derived& other,
bool transpose)
290 : m_spqr(spqr), m_other(other), m_transpose(transpose) {}
292 inline Index rows()
const {
return m_spqr.rows(); }
293 inline Index cols()
const {
return m_other.cols(); }
295 template <
typename ResType>
296 void evalTo(ResType& res)
const {
297 evalToImpl(res, m_other);
301 template <
typename ResType,
typename OtherDerived>
302 void evalToImpl(ResType& res,
const MatrixBase<OtherDerived>& otherExpr)
const {
305 int method = m_transpose ? SPQR_QTX : SPQR_QX;
306 cholmod_common* cc = m_spqr.cholmodCommon();
310 EIGEN_STATIC_ASSERT((std::is_same<Scalar, typename OtherDerived::Scalar>::value), YOU_MIXED_DIFFERENT_NUMERIC_TYPES)
311 Ref<const Matrix<Scalar, Dynamic, Dynamic, ColMajor> > other(otherExpr.derived());
312 y_cd = viewAsCholmod(other);
313 x_cd = SuiteSparseQR_qmult<Scalar>(method, m_spqr.m_H, m_spqr.m_HTau, m_spqr.m_HPinv, &y_cd, cc);
314 res = Matrix<Scalar, ResType::RowsAtCompileTime, ResType::ColsAtCompileTime>::Map(
315 reinterpret_cast<Scalar*
>(x_cd->x), x_cd->nrow, x_cd->ncol);
316 cholmod_l_free_dense(&x_cd, cc);
319 template <
typename ResType,
typename OtherDerived>
320 void evalToImpl(ResType& res,
const SparseMatrixBase<OtherDerived>& otherExpr)
const {
322 cholmod_sparse* x_cs;
323 int method = m_transpose ? SPQR_QTX : SPQR_QX;
324 cholmod_common* cc = m_spqr.cholmodCommon();
325 typename OtherDerived::PlainObject other = otherExpr;
326 other.makeCompressed();
327 y_cs = viewAsCholmod(other);
328 x_cs = SuiteSparseQR_qmult<Scalar>(method, m_spqr.m_H, m_spqr.m_HTau, m_spqr.m_HPinv, &y_cs, cc);
329 res = viewAsEigen<Scalar, StorageIndex>(*x_cs);
330 cholmod_l_free_sparse(&x_cs, cc);
333 template <
typename ResType,
typename OtherScalar,
int OtherOptions,
typename OtherStorageIndex,
334 std::enable_if_t<std::is_same<OtherStorageIndex, StorageIndex>::value,
int> = 0>
335 void evalToImpl(ResType& res,
const SparseMatrix<OtherScalar, OtherOptions, OtherStorageIndex>& otherExpr)
const {
337 cholmod_sparse* x_cs;
338 int method = m_transpose ? SPQR_QTX : SPQR_QX;
339 cholmod_common* cc = m_spqr.cholmodCommon();
340 const SparseMatrix<OtherScalar, OtherOptions, OtherStorageIndex>* otherPtr = &otherExpr;
341 SparseMatrix<OtherScalar, OtherOptions, OtherStorageIndex> other;
343 if (!otherExpr.isCompressed()) {
345 other.makeCompressed();
349 y_cs = viewAsCholmod(*otherPtr);
350 x_cs = SuiteSparseQR_qmult<Scalar>(method, m_spqr.m_H, m_spqr.m_HTau, m_spqr.m_HPinv, &y_cs, cc);
351 res = viewAsEigen<Scalar, StorageIndex>(*x_cs);
352 cholmod_l_free_sparse(&x_cs, cc);
356 const SPQRType& m_spqr;
357 const Derived& m_other;
360template <
typename SPQRType>
361struct SPQRMatrixQReturnType :
public EigenBase<SPQRMatrixQReturnType<SPQRType> > {
362 using Scalar =
typename SPQRType::Scalar;
363 static constexpr int RowsAtCompileTime = Dynamic;
364 static constexpr int ColsAtCompileTime = Dynamic;
365 SPQRMatrixQReturnType(
const SPQRType& spqr) : m_spqr(spqr) {}
366 template <
typename Derived>
367 SPQR_QProduct<SPQRType, Derived> operator*(
const MatrixBase<Derived>& other) {
368 return SPQR_QProduct<SPQRType, Derived>(m_spqr, other.derived(),
false);
370 template <
typename Derived>
371 SPQR_QProduct<SPQRType, Derived> operator*(
const SparseMatrixBase<Derived>& other) {
372 return SPQR_QProduct<SPQRType, Derived>(m_spqr, other.derived(),
false);
374 SPQRMatrixQTransposeReturnType<SPQRType> adjoint()
const {
return SPQRMatrixQTransposeReturnType<SPQRType>(m_spqr); }
375 inline Index rows()
const {
return m_spqr.rows(); }
376 inline Index cols()
const {
return m_spqr.rows(); }
378 SPQRMatrixQTransposeReturnType<SPQRType> transpose()
const {
379 return SPQRMatrixQTransposeReturnType<SPQRType>(m_spqr);
381 const SPQRType& m_spqr;
384template <
typename SPQRType>
385struct SPQRMatrixQTransposeReturnType {
386 SPQRMatrixQTransposeReturnType(
const SPQRType& spqr) : m_spqr(spqr) {}
387 template <
typename Derived>
388 SPQR_QProduct<SPQRType, Derived> operator*(
const MatrixBase<Derived>& other) {
389 return SPQR_QProduct<SPQRType, Derived>(m_spqr, other.derived(),
true);
391 template <
typename Derived>
392 SPQR_QProduct<SPQRType, Derived> operator*(
const SparseMatrixBase<Derived>& other) {
393 return SPQR_QProduct<SPQRType, Derived>(m_spqr, other.derived(),
true);
395 const SPQRType& m_spqr;
400template <
typename SPQRType>
401struct evaluator_traits<SPQRMatrixQReturnType<SPQRType> > {
402 using MatrixType =
typename SPQRType::MatrixType;
403 using Kind =
typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind;
404 using Shape = SparseShape;
407template <
typename DstXprType,
typename SPQRType>
408struct Assignment<DstXprType, SPQRMatrixQReturnType<SPQRType>,
409 internal::assign_op<typename DstXprType::Scalar, typename DstXprType::Scalar>, Sparse2Sparse> {
410 using SrcXprType = SPQRMatrixQReturnType<SPQRType>;
411 using Scalar =
typename DstXprType::Scalar;
413 static void run(DstXprType& dst,
const SrcXprType& src,
const internal::assign_op<Scalar, Scalar>& ) {
414 typename DstXprType::PlainObject idMat(src.rows(), src.cols());
416 dst = src.m_spqr.matrixQ() * idMat;
420template <
typename DstXprType,
typename SPQRType>
421struct Assignment<DstXprType, SPQRMatrixQReturnType<SPQRType>,
422 internal::assign_op<typename DstXprType::Scalar, typename DstXprType::Scalar>, Sparse2Dense> {
423 using SrcXprType = SPQRMatrixQReturnType<SPQRType>;
424 using Scalar =
typename DstXprType::Scalar;
426 static void run(DstXprType& dst,
const SrcXprType& src,
const internal::assign_op<Scalar, Scalar>& ) {
427 dst = src.m_spqr.matrixQ() * DstXprType::Identity(src.rows(), src.cols());
constexpr RowXpr row(Index i)
Definition DenseBase.h:1095
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
Sparse QR factorization based on SuiteSparseQR library.
Definition SuiteSparseQRSupport.h:73
void setPivotThreshold(const RealScalar &tol)
Set the tolerance tol to treat columns with 2-norm <= tol as zero.
Definition SuiteSparseQRSupport.h:234
Index rank() const
Definition SuiteSparseQRSupport.h:227
cholmod_common * cholmodCommon() const
Definition SuiteSparseQRSupport.h:240
SPQRMatrixQReturnType< SPQR > matrixQ() const
Get an expression of the matrix Q.
Definition SuiteSparseQRSupport.h:216
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SuiteSparseQRSupport.h:247
Index rows() const
Definition SuiteSparseQRSupport.h:174
Index cols() const
Definition SuiteSparseQRSupport.h:179
const MatrixType & matrixR() const
Definition SuiteSparseQRSupport.h:207
PermutationType colsPermutation() const
Get the permutation that was applied to columns of A.
Definition SuiteSparseQRSupport.h:218
void setSPQROrdering(int ord)
Set the fill-reducing ordering method to be used.
Definition SuiteSparseQRSupport.h:232
constexpr FixedBlockXpr<...,... >::Type topLeftCorner(NRowsType cRows, NColsType cCols)
Definition SparseMatrixBase.h:287
constexpr ColXpr col(Index i)
Definition SparseMatrixBase.h:1082
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
Index cols() const
Definition SparseMatrix.h:162
Index rows() const
Definition SparseMatrix.h:160
SparseSolverBase()=default
ComputationInfo
Definition Constants.h:455
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
Definition EigenBase.h:34
Eigen::Index Index
Definition EigenBase.h:44