codac 1.5.6
Loading...
Searching...
No Matches
codac2_arith_sub.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 SubOp
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<SubOp,ScalarType,ScalarType>>(x1) };
62 }
63
64 inline VectorExpr
65 operator-(const VectorExpr& x1)
66 {
67 return { std::make_shared<AnalyticOperationExpr<SubOp,VectorType,VectorType>>(x1) };
68 }
69
70 inline MatrixExpr
71 operator-(const MatrixExpr& x1)
72 {
73 return { std::make_shared<AnalyticOperationExpr<SubOp,MatrixType,MatrixType>>(x1) };
74 }
75
76 // operator-
77 // The following functions can be used to build analytic expressions.
78
79 inline ScalarExpr
80 operator-(const ScalarExpr& x1, const ScalarExpr& x2)
81 {
82 return { std::make_shared<AnalyticOperationExpr<SubOp,ScalarType,ScalarType,ScalarType>>(x1,x2) };
83 }
84
85 inline VectorExpr
86 operator-(const VectorExpr& x1, const VectorExpr& x2)
87 {
88 return { std::make_shared<AnalyticOperationExpr<SubOp,VectorType,VectorType,VectorType>>(x1,x2) };
89 }
90
91 inline MatrixExpr
92 operator-(const MatrixExpr& x1, const MatrixExpr& x2)
93 {
94 return { std::make_shared<AnalyticOperationExpr<SubOp,MatrixType,MatrixType,MatrixType>>(x1,x2) };
95 }
96
97 // Inline functions
98
99 inline Interval SubOp::fwd(const Interval& x1)
100 {
101 return -x1;
102 }
103
104 inline ScalarType SubOp::fwd_natural(const ScalarType& x1)
105 {
106 return {
107 fwd(x1.a),
108 x1.def_domain
109 };
110 }
111
112 inline ScalarType SubOp::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 SubOp::bwd(const Interval& y, Interval& x1)
123 {
124 Interval x2_(0.);
125 SubOp::bwd(y, x2_, x1);
126 }
127
128 inline IntervalVector SubOp::fwd(const IntervalVector& x1)
129 {
130 return -x1;
131 }
132
133 inline VectorType SubOp::fwd_natural(const VectorType& x1)
134 {
135 return {
136 fwd(x1.a),
137 x1.def_domain
138 };
139 }
140
141 inline VectorType SubOp::fwd_centered(const VectorType& x1)
142 {
143 return {
144 fwd(x1.m),
145 fwd(x1.a),
146 -x1.da,
147 x1.def_domain
148 };
149 }
150
151 inline void SubOp::bwd(const IntervalVector& y, IntervalVector& x1)
152 {
153 assert(y.size() == x1.size());
154 for(Index i = 0 ; i < y.size() ; i++)
155 bwd(y[i], x1[i]);
156 }
157
158 inline IntervalMatrix SubOp::fwd(const IntervalMatrix& x1)
159 {
160 return -x1;
161 }
162
163 inline MatrixType SubOp::fwd_natural(const MatrixType& x1)
164 {
165 return {
166 fwd(x1.a),
167 x1.def_domain
168 };
169 }
170
171 inline MatrixType SubOp::fwd_centered(const MatrixType& x1)
172 {
173 return {
174 fwd(x1.m),
175 fwd(x1.a),
176 IntervalMatrix(0,0), // not supported yet for matrices
177 x1.def_domain
178 };
179 }
180
181 inline void SubOp::bwd(const IntervalMatrix& y, IntervalMatrix& x1)
182 {
183 assert(y.size() == x1.size());
184 for(Index i = 0 ; i < y.size() ; i++)
185 SubOp::bwd(*(y.data()+i), *(x1.data()+i));
186 }
187
188 inline Interval SubOp::fwd(const Interval& x1, const Interval& x2)
189 {
190 return x1 - x2;
191 }
192
193 inline ScalarType SubOp::fwd_natural(const ScalarType& x1, const ScalarType& x2)
194 {
195 return {
196 fwd(x1.a, x2.a),
197 x1.def_domain && x2.def_domain
198 };
199 }
200
201 inline ScalarType SubOp::fwd_centered(const ScalarType& x1, const ScalarType& x2)
202 {
203 if(centered_form_not_available_for_args(x1,x2))
204 return fwd_natural(x1,x2);
205
206 assert(x1.da.rows() == x2.da.rows() && x1.da.cols() == x2.da.cols());
207 return {
208 fwd(x1.m, x2.m),
209 fwd(x1.a, x2.a),
210 x1.da - x2.da,
211 x1.def_domain && x2.def_domain
212 };
213 }
214
215 inline void SubOp::bwd(const Interval& y, Interval& x1, Interval& x2)
216 {
217 if((x1 &= y+x2).is_empty())
218 x2.set_empty();
219
220 else if((x2 &= x1-y).is_empty())
221 x1.set_empty();
222 }
223
224 inline IntervalVector SubOp::fwd(const IntervalVector& x1, const IntervalVector& x2)
225 {
226 assert(x1.size() == x2.size());
227 return x1 - x2;
228 }
229
230 inline VectorType SubOp::fwd_natural(const VectorType& x1, const VectorType& x2)
231 {
232 return {
233 fwd(x1.a, x2.a),
234 x1.def_domain && x2.def_domain
235 };
236 }
237
238 inline VectorType SubOp::fwd_centered(const VectorType& x1, const VectorType& x2)
239 {
240 if(centered_form_not_available_for_args(x1,x2))
241 return fwd_natural(x1,x2);
242
243 assert(x1.da.rows() == x2.da.rows() && x1.da.cols() == x2.da.cols());
244 return {
245 fwd(x1.m, x2.m),
246 fwd(x1.a, x2.a),
247 x1.da - x2.da,
248 x1.def_domain && x2.def_domain
249 };
250 }
251
252 inline void SubOp::bwd(const IntervalVector& y, IntervalVector& x1, IntervalVector& x2)
253 {
254 assert(y.size() == x1.size() && y.size() == x2.size());
255 for(Index i = 0 ; i < y.size() ; i++)
256 SubOp::bwd(y[i], x1[i], x2[i]);
257 }
258
259 inline IntervalMatrix SubOp::fwd(const IntervalMatrix& x1, const IntervalMatrix& x2)
260 {
261 assert(x1.size() == x2.size());
262 return x1 - x2;
263 }
264
265 inline MatrixType SubOp::fwd_natural(const MatrixType& x1, const MatrixType& x2)
266 {
267 assert(x1.a.cols() == x2.a.cols() && x1.a.rows() == x2.a.rows());
268 return {
269 fwd(x1.a, x2.a),
270 x1.def_domain && x2.def_domain
271 };
272 }
273
274 inline MatrixType SubOp::fwd_centered(const MatrixType& x1, const MatrixType& x2)
275 {
276 assert(x1.a.cols() == x2.a.cols() && x1.a.rows() == x2.a.rows());
277 return {
278 fwd(x1.m, x2.m),
279 fwd(x1.a, x2.a),
280 IntervalMatrix(0,0), // not supported yet for matrices
281 x1.def_domain && x2.def_domain
282 };
283 }
284
285 inline void SubOp::bwd(const IntervalMatrix& y, IntervalMatrix& x1, IntervalMatrix& x2)
286 {
287 assert(y.size() == x1.size() && y.size() == x2.size());
288 for(Index i = 0 ; i < y.size() ; i++)
289 SubOp::bwd(*(y.data()+i), *(x1.data()+i), *(x2.data()+i));
290 }
291}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval operator-(const Interval &x, double y)
Returns with .
Definition codac2_Interval_impl.h:389