codac 1.5.6
Loading...
Searching...
No Matches
codac2_extend.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
13#include "codac2_AnalyticType.h"
16
17namespace codac2
18{
19 // extend(f(x),g(x)) = f(x) if x \in D_f, otherwise g(x)
20
21 struct ExtendOp
22 {
23 template<typename X1,typename X2>
24 static std::string str(const X1& x1, const X2& x2)
25 {
26 return "extend(" + x1->str() + "," + x2->str() + ")";
27 }
28
29 template<typename X1, typename X2>
30 static std::pair<Index,Index> output_shape(const X1& s1, [[maybe_unused]] const X2& s2)
31 {
32 auto shape1 = s1->output_shape();
33 assert(shape1 == s2->output_shape());
34 return shape1;
35 }
36
37 template<typename T>
38 static inline T fwd(const T& x1, const T& x2)
39 {
40 return x1 | x2;
41 }
42
43 template<typename T>
44 static inline T fwd_natural(const T& x1, const T& x2)
45 {
46 return {
47 (x1.def_domain ? x1.a : fwd(x1.a,x2.a)),
48 x1.def_domain || x2.def_domain
49 };
50 }
51
52 template<typename T>
53 static inline T fwd_centered(const T& x1, const T& x2)
54 {
55 return {
56 (x1.def_domain ? x1.m : fwd(x1.m,x2.m)),
57 (x1.def_domain ? x1.a : fwd(x1.a,x2.a)),
58 (x1.def_domain ? x1.da : (x1.da | x2.da)),
59 x1.def_domain || x2.def_domain
60 };
61 }
62
63 template<typename T>
64 static inline void bwd(const T& y, T& x1, T& x2)
65 {
66 // Any value in x1 must be in y
67 x1 &= y;
68 if (x1.is_empty())
69 {
70 // Only if x1 is empty, values of x2 must be in y
71 x2 &= y;
72 }
73 }
74 };
75
76 // Analytic operator
77 // The following function can be used to build analytic expressions.
78
79 template<typename T1,typename T2,typename T=ExprType<T1>::Type>
80 inline AnalyticExprWrapper<T>
81 extend(const T1& x1, const T2& x2)
82 {
83 return { std::make_shared<AnalyticOperationExpr<ExtendOp,T,T,T>>(
84 (AnalyticExprWrapper<T>)x1,(AnalyticExprWrapper<T>)x2) };
85 }
86}