CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
18  TEMPL(XT1) DERIV(X, A) derivative(const A& _) { return Derivative<X, A>::get(_); }
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
#define TYPXT1
typedef DERIV(X, A) A1
#define EXP(A)
const Numerical< n > & num()
Definition: Numerical.h:18
#define DERIV_RULE(TMPL, T, RES, COMB)
typedef POWER(A, NUM(n)) arg
Derivative< X, A >::type derivative(const A &_)
Definition: Derivative.h:18
#define COS(A)
MINUS_S(B)>
Definition: Factorize.h:94
#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:38
#define TYPXN2T1
POWER_S(A, NUM(n))>
Definition: Factorize.h:40
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:15
#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:14
typedef DIFF(PROD(A, B1), PRIMIT(X, PROD(A1, B1))) type
#define SIN(A)
NUM(n))
Definition: Factorize.h:87
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
#define TYPXT2
#define TYPX
#define TAN_S(A)
#define ABS_S(A)
#define SIN_S(A)