codac 2.0.0
Loading...
Searching...
No Matches
codac2_pow.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 PowOp
19 {
20 template<typename X1,typename X2>
21 static std::string str(const X1& x1, const X2& x2)
22 {
23 return x1->str(!x1->is_str_leaf()) + "^" + x2->str(!x2->is_str_leaf());
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, int 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 pow(const ScalarExpr& x1, const ScalarExpr& x2)
44 {
45 return { std::make_shared<AnalyticOperationExpr<PowOp,ScalarType,ScalarType,ScalarType>>(x1,x2) };
46 }
47
48 inline ScalarExpr
49 operator^(const ScalarExpr& x1, const ScalarExpr& x2)
50 {
51 return { std::make_shared<AnalyticOperationExpr<PowOp,ScalarType,ScalarType,ScalarType>>(x1,x2) };
52 }
53
54 // Inline functions
55
56 inline Interval PowOp::fwd(const Interval& x1, const Interval& x2)
57 {
58 return pow(x1,x2);
59 }
60
61 inline ScalarType PowOp::fwd_natural(const ScalarType& x1, const ScalarType& x2)
62 {
63 return {
64 fwd(x1.a, x2.a),
65 x1.def_domain && x2.def_domain
66 };
67 }
68
69 inline ScalarType PowOp::fwd_centered(const ScalarType& x1, const ScalarType& x2)
70 {
71 if(centered_form_not_available_for_args(x1,x2))
72 return fwd_natural(x1,x2);
73
74 IntervalMatrix d(1,x1.da.size());
75 for(Index i = 0 ; i < d.size() ; i++)
76 d(0,i) = x2.a*x1.da(0,i)*pow(x1.a,x2.a-1.);
77
78 return {
79 fwd(x1.m, x2.m),
80 fwd(x1.a, x2.a),
81 d,
82 x1.def_domain && x2.def_domain
83 };
84 }
85
86 inline void PowOp::bwd(const Interval& y, Interval& x1, int x2)
87 {
88 // The content of this function comes from the IBEX library.
89 // See ibex::Interval (IBEX lib, main author: Gilles Chabert)
90 // https://ibex-lib.readthedocs.io
91
92 if(x2 % 2 == 0)
93 {
94 Interval proj = root(y, x2);
95 Interval pos_proj = proj & x1;
96 Interval neg_proj = (-proj) & x1;
97 x1 = pos_proj | neg_proj;
98 }
99
100 else
101 x1 &= root(y, x2);
102 }
103
104 inline void PowOp::bwd(const Interval& y, Interval& x1, Interval& x2)
105 {
106 assert_release(x2.is_degenerated() && "PowOp::bwd(y,x1,x2) (with x1 and x2 intervals) not implemented yet with Gaol");
107 assert_release(x2 == (int)(x2.mid()) && "PowOp::bwd(y,x1,x2) (x2 not integer) not implemented yet with Gaol");
108 PowOp::bwd(y, x1, x2.mid());
109 }
110}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval pow(const Interval &x, int n)
Returns , .
Definition codac2_Interval_operations_impl.h:33
Interval root(const Interval &x, int p)
Returns the p-th root: .
Definition codac2_Interval_operations_impl.h:60