CMS 3D CMS Logo

Derivative.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_Derivative_h
2 #define PhysicsTools_Utilities_Derivative_h
3 
6 #include <boost/type_traits.hpp>
7 
9 
10 namespace funct {
11 
12  template<typename X, typename A>
13  struct Derivative {
14  typedef NUM(0) type;
15  GET(A, num<0>());
16  };
17 
18  TEMPL(XT1) DERIV(X, A) derivative(const A& _) {
19  return Derivative<X, A>::get(_);
20  }
21 
22  TEMPL(XT1) struct Independent :
23  public ::boost::is_same<DERIV(X, A), NUM(0)> { };
24 
25  // dx / dx = 1
26  DERIV_RULE(TYPX, X, NUM(1), num<1>());
27 
28  // d exp(x) / dx = exp(x)
29  DERIV_RULE(TYPXT1, EXP_S(A), PROD(EXP(A), DERIV(X, A)), _ * derivative<X>(_._));
30 
31  // d log(x) / dx = 1 / x
32  DERIV_RULE(TYPXT1, LOG_S(A), PROD(RATIO(NUM(1), A), DERIV(X, A)),
33  (num<1>() / _._) * derivative<X>(_._));
34 
35  // d abs(x) / dx = sgn(x)
37  sgn(_._) * derivative<X>(_._));
38 
39  // d sin(x) / dx = cos(x)
41  cos(_._) * derivative<X>(_._));
42 
43  // d cos(x) / dx = - sin(x)
45  - (sin(_._) * derivative<X>(_._)));
46 
47  // d tan(x) / dx = 1 / cos(x)^2
49  DERIV(X, A)),
50  (num<1>() / sqr(cos(_._))) * derivative<X>(_._));
51 
52  // d/dx (f + g) = d/dx f + d/dx g
53  DERIV_RULE(TYPXT2, SUM_S(A, B), SUM(DERIV(X, A), DERIV(X, B)),
54  derivative<X>(_._1) + derivative<X>(_._2));
55 
56  // d/dx (-f) = - d/dx f
58  - derivative<X>(_._));
59 
60  // d/dx (f * g) = d/dx f * g + f * d/dx g
62  PROD(A, DERIV(X, B))),
63  derivative<X>(_._1) * _._2 + _._1 * derivative<X>(_._2));
64 
65  // d/dx (f / g) = (d/dx f * g - f * d/dx g) / g^2
67  RATIO(DIFF(PROD(DERIV(X, A), B),
68  PROD(A, DERIV(X, B))),
69  SQUARE(B)),
70  (derivative<X>(_._1) * _._2 -
71  _._1 * derivative<X>(_._2)) / sqr(_._2));
72 
73  // d/dx f ^ n = n f ^ (n - 1) d/dx f
75  PROD(PROD(NUM(n), POWER(A, NUM(n - 1))), DERIV(X, A)),
76  _._2 * pow(_._1, num<n - 1>()) * derivative<X>(_._1));
77 
78  // d/dx f ^ n/m = n/m f ^ (n/m - 1) d/dx f
80  PROD(PROD(FRACT(n, m), POWER(A, FRACT(n - m, n))),
81  DERIV(X, A)),
82  _._2 * pow(_._1, fract<n - m, m>()) * derivative<X>(_._1));
83 
84  // d sqrt(x) / dx = 1/2 1/sqrt(x)
86  PROD(PROD(FRACT(1, 2), RATIO(NUM(1), SQRT(A))),
87  DERIV(X, A)),
88  (fract<1, 2>() * (num<1>() / sqrt(_._))) *
89  derivative<X>(_._));
90 }
91 
93 
94 #endif
#define TYPXT1
typedef DERIV(X, A) A1
#define EXP(A)
const Numerical< n > & num()
Definition: Numerical.h:16
#define DERIV_RULE(TMPL, T, RES, COMB)
typedef POWER(A, NUM(n)) arg
Definition: Abs.h:5
Derivative< X, A >::type derivative(const A &_)
Definition: Derivative.h:18
Definition: CLHEP.h:16
#define COS(A)
MINUS_S(B)>
Definition: Factorize.h:100
#define MINUS(A)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
#define SQRT_S(A)
PROD_S(A, B)>
Definition: Factorize.h:45
#define TYPXN2T1
POWER_S(A, NUM(n))>
Definition: Factorize.h:49
static type get(const A &_)
Definition: Derivative.h:15
#define SQUARE(A)
#define RATIO_S(A, B)
Sqrt< T >::type sqrt(const T &t)
Definition: Sqrt.h:22
#define FRACT(N, M)
#define LOG_S(A)
#define FRACT_S(N, M)
#define SUM_S(A, B)
#define RATIO(A, B)
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
#define COS_S(A)
#define SGN(A)
#define SUM(A, B)
#define EXP_S(A)
Sgn< T >::type sgn(const T &t)
Definition: Sgn.h:21
#define TYPXN1T1
TEMPL(T1) struct Divides0
Definition: Factorize.h:19
#define SQRT(A)
typedef PROD(F, SUM(RATIO(A, F), RATIO(B, F))) type
GET(arg, _)
Square< F >::type sqr(const F &f)
Definition: Square.h:13
typedef DIFF(PROD(A, B1), PRIMIT(X, PROD(A1, B1))) type
#define SIN(A)
NUM(n))
Definition: Factorize.h:93
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
#define TYPXT2
#define TYPX
#define TAN_S(A)
#define ABS_S(A)
#define SIN_S(A)