codac 2.0.0
Loading...
Searching...
No Matches
codac2_chi.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 // chi([x1],[x2],[x3]) = [x2] if (x1^+)<=0, [x3] if (x1^-)>0, hull([x2],[x3]) else
20
21 struct ChiOp
22 {
23 template<typename X1,typename X2,typename X3>
24 static std::string str(const X1& x1, const X2& x2, const X3& x3)
25 {
26 return "χ(" + x1->str() + "," + x2->str() + "," + x3->str() + ")";
27 }
28
29 template<typename X1, typename X2, typename X3>
30 static std::pair<Index,Index> output_shape([[maybe_unused]] const X1& s1, const X2& s2, [[maybe_unused]] const X3& s3)
31 {
32 auto shape2 = s2->output_shape();
33 assert(shape2 == s3->output_shape());
34 return shape2;
35 }
36
37 template<typename T>
38 static inline T fwd(const Interval& x1, const T& x2, const T& x3)
39 {
40 // Copy of chi from codac2_Interval_operations_impl.h
41 if(x1.ub() <= 0)
42 return x2;
43 else if(x1.lb() > 0)
44 return x3;
45 else
46 return x2 | x3;
47 }
48
49 template<typename T>
50 static inline T fwd_natural(const ScalarType& x1, const T& x2, const T& x3)
51 {
52 // A change of branch may introduce a discontinuity.
53 return {
54 fwd(x1.a,x2.a,x3.a),
55 x1.def_domain
56 && ((x1.a.ub() <= 0. && x2.def_domain)
57 || (x1.a.lb() > 0. && x3.def_domain))
58 };
59 }
60
61 template<typename T>
62 static inline T fwd_centered(const ScalarType& x1, const T& x2, const T& x3)
63 {
64 if(centered_form_not_available_for_args(x1,x2,x3))
65 return fwd_natural(x1,x2,x3);
66
67 assert(x2.da.rows() == x3.da.rows());
68
69 return {
70 fwd(x1.m,x2.m,x3.m),
71 fwd(x1.a,x2.a,x3.a),
72 (x1.a.lb()>0 ? x3.da : ((x1.a.ub()<=0) ? x2.da : (x2.da | x3.da))),
73 x1.def_domain
74 && ((x1.a.ub() <= 0. && x2.def_domain)
75 || (x1.a.lb() > 0. && x3.def_domain))
76 };
77 }
78
79 template<typename T>
80 static inline void bwd(const T& y, Interval& x1, T& x2, T& x3)
81 {
82 // The content of this function comes from the IBEX library.
83 // See ibex::Interval (IBEX lib, main author: Gilles Chabert)
84 // https://ibex-lib.readthedocs.io
85 // Some updates have been proposed by Damien Massé.
86
87 if(x1.ub() <= 0)
88 {
89 if((x2 &= y).is_empty())
90 {
91 x1.set_empty();
92 x3.set_empty();
93 }
94 }
95
96 else if(x1.lb() > 0)
97 {
98 if((x3 &= y).is_empty())
99 {
100 x1.set_empty();
101 x2.set_empty();
102 }
103 }
104
105 if(y.is_disjoint(x2))
106 {
107 if((x1 &= Interval(0,oo)).is_empty())
108 {
109 x2.set_empty();
110 x3.set_empty();
111 }
112
113 if((x3 &= y).is_empty())
114 {
115 x1.set_empty();
116 x2.set_empty();
117 }
118 }
119
120 if(y.is_disjoint(x3))
121 {
122 if((x1 &= Interval(-oo,0)).is_empty())
123 {
124 x2.set_empty();
125 x3.set_empty();
126 }
127
128 if((x2 &= y).is_empty())
129 {
130 x1.set_empty();
131 x3.set_empty();
132 }
133 }
134 }
135 };
136
137 // Analytic operator
138 // The following function can be used to build analytic expressions.
139
140 template<typename T2,typename T3,typename T=ExprType<T2>::Type>
141 inline AnalyticExprWrapper<T>
142 chi(const ScalarExpr& x1, const T2& x2, const T3& x3)
143 {
144 return { std::make_shared<AnalyticOperationExpr<ChiOp,T,ScalarType,T,T>>(
145 x1,(AnalyticExprWrapper<T>)x2,(AnalyticExprWrapper<T>)x3) };
146 }
147
148}
bool is_empty() const
Checks whether the interval matrix is empty.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:56
Definition codac2_OctaSym.h:21
Interval chi(const Interval &x, const Interval &y, const Interval &z)
Return if , if , else.
Definition codac2_Interval_operations_impl.h:299