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"
15
16namespace codac2
17{
18 // chi([x1],[x2],[x3]) = [x2] if (x1^+)<=0, [x3] if (x1^-)>0, hull([x2],[x3]) else
19 struct ChiOp
20 {
21 template<typename X1,typename X2,typename X3>
22 static std::string str(const X1& x1, const X2& x2, const X3& x3)
23 {
24 return "χ(" + x1->str() + "," + x2->str() + "," + x3->str() + ")";
25 }
26
27 template<typename X1, typename X2, typename X3>
28 static std::pair<Index,Index> output_shape([[maybe_unused]] const X1& s1, const X2& s2, const X3& s3)
29 {
30 auto shape2 = s2->output_shape();
31 auto shape3 = s3->output_shape();
32 assert(shape2 == shape3);
33 return shape2;
34 }
35
36 static Interval fwd(const Interval& x1, const Interval& x2, const Interval& x3);
37 static ScalarType fwd_natural(const ScalarType& x1, const ScalarType& x2, const ScalarType& x3);
38 static ScalarType fwd_centered(const ScalarType& x1, const ScalarType& x2, const ScalarType& x3);
39 static void bwd(const Interval& y, Interval& x1, Interval& x2, Interval& x3);
40 };
41
42 // Analytic operator
43 // The following function can be used to build analytic expressions.
44
45 inline ScalarExpr
46 chi(const ScalarExpr& x1, const ScalarExpr& x2, const ScalarExpr& x3)
47 {
48 return { std::make_shared<AnalyticOperationExpr<ChiOp,ScalarType,ScalarType,ScalarType,ScalarType>>(x1,x2,x3) };
49 }
50
51 // Inline functions
52
53 inline Interval ChiOp::fwd(const Interval& x1, const Interval& x2, const Interval& x3)
54 {
55 return chi(x1,x2,x3);
56 }
57
58 inline ScalarType ChiOp::fwd_natural(const ScalarType& x1, const ScalarType& x2, const ScalarType& x3)
59 {
60 return {
61 fwd(x1.a,x2.a,x3.a),
62 x1.def_domain && x2.def_domain && x3.def_domain
63 };
64 }
65
66 inline ScalarType ChiOp::fwd_centered(const ScalarType& x1, const ScalarType& x2, const ScalarType& x3)
67 {
68 return {
69 fwd(x1.m,x2.m,x3.m),
70 fwd(x1.a,x2.a,x3.a),
71 IntervalMatrix(0,0), // not supported yet for auto diff
72 x1.def_domain && x2.def_domain && x3.def_domain
73 };
74 }
75
76 inline void ChiOp::bwd(const Interval& y, Interval& x1, Interval& x2, Interval& x3)
77 {
78 // The content of this function comes from the IBEX library.
79 // See ibex::Interval (IBEX lib, main author: Gilles Chabert)
80 // https://ibex-lib.readthedocs.io
81
82 if(x1.ub() <= 0)
83 {
84 if((x2 &= y).is_empty())
85 {
86 x1.set_empty();
87 x3.set_empty();
88 }
89 }
90
91 else if(x1.lb() > 0)
92 {
93 if((x3 &= y).is_empty())
94 {
95 x1.set_empty();
96 x2.set_empty();
97 }
98 }
99
100 if(y.is_disjoint(x2))
101 {
102 if((x1 &= Interval(0,oo)).is_empty())
103 {
104 x2.set_empty();
105 x3.set_empty();
106 }
107
108 if((x3 &= y).is_empty())
109 {
110 x1.set_empty();
111 x2.set_empty();
112 }
113 }
114
115 if(y.is_disjoint(x3))
116 {
117 if((x1 &= Interval(-oo,0)).is_empty())
118 {
119 x2.set_empty();
120 x3.set_empty();
121 }
122
123 if((x2 &= y).is_empty())
124 {
125 x1.set_empty();
126 x3.set_empty();
127 }
128 }
129 }
130}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval chi(const Interval &x, const Interval &y, const Interval &z)
Return if , if , else.
Definition codac2_Interval_operations_impl.h:299