G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsSumOp.h
Go to the documentation of this file.
1
13#pragma once
14
16
17namespace gismo
18{
19
23template<typename T>
24class gsSumOp GISMO_FINAL : public gsLinearOperator<T>
25{
26 typedef typename gsLinearOperator<T>::Ptr BasePtr;
27public:
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
124private:
125 std::vector<BasePtr> m_ops;
126
127};
128
129}
Simple abstract class for discrete operators.
Definition gsLinearOperator.h:29
memory::shared_ptr< gsLinearOperator > Ptr
Shared pointer for gsLinearOperator.
Definition gsLinearOperator.h:33
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Class for representing the sum of objects of type gsLinearOperator as gsLinearOperator.
Definition gsSumOp.h:25
index_t rows() const
Returns the number of rows of the operator.
Definition gsSumOp.h:109
static uPtr make(BasePtr op0, BasePtr op1, BasePtr op2)
Make command returning a smart pointer.
Definition gsSumOp.h:82
memory::unique_ptr< gsSumOp > uPtr
Unique pointer for gsSumOp.
Definition gsSumOp.h:33
gsSumOp(std::vector< BasePtr > ops)
Constructor taking a vector of Linear Operators.
Definition gsSumOp.h:39
void addOperator(BasePtr op)
Add another operator.
Definition gsSumOp.h:86
gsSumOp(BasePtr op0, BasePtr op1)
Constructor taking two Linear Operators.
Definition gsSumOp.h:53
const std::vector< BasePtr > & getOps() const
Return a vector of shared pointers to all operators.
Definition gsSumOp.h:122
memory::shared_ptr< gsSumOp > Ptr
Shared pointer for gsSumOp.
Definition gsSumOp.h:30
gsSumOp()
Empty constructor. To be filled with addOperator()
Definition gsSumOp.h:36
static uPtr make(std::vector< BasePtr > ops)
Make command returning a smart pointer.
Definition gsSumOp.h:74
gsSumOp(BasePtr op0, BasePtr op1, BasePtr op2)
Constructor taking three Linear Operators.
Definition gsSumOp.h:61
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
static uPtr make(BasePtr op0, BasePtr op1)
Make command returning a smart pointer.
Definition gsSumOp.h:78
index_t cols() const
Returns the number of columns of the operator.
Definition gsSumOp.h:115
static uPtr make()
Make command returning a smart pointer.
Definition gsSumOp.h:70
#define index_t
Definition gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
Simple abstract class for (discrete) linear operators.
The G+Smo namespace, containing all definitions for the library.
S give(S &x)
Definition gsMemory.h:266