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