G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsLinearOperator.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include <gsCore/gsLinearAlgebra.h>
16 #include <gsIO/gsOptionList.h>
17 
18 namespace gismo
19 {
20 
27 template<class T>
29 {
30 public:
31 
33  typedef memory::shared_ptr<gsLinearOperator> Ptr;
34 
36  typedef memory::unique_ptr<gsLinearOperator> uPtr;
37 
39  static gsIdentityOp<T> Identity(const index_t dim) {return gsIdentityOp<T>(dim); }
40 
41  virtual ~gsLinearOperator() {}
42 
48  virtual void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const = 0;
49 
51  virtual index_t rows() const = 0;
52 
54  virtual index_t cols() const = 0;
55 
56  // NOTE: this is rather inefficient and is only provided for debugging and testing purposes
57  void toMatrix(gsMatrix<T>& result)
58  {
59  gsMatrix<T> eye = gsMatrix<T>::Identity(cols(), cols());
60  this->apply(eye, result);
61  }
62 
64  // This implementation provides an empty object
65  static gsOptionList defaultOptions() { return gsOptionList(); }
66 
68  // This implementation does not read any input
69  virtual void setOptions(const gsOptionList &) {}
70 
71 }; // gsLinearOperator
72 
76 template<class T>
77 class gsScaledOp GISMO_FINAL : public gsLinearOperator<T>
78 {
79 public:
81  typedef memory::shared_ptr<gsScaledOp> Ptr;
82 
84  typedef memory::unique_ptr<gsScaledOp> uPtr;
85 
88 
90  gsScaledOp(BasePtr op, T scalar = 1) : m_op(give(op)), m_scalar(scalar) {}
91 
93  static uPtr make(BasePtr op, T scalar = 1)
94  { return uPtr( new gsScaledOp(give(op), scalar) ); }
95 
96  virtual void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
97  {
98  m_op->apply(input, x);
99  x *= m_scalar;
100  }
101 
103  index_t rows() const {return m_op->rows();}
104 
106  index_t cols() const {return m_op->cols();}
107 
108 private:
109  const BasePtr m_op;
110  const T m_scalar;
111 }; // gsScaladOp
112 
113 
117 template<class T>
118 class gsIdentityOp GISMO_FINAL : public gsLinearOperator<T>
119 {
120 public:
121 
123  typedef memory::shared_ptr<gsIdentityOp> Ptr;
124 
126  typedef memory::unique_ptr<gsIdentityOp> uPtr;
127 
129  gsIdentityOp(index_t dim) : m_dim(dim) {}
130 
132  static uPtr make(index_t dim) { return uPtr( new gsIdentityOp(dim) ); }
133 
134  void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
135  {
136  x = input;
137  }
138 
139  index_t rows() const {return m_dim;}
140 
141  index_t cols() const {return m_dim;}
142 
143 private:
144  const index_t m_dim;
145 };
146 
150 template<class T, class L>
151 class gsLinearLambdaOp GISMO_FINAL : public gsLinearOperator<T>
152 {
153 public:
156  : m_lambda(lambda), m_rows(rows), m_cols(cols) {}
157  void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
158  {
159  m_lambda(input, x);
160  }
161  index_t rows() const {return m_rows;}
162  index_t cols() const {return m_cols;}
163 private:
164  L m_lambda;
165  index_t m_rows, m_cols;
166 };
167 
176 template<class T=real_t, class L>
178 {
179  return typename gsLinearOperator<T>::uPtr(new gsLinearLambdaOp<T,L>(lambda,rows,cols));
180 }
181 
182 
183 } // namespace gismo
index_t rows() const
Returns the number of rows of the operator.
Definition: gsLinearOperator.h:161
void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition: gsLinearOperator.h:157
index_t cols() const
Returns the number of columns of the operator.
Definition: gsLinearOperator.h:141
Identity operator.
Definition: gsLinearOperator.h:118
gsLinearOperator< T >::Ptr BasePtr
Shared pointer for gsLinearOperator.
Definition: gsLinearOperator.h:87
virtual index_t rows() const =0
Returns the number of rows of the operator.
static gsOptionList defaultOptions()
Get the default options as gsOptionList object.
Definition: gsLinearOperator.h:65
gsLinearOperator< T >::uPtr makeLinearOp(L lambda, index_t rows, index_t cols)
Wrapper that allows to use lambdas as a gsLinearOperator.
Definition: gsLinearOperator.h:177
Wrapper that allows to use lambdas as a gsLinearOperator.
Definition: gsLinearOperator.h:151
S give(S &x)
Definition: gsMemory.h:266
static uPtr make(index_t dim)
Make function returning a smart pointer.
Definition: gsLinearOperator.h:132
#define index_t
Definition: gsConfig.h:32
virtual void setOptions(const gsOptionList &)
Set options based on a gsOptionList object.
Definition: gsLinearOperator.h:69
static uPtr make(BasePtr op, T scalar=1)
Make function returning a smart pointer.
Definition: gsLinearOperator.h:93
void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition: gsLinearOperator.h:134
index_t cols() const
Returns the number of columns of the operator.
Definition: gsLinearOperator.h:162
Provides a list of labeled parameters/options that can be set and accessed easily.
memory::shared_ptr< gsLinearOperator > Ptr
Shared pointer for gsLinearOperator.
Definition: gsLinearOperator.h:33
memory::shared_ptr< gsScaledOp > Ptr
Shared pointer for gsScaledOp.
Definition: gsLinearOperator.h:81
memory::unique_ptr< gsLinearOperator > uPtr
Unique pointer for gsLinearOperator.
Definition: gsLinearOperator.h:36
gsScaledOp(BasePtr op, T scalar=1)
Constructor taking a shared pointer to a linear operator and a scalar.
Definition: gsLinearOperator.h:90
index_t cols() const
Returns the number of columns in the preconditioner.
Definition: gsLinearOperator.h:106
index_t rows() const
Returns the number of rows in the preconditioner.
Definition: gsLinearOperator.h:103
memory::unique_ptr< gsIdentityOp > uPtr
Unique pointer for gsIdentityOp.
Definition: gsLinearOperator.h:126
static gsIdentityOp< T > Identity(const index_t dim)
Identity operator.
Definition: gsLinearOperator.h:39
Simple abstract class for discrete operators.
Definition: gsLinearOperator.h:28
This is the main header file that collects wrappers of Eigen for linear algebra.
virtual void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const =0
apply the operator on the input vector and store the result in x
virtual index_t cols() const =0
Returns the number of columns of the operator.
memory::unique_ptr< gsScaledOp > uPtr
Unique pointer for gsScaledOp.
Definition: gsLinearOperator.h:84
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
index_t rows() const
Returns the number of rows of the operator.
Definition: gsLinearOperator.h:139
memory::shared_ptr< gsIdentityOp > Ptr
Shared pointer for gsIdentityOp.
Definition: gsLinearOperator.h:123
gsLinearLambdaOp(L lambda, index_t rows, index_t cols)
Constructor; see makeLinearOp for details.
Definition: gsLinearOperator.h:155
Allows an operator to be multiplied with a scalar.
Definition: gsLinearOperator.h:77
virtual void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition: gsLinearOperator.h:96
gsIdentityOp(index_t dim)
Constructor taking the dimension of the identity operator.
Definition: gsLinearOperator.h:129