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 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 min(const ScalarExpr& x1, const ScalarExpr& x2)
31 {
32 return { std::make_shared<AnalyticOperationExpr<MinOp,ScalarType,ScalarType,ScalarType>>(x1,x2) };
33 }
34
35 // Inline functions
36
37 inline Interval MinOp::fwd(const Interval& x1, const Interval& x2)
38 {
39 return min(x1,x2);
40 }
41
42 inline ScalarType MinOp::fwd_natural(const ScalarType& x1, const ScalarType& x2)
43 {
44 return {
45 fwd(x1.a, x2.a),
46 x1.def_domain && x2.def_domain
47 };
48 }
49
50 inline ScalarType MinOp::fwd_centered(const ScalarType& x1, const ScalarType& x2)
51 {
52 if(centered_form_not_available_for_args(x1,x2))
53 return fwd_natural(x1,x2);
54
55 assert(x1.da.rows() == 1);
56 assert(x1.da.rows() == x2.da.rows() && x1.da.cols() == x2.da.cols());
57
58 IntervalMatrix d(1,x1.da.cols());
59 for(Index i = 0 ; i < d.size() ; i++)
60 d(0,i) = chi(x1.a-x2.a, x1.da(0,i), x2.da(0,i));
61
62 return {
63 fwd(x1.m, x2.m),
64 fwd(x1.a, x2.a),
65 d,
66 x1.def_domain && x2.def_domain
67 && (x1.a != x2.a) // def domain of the derivative of min
68 };
69 }
70
71 inline void MinOp::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 Interval mx1 = -x1;
78 Interval mx2 = -x2;
79
80 MaxOp::bwd(-y,mx1,mx2);
81
82 if(mx1.is_empty() || mx2.is_empty())
83 {
84 x1.set_empty();
85 x2.set_empty();
86 }
87
88 else
89 {
90 x1 = -mx1;
91 x2 = -mx2;
92 }
93 }
94}
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