G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsProductOp.h
Go to the documentation of this file.
1 
16 #pragma once
17 
19 
20 namespace gismo {
32 template<typename T>
33 class gsProductOp GISMO_FINAL : public gsLinearOperator<T>
34 {
35  typedef typename gsLinearOperator<T>::Ptr BasePtr;
36 public:
37 
39  typedef memory::shared_ptr<gsProductOp> Ptr;
40 
42  typedef memory::unique_ptr<gsProductOp> uPtr;
43 
45  gsProductOp() : m_ops() {}
46 
48  gsProductOp(std::vector<BasePtr> ops) : m_ops(give(ops))
49  {
50 #ifndef NDEBUG
51  const index_t sz = m_ops.size();
52  for (index_t i=0; i<sz-1; ++i)
53  {
54  GISMO_ASSERT ( m_ops[i]->rows() == m_ops[i+1]->cols(),
55  "Dimensions of the operators do not fit." );
56  }
57 #endif
58  }
59 
61  gsProductOp(BasePtr op0, BasePtr op1) : m_ops(2)
62  {
63  GISMO_ASSERT ( op0->rows() == op1->cols(),
64  "Dimensions of the operators do not fit." );
65  m_ops[0]=give(op0); m_ops[1]=give(op1);
66  }
67 
69  gsProductOp(BasePtr op0, BasePtr op1, BasePtr op2) : m_ops(3)
70  {
71  GISMO_ASSERT ( op0->rows() == op1->cols() && op1->rows() == op2->cols(),
72  "Dimensions of the operators do not fit." );
73  m_ops[0]=give(op0); m_ops[1]=give(op1); m_ops[2]=give(op2);
74  }
75 
77  static uPtr make()
78  { return uPtr( new gsProductOp() ); }
79 
81  static uPtr make(std::vector<BasePtr> ops)
82  { return uPtr( new gsProductOp(give(ops)) ); }
83 
85  static uPtr make(BasePtr op0, BasePtr op1)
86  { return uPtr( new gsProductOp(give(op0),give(op1)) ); }
87 
89  static uPtr make(BasePtr op0, BasePtr op1, BasePtr op2)
90  { return uPtr( new gsProductOp(give(op0),give(op1),give(op2)) ); }
91 
93  void addOperator( BasePtr op )
94  {
95  GISMO_ASSERT ( m_ops.empty() || m_ops.back()->rows() == op->cols(),
96  "Dimensions of the operators do not fit." );
97  m_ops.push_back(give(op));
98  }
99 
100  void apply(const gsMatrix<T> & input, gsMatrix<T> & x) const
101  {
102  // The product of 0 operators is the identity
103  if ( m_ops.empty() ) { x = input; return; }
104 
105  // Here, we could make a permanently allocated vector
106  gsMatrix<T> temp;
107  const size_t sz = m_ops.size();
108 
109  m_ops[0]->apply(input,x);
110  for(size_t i=1; i<sz;++i)
111  {
112  temp.swap(x);
113  m_ops[i]->apply(temp,x);
114  }
115  }
116 
117  index_t rows() const {
118  GISMO_ASSERT( !m_ops.empty(), "gsProductOp::rows does not work for 0 operators.");
119  return m_ops.back()->rows();
120 
121  }
122 
123  index_t cols() const {
124  GISMO_ASSERT( !m_ops.empty(), "gsProductOp::cols does not work for 0 operators.");
125  return m_ops.front()->cols();
126  }
127 
129  const std::vector<BasePtr>& getOps() const { return m_ops; }
130 
131 private:
132  std::vector<BasePtr> m_ops;
133 };
134 
135 } // namespace gismo
static uPtr make()
Make command returning a smart pointer.
Definition: gsProductOp.h:77
memory::shared_ptr< gsProductOp > Ptr
Shared pointer for gsProductOp.
Definition: gsProductOp.h:39
gsProductOp(BasePtr op0, BasePtr op1, BasePtr op2)
Constructor taking three Linear Operators.
Definition: gsProductOp.h:69
void apply(const gsMatrix< T > &input, gsMatrix< T > &x) const
apply the operator on the input vector and store the result in x
Definition: gsProductOp.h:100
static uPtr make(std::vector< BasePtr > ops)
Make command returning a smart pointer.
Definition: gsProductOp.h:81
gsProductOp()
Empty constructor. To be filled with addOperator()
Definition: gsProductOp.h:45
index_t cols() const
Returns the number of columns of the operator.
Definition: gsProductOp.h:123
static uPtr make(BasePtr op0, BasePtr op1, BasePtr op2)
Make command returning a smart pointer.
Definition: gsProductOp.h:89
S give(S &x)
Definition: gsMemory.h:266
#define index_t
Definition: gsConfig.h:32
const std::vector< BasePtr > & getOps() const
Return a vector of shared pointers to all operators.
Definition: gsProductOp.h:129
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
memory::unique_ptr< gsProductOp > uPtr
Unique pointer for gsProductOp.
Definition: gsProductOp.h:42
memory::shared_ptr< gsLinearOperator > Ptr
Shared pointer for gsLinearOperator.
Definition: gsLinearOperator.h:33
memory::unique_ptr< gsLinearOperator > uPtr
Unique pointer for gsLinearOperator.
Definition: gsLinearOperator.h:36
Class for representing the product of objects of type gsLinearOperator as gsLinearOperator.
Definition: gsProductOp.h:33
Simple abstract class for discrete operators.
Definition: gsLinearOperator.h:28
index_t rows() const
Returns the number of rows of the operator.
Definition: gsProductOp.h:117
gsProductOp(std::vector< BasePtr > ops)
Constructor taking a vector of Linear Operators.
Definition: gsProductOp.h:48
void addOperator(BasePtr op)
Add another operator at the end.
Definition: gsProductOp.h:93
gsProductOp(BasePtr op0, BasePtr op1)
Constructor taking two Linear Operators.
Definition: gsProductOp.h:61
static uPtr make(BasePtr op0, BasePtr op1)
Make command returning a smart pointer.
Definition: gsProductOp.h:85
Simple abstract class for (discrete) linear operators.