codac 2.0.0
Loading...
Searching...
No Matches
codac2_floor.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 FloorOp
19 {
20 template<typename X1>
21 static std::string str(const X1& x1)
22 {
23 return "⌊" + x1->str() + "⌋";
24 }
25
26 template<typename X1>
27 static std::pair<Index,Index> output_shape([[maybe_unused]] const X1& s1)
28 {
29 return {1,1};
30 }
31
32 static Interval fwd(const Interval& x1);
33 static ScalarType fwd_natural(const ScalarType& x1);
34 static ScalarType fwd_centered(const ScalarType& x1);
35 static void bwd(const Interval& y, Interval& x1);
36 };
37
38 // Analytic operator
39 // The following function can be used to build analytic expressions.
40
41 inline ScalarExpr
42 floor(const ScalarExpr& x1)
43 {
44 return { std::make_shared<AnalyticOperationExpr<FloorOp,ScalarType,ScalarType>>(x1) };
45 }
46
47 // Inline functions
48
49 inline Interval FloorOp::fwd(const Interval& x1)
50 {
51 return floor(x1);
52 }
53
54 inline ScalarType FloorOp::fwd_natural(const ScalarType& x1)
55 {
56 return {
57 fwd(x1.a),
58 x1.def_domain
59 };
60 }
61
62 inline ScalarType FloorOp::fwd_centered(const ScalarType& x1)
63 {
64 if(centered_form_not_available_for_args(x1))
65 return fwd_natural(x1);
66
67 return {
68 fwd(x1.m),
69 fwd(x1.a),
70 IntervalMatrix(0,0), // not supported yet for auto diff
71 x1.def_domain
72 };
73 }
74
75 inline void FloorOp::bwd(const Interval& y, Interval& x1)
76 {
77 // The content of this function comes from the IBEX library.
78 // See ibex::Interval (IBEX lib, main author: Gilles Chabert)
79 // https://ibex-lib.readthedocs.io
80
81 if(y.is_empty())
82 x1.set_empty();
83
84 else
85 {
86 double r = std::floor(y.ub());
87 double l = std::ceil(y.lb());
88
89 if(r < l)
90 x1.set_empty();
91
92 else
93 x1 &= Interval(l,r) + Interval(0,1);
94 }
95 }
96}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval floor(const Interval &x)
Returns floor of .
Definition codac2_Interval_operations_impl.h:289