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