codac 2.0.0
Loading...
Searching...
No Matches
codac2_atan2.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#include "codac2_arith_div.h"
16
17namespace codac2
18{
19 struct Atan2Op
20 {
21 template<typename X1,typename X2>
22 static std::string str(const X1& x1, const X2& x2)
23 {
24 return "atan2(" + x1->str() + "," + x2->str() + ")";
25 }
26
27 template<typename X1, typename X2>
28 static std::pair<Index,Index> output_shape([[maybe_unused]] const X1& s1, [[maybe_unused]] const X2& s2)
29 {
30 return {1,1};
31 }
32
33 static Interval fwd(const Interval& x1, const Interval& x2);
34 static ScalarType fwd_natural(const ScalarType& x1, const ScalarType& x2);
35 static ScalarType fwd_centered(const ScalarType& x1, const ScalarType& x2);
36 static void bwd(const Interval& y, Interval& x1, Interval& x2);
37 };
38
39 // Analytic operator
40 // The following function can be used to build analytic expressions.
41
42 inline ScalarExpr
43 atan2(const ScalarExpr& x1, const ScalarExpr& x2)
44 {
45 return { std::make_shared<AnalyticOperationExpr<Atan2Op,ScalarType,ScalarType,ScalarType>>(x1,x2) };
46 }
47
48 // Inline functions
49
50 inline Interval Atan2Op::fwd(const Interval& x1, const Interval& x2)
51 {
52 return atan2(x1,x2);
53 }
54
55 inline ScalarType Atan2Op::fwd_natural(const ScalarType& x1, const ScalarType& x2)
56 {
57 return {
58 fwd(x1.a, x2.a),
59 x1.def_domain && x2.def_domain
60 && !(x1.a.contains(0.) && x2.a.lb()<=0.0) // def domain of the derivative of atan2
61 };
62 }
63
64 inline ScalarType Atan2Op::fwd_centered(const ScalarType& x1, const ScalarType& x2)
65 {
66 if(centered_form_not_available_for_args(x1,x2))
67 return fwd_natural(x1,x2);
68
69 assert(x1.da.rows() == 1);
70 assert(x1.da.rows() == x2.da.rows() && x1.da.cols() == x2.da.cols());
71
72 IntervalMatrix d(1,x1.da.cols());
73 for(Index i = 0 ; i < d.size() ; i++)
74 d(0,i) = (-x1.a*x2.da(0,i)+x2.a*x1.da(0,i))/(sqr(x2.a)+sqr(x1.a));
75
76 return {
77 fwd(x1.m, x2.m),
78 fwd(x1.a, x2.a),
79 d,
80 x1.def_domain && x2.def_domain
81 && !(x1.a.contains(0.) && x2.a.lb()<=0.) // def domain of the derivative of atan2
82 };
83 }
84
85 inline void Atan2Op::bwd(const Interval& y, Interval& x1, Interval& x2)
86 {
87 // The content of this function comes from the IBEX library.
88 // See ibex::Interval (IBEX lib, main author: Gilles Chabert)
89 // https://ibex-lib.readthedocs.io
90
91 // Probably not the most efficient implementation ever...
92
93 // half-plane x2>0
94 Interval y_x2pos = y & Interval(-1,1)*Interval::half_pi();
95 // upper left quadrant
96 Interval y_x2neg_x1pos = y & (Interval::half_pi() | Interval::pi());
97 // lower left quadrant
98 Interval y_x2neg_x1neg = y & -(Interval::half_pi() | Interval::pi());
99
100 Interval xres = Interval::empty();
101 Interval yres = Interval::empty();
102
103 if(!y_x2pos.is_empty())
104 {
105 Interval xpos = x2 & Interval(0,oo);
106 Interval yall = x1;
107
108 if(y_x2neg_x1pos.is_empty() || y_x2neg_x1neg.is_empty())
109 {
110 Interval z = yall/xpos;
111 AtanOp::bwd(y_x2pos,z);
112 DivOp::bwd(z,yall,xpos);
113 } // Otherwise, all is valid (useless to contract)
114
115 xres |= xpos;
116 yres |= yall;
117
118 // Because atan is not defined for pi/2 and -pi/2
119 if(y_x2pos.lb() >= Interval::half_pi().lb())
120 {
121 xres |= (x2 & Interval::zero());
122 yres |= (x1 & Interval(0,oo));
123 }
124 else if(y_x2pos.ub() <= -Interval::half_pi().lb())
125 {
126 xres |= (x2 & Interval::zero());
127 yres |= (x1 & Interval(-oo,0));
128 }
129 }
130
131 if(!y_x2neg_x1pos.is_empty())
132 {
133 Interval xneg = x2 & Interval(-oo,0);
134 Interval ypos = x1 & Interval(0,oo);
135 Interval z = ypos/xneg;
136 AtanOp::bwd(y_x2neg_x1pos - Interval::pi(),z);
137 DivOp::bwd(z,ypos,xneg);
138 xres |= xneg;
139 yres |= ypos;
140 }
141
142 if(!y_x2neg_x1neg.is_empty())
143 {
144 Interval xneg = x2 & Interval(-oo,0);
145 Interval yneg = x1 & Interval(-oo,0);
146 Interval z = yneg/xneg;
147 AtanOp::bwd(y_x2neg_x1neg + Interval::pi(),z);
148 DivOp::bwd(z,yneg,xneg);
149 xres |= xneg;
150 yres |= yneg;
151 }
152
153 x2 = xres;
154 x1 = yres;
155 }
156}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:50
static Interval pi()
Provides an interval for .
Definition codac2_Interval_impl.h:604
static Interval zero()
Provides an interval for .
Definition codac2_Interval_impl.h:589
double lb() const
Returns the lower bound of this.
Definition codac2_Interval_impl.h:126
static Interval empty()
Provides an empty interval.
Definition codac2_Interval_impl.h:584
static Interval half_pi()
Provides an interval for .
Definition codac2_Interval_impl.h:599
Definition codac2_OctaSym.h:21
Interval atan2(const Interval &y, const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:140
Interval sqr(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:21
Eigen::Matrix< Interval,-1,-1 > IntervalMatrix
Alias for a dynamic-size matrix of intervals.
Definition codac2_IntervalMatrix.h:25