codac 1.5.6
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 static Interval fwd(const IntervalVector& x1, Index i);
21 static ScalarType fwd_natural(const VectorType& x1, Index i);
22 static ScalarType fwd_centered(const VectorType& x1, Index i);
23 static void bwd(const Interval& y, IntervalVector& x1, Index i);
24
25 static Interval fwd(const IntervalMatrix& x1, Index i, Index j);
26 static ScalarType fwd_natural(const MatrixType& x1, Index i, Index j);
27 static ScalarType fwd_centered(const MatrixType& x1, Index i, Index j);
28 static void bwd(const Interval& y, IntervalMatrix& x1, Index i, Index j);
29 };
30
31 // Analytic operator
32
33 template<>
34 class AnalyticOperationExpr<ComponentOp,ScalarType,VectorType> : public AnalyticExpr<ScalarType>, public OperationExprBase<AnalyticExpr<VectorType>>
35 {
36 public:
37
38 AnalyticOperationExpr(const std::shared_ptr<AnalyticExpr<VectorType>>& x1, Index i)
39 : OperationExprBase<AnalyticExpr<VectorType>>(x1), _i(i)
40 { }
41
42 AnalyticOperationExpr(const AnalyticOperationExpr& e)
43 : OperationExprBase<AnalyticExpr<VectorType>>(e), _i(e._i)
44 { }
45
46 std::shared_ptr<ExprBase> copy() const
47 {
48 return std::make_shared<AnalyticOperationExpr<ComponentOp,ScalarType,VectorType>>(*this);
49 }
50
51 void replace_expr(const ExprID& old_expr_id, const std::shared_ptr<ExprBase>& new_expr)
52 {
53 return OperationExprBase<AnalyticExpr<VectorType>>::replace_expr(old_expr_id, new_expr);
54 }
55
56 ScalarType fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
57 {
58 if(natural_eval)
59 return AnalyticExpr<ScalarType>::init_value(
60 v, ComponentOp::fwd_natural(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i));
61 else
62 return AnalyticExpr<ScalarType>::init_value(
63 v, ComponentOp::fwd_centered(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i));
64 }
65
66 void bwd_eval(ValuesMap& v) const
67 {
68 ComponentOp::bwd(AnalyticExpr<ScalarType>::value(v).a, std::get<0>(this->_x)->value(v).a, _i);
69 std::get<0>(this->_x)->bwd_eval(v);
70 }
71
72 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
73 {
74 return std::get<0>(this->_x)->belongs_to_args_list(args);
75 }
76
77 protected:
78
79 const Index _i;
80 };
81
82 template<>
83 class AnalyticOperationExpr<ComponentOp,ScalarType,MatrixType> : public AnalyticExpr<ScalarType>, public OperationExprBase<AnalyticExpr<MatrixType>>
84 {
85 public:
86
87 AnalyticOperationExpr(const std::shared_ptr<AnalyticExpr<MatrixType>>& x1, Index i, Index j)
88 : OperationExprBase<AnalyticExpr<MatrixType>>(x1), _i(i), _j(j)
89 { }
90
91 AnalyticOperationExpr(const AnalyticOperationExpr& e)
92 : OperationExprBase<AnalyticExpr<MatrixType>>(e), _i(e._i), _j(e._j)
93 { }
94
95 std::shared_ptr<ExprBase> copy() const
96 {
97 return std::make_shared<AnalyticOperationExpr<ComponentOp,ScalarType,MatrixType>>(*this);
98 }
99
100 void replace_expr(const ExprID& old_expr_id, const std::shared_ptr<ExprBase>& new_expr)
101 {
102 return OperationExprBase<AnalyticExpr<MatrixType>>::replace_expr(old_expr_id, new_expr);
103 }
104
105 ScalarType fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
106 {
107 if(natural_eval)
108 return AnalyticExpr<ScalarType>::init_value(
109 v, ComponentOp::fwd_natural(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
110 else
111 return AnalyticExpr<ScalarType>::init_value(
112 v, ComponentOp::fwd_centered(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
113 }
114
115 void bwd_eval(ValuesMap& v) const
116 {
117 ComponentOp::bwd(AnalyticExpr<ScalarType>::value(v).a, std::get<0>(this->_x)->value(v).a, _i, _j);
118 std::get<0>(this->_x)->bwd_eval(v);
119 }
120
121 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
122 {
123 return std::get<0>(this->_x)->belongs_to_args_list(args);
124 }
125
126 protected:
127
128 const Index _i, _j;
129 };
130
131 // Inline functions
132
133 // Vector component
134
135 inline Interval ComponentOp::fwd(const IntervalVector& x1, Index i)
136 {
137 assert(i >= 0 && i < x1.size());
138 return x1[i];
139 }
140
141 inline ScalarType ComponentOp::fwd_natural(const VectorType& x1, Index i)
142 {
143 assert(i >= 0 && i < x1.a.rows());
144 return {
145 fwd(x1.a,i),
146 x1.def_domain
147 };
148 }
149
150 inline ScalarType ComponentOp::fwd_centered(const VectorType& x1, Index i)
151 {
152 if(centered_form_not_available_for_args(x1))
153 return fwd_natural(x1,i);
154
155 assert(i >= 0 && i < x1.a.rows());
156 return {
157 fwd(x1.m,i),
158 fwd(x1.a,i),
159 x1.da.row(i),
160 x1.def_domain
161 };
162 }
163
164 inline void ComponentOp::bwd(const Interval& y, IntervalVector& x1, Index i)
165 {
166 assert(i >= 0 && i < x1.size());
167 x1[i] &= y;
168 }
169
170 // Matrix component
171
172 inline Interval ComponentOp::fwd(const IntervalMatrix& x1, Index i, Index j)
173 {
174 assert(i >= 0 && i < x1.rows());
175 assert(j >= 0 && j < x1.cols());
176 return x1(i,j);
177 }
178
179 inline ScalarType ComponentOp::fwd_natural(const MatrixType& x1, Index i, Index j)
180 {
181 assert(i >= 0 && i < x1.a.rows());
182 assert(j >= 0 && j < x1.a.cols());
183 return {
184 fwd(x1.a,i,j),
185 x1.def_domain
186 };
187 }
188
189 inline ScalarType ComponentOp::fwd_centered(const MatrixType& x1, Index i, Index j)
190 {
191 if(centered_form_not_available_for_args(x1))
192 return fwd_natural(x1,i,j);
193
194 assert(i >= 0 && i < x1.a.rows());
195 assert(j >= 0 && j < x1.a.cols());
196 return {
197 fwd(x1.m,i,j),
198 fwd(x1.a,i,j),
199 IntervalMatrix(0,0), // centered form not yet available
200 x1.def_domain
201 };
202 }
203
204 inline void ComponentOp::bwd(const Interval& y, IntervalMatrix& x1, Index i, Index j)
205 {
206 assert(i >= 0 && i < x1.rows());
207 assert(j >= 0 && j < x1.cols());
208 x1(i,j) &= y;
209 }
210}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62