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 <type_traits>
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 
19 
20  TEMPL(XT1) struct Independent : public ::std::is_same<DERIV(X, A), NUM(0)> {};
21 
22  // dx / dx = 1
23  DERIV_RULE(TYPX, X, NUM(1), num<1>());
24 
25  // d exp(x) / dx = exp(x)
26  DERIV_RULE(TYPXT1, EXP_S(A), PROD(EXP(A), DERIV(X, A)), _* derivative<X>(_._));
27 
28  // d log(x) / dx = 1 / x
29  DERIV_RULE(TYPXT1, LOG_S(A), PROD(RATIO(NUM(1), A), DERIV(X, A)), (num<1>() / _._) * derivative<X>(_._));
30 
31  // d abs(x) / dx = sgn(x)
32  DERIV_RULE(TYPXT1, ABS_S(A), PROD(SGN(A), DERIV(X, A)), sgn(_._) * derivative<X>(_._));
33 
34  // d sin(x) / dx = cos(x)
35  DERIV_RULE(TYPXT1, SIN_S(A), PROD(COS(A), DERIV(X, A)), cos(_._) * derivative<X>(_._));
36 
37  // d cos(x) / dx = - sin(x)
38  DERIV_RULE(TYPXT1, COS_S(A), MINUS(PROD(SIN(A), DERIV(X, A))), -(sin(_._) * derivative<X>(_._)));
39 
40  // d tan(x) / dx = 1 / cos(x)^2
42  TAN_S(A),
43  PROD(RATIO(NUM(1), SQUARE(COS(A))), DERIV(X, A)),
44  (num<1>() / sqr(cos(_._))) * derivative<X>(_._));
45 
46  // d/dx (f + g) = d/dx f + d/dx g
47  DERIV_RULE(TYPXT2, SUM_S(A, B), SUM(DERIV(X, A), DERIV(X, B)), derivative<X>(_._1) + derivative<X>(_._2));
48 
49  // d/dx (-f) = - d/dx f
50  DERIV_RULE(TYPXT1, MINUS_S(A), MINUS(DERIV(X, A)), -derivative<X>(_._));
51 
52  // d/dx (f * g) = d/dx f * g + f * d/dx g
54  PROD_S(A, B),
55  SUM(PROD(DERIV(X, A), B), PROD(A, DERIV(X, B))),
56  derivative<X>(_._1) * _._2 + _._1 * derivative<X>(_._2));
57 
58  // d/dx (f / g) = (d/dx f * g - f * d/dx g) / g^2
60  RATIO_S(A, B),
61  RATIO(DIFF(PROD(DERIV(X, A), B), PROD(A, DERIV(X, B))), SQUARE(B)),
62  (derivative<X>(_._1) * _._2 - _._1 * derivative<X>(_._2)) / sqr(_._2));
63 
64  // d/dx f ^ n = n f ^ (n - 1) d/dx f
66  POWER_S(A, NUM(n)),
67  PROD(PROD(NUM(n), POWER(A, NUM(n - 1))), DERIV(X, A)),
68  _._2* pow(_._1, num<n - 1>()) * derivative<X>(_._1));
69 
70  // d/dx f ^ n/m = n/m f ^ (n/m - 1) d/dx f
72  POWER_S(A, FRACT_S(n, m)),
73  PROD(PROD(FRACT(n, m), POWER(A, FRACT(n - m, n))), DERIV(X, A)),
74  _._2* pow(_._1, fract<n - m, m>()) * derivative<X>(_._1));
75 
76  // d sqrt(x) / dx = 1/2 1/sqrt(x)
78  SQRT_S(A),
79  PROD(PROD(FRACT(1, 2), RATIO(NUM(1), SQRT(A))), DERIV(X, A)),
80  (fract<1, 2>() * (num<1>() / sqrt(_._))) * derivative<X>(_._));
81 } // namespace funct
82 
84 
85 #endif
SUM
#define SUM(A, B)
Definition: Simplify_begin.h:52
TAN_S
#define TAN_S(A)
Definition: Simplify_begin.h:48
POWER_S
#define POWER_S(A, B)
Definition: Simplify_begin.h:41
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
Fraction.h
TYPX
#define TYPX
Definition: Simplify_begin.h:22
FRACT_S
#define FRACT_S(N, M)
Definition: Simplify_begin.h:42
funct::PROD_S
PROD_S(B, C)>
Definition: Factorize.h:114
SIN
#define SIN(A)
Definition: Simplify_begin.h:62
funct::B
TEMPL(T2) struct Divides B
Definition: Factorize.h:24
funct::DERIV
typedef DERIV(X, A) A1
COS
#define COS(A)
Definition: Simplify_begin.h:63
funct::DIFF
typedef DIFF(PROD(A, B1), PRIMIT(X, PROD(A1, B1))) type
SGN
#define SGN(A)
Definition: Simplify_begin.h:68
LOG_S
#define LOG_S(A)
Definition: Simplify_begin.h:45
RATIO_S
#define RATIO_S(A, B)
Definition: Simplify_begin.h:40
crabWrapper._
_
Definition: crabWrapper.py:19
COS_S
#define COS_S(A)
Definition: Simplify_begin.h:47
funct::derivative
Derivative< X, A >::type derivative(const A &_)
Definition: Derivative.h:18
SQUARE
#define SQUARE(A)
Definition: Simplify_begin.h:58
funct::X
Definition: Variables.h:51
watchdog.const
const
Definition: watchdog.py:83
DERIV_RULE
#define DERIV_RULE(TMPL, T, RES, COMB)
Definition: Simplify_begin.h:159
funct::POWER
typedef POWER(A, NUM(n)) arg
MINUS
#define MINUS(A)
Definition: Simplify_begin.h:54
funct::sin
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
RATIO
#define RATIO(A, B)
Definition: Simplify_begin.h:56
funct::cos
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
funct::num
const Numerical< n > & num()
Definition: Numerical.h:18
funct::sqrt
Sqrt< T >::type sqrt(const T &t)
Definition: Sqrt.h:22
SQRT
#define SQRT(A)
Definition: Simplify_begin.h:59
funct::Derivative::get
static type get(const A &_)
Definition: Derivative.h:15
funct::sqr
Square< F >::type sqr(const F &f)
Definition: Square.h:14
funct::m
m
Definition: Factorize.h:45
SUM_S
#define SUM_S(A, B)
Definition: Simplify_begin.h:37
funct::GET
GET(arg, _)
funct::Independent
Definition: Derivative.h:20
SQRT_S
#define SQRT_S(A)
Definition: Simplify_begin.h:43
ABS_S
#define ABS_S(A)
Definition: Simplify_begin.h:49
funct::MINUS_S
typedef MINUS_S(A) arg
TYPXT1
#define TYPXT1
Definition: Simplify_begin.h:24
A
funct::TEMPL
TEMPL(T1) struct Divides0
Definition: Factorize.h:15
TYPXN2T1
#define TYPXN2T1
Definition: Simplify_begin.h:31
funct::Derivative
Definition: Derivative.h:13
EXP_S
#define EXP_S(A)
Definition: Simplify_begin.h:44
Simplify_end.h
EXP
#define EXP(A)
Definition: Simplify_begin.h:60
funct::NUM
PROD_S(A, B)> NUM(n))
Definition: Factorize.h:87
TYPXT2
#define TYPXT2
Definition: Simplify_begin.h:25
Functions.h
SIN_S
#define SIN_S(A)
Definition: Simplify_begin.h:46
funct::Numerical
Definition: Numerical.h:7
funct::pow
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
Simplify_begin.h
funct::sgn
Sgn< T >::type sgn(const T &t)
Definition: Sgn.h:21
TYPXN1T1
#define TYPXN1T1
Definition: Simplify_begin.h:30
funct::PROD
typedef PROD(F, SUM(RATIO(A, F), RATIO(B, F))) type
funct
Definition: Abs.h:5
FRACT
#define FRACT(N, M)
Definition: Simplify_begin.h:36