CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Primitive.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_Primitive_h
2 #define PhysicsTools_Utilities_Primitive_h
9 #include <boost/type_traits.hpp>
10 
12 
13 namespace funct {
14 
15  struct no_var;
16 
17  struct UndefinedIntegral { };
18 
20  struct ConstPrimitive {
22  inline static type get(const F& f) { return type(); }
23  };
24 
25  // /
26  // | c dx = c * x
27  // /
28  template<typename X, typename F>
29  struct ConstPrimitive<X, F, true> {
30  typedef PROD(F, X) type;
31  inline static type get(const F& f) { return f * X(); }
32  };
33 
34  template<typename F, typename X = no_var>
35  struct Primitive : public ConstPrimitive<X, F> { };
36 
37  template<typename F>
38  struct Primitive<F> { };
39 
40  template<typename X, typename F>
41  typename Primitive<F, X>::type primitive(const F& f) {
42  return Primitive<F, X>::get(f);
43  }
44 
45  template<typename F>
46  typename Primitive<F>::type primitive(const F& f) {
47  return Primitive<F>::get(f);
48  }
49 
50  // /
51  // | f ^ g dx : UNDEFINED
52  // /
53  PRIMIT_RULE(TYPXT2, POWER_S(A, B), UndefinedIntegral, type());
54 
55  // /
56  // | x dx = x ^ 2 / 2
57  // /
58  PRIMIT_RULE(TYPX, X, RATIO(POWER(X, NUM(2)), NUM(2)), pow(_, num<2>()) / num<2>());
59 
60  // /
61  // | x ^ n dx = x ^ (n + 1) / (n + 1)
62  // /
64  RATIO(POWER(X, NUM(n + 1)), NUM(n + 1)),
65  pow(_._1, num<n + 1>()) / num<n + 1>());
66 
67  // /
68  // | 1 / x ^ n dx = (- 1) / ((n - 1) x ^ (n - 1))
69  // /
71  RATIO(NUM(-1), PROD(NUM(n - 1), POWER(X, NUM(n - 1)))),
72  num<-1>()/(num<n - 1>() * pow(_._2._1, num<n - 1>())));
73 
75  RATIO(NUM(-1), PROD(NUM(n - 1), POWER(X, NUM(n - 1)))),
76  num<-1>()/(num<n - 1>() * pow(_._1._2, num<n - 1>())));
77 
78  // /
79  // | x ^ n/m dx = m / (n + m) (x)^ (n + m / m)
80  // /
82  PROD(FRACT(m, n + m), POWER(X, FRACT(n + m, m))),
83  (fract<m, n + m>() * pow(_._1, fract<n + m, m>())));
84 
85  // /
86  // | sqrt(x) dx = 2/3 (x)^ 3/2
87  // /
89  (fract<2, 3>() * pow(_._, fract<3, 2>())));
90 
91  // /
92  // | exp(x) dx = exp(x)
93  // /
94  PRIMIT_RULE(TYPX, EXP_S(X), EXP(X), _);
95 
96  // /
97  // | log(x) dx = x(log(x) - 1)
98  // /
99  PRIMIT_RULE(TYPX, LOG_S(X), PROD(X, DIFF(LOG(X), NUM(1))),
100  _._ * (_ - num<1>()));
101 
102  // /
103  // | sgn(x) dx = abs(x)
104  // /
105  PRIMIT_RULE(TYPX, SGN_S(X), ABS(X), abs(_._));
106 
107 
108  // /
109  // | sin(x) dx = - cos(x)
110  // /
111  PRIMIT_RULE(TYPX, SIN_S(X), MINUS(COS(X)), - cos(_._));
112 
113  // /
114  // | cos(x) dx = sin(x)
115  // /
116  PRIMIT_RULE(TYPX, COS_S(X), SIN(X), sin(_._));
117 
118  // /
119  // | tan(x) dx = - log(abs(cos(x)))
120  // /
121  PRIMIT_RULE(TYPX, TAN_S(X), MINUS(LOG(ABS(COS(X)))), - log(abs(cos(_._))));
122 
123  // /
124  // | 1 / x dx = log(abs(x))
125  // /
126  PRIMIT_RULE(TYPX, RATIO_S(NUM(1), X), LOG(ABS(X)), log(abs(_._2)));
127 
128  PRIMIT_RULE(TYPX, POWER_S(X, NUM(-1)), LOG(ABS(X)), log(abs(_._1)));
129 
130  // /
131  // | 1 / cos(x)^2 dx = tan(x)
132  // /
133  PRIMIT_RULE(TYPX, RATIO_S(NUM(1), POWER_S(COS_S(X), NUM(2))), TAN(X), tan(_._2._1._));
134 
135  // /
136  // | 1 / sin(x)^2 dx = - 1 / tan(x)
137  // /
138  PRIMIT_RULE(TYPX, RATIO_S(NUM(1), POWER_S(SIN_S(X), NUM(2))),
139  RATIO(NUM(-1), TAN(X)), num<-1>() / tan(_._2._1._));
140 
141  // composite primitives
142 
143  // / / /
144  // | (f(x) + g(x)) dx = | f(x) dx + | g(x) dx
145  // / / /
146  PRIMIT_RULE(TYPXT2, SUM_S(A, B), SUM(PRIMIT(X, A), PRIMIT(X, B)),
147  primitive<X>(_._1) + primitive<X>(_._2));
148 
149  // / /
150  // | (- f(x)) dx = - | f(x) dx
151  // / /
152  PRIMIT_RULE(TYPXT1, MINUS_S(A), MINUS(PRIMIT(X, A)), - primitive<X>(_._));
153 
154  // /
155  // | f * g dx : defined only for f or g indep. of x or part. int.
156  // /
157 
158  template <TYPXT2,
159  bool bint = ::boost::type_traits::ice_not<
161  bool aint = ::boost::type_traits::ice_not<
163  struct PartIntegral {
165  GET(PROD_S(A, B), type());
166  };
167 
168  TEMPL(XT2) struct PartIntegral<X, A, B, true, false> {
169  typedef PRIMIT(X, B) B1;
170  typedef DERIV(X, A) A1;
171  typedef PRIMIT(X, PROD(A1, B1)) AB1;
172  typedef DIFF(PROD(A, B1), PRIMIT(X, PROD(A1, B1))) type;
173  inline static type get(const PROD_S(A, B)& _) {
174  const A& a = _._1;
175  B1 b = primitive<X>(_._2);
176  return a * b - primitive<X>(derivative<X>(a) * b);
177  }
178  };
179 
180  TEMPL(XT2) struct PartIntegral<X, B, A, false, true> {
181  typedef PRIMIT(X, B) B1;
182  typedef DERIV(X, A) A1;
183  typedef PRIMIT(X, PROD(A1, B1)) AB1;
184  typedef DIFF(PROD(A, B1), PRIMIT(X, PROD(A1, B1))) type;
185  inline static type get(const PROD_S(B, A)& _) {
186  const A& a = _._2;
187  B1 b = primitive<X>(_._1);
188  return a * b - primitive<X>(derivative<X>(a) * b);
189  }
190  };
191 
192  TEMPL(XT2) struct PartIntegral<X, A, B, true, true> :
193  public PartIntegral<X, A, B, true, false> { };
194 
196  bool indepg = Independent<X, B>::value>
197  struct ProductPrimitive : public PartIntegral<X, A, B> { };
198 
199  TEMPL(XT2) struct ProductPrimitive<X, A, B, true, false> {
200  typedef PROD(A, PRIMIT(X, B)) type;
201  GET(PROD_S(A, B), _._1 * primitive<X>(_._2));
202  };
203 
204  TEMPL(XT2) struct ProductPrimitive<X, A, B, false, true> {
205  typedef PROD(B, PRIMIT(X, A)) type;
206  GET(PROD_S(A, B), _._2 * primitive<X>(_._1));
207  };
208 
209  TEMPL(XT2) struct ProductPrimitive<X, A, B, true, true> {
210  typedef PROD(PROD(A, B), X) type;
211  GET(PROD_S(A, B), _ * X());
212  };
213 
214  TEMPL(XT2) struct Primitive<PROD_S(A, B), X> :
215  public ProductPrimitive<X, A, B> { };
216 
217  // /
218  // | f / g dx : defined only for f or g indep. of x; try part. int.
219  // /
220 
221  template <TYPXT2,
222  bool bint = ::boost::type_traits::ice_not<
223  ::boost::is_same<PRIMIT(X, RATIO(NUM(1), B)),
224  UndefinedIntegral>::value>::value,
225  bool aint = ::boost::type_traits::ice_not<
226  ::boost::is_same<PRIMIT(X, A),
227  UndefinedIntegral>::value>::value>
228  struct PartIntegral2 {
230  GET(RATIO_S(A, B), type());
231  };
232 
233  TEMPL(XT2) struct PartIntegral2<X, A, B, true, false> {
234  typedef PRIMIT(X, RATIO(NUM(1), B)) B1;
235  typedef DERIV(X, A) A1;
236  typedef PRIMIT(X, PROD(A1, B1)) AB1;
237  typedef DIFF(PROD(A, B1), AB1) type;
238  inline static type get(const RATIO_S(A, B)& _) {
239  const A& a = _._1;
240  B1 b = primitive<X>(num<1>() / _._2);
241  return a * b - primitive<X>(derivative<X>(a) * b);
242  }
243  };
244 
245  TEMPL(XT2) struct PartIntegral2<X, B, A, false, true> {
246  typedef PRIMIT(X, RATIO(NUM(1), B)) B1;
247  typedef DERIV(X, A) A1;
248  typedef PRIMIT(X, PROD(A1, B1)) AB1;
249  typedef DIFF(PROD(A, B1), AB1) type;
250  inline static type get(const RATIO_S(B, A)& _) {
251  const A& a = _._1;
252  B1 b = primitive<X>(num<1>() / _._2);
253  return a * b - primitive<X>(derivative<X>(a) * b);
254  }
255  };
256 
257  // should be improved: try both...
258  TEMPL(XT2) struct PartIntegral2<X, A, B, true, true> :
259  public PartIntegral2<X, A, B, true, false> { };
260 
262  bool indepb = Independent<X, B>::value>
263  struct RatioPrimitive : public PartIntegral2<X, A, B> { };
264 
265  TEMPL(XT2) struct RatioPrimitive<X, A, B, true, false> {
266  typedef PROD(A, PRIMIT(X, RATIO(NUM(1), B))) type;
267  GET(RATIO_S(A, B), _._1 * primitive<X>(num<1> / _._2));
268  };
269 
270  TEMPL(XT2) struct RatioPrimitive<X, A, B, false, true> {
271  typedef RATIO(PRIMIT(X, A), B) type;
272  GET(RATIO_S(A, B), primitive<X>(_._1) / _._2);
273  };
274 
275  TEMPL(XT2) struct RatioPrimitive<X, A, B, true, true> {
276  typedef RATIO(RATIO(A, B), X) type;
277  GET(RATIO_S(A, B), _ * X());
278  };
279 
280  TEMPL(XT2) struct Primitive<RATIO_S(A, B), X> :
281  public RatioPrimitive<X, A, B> { };
282 
283  // Function integrals
284 
285  // /
286  // | c dx = c * x
287  // /
288  template<>
291  inline static type get(const Parameter & p) {
292  return p * Identity();
293  }
294  };
295 
296 }
297 
298 #define DECLARE_PRIMITIVE(X, F, P) \
299 namespace funct { \
300 template<typename X> struct Primitive<F, X> { \
301  typedef P type; \
302  inline static type get(const F& _) \
303  { return type(_._); } \
304 }; \
305 } \
306 struct __useless_ignoreme
307 
308 #define DECLARE_FUNCT_PRIMITIVE(F, P) \
309 namespace funct { \
310 template<> struct Primitive<F> { \
311  typedef P type; \
312  inline static type get(const F& _) \
313  { return type(); } \
314 }; \
315 } \
316 struct __useless_ignoreme
317 
319 
320 #endif
static const bool value
Definition: Factorize.h:107
#define TYPXN2
type
Definition: HCALResponse.h:21
#define TYPXT1
typedef DERIV(X, A) A1
#define EXP(A)
const Numerical< n > & num()
Definition: Numerical.h:16
UndefinedIntegral type
Definition: Primitive.h:229
typedef POWER(A, NUM(n)) arg
#define COS(A)
MINUS_S(B)>
Definition: Factorize.h:99
#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:44
#define X(str)
Definition: MuonsGrabber.cc:48
POWER_S(A, NUM(n))>
Definition: Factorize.h:48
#define SGN_S(A)
#define ABS(A)
#define LOG(A)
typedef SUM(SUM(PROD(NUM(n-p), SIN2(A)), PROD(NUM(m-p), COS2(A))), NUM(p)) type
#define TYPXN1
SUM_S(B, A)>
#define RATIO_S(A, B)
Primitive< F, X >::type primitive(const F &f)
Definition: Primitive.h:41
UndefinedIntegral type
Definition: Primitive.h:164
#define FRACT(N, M)
#define LOG_S(A)
#define FRACT_S(N, M)
GET(PROD_S(A, B), type())
#define TAN(A)
#define RATIO(A, B)
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
typedef PRIMIT(X, PROD(A1, B1)) AB1
Product< Parameter, Identity >::type type
Definition: Primitive.h:290
static type get(const F &f)
Definition: Primitive.h:22
Log< T >::type log(const T &t)
Definition: Log.h:22
#define EXP_S(A)
double b
Definition: hdecay.h:120
string const
Definition: compareJSON.py:14
arg type
Definition: Factorize.h:37
typedef PROD(F, SUM(RATIO(A, F), RATIO(B, F))) type
GET(arg, _)
double a
Definition: hdecay.h:121
TEMPL(XT1) DERIV(X
Definition: Derivative.h:22
PRIMIT_RULE(TYPXT2, POWER_S(A, B), UndefinedIntegral, type())
typedef DIFF(PROD(A, B1), PRIMIT(X, PROD(A1, B1))) type
#define SIN(A)
static const int p
Definition: Factorize.h:57
NUM(n))
Definition: Factorize.h:92
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
static type get(const PROD_S(A, B)&_)
Definition: Primitive.h:173
GET(RATIO_S(A, B), type())
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
#define TYPXT2
#define TYPX
UndefinedIntegral type
Definition: Primitive.h:21