codac 1.5.6
Loading...
Searching...
No Matches
codac2_min.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 MinOp
19 {
20 template<typename X1,typename X2>
21 static std::string str(const X1& x1, const X2& x2)
22 {
23 return "min(" + 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 min(const ScalarExpr& x1, const ScalarExpr& x2)
43 {
44 return { std::make_shared<AnalyticOperationExpr<MinOp,ScalarType,ScalarType,ScalarType>>(x1,x2) };
45 }
46
47 // Inline functions
48
49 inline Interval MinOp::fwd(const Interval& x1, const Interval& x2)
50 {
51 return min(x1,x2);
52 }
53
54 inline ScalarType MinOp::fwd_natural(const ScalarType& x1, const ScalarType& x2)
55 {
56 return {
57 fwd(x1.a, x2.a),
58 x1.def_domain && x2.def_domain
59 };
60 }
61
62 inline ScalarType MinOp::fwd_centered(const ScalarType& x1, const ScalarType& x2)
63 {
64 if(centered_form_not_available_for_args(x1,x2))
65 return fwd_natural(x1,x2);
66
67 assert(x1.da.rows() == 1);
68 assert(x1.da.rows() == x2.da.rows() && x1.da.cols() == x2.da.cols());
69
70 IntervalMatrix d(1,x1.da.cols());
71 for(Index i = 0 ; i < d.size() ; i++)
72 d(0,i) = chi(x1.a-x2.a, x1.da(0,i), x2.da(0,i));
73
74 return {
75 fwd(x1.m, x2.m),
76 fwd(x1.a, x2.a),
77 d,
78 x1.def_domain && x2.def_domain
79 && (x1.a != x2.a) // def domain of the derivative of min
80 };
81 }
82
83 inline void MinOp::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 Interval mx1 = -x1;
90 Interval mx2 = -x2;
91
92 MaxOp::bwd(-y,mx1,mx2);
93
94 if(mx1.is_empty() || mx2.is_empty())
95 {
96 x1.set_empty();
97 x2.set_empty();
98 }
99
100 else
101 {
102 x1 = -mx1;
103 x2 = -mx2;
104 }
105 }
106}
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
Interval min(const Interval &x, const Interval &y)
Returns .
Definition codac2_Interval_operations_impl.h:269