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([[maybe_unused]] const X1& s1, [[maybe_unused]] Index i)
34 {
35 [[maybe_unused]] 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([[maybe_unused]] const X1& s1, [[maybe_unused]] Index i, [[maybe_unused]] Index j)
42 {
43 [[maybe_unused]] 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 Index i() const
122 {
123 return _i;
124 }
125
126 std::vector<std::shared_ptr<ExprBase>> children_expr_base() const override
127 {
129 }
130
131 protected:
132
133 const Index _i;
134 };
135
136 template<>
137 class AnalyticOperationExpr<ComponentOp,ScalarType,MatrixType> : public AnalyticExpr<ScalarType>, public OperationExprBase<AnalyticExpr<MatrixType>>
138 {
139 public:
140
141 AnalyticOperationExpr(const std::shared_ptr<AnalyticExpr<MatrixType>>& x1, Index i, Index j)
142 : OperationExprBase<AnalyticExpr<MatrixType>>(x1), _i(i), _j(j)
143 { }
144
145 AnalyticOperationExpr(const AnalyticOperationExpr& e)
146 : OperationExprBase<AnalyticExpr<MatrixType>>(e), _i(e._i), _j(e._j)
147 { }
148
149 std::shared_ptr<ExprBase> copy() const
150 {
151 return std::make_shared<AnalyticOperationExpr<ComponentOp,ScalarType,MatrixType>>(*this);
152 }
153
154 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
155 {
156 return OperationExprBase<AnalyticExpr<MatrixType>>::replace_arg(old_arg_id, new_expr);
157 }
158
159 ScalarType fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
160 {
161 if(natural_eval)
162 return AnalyticExpr<ScalarType>::init_value(
163 v, ComponentOp::fwd_natural(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
164 else
165 return AnalyticExpr<ScalarType>::init_value(
166 v, ComponentOp::fwd_centered(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
167 }
168
169 void bwd_eval(ValuesMap& v) const
170 {
171 ComponentOp::bwd(AnalyticExpr<ScalarType>::value(v).a, std::get<0>(this->_x)->value(v).a, _i, _j);
172 std::get<0>(this->_x)->bwd_eval(v);
173 }
174
175 std::pair<Index,Index> output_shape() const
176 {
177 return ComponentOp::output_shape(std::get<0>(this->_x),_i,_j);
178 }
179
180 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
181 {
182 return std::get<0>(this->_x)->belongs_to_args_list(args);
183 }
184
185 std::string str(bool in_parentheses = false) const
186 {
187 std::string s = ComponentOp::str(std::get<0>(this->_x), _i, _j);
188 return in_parentheses ? "(" + s + ")" : s;
189 }
190
191 virtual bool is_str_leaf() const
192 {
193 return true;
194 }
195
196 Index i() const
197 {
198 return _i;
199 }
200
201 Index j() const
202 {
203 return _j;
204 }
205
206 std::vector<std::shared_ptr<ExprBase>> children_expr_base() const override
207 {
209 }
210
211 protected:
212
213 const Index _i, _j;
214 };
215
216 // Inline functions
217
218 // Vector component
219
220 inline Interval ComponentOp::fwd(const IntervalVector& x1, Index i)
221 {
222 assert(i >= 0 && i < x1.size());
223 return x1[i];
224 }
225
226 inline ScalarType ComponentOp::fwd_natural(const VectorType& x1, Index i)
227 {
228 assert(i >= 0 && i < x1.a.rows());
229 return {
230 fwd(x1.a,i),
231 x1.def_domain
232 };
233 }
234
235 inline ScalarType ComponentOp::fwd_centered(const VectorType& x1, Index i)
236 {
237 if(centered_form_not_available_for_args(x1))
238 return fwd_natural(x1,i);
239
240 assert(i >= 0 && i < x1.a.rows());
241 return {
242 fwd(x1.m,i),
243 fwd(x1.a,i),
244 x1.da.row(i),
245 x1.def_domain
246 };
247 }
248
249 inline void ComponentOp::bwd(const Interval& y, IntervalVector& x1, Index i)
250 {
251 assert(i >= 0 && i < x1.size());
252 x1[i] &= y;
253 }
254
255 // Matrix component
256
257 inline Interval ComponentOp::fwd(const IntervalMatrix& x1, Index i, Index j)
258 {
259 assert(i >= 0 && i < x1.rows());
260 assert(j >= 0 && j < x1.cols());
261 return x1(i,j);
262 }
263
264 inline ScalarType ComponentOp::fwd_natural(const MatrixType& x1, Index i, Index j)
265 {
266 assert(i >= 0 && i < x1.a.rows());
267 assert(j >= 0 && j < x1.a.cols());
268 return {
269 fwd(x1.a,i,j),
270 x1.def_domain
271 };
272 }
273
274 inline ScalarType ComponentOp::fwd_centered(const MatrixType& x1, Index i, Index j)
275 {
276 if(centered_form_not_available_for_args(x1))
277 return fwd_natural(x1,i,j);
278
279 assert(i >= 0 && i < x1.a.rows());
280 assert(j >= 0 && j < x1.a.cols());
281 return {
282 fwd(x1.m,i,j),
283 fwd(x1.a,i,j),
284 x1.da.row(x1.a.rows()*i+j), // centered form, ColMajor
285 x1.def_domain
286 };
287 }
288
289 inline void ComponentOp::bwd(const Interval& y, IntervalMatrix& x1, Index i, Index j)
290 {
291 assert(i >= 0 && i < x1.rows());
292 assert(j >= 0 && j < x1.cols());
293 x1(i,j) &= y;
294 }
295}
virtual std::shared_ptr< ExprBase > copy() const =0
Creates a copy of the current expression.
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:49
A base class for expressions representing operations with multiple operands.
Definition codac2_ExprBase.h:178
std::vector< std::shared_ptr< ExprBase > > children_expr_base() const
Definition codac2_ExprBase.h:240
OperationExprBase(std::shared_ptr< X >... x)
Definition codac2_ExprBase.h:190
std::tuple< std::shared_ptr< X >... > _x
Definition codac2_ExprBase.h:292
Definition codac2_OctaSym.h:21
Eigen::Matrix< Interval,-1, 1 > IntervalVector
Alias for a dynamic-size column vector of intervals.
Definition codac2_IntervalVector.h:25
Eigen::Matrix< Interval,-1,-1 > IntervalMatrix
Alias for a dynamic-size matrix of intervals.
Definition codac2_IntervalMatrix.h:25