G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsMatrixOp.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include <gsCore/gsLinearAlgebra.h>
17 
18 namespace gismo
19 {
20 
21 // left here for debugging purposes
22 // template<typename T> struct is_ref { static const bool value = false; };
23 // template<typename T> struct is_ref<T&> { static const bool value = true; };
24 
32 template <class MatrixType>
33 class gsMatrixOp GISMO_FINAL : public gsLinearOperator<typename MatrixType::Scalar>
34 {
35  typedef memory::shared_ptr<MatrixType> MatrixPtr;
36  typedef typename MatrixType::Nested NestedMatrix;
37 
38 public:
39  typedef typename MatrixType::Scalar T;
40 
42  typedef memory::shared_ptr<gsMatrixOp> Ptr;
43 
45  typedef memory::unique_ptr<gsMatrixOp> uPtr;
46 
52  gsMatrixOp(const MatrixType& mat)
53  : m_mat(), m_expr(mat.derived())
54  {
55  //gsDebug<<typeid(m_expr).name()<<" Ref: "<<is_ref<NestedMatrix>::value<<"\n";
56  }
57 
59  gsMatrixOp(MatrixPtr mat)
60  : m_mat(give(mat)), m_expr(m_mat->derived())
61  { }
62 
67  static uPtr make(const MatrixType& mat)
68  { return uPtr( new gsMatrixOp(mat) ); }
69 
71  static uPtr make(MatrixPtr mat)
72  { return uPtr( new gsMatrixOp(give(mat)) ); }
73 
74  void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
75  { x.noalias() = m_expr * input; }
76 
77  index_t rows() const
78  { return m_expr.rows(); }
79 
80  index_t cols() const
81  { return m_expr.cols(); }
82 
84  NestedMatrix matrix() const
85  { return m_expr; }
86 
88  MatrixPtr matrixPtr() const {
89  GISMO_ENSURE( m_mat, "A shared pointer is only available if it was provided to gsMatrixOp." );
90  return m_mat;
91  }
92 
93 private:
94  const MatrixPtr m_mat;
95  NestedMatrix m_expr;
96 };
97 
125 template <class Derived>
126 typename gsMatrixOp<Derived>::uPtr makeMatrixOp(const gsEigen::EigenBase<Derived>& mat)
127 {
128  return gsMatrixOp<Derived>::make(mat.derived());
129 }
130 
152 template <class Derived>
153 typename gsMatrixOp<Derived>::uPtr makeMatrixOp(memory::shared_ptr<Derived> mat)
154 {
155  return memory::make_unique(new gsMatrixOp<Derived>(give(mat)));
156 }
157 
158 // We need an additional guide for the compiler to be able to work well with unique ptrs
159 template <class Derived>
160 typename gsMatrixOp<Derived>::uPtr makeMatrixOp(memory::unique_ptr<Derived> mat)
161 {
162  return memory::make_unique(new gsMatrixOp<Derived>(memory::shared_ptr<Derived>(mat.release())));
163 }
164 
170 template <class SolverType>
171 class gsSolverOp GISMO_FINAL : public gsLinearOperator<typename SolverType::Scalar>
172 {
173 public:
174  typedef typename SolverType::Scalar T;
175 
176  typedef typename SolverType::MatrixType MatrixType;
177 
179  typedef memory::shared_ptr<gsSolverOp> Ptr;
180 
182  typedef memory::unique_ptr<gsSolverOp> uPtr;
183 
184 
186  gsSolverOp(const MatrixType& mat)
187  {
188  GISMO_ASSERT(mat.rows() == mat.cols(), "Need square matrix");
189  m_size = mat.rows();
190 
191  m_solver.compute(mat);
192  }
193 
195  gsSolverOp(const memory::shared_ptr<MatrixType>& mat)
196  {
197  GISMO_ASSERT(mat->rows() == mat->cols(), "Need square matrix");
198  m_size = mat->rows();
199 
200  m_solver.compute(*mat);
201  }
202 
204  static uPtr make(const MatrixType& mat) { return memory::make_unique( new gsSolverOp(mat) ); }
205 
206  void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
207  {
208  x = m_solver.solve(input);
209  }
210 
211  index_t rows() const { return m_size; }
212 
213  index_t cols() const { return m_size; }
214 
216  SolverType& solver() { return m_solver; }
217 
219  const SolverType& solver() const { return m_solver; }
220 
221 private:
222  SolverType m_solver;
223  index_t m_size;
224 };
225 
226 
231 template <class T, int _Rows, int _Cols, int _Opt>
233 {
234  return memory::make_unique( new gsSolverOp< gsEigen::PartialPivLU< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
235 }
236 
242 template <class T, int _Rows, int _Cols, int _Opt>
244 {
245  return memory::make_unique( new gsSolverOp< gsEigen::PartialPivLU< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
246 }
247 
248 
253 template <class T, int _Rows, int _Cols, int _Opt>
255 {
256  return memory::make_unique( new gsSolverOp< gsEigen::FullPivLU< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
257 }
258 
264 template <class T, int _Rows, int _Cols, int _Opt>
266 {
267  return memory::make_unique( new gsSolverOp< gsEigen::FullPivLU< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
268 }
269 
270 
278 template <class T, int _Rows, int _Cols, int _Opt>
280 {
281  return memory::make_unique( new gsSolverOp< gsEigen::LDLT< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
282 }
283 
292 template <class T, int _Rows, int _Cols, int _Opt>
294 {
295  return memory::make_unique( new gsSolverOp< gsEigen::LDLT< gsEigen::Matrix<T, _Rows, _Cols, _Opt> > >(mat) );
296 }
297 
298 
305 template <typename T, int _Opt, typename _Index>
307 {
308  return memory::make_unique( new gsSolverOp< typename gsSparseSolver<T>::LU >(mat) );
309 }
310 
317 template <typename T, int _Opt, typename _Index>
319 {
320  return memory::make_unique( new gsSolverOp< typename gsSparseSolver<T>::LU >(mat) );
321 }
322 
323 
330 template <typename T, int _Opt, typename _Index>
332 {
333  return memory::make_unique( new gsSolverOp< typename gsSparseSolver<T>::QR >(mat) );
334 }
335 
342 template <typename T, int _Opt, typename _Index>
344 {
345  return memory::make_unique( new gsSolverOp< typename gsSparseSolver<T>::QR >(mat) );
346 }
347 
348 
356 template <typename T, int _Opt, typename _Index>
358 {
359  return memory::make_unique( new gsSolverOp<typename gsSparseSolver<T>::SimplicialLDLT >(mat) );
360 }
361 
369 template <typename T, int _Opt, typename _Index>
371 {
372  return memory::make_unique( new gsSolverOp<typename gsSparseSolver<T>::SimplicialLDLT >(mat) );
373 }
374 
375 
376 } // namespace gismo
gsSolverOp< typename gsSparseSolver< T >::LU >::uPtr makeSparseLUSolver(const gsSparseMatrix< T, _Opt, _Index > &mat)
Convenience function to create a sparse LU solver as a gsLinearOperator.
Definition: gsMatrixOp.h:306
memory::shared_ptr< gsMatrixOp > Ptr
Shared pointer for gsMatrixOp.
Definition: gsMatrixOp.h:42
MatrixPtr matrixPtr() const
Returns a shared pinter to the matrix.
Definition: gsMatrixOp.h:88
unique_ptr< T > make_unique(T *x)
Definition: gsMemory.h:198
const SolverType & solver() const
Access the solver class.
Definition: gsMatrixOp.h:219
SolverType & solver()
Access the solver class.
Definition: gsMatrixOp.h:216
memory::unique_ptr< gsMatrixOp > uPtr
Unique pointer for gsMatrixOp.
Definition: gsMatrixOp.h:45
static uPtr make(const MatrixType &mat)
Make function returning a smart pointer.
Definition: gsMatrixOp.h:67
gsSolverOp< gsEigen::PartialPivLU< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makePartialPivLUSolver(const memory::shared_ptr< gsMatrix< T, _Rows, _Cols, _Opt > > &mat)
Convenience function to create an LU solver with partial pivoting (for dense matrices) as a gsLinearO...
Definition: gsMatrixOp.h:243
Simple adapter class to use a matrix (or matrix-like object) as a linear operator. Needed for the iterative method classes.
Definition: gsMatrixOp.h:33
NestedMatrix matrix() const
Returns the matrix.
Definition: gsMatrixOp.h:84
void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition: gsMatrixOp.h:206
memory::shared_ptr< gsSolverOp > Ptr
Shared pointer for gsSolverOp.
Definition: gsMatrixOp.h:179
S give(S &x)
Definition: gsMemory.h:266
#define index_t
Definition: gsConfig.h:32
#define GISMO_ENSURE(cond, message)
Definition: gsDebug.h:102
gsSolverOp< gsEigen::FullPivLU< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makeFullPivLUSolver(const memory::shared_ptr< gsMatrix< T, _Rows, _Cols, _Opt > > &mat)
Convenience function to create an LU solver with full pivoting (for dense matrices) as a gsLinearOper...
Definition: gsMatrixOp.h:265
gsSolverOp< typename gsSparseSolver< T >::QR >::uPtr makeSparseQRSolver(const memory::shared_ptr< gsSparseMatrix< T, _Opt, _Index > > &mat)
Convenience function to create a sparse QR solver as a gsLinearOperator taking a shared pointer...
Definition: gsMatrixOp.h:343
gsSolverOp< gsEigen::FullPivLU< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makeFullPivLUSolver(const gsMatrix< T, _Rows, _Cols, _Opt > &mat)
Convenience function to create an LU solver with full pivoting (for dense matrices) as a gsLinearOper...
Definition: gsMatrixOp.h:254
index_t cols() const
Returns the number of columns of the operator.
Definition: gsMatrixOp.h:80
gsSolverOp< gsEigen::LDLT< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makeCholeskySolver(const gsMatrix< T, _Rows, _Cols, _Opt > &mat)
Convenience function to create a Cholesky (LDL^T) solver (for dense matrices) as a gsLinearOperator...
Definition: gsMatrixOp.h:279
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition: gsSparseMatrix.h:140
gsSolverOp< typename gsSparseSolver< T >::SimplicialLDLT >::uPtr makeSparseCholeskySolver(const memory::shared_ptr< gsSparseMatrix< T, _Opt, _Index > > &mat)
Convenience function to create a sparse Cholesky (simplicial LDL^T) solver as a gsLinearOperator.
Definition: gsMatrixOp.h:370
gsSolverOp(const MatrixType &mat)
Constructor taking a matrix.
Definition: gsMatrixOp.h:186
Simple adapter class to use an Eigen solver (having a compute() and a solve() method) as a linear ope...
Definition: gsMatrixOp.h:171
gsSolverOp< typename gsSparseSolver< T >::QR >::uPtr makeSparseQRSolver(const gsSparseMatrix< T, _Opt, _Index > &mat)
Convenience function to create a sparse QR solver as a gsLinearOperator.
Definition: gsMatrixOp.h:331
gsSolverOp< gsEigen::LDLT< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makeCholeskySolver(const memory::shared_ptr< gsMatrix< T, _Rows, _Cols, _Opt > > &mat)
Convenience function to create a Cholesky (LDL^T) solver (for dense matrices) as a gsLinearOperator t...
Definition: gsMatrixOp.h:293
void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition: gsMatrixOp.h:74
index_t cols() const
Returns the number of columns of the operator.
Definition: gsMatrixOp.h:213
gsSolverOp(const memory::shared_ptr< MatrixType > &mat)
Constructor taking a shared pointer.
Definition: gsMatrixOp.h:195
index_t rows() const
Returns the number of rows of the operator.
Definition: gsMatrixOp.h:211
static uPtr make(MatrixPtr mat)
Make function returning a smart pointer.
Definition: gsMatrixOp.h:71
index_t rows() const
Returns the number of rows of the operator.
Definition: gsMatrixOp.h:77
gsMatrixOp< Derived >::uPtr makeMatrixOp(const gsEigen::EigenBase< Derived > &mat)
This essentially just calls the gsMatrixOp constructor, but the use of a template functions allows us...
Definition: gsMatrixOp.h:126
gsSolverOp< typename gsSparseSolver< T >::SimplicialLDLT >::uPtr makeSparseCholeskySolver(const gsSparseMatrix< T, _Opt, _Index > &mat)
Convenience function to create a sparse Cholesky (simplicial LDL^T) solver as a gsLinearOperator.
Definition: gsMatrixOp.h:357
Simple abstract class for discrete operators.
Definition: gsLinearOperator.h:28
This is the main header file that collects wrappers of Eigen for linear algebra.
gsMatrixOp(const MatrixType &mat)
Constructor taking a reference.
Definition: gsMatrixOp.h:52
NestedMatrix m_expr
Nested Eigen expression.
Definition: gsMatrixOp.h:95
gsMatrixOp< Derived >::uPtr makeMatrixOp(memory::shared_ptr< Derived > mat)
This essentially just calls the gsMatrixOp constructor, but the use of a template functions allows us...
Definition: gsMatrixOp.h:153
gsMatrixOp(MatrixPtr mat)
Constructor taking a shared pointer.
Definition: gsMatrixOp.h:59
static uPtr make(const MatrixType &mat)
Make function taking a matrix OR a shared pointer.
Definition: gsMatrixOp.h:204
const MatrixPtr m_mat
Shared pointer to matrix (if needed)
Definition: gsMatrixOp.h:94
gsSolverOp< gsEigen::PartialPivLU< gsEigen::Matrix< T, _Rows, _Cols, _Opt > > >::uPtr makePartialPivLUSolver(const gsMatrix< T, _Rows, _Cols, _Opt > &mat)
Convenience function to create an LU solver with partial pivoting (for dense matrices) as a gsLinearO...
Definition: gsMatrixOp.h:232
memory::unique_ptr< gsSolverOp > uPtr
Unique pointer for gsSolverOp.
Definition: gsMatrixOp.h:182
gsSolverOp< typename gsSparseSolver< T >::LU >::uPtr makeSparseLUSolver(const memory::shared_ptr< gsSparseMatrix< T, _Opt, _Index > > &mat)
Convenience function to create a sparse LU solver as a gsLinearOperator taking a shared pointer...
Definition: gsMatrixOp.h:318
Simple abstract class for (discrete) linear operators.