codac 2.0.0
Loading...
Searching...
No Matches
codac2_max.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 struct MaxOp
19 {
20 template<typename X1,typename X2>
21 static std::string str(const X1& x1, const X2& x2)
22 {
23 return "max(" + x1->str() + "," + x2->str() + ")";
24 }
25
26 template<typename X1, typename X2>
27 static std::pair<Index,Index> output_shape([[maybe_unused]] const X1& s1, [[maybe_unused]] const X2& s2)
28 {
29 return {1,1};
30 }
31
32 static Interval fwd(const Interval& x1, const Interval& x2);
33 static ScalarType fwd_natural(const ScalarType& x1, const ScalarType& x2);
34 static ScalarType fwd_centered(const ScalarType& x1, const ScalarType& x2);
35 static void bwd(const Interval& y, Interval& x1, Interval& x2);
36 };
37
38 // Analytic operator
39 // The following function can be used to build analytic expressions.
40
41 inline ScalarExpr
42 max(const ScalarExpr& x1, const ScalarExpr& x2)
43 {
44 return { std::make_shared<AnalyticOperationExpr<MaxOp,ScalarType,ScalarType,ScalarType>>(x1,x2) };
45 }
46
47 inline ScalarType MaxOp::fwd_natural(const ScalarType& x1, const ScalarType& x2)
48 {
49 return {
50 fwd(x1.a, x2.a),
51 x1.def_domain && x2.def_domain
52 };
53 }
54
55 inline ScalarType MaxOp::fwd_centered(const ScalarType& x1, const ScalarType& x2)
56 {
57 if(centered_form_not_available_for_args(x1,x2))
58 return fwd_natural(x1,x2);
59
60 assert(x1.da.rows() == 1);
61 assert(x1.da.rows() == x2.da.rows() && x1.da.cols() == x2.da.cols());
62
63 IntervalMatrix d(1,x1.da.cols());
64 for(Index i = 0 ; i < d.size() ; i++)
65 d(0,i) = chi(x1.a-x2.a, x2.da(0,i), x1.da(0,i));
66
67 return {
68 fwd(x1.m, x2.m),
69 fwd(x1.a, x2.a),
70 d,
71 x1.def_domain && x2.def_domain
72 && (x1.a != x2.a) // def domain of the derivative of max
73 };
74 }
75
76 // Inline functions
77
78 inline Interval MaxOp::fwd(const Interval& x1, const Interval& x2)
79 {
80 return max(x1,x2);
81 }
82
83 inline void MaxOp::bwd(const Interval& y, Interval& x1, Interval& x2)
84 {
85 // The content of this function comes from the IBEX library.
86 // See ibex::Interval (IBEX lib, main author: Gilles Chabert)
87 // https://ibex-lib.readthedocs.io
88
89 if(y.is_empty())
90 {
91 x1.set_empty();
92 x2.set_empty();
93 }
94
95 // Disjoint intervals
96 else if(x2.lb() > x1.ub() || y.lb() > x1.ub())
97 {
98 // Then, max(x,x2) is necessarily x2
99 if((x2 &= y).is_empty())
100 x1.set_empty();
101 }
102
103 else if(x1.lb() > x2.ub() || y.lb() > x2.ub())
104 {
105 if((x1 &= y).is_empty())
106 x2.set_empty();
107 }
108
109 else if(y.ub() < x1.lb() || y.ub() < x2.lb())
110 {
111 x1.set_empty();
112 x2.set_empty();
113 }
114
115 else
116 {
117 // At this point, x1, x2 and y all mutually intersect
118
119 if(x1.ub() > y.ub())
120 x1 = Interval(x1.lb(),y.ub());
121
122 if(x2.ub() > y.ub())
123 x2 = Interval(x2.lb(),y.ub());
124 }
125 }
126}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval max(const Interval &x, const Interval &y)
Returns .
Definition codac2_Interval_operations_impl.h:274
Interval chi(const Interval &x, const Interval &y, const Interval &z)
Return if , if , else.
Definition codac2_Interval_operations_impl.h:299