SparseSolverBase.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSESOLVERBASE_H
11 #define EIGEN_SPARSESOLVERBASE_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
23 template<typename Decomposition, typename Rhs, typename Dest>
24 std::enable_if_t<Rhs::ColsAtCompileTime!=1 && Dest::ColsAtCompileTime!=1>
25 solve_sparse_through_dense_panels(const Decomposition &dec, const Rhs& rhs, Dest &dest)
26 {
27  EIGEN_STATIC_ASSERT((Dest::Flags&RowMajorBit)==0,THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
28  typedef typename Dest::Scalar DestScalar;
29  // we process the sparse rhs per block of NbColsAtOnce columns temporarily stored into a dense matrix.
30  static const Index NbColsAtOnce = 4;
31  Index rhsCols = rhs.cols();
32  Index size = rhs.rows();
33  // the temporary matrices do not need more columns than NbColsAtOnce:
34  Index tmpCols = (std::min)(rhsCols, NbColsAtOnce);
37  for(Index k=0; k<rhsCols; k+=NbColsAtOnce)
38  {
39  Index actualCols = std::min<Index>(rhsCols-k, NbColsAtOnce);
40  tmp.leftCols(actualCols) = rhs.middleCols(k,actualCols);
41  tmpX.leftCols(actualCols) = dec.solve(tmp.leftCols(actualCols));
42  dest.middleCols(k,actualCols) = tmpX.leftCols(actualCols).sparseView();
43  }
44 }
45 
46 // Overload for vector as rhs
47 template<typename Decomposition, typename Rhs, typename Dest>
48 std::enable_if_t<Rhs::ColsAtCompileTime==1 || Dest::ColsAtCompileTime==1>
49 solve_sparse_through_dense_panels(const Decomposition &dec, const Rhs& rhs, Dest &dest)
50 {
51  typedef typename Dest::Scalar DestScalar;
52  Index size = rhs.rows();
55  dest_dense = dec.solve(rhs_dense);
56  dest = dest_dense.sparseView();
57 }
58 
59 } // end namespace internal
60 
68 template<typename Derived>
69 class SparseSolverBase : internal::noncopyable
70 {
71  public:
72 
75  : m_isInitialized(false)
76  {}
77 
79 
81  {}
82 
83  Derived& derived() { return *static_cast<Derived*>(this); }
84  const Derived& derived() const { return *static_cast<const Derived*>(this); }
85 
90  template<typename Rhs>
91  inline const Solve<Derived, Rhs>
92  solve(const MatrixBase<Rhs>& b) const
93  {
94  eigen_assert(m_isInitialized && "Solver is not initialized.");
95  eigen_assert(derived().rows()==b.rows() && "solve(): invalid number of rows of the right hand side matrix b");
96  return Solve<Derived, Rhs>(derived(), b.derived());
97  }
98 
103  template<typename Rhs>
104  inline const Solve<Derived, Rhs>
106  {
107  eigen_assert(m_isInitialized && "Solver is not initialized.");
108  eigen_assert(derived().rows()==b.rows() && "solve(): invalid number of rows of the right hand side matrix b");
109  return Solve<Derived, Rhs>(derived(), b.derived());
110  }
111 
112  #ifndef EIGEN_PARSED_BY_DOXYGEN
114  template<typename Rhs,typename Dest>
115  void _solve_impl(const SparseMatrixBase<Rhs> &b, SparseMatrixBase<Dest> &dest) const
116  {
118  }
119  #endif // EIGEN_PARSED_BY_DOXYGEN
120 
121  protected:
122 
123  mutable bool m_isInitialized;
124 };
125 
126 } // end namespace Eigen
127 
128 #endif // EIGEN_SPARSESOLVERBASE_H
Array< int, 3, 1 > b
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Pseudo expression representing a solving operation.
Definition: Solve.h:65
Base class of any sparse matrices or sparse expressions.
A base class for sparse solvers.
SparseSolverBase(SparseSolverBase &&other)
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
const Derived & derived() const
const SparseView< Derived > sparseView(const Scalar &m_reference=Scalar(0), const typename NumTraits< Scalar >::Real &m_epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:228
const unsigned int RowMajorBit
Definition: Constants.h:68
bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:684
std::enable_if_t< Rhs::ColsAtCompileTime!=1 &&Dest::ColsAtCompileTime!=1 > solve_sparse_through_dense_panels(const Decomposition &dec, const Rhs &rhs, Dest &dest)
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Derived & derived()
Definition: EigenBase.h:48