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