codac 1.5.6
Loading...
Searching...
No Matches
codac2_arith_add.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
15#include "codac2_AnalyticType.h"
17
18namespace codac2
19{
20 struct AddOp
21 {
22 // Unary operations
23 static Interval fwd(const Interval& x1);
24 static ScalarType fwd_natural(const ScalarType& x1);
25 static ScalarType fwd_centered(const ScalarType& x1);
26 static void bwd(const Interval& y, Interval& x1);
27
28 static IntervalVector fwd(const IntervalVector& x1);
29 static VectorType fwd_natural(const VectorType& x1);
30 static VectorType fwd_centered(const VectorType& x1);
31 static void bwd(const IntervalVector& y, IntervalVector& x1);
32
33 static IntervalMatrix fwd(const IntervalMatrix& x1);
34 static MatrixType fwd_natural(const MatrixType& x1);
35 static MatrixType fwd_centered(const MatrixType& x1);
36 static void bwd(const IntervalMatrix& y, IntervalMatrix& x1);
37
38 // Binary operations
39 static Interval fwd(const Interval& x1, const Interval& x2);
40 static ScalarType fwd_natural(const ScalarType& x1, const ScalarType& x2);
41 static ScalarType fwd_centered(const ScalarType& x1, const ScalarType& x2);
42 static void bwd(const Interval& y, Interval& x1, Interval& x2);
43
44 static IntervalVector fwd(const IntervalVector& x1, const IntervalVector& x2);
45 static VectorType fwd_natural(const VectorType& x1, const VectorType& x2);
46 static VectorType fwd_centered(const VectorType& x1, const VectorType& x2);
47 static void bwd(const IntervalVector& y, IntervalVector& x1, IntervalVector& x2);
48
49 static IntervalMatrix fwd(const IntervalMatrix& x1, const IntervalMatrix& x2);
50 static MatrixType fwd_natural(const MatrixType& x1, const MatrixType& x2);
51 static MatrixType fwd_centered(const MatrixType& x1, const MatrixType& x2);
52 static void bwd(const IntervalMatrix& y, IntervalMatrix& x1, IntervalMatrix& x2);
53 };
54
55 // operator+ (unary case)
56 // The following functions can be used to build analytic expressions.
57
58 inline ScalarExpr
59 operator+(const ScalarExpr& x1)
60 {
61 return { std::make_shared<AnalyticOperationExpr<AddOp,ScalarType,ScalarType>>(x1) };
62 }
63
64 inline VectorExpr
65 operator+(const VectorExpr& x1)
66 {
67 return { std::make_shared<AnalyticOperationExpr<AddOp,VectorType,VectorType>>(x1) };
68 }
69
70 inline MatrixExpr
71 operator+(const MatrixExpr& x1)
72 {
73 return { std::make_shared<AnalyticOperationExpr<AddOp,MatrixType,MatrixType>>(x1) };
74 }
75
76 // operator+
77 // The following functions can be used to build analytic expressions.
78
79 inline ScalarExpr
80 operator+(ScalarExpr x1, ScalarExpr x2)
81 {
82 return { std::make_shared<AnalyticOperationExpr<AddOp,ScalarType,ScalarType,ScalarType>>(x1,x2) };
83 }
84
85 inline VectorExpr
86 operator+(VectorExpr x1, VectorExpr x2)
87 {
88 return { std::make_shared<AnalyticOperationExpr<AddOp,VectorType,VectorType,VectorType>>(x1,x2) };
89 }
90
91 inline MatrixExpr
92 operator+(MatrixExpr x1, MatrixExpr x2)
93 {
94 return { std::make_shared<AnalyticOperationExpr<AddOp,MatrixType,MatrixType,MatrixType>>(x1,x2) };
95 }
96
97 // Inline functions
98
99 inline Interval AddOp::fwd(const Interval& x1)
100 {
101 return x1;
102 }
103
104 inline ScalarType AddOp::fwd_natural(const ScalarType& x1)
105 {
106 return {
107 fwd(x1.a),
108 x1.def_domain
109 };
110 }
111
112 inline ScalarType AddOp::fwd_centered(const ScalarType& x1)
113 {
114 return {
115 fwd(x1.m),
116 fwd(x1.a),
117 x1.da,
118 x1.def_domain
119 };
120 }
121
122 inline void AddOp::bwd([[maybe_unused]] const Interval& y, [[maybe_unused]] Interval& x1)
123 { }
124
125 inline IntervalVector AddOp::fwd(const IntervalVector& x1)
126 {
127 return x1;
128 }
129
130 inline VectorType AddOp::fwd_natural(const VectorType& x1)
131 {
132 return {
133 fwd(x1.a),
134 x1.def_domain
135 };
136 }
137
138 inline VectorType AddOp::fwd_centered(const VectorType& x1)
139 {
140 return {
141 fwd(x1.m),
142 fwd(x1.a),
143 x1.da,
144 x1.def_domain
145 };
146 }
147
148 inline void AddOp::bwd([[maybe_unused]] const IntervalVector& y, [[maybe_unused]] IntervalVector& x1)
149 {
150 assert(y.size() == x1.size());
151 }
152
153 inline IntervalMatrix AddOp::fwd(const IntervalMatrix& x1)
154 {
155 return x1;
156 }
157
158 inline MatrixType AddOp::fwd_natural(const MatrixType& x1)
159 {
160 return {
161 fwd(x1.a),
162 x1.def_domain
163 };
164 }
165
166 inline MatrixType AddOp::fwd_centered(const MatrixType& x1)
167 {
168 if(centered_form_not_available_for_args(x1))
169 return fwd_natural(x1);
170
171 return {
172 fwd(x1.m),
173 fwd(x1.a),
174 IntervalMatrix(0,0), // not supported yet for matrices
175 x1.def_domain
176 };
177 }
178
179 inline void AddOp::bwd([[maybe_unused]] const IntervalMatrix& y, [[maybe_unused]] IntervalMatrix& x1)
180 {
181 assert(y.size() == x1.size());
182 }
183
184 inline Interval AddOp::fwd(const Interval& x1, const Interval& x2)
185 {
186 return x1 + x2;
187 }
188
189 inline ScalarType AddOp::fwd_natural(const ScalarType& x1, const ScalarType& x2)
190 {
191 return {
192 fwd(x1.a, x2.a),
193 x1.def_domain && x2.def_domain
194 };
195 }
196
197 inline ScalarType AddOp::fwd_centered(const ScalarType& x1, const ScalarType& x2)
198 {
199 if(centered_form_not_available_for_args(x1,x2))
200 return fwd_natural(x1,x2);
201
202 assert(x1.da.size() == x2.da.size());
203 return {
204 fwd(x1.m, x2.m),
205 fwd(x1.a, x2.a),
206 x1.da + x2.da,
207 x1.def_domain && x2.def_domain
208 };
209 }
210
211 inline void AddOp::bwd(const Interval& y, Interval& x1, Interval& x2)
212 {
213 if((x1 &= y-x2).is_empty())
214 x2.set_empty();
215
216 else if((x2 &= y-x1).is_empty())
217 x1.set_empty();
218 }
219
220 inline IntervalVector AddOp::fwd(const IntervalVector& x1, const IntervalVector& x2)
221 {
222 assert(x1.size() == x2.size());
223 return x1 + x2;
224 }
225
226 inline VectorType AddOp::fwd_natural(const VectorType& x1, const VectorType& x2)
227 {
228 return {
229 fwd(x1.a, x2.a),
230 x1.def_domain && x2.def_domain
231 };
232 }
233
234 inline VectorType AddOp::fwd_centered(const VectorType& x1, const VectorType& x2)
235 {
236 if(centered_form_not_available_for_args(x1,x2))
237 return fwd_natural(x1,x2);
238
239 assert(x1.da.rows() == x2.da.rows() && x1.da.cols() == x2.da.cols());
240 return {
241 fwd(x1.m, x2.m),
242 fwd(x1.a, x2.a),
243 x1.da + x2.da,
244 x1.def_domain && x2.def_domain
245 };
246 }
247
248 inline void AddOp::bwd(const IntervalVector& y, IntervalVector& x1, IntervalVector& x2)
249 {
250 assert(y.size() == x1.size() && y.size() == x2.size());
251 for(Index i = 0 ; i < y.size() ; i++)
252 AddOp::bwd(y[i], x1[i], x2[i]);
253 }
254
255 inline IntervalMatrix AddOp::fwd(const IntervalMatrix& x1, const IntervalMatrix& x2)
256 {
257 assert(x1.size() == x2.size());
258 return x1 + x2;
259 }
260
261 inline MatrixType AddOp::fwd_natural(const MatrixType& x1, const MatrixType& x2)
262 {
263 return {
264 fwd(x1.a, x2.a),
265 x1.def_domain && x2.def_domain
266 };
267 }
268
269 inline MatrixType AddOp::fwd_centered(const MatrixType& x1, const MatrixType& x2)
270 {
271 return {
272 fwd(x1.m, x2.m),
273 fwd(x1.a, x2.a),
274 IntervalMatrix(0,0), // not supported yet for matrices
275 x1.def_domain && x2.def_domain
276 };
277 }
278
279 inline void AddOp::bwd(const IntervalMatrix& y, IntervalMatrix& x1, IntervalMatrix& x2)
280 {
281 assert(y.size() == x1.size() && y.size() == x2.size());
282 for(Index i = 0 ; i < y.size() ; i++)
283 AddOp::bwd(*(y.data()+i), *(x1.data()+i), *(x2.data()+i));
284 }
285}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Ellipsoid operator+(const Ellipsoid &e1, const Ellipsoid &e2)
Compute the Minkowski sum of two ellipsoids.