Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CladDerivator.h
Go to the documentation of this file.
1/// \file CladDerivator.h
2///
3/// \brief The file is a bridge between ROOT and clad automatic differentiation
4/// plugin.
5///
6/// \author Vassil Vassilev <vvasilev@cern.ch>
7///
8/// \date July, 2018
9
10/*************************************************************************
11 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
12 * All rights reserved. *
13 * *
14 * For the licensing terms see $ROOTSYS/LICENSE. *
15 * For the list of contributors see $ROOTSYS/README/CREDITS. *
16 *************************************************************************/
17
18#ifndef CLAD_DERIVATOR
19#define CLAD_DERIVATOR
20
21#ifndef __CLING__
22#error "This file must not be included by compiled programs."
23#endif //__CLING__
24
25#include <plugins/include/clad/Differentiator/Differentiator.h>
26#include "TMath.h"
27namespace clad {
28namespace custom_derivatives {
29namespace TMath {
30template <typename T>
31ValueAndPushforward<T, T> Abs_pushforward(T x, T d_x)
32{
33 return {::TMath::Abs(x), ((x < 0) ? -1 : 1) * d_x};
34}
35
36template <typename T>
37ValueAndPushforward<T, T> ACos_pushforward(T x, T d_x)
38{
39 return {::TMath::ACos(x), (-1. / ::TMath::Sqrt(1 - x * x)) * d_x};
40}
41
42template <typename T>
43ValueAndPushforward<T, T> ACosH_pushforward(T x, T d_x)
44{
45 return {::TMath::ACosH(x), (1. / ::TMath::Sqrt(x * x - 1)) * d_x};
46}
47
48template <typename T>
49ValueAndPushforward<T, T> ASin_pushforward(T x, T d_x)
50{
51 return {::TMath::ASin(x), (1. / ::TMath::Sqrt(1 - x * x)) * d_x};
52}
53
54template <typename T>
55ValueAndPushforward<T, T> ASinH_pushforward(T x, T d_x)
56{
57 return {::TMath::ASinH(x), (1. / ::TMath::Sqrt(x * x + 1)) * d_x};
58}
59
60template <typename T>
61ValueAndPushforward<T, T> ATan_pushforward(T x, T d_x)
62{
63 return {::TMath::ATan(x), (1. / (x * x + 1)) * d_x};
64}
65
66template <typename T>
67ValueAndPushforward<T, T> ATanH_pushforward(T x, T d_x)
68{
69 return {::TMath::ATanH(x), (1. / (1 - x * x)) * d_x};
70}
71
72template <typename T>
73ValueAndPushforward<T, T> Cos_pushforward(T x, T d_x)
74{
75 return {::TMath::Cos(x), -::TMath::Sin(x) * d_x};
76}
77
78template <typename T>
79ValueAndPushforward<T, T> CosH_pushforward(T x, T d_x)
80{
81 return {::TMath::CosH(x), ::TMath::SinH(x) * d_x};
82}
83
84template <typename T>
85ValueAndPushforward<T, T> Erf_pushforward(T x, T d_x)
86{
87 return {::TMath::Erf(x), (2 * ::TMath::Exp(-x * x) / ::TMath::Sqrt(::TMath::Pi())) * d_x};
88}
89
90template <typename T>
91ValueAndPushforward<T, T> Erfc_pushforward(T x, T d_x)
92{
93 return {::TMath::Erfc(x), -Erf_pushforward(x, d_x).pushforward};
94}
95
96template <typename T>
97ValueAndPushforward<T, T> Exp_pushforward(T x, T d_x)
98{
99 return {::TMath::Exp(x), ::TMath::Exp(x) * d_x};
100}
101
102template <typename T>
103ValueAndPushforward<T, T> Hypot_pushforward(T x, T y, T d_x, T d_y)
104{
105 return {::TMath::Hypot(x, y), x / ::TMath::Hypot(x, y) * d_x + y / ::TMath::Hypot(x, y) * d_y};
106}
107
108template <typename T, typename U>
109void Hypot_pullback(T x, T y, U p, clad::array_ref<T> d_x, clad::array_ref<T> d_y)
110{
111 T h = ::TMath::Hypot(x, y);
112 *d_x += x / h * p;
113 *d_y += y / h * p;
114}
115
116template <typename T>
117ValueAndPushforward<T, T> Log_pushforward(T x, T d_x)
118{
119 return {::TMath::Log(x), (1. / x) * d_x};
120}
121
122template <typename T>
123ValueAndPushforward<T, T> Log10_pushforward(T x, T d_x)
124{
125 return {::TMath::Log10(x), (1.0 / (x * ::TMath::Ln10())) * d_x};
126}
127
128template <typename T>
129ValueAndPushforward<T, T> Log2_pushforward(T x, T d_x)
130{
131 return {::TMath::Log2(x), (1.0 / (x * ::TMath::Log(2.0))) * d_x};
132}
133
134template <typename T>
135ValueAndPushforward<T, T> Max_pushforward(T x, T y, T d_x, T d_y)
136{
137 T derivative = 0;
138 if (x >= y)
139 derivative = d_x;
140 else
141 derivative = d_y;
142 return {::TMath::Max(x, y), derivative};
143}
144
145template <typename T, typename U>
146void Max_pullback(T a, T b, U p, clad::array_ref<T> d_a, clad::array_ref<T> d_b)
147{
148 if (a >= b)
149 *d_a += p;
150 else
151 *d_b += p;
152}
153
154template <typename T>
155ValueAndPushforward<T, T> Min_pushforward(T x, T y, T d_x, T d_y)
156{
157 T derivative = 0;
158 if (x <= y)
159 derivative = d_x;
160 else
161 derivative = d_y;
162 return {::TMath::Min(x, y), derivative};
163}
164
165template <typename T, typename U>
166void Min_pullback(T a, T b, U p, clad::array_ref<T> d_a, clad::array_ref<T> d_b)
167{
168 if (a <= b)
169 *d_a += p;
170 else
171 *d_b += p;
172}
173
174template <typename T>
175ValueAndPushforward<T, T> Power_pushforward(T x, T y, T d_x, T d_y)
176{
177 T pushforward = y * ::TMath::Power(x, y - 1) * d_x;
178 if (d_y) {
179 pushforward += (::TMath::Power(x, y) * ::TMath::Log(x)) * d_y;
180 }
181 return {::TMath::Power(x, y), pushforward};
182}
183
184template <typename T, typename U>
185void Power_pullback(T x, T y, U p, clad::array_ref<T> d_x, clad::array_ref<T> d_y)
186{
187 auto t = pow_pushforward(x, y, 1, 0);
188 *d_x += t.pushforward * p;
189 t = pow_pushforward(x, y, 0, 1);
190 *d_y += t.pushforward * p;
191}
192
193template <typename T>
194ValueAndPushforward<T, T> Sin_pushforward(T x, T d_x)
195{
196 return {::TMath::Sin(x), ::TMath::Cos(x) * d_x};
197}
198
199template <typename T>
200ValueAndPushforward<T, T> SinH_pushforward(T x, T d_x)
201{
202 return {::TMath::SinH(x), ::TMath::CosH(x) * d_x};
203}
204
205template <typename T>
206ValueAndPushforward<T, T> Sq_pushforward(T x, T d_x)
207{
208 return {::TMath::Sq(x), 2 * x * d_x};
209}
210
211template <typename T>
212ValueAndPushforward<T, T> Sqrt_pushforward(T x, T d_x)
213{
214 return {::TMath::Sqrt(x), (0.5 / ::TMath::Sqrt(x)) * d_x};
215}
216
217template <typename T>
218ValueAndPushforward<T, T> Tan_pushforward(T x, T d_x)
219{
220 return {::TMath::Tan(x), (1. / ::TMath::Sq(::TMath::Cos(x))) * d_x};
221}
222
223template <typename T>
224ValueAndPushforward<T, T> TanH_pushforward(T x, T d_x)
225{
226 return {::TMath::TanH(x), (1. / ::TMath::Sq(::TMath::CosH(x))) * d_x};
227}
228
229#ifdef WIN32
230// Additional custom derivatives that can be removed
231// after Issue #12108 in ROOT is resolved
232// constexpr is removed
233ValueAndPushforward<Double_t, Double_t> Pi_pushforward()
234{
235 return {3.1415926535897931, 0.};
236}
237// constexpr is removed
238ValueAndPushforward<Double_t, Double_t> Ln10_pushforward()
239{
240 return {2.3025850929940459, 0.};
241}
242#endif
243} // namespace TMath
244} // namespace custom_derivatives
245} // namespace clad
246
247#endif // CLAD_DERIVATOR
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
winID h TVirtualViewer3D TVirtualGLPainter p
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
TMath.
Definition TMathBase.h:35
Double_t CosH(Double_t)
Returns the hyperbolic cosine of x.
Definition TMath.h:612
Double_t ACos(Double_t)
Returns the principal value of the arc cosine of x, expressed in radians.
Definition TMath.h:632
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250
Double_t ASin(Double_t)
Returns the principal value of the arc sine of x, expressed in radians.
Definition TMath.h:624
Double_t Log2(Double_t x)
Returns the binary (base-2) logarithm of x.
Definition TMath.cxx:107
Double_t Exp(Double_t x)
Returns the base-e exponential function of x, which is e raised to the power x.
Definition TMath.h:709
Double_t Erf(Double_t x)
Computation of the error function erf(x).
Definition TMath.cxx:190
Double_t ATan(Double_t)
Returns the principal value of the arc tangent of x, expressed in radians.
Definition TMath.h:640
Double_t ASinH(Double_t)
Returns the area hyperbolic sine of x.
Definition TMath.cxx:67
Double_t TanH(Double_t)
Returns the hyperbolic tangent of x.
Definition TMath.h:618
Double_t ACosH(Double_t)
Returns the nonnegative area hyperbolic cosine of x.
Definition TMath.cxx:81
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:756
Double_t Erfc(Double_t x)
Computes the complementary error function erfc(x).
Definition TMath.cxx:199
Double_t Sq(Double_t x)
Returns x*x.
Definition TMath.h:656
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:662
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:721
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
constexpr Double_t Ln10()
Natural log of 10 (to convert log to ln)
Definition TMath.h:100
Double_t Hypot(Double_t x, Double_t y)
Returns sqrt(x*x + y*y)
Definition TMath.cxx:59
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:594
constexpr Double_t Pi()
Definition TMath.h:37
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:588
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:600
Double_t ATanH(Double_t)
Returns the area hyperbolic tangent of x.
Definition TMath.cxx:95
Double_t Log10(Double_t x)
Returns the common (base-10) logarithm of x.
Definition TMath.h:762
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
Double_t SinH(Double_t)
Returns the hyperbolic sine of `x.
Definition TMath.h:606
ValueAndPushforward< T, T > CosH_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Abs_pushforward(T x, T d_x)
void Min_pullback(T a, T b, U p, clad::array_ref< T > d_a, clad::array_ref< T > d_b)
ValueAndPushforward< T, T > Sq_pushforward(T x, T d_x)
void Max_pullback(T a, T b, U p, clad::array_ref< T > d_a, clad::array_ref< T > d_b)
ValueAndPushforward< T, T > Erf_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Erfc_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Sin_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Max_pushforward(T x, T y, T d_x, T d_y)
ValueAndPushforward< T, T > Hypot_pushforward(T x, T y, T d_x, T d_y)
ValueAndPushforward< T, T > ASinH_pushforward(T x, T d_x)
ValueAndPushforward< T, T > ACosH_pushforward(T x, T d_x)
ValueAndPushforward< T, T > ASin_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Cos_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Sqrt_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Tan_pushforward(T x, T d_x)
void Hypot_pullback(T x, T y, U p, clad::array_ref< T > d_x, clad::array_ref< T > d_y)
ValueAndPushforward< T, T > Power_pushforward(T x, T y, T d_x, T d_y)
void Power_pullback(T x, T y, U p, clad::array_ref< T > d_x, clad::array_ref< T > d_y)
ValueAndPushforward< T, T > Min_pushforward(T x, T y, T d_x, T d_y)
ValueAndPushforward< T, T > Log_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Log10_pushforward(T x, T d_x)
ValueAndPushforward< T, T > TanH_pushforward(T x, T d_x)
ValueAndPushforward< T, T > ACos_pushforward(T x, T d_x)
ValueAndPushforward< T, T > SinH_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Exp_pushforward(T x, T d_x)
ValueAndPushforward< T, T > Log2_pushforward(T x, T d_x)
ValueAndPushforward< T, T > ATanH_pushforward(T x, T d_x)
ValueAndPushforward< T, T > ATan_pushforward(T x, T d_x)