codac 1.5.6
Loading...
Searching...
No Matches
codac2_sqrt.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 SqrtOp
19 {
20 static Interval fwd(const Interval& x1);
21 static ScalarType fwd_natural(const ScalarType& x1);
22 static ScalarType fwd_centered(const ScalarType& x1);
23 static void bwd(const Interval& y, Interval& x1);
24 };
25
26 // Analytic operator
27 // The following function can be used to build analytic expressions.
28
29 inline ScalarExpr
30 sqrt(const ScalarExpr& x1)
31 {
32 return { std::make_shared<AnalyticOperationExpr<SqrtOp,ScalarType,ScalarType>>(x1) };
33 }
34
35 // Inline functions
36
37 inline Interval SqrtOp::fwd(const Interval& x1)
38 {
39 return sqrt(x1);
40 }
41
42 inline ScalarType SqrtOp::fwd_natural(const ScalarType& x1)
43 {
44 return {
45 fwd(x1.a),
46 x1.a.is_subset({0,oo}) // def domain of sqrt
47 && x1.a != 0. // def domain of the derivative of sqrt
48 && x1.def_domain
49 };
50 }
51
52 inline ScalarType SqrtOp::fwd_centered(const ScalarType& x1)
53 {
54 if(centered_form_not_available_for_args(x1))
55 return fwd_natural(x1);
56
57 IntervalMatrix d(1,x1.da.size());
58 for(Index i = 0 ; i < d.size() ; i++)
59 d(0,i) = x1.da(0,i)/(2.*sqrt(x1.a));
60
61 return {
62 fwd(x1.m),
63 fwd(x1.a),
64 d,
65 x1.a.is_subset({0,oo}) // def domain of sqrt
66 && x1.a != 0. // def domain of the derivative of sqrt
67 && x1.def_domain
68 };
69 }
70
71 inline void SqrtOp::bwd(const Interval& y, Interval& x1)
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 if(y.is_empty() || y.ub() < 0)
78 x1.set_empty();
79
80 else if(y.lb() < 0)
81 x1 &= sqr(Interval(0,y.ub()));
82
83 else
84 x1 &= sqr(y);
85 }
86}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval sqrt(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:26
Interval sqr(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:21