codac 2.0.0
Loading...
Searching...
No Matches
codac2_component.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
14#include "codac2_AnalyticType.h"
15
16namespace codac2
17{
18 struct ComponentOp
19 {
20 template<typename X1>
21 static std::string str(const X1& x1, Index i)
22 {
23 return x1->str(!x1->is_str_leaf()) + "[" + std::to_string(i) + "]";
24 }
25
26 template<typename X1>
27 static std::string str(const X1& x1, Index i, Index j)
28 {
29 return x1->str(!x1->is_str_leaf()) + "(" + std::to_string(i) + "," + std::to_string(j) + ")";
30 }
31
32 template<typename X1>
33 static std::pair<Index,Index> output_shape(const X1& s1, Index i)
34 {
35 auto shape1 = s1->output_shape();
36 assert(shape1.second==1 && i<shape1.first);
37 return {1,1};
38 }
39
40 template<typename X1>
41 static std::pair<Index,Index> output_shape(const X1& s1, Index i, Index j)
42 {
43 auto shape1 = s1->output_shape();
44 assert(j<shape1.second && i<shape1.first);
45 return {1,1};
46 }
47
48 static Interval fwd(const IntervalVector& x1, Index i);
49 static ScalarType fwd_natural(const VectorType& x1, Index i);
50 static ScalarType fwd_centered(const VectorType& x1, Index i);
51 static void bwd(const Interval& y, IntervalVector& x1, Index i);
52
53 static Interval fwd(const IntervalMatrix& x1, Index i, Index j);
54 static ScalarType fwd_natural(const MatrixType& x1, Index i, Index j);
55 static ScalarType fwd_centered(const MatrixType& x1, Index i, Index j);
56 static void bwd(const Interval& y, IntervalMatrix& x1, Index i, Index j);
57 };
58
59 // Analytic operator
60
61 template<>
62 class AnalyticOperationExpr<ComponentOp,ScalarType,VectorType> : public AnalyticExpr<ScalarType>, public OperationExprBase<AnalyticExpr<VectorType>>
63 {
64 public:
65
66 AnalyticOperationExpr(const std::shared_ptr<AnalyticExpr<VectorType>>& x1, Index i)
67 : OperationExprBase<AnalyticExpr<VectorType>>(x1), _i(i)
68 { }
69
70 AnalyticOperationExpr(const AnalyticOperationExpr& e)
71 : OperationExprBase<AnalyticExpr<VectorType>>(e), _i(e._i)
72 { }
73
74 std::shared_ptr<ExprBase> copy() const
75 {
76 return std::make_shared<AnalyticOperationExpr<ComponentOp,ScalarType,VectorType>>(*this);
77 }
78
79 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
80 {
81 return OperationExprBase<AnalyticExpr<VectorType>>::replace_arg(old_arg_id, new_expr);
82 }
83
84 ScalarType fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
85 {
86 if(natural_eval)
87 return AnalyticExpr<ScalarType>::init_value(
88 v, ComponentOp::fwd_natural(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i));
89 else
90 return AnalyticExpr<ScalarType>::init_value(
91 v, ComponentOp::fwd_centered(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i));
92 }
93
94 void bwd_eval(ValuesMap& v) const
95 {
96 ComponentOp::bwd(AnalyticExpr<ScalarType>::value(v).a, std::get<0>(this->_x)->value(v).a, _i);
97 std::get<0>(this->_x)->bwd_eval(v);
98 }
99
100 std::pair<Index,Index> output_shape() const
101 {
102 return ComponentOp::output_shape(std::get<0>(this->_x),_i);
103 }
104
105 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
106 {
107 return std::get<0>(this->_x)->belongs_to_args_list(args);
108 }
109
110 std::string str(bool in_parentheses = false) const
111 {
112 std::string s = ComponentOp::str(std::get<0>(this->_x), _i);
113 return in_parentheses ? "(" + s + ")" : s;
114 }
115
116 virtual bool is_str_leaf() const
117 {
118 return true;
119 }
120
121 protected:
122
123 const Index _i;
124 };
125
126 template<>
127 class AnalyticOperationExpr<ComponentOp,ScalarType,MatrixType> : public AnalyticExpr<ScalarType>, public OperationExprBase<AnalyticExpr<MatrixType>>
128 {
129 public:
130
131 AnalyticOperationExpr(const std::shared_ptr<AnalyticExpr<MatrixType>>& x1, Index i, Index j)
132 : OperationExprBase<AnalyticExpr<MatrixType>>(x1), _i(i), _j(j)
133 { }
134
135 AnalyticOperationExpr(const AnalyticOperationExpr& e)
136 : OperationExprBase<AnalyticExpr<MatrixType>>(e), _i(e._i), _j(e._j)
137 { }
138
139 std::shared_ptr<ExprBase> copy() const
140 {
141 return std::make_shared<AnalyticOperationExpr<ComponentOp,ScalarType,MatrixType>>(*this);
142 }
143
144 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
145 {
146 return OperationExprBase<AnalyticExpr<MatrixType>>::replace_arg(old_arg_id, new_expr);
147 }
148
149 ScalarType fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
150 {
151 if(natural_eval)
152 return AnalyticExpr<ScalarType>::init_value(
153 v, ComponentOp::fwd_natural(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
154 else
155 return AnalyticExpr<ScalarType>::init_value(
156 v, ComponentOp::fwd_centered(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
157 }
158
159 void bwd_eval(ValuesMap& v) const
160 {
161 ComponentOp::bwd(AnalyticExpr<ScalarType>::value(v).a, std::get<0>(this->_x)->value(v).a, _i, _j);
162 std::get<0>(this->_x)->bwd_eval(v);
163 }
164
165 std::pair<Index,Index> output_shape() const
166 {
167 return ComponentOp::output_shape(std::get<0>(this->_x),_i,_j);
168 }
169
170 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
171 {
172 return std::get<0>(this->_x)->belongs_to_args_list(args);
173 }
174
175 std::string str(bool in_parentheses = false) const
176 {
177 std::string s = ComponentOp::str(std::get<0>(this->_x), _i, _j);
178 return in_parentheses ? "(" + s + ")" : s;
179 }
180
181 virtual bool is_str_leaf() const
182 {
183 return true;
184 }
185
186 protected:
187
188 const Index _i, _j;
189 };
190
191 // Inline functions
192
193 // Vector component
194
195 inline Interval ComponentOp::fwd(const IntervalVector& x1, Index i)
196 {
197 assert(i >= 0 && i < x1.size());
198 return x1[i];
199 }
200
201 inline ScalarType ComponentOp::fwd_natural(const VectorType& x1, Index i)
202 {
203 assert(i >= 0 && i < x1.a.rows());
204 return {
205 fwd(x1.a,i),
206 x1.def_domain
207 };
208 }
209
210 inline ScalarType ComponentOp::fwd_centered(const VectorType& x1, Index i)
211 {
212 if(centered_form_not_available_for_args(x1))
213 return fwd_natural(x1,i);
214
215 assert(i >= 0 && i < x1.a.rows());
216 return {
217 fwd(x1.m,i),
218 fwd(x1.a,i),
219 x1.da.row(i),
220 x1.def_domain
221 };
222 }
223
224 inline void ComponentOp::bwd(const Interval& y, IntervalVector& x1, Index i)
225 {
226 assert(i >= 0 && i < x1.size());
227 x1[i] &= y;
228 }
229
230 // Matrix component
231
232 inline Interval ComponentOp::fwd(const IntervalMatrix& x1, Index i, Index j)
233 {
234 assert(i >= 0 && i < x1.rows());
235 assert(j >= 0 && j < x1.cols());
236 return x1(i,j);
237 }
238
239 inline ScalarType ComponentOp::fwd_natural(const MatrixType& x1, Index i, Index j)
240 {
241 assert(i >= 0 && i < x1.a.rows());
242 assert(j >= 0 && j < x1.a.cols());
243 return {
244 fwd(x1.a,i,j),
245 x1.def_domain
246 };
247 }
248
249 inline ScalarType ComponentOp::fwd_centered(const MatrixType& x1, Index i, Index j)
250 {
251 if(centered_form_not_available_for_args(x1))
252 return fwd_natural(x1,i,j);
253
254 assert(i >= 0 && i < x1.a.rows());
255 assert(j >= 0 && j < x1.a.cols());
256 return {
257 fwd(x1.m,i,j),
258 fwd(x1.a,i,j),
259 x1.da.row(x1.a.rows()*i+j), // centered form, ColMajor
260 x1.def_domain
261 };
262 }
263
264 inline void ComponentOp::bwd(const Interval& y, IntervalMatrix& x1, Index i, Index j)
265 {
266 assert(i >= 0 && i < x1.rows());
267 assert(j >= 0 && j < x1.cols());
268 x1(i,j) &= y;
269 }
270}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
A base class for expressions representing operations with multiple operands.
Definition codac2_ExprBase.h:164
OperationExprBase(std::shared_ptr< X >... x)
Definition codac2_ExprBase.h:176
std::tuple< std::shared_ptr< X >... > _x
Definition codac2_ExprBase.h:258