test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Integral.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_Integral_h
2 #define PhysicsTools_Utilities_Integral_h
5 
6 namespace funct {
7 
8  struct no_var;
9 
10  template<typename F, typename X = no_var>
11  struct IntegralStruct {
12  IntegralStruct(const F& f) : p(primitive<X>(f)) { }
13  double operator()(double min, double max) const {
14  X::set(min); double pMin = p();
15  X::set(max); double pMax = p();
16  return pMax - pMin;
17  }
18  private:
20  };
21 
22  template<typename F>
23  struct IntegralStruct<F> {
24  IntegralStruct(const F& f) : p(primitive(f)) { }
25  double operator()(double min, double max) const {
26  return p(max) - p(min);
27  }
28  private:
30  };
31 
32  template<typename Integrator, typename F, typename X = no_var>
34  NumericalIntegral(const F& f, const Integrator & integrator) :
35  f_(f), integrator_(integrator) { }
36  inline double operator()(double min, double max) const {
37  return integrator_(f_, min, max);
38  }
39  private:
40  struct function {
41  function(const F & f) : f_(f) { }
42  inline double operator()(double x) const {
43  X::set(x); return f_();
44  }
45  private:
46  F f_;
47  };
48  function f_;
49  Integrator integrator_;
50  };
51 
52  template<typename Integrator, typename F>
53  struct NumericalIntegral<Integrator, F, no_var> {
54  NumericalIntegral(const F& f, const Integrator & integrator) :
55  f_(f), integrator_(integrator) { }
56  double operator()(double min, double max) const {
57  return integrator_(f_, min, max);
58  }
59  private:
60  F f_;
61  Integrator integrator_;
62  };
63 
64  template<typename F, typename X = no_var> struct Integral {
66  };
67 
68  template<typename X, typename F>
69  typename Integral<F, X>::type integral(const F& f) {
70  return typename Integral<F, X>::type(f);
71  }
72 
73  template<typename X, typename F, typename Integrator>
74  typename Integral<F, X>::type integral(const F& f, const Integrator & integrator) {
75  return typename Integral<F, X>::type(f, integrator);
76  }
77 
78  template<typename F, typename Integrator>
79  typename Integral<F>::type integral_f(const F& f, const Integrator & integrator) {
80  return typename Integral<F>::type(f, integrator);
81  }
82 
83  template<typename X, typename F>
84  double integral(const F& f, double min, double max) {
85  return integral<X>(f)(min, max);
86  }
87 
88  template<typename X, typename F, typename Integrator>
89  double integral(const F& f, double min, double max, const Integrator & integrator) {
90  return integral<X>(f, integrator)(min, max);
91  }
92 
93  template<typename F>
94  typename Integral<F>::type integral_f(const F& f) {
95  return typename Integral<F>::type(f);
96  }
97 
98  template<typename F>
99  double integral_f(const F& f, double min, double max) {
100  return integral_f(f)(min, max);
101  }
102 
103  template<typename F, typename Integrator>
104  double integral_f(const F& f, double min, double max, const Integrator & integrator) {
105  return integral_f(f, integrator)(min, max);
106  }
107 
108  template<typename F, typename MIN, typename MAX, typename Integrator = no_var, typename X = no_var>
109  struct DefIntegral {
110  DefIntegral(const F & f, const MIN & min, const MAX & max, const Integrator & integrator) :
111  f_(f), min_(min), max_(max), integrator_(integrator) { }
112  double operator()() const {
113  return integral<X>(f_, min_(), max_(), integrator_);
114  }
115  private:
116  F f_;
117  MIN min_;
118  MAX max_;
119  Integrator integrator_;
120  };
121 
122  template<typename F, typename MIN, typename MAX, typename X>
123  struct DefIntegral<F, MIN, MAX, no_var, X> {
124  DefIntegral(const F & f, const MIN & min, const MAX & max) :
125  f_(f), min_(min), max_(max) { }
126  double operator()(double x) const {
127  return integral<X>(f_, min_(x), max_(x));
128  }
129  private:
130  F f_;
131  MIN min_;
132  MAX max_;
133  };
134 
135  template<typename F, typename MIN, typename MAX, typename Integrator>
136  struct DefIntegral<F, MIN, MAX, Integrator, no_var> {
137  DefIntegral(const F & f, const MIN & min, const MAX & max, const Integrator & integrator) :
138  f_(f), min_(min), max_(max), integrator_(integrator) { }
139  double operator()(double x) const {
140  return integral_f(f_, min_(x), max_(x), integrator_);
141  }
142  private:
143  F f_;
144  MIN min_;
145  MAX max_;
146  Integrator integrator_;
147  };
148 
149  template<typename F, typename MIN, typename MAX>
150  struct DefIntegral<F, MIN, MAX, no_var, no_var> {
151  DefIntegral(const F & f, const MIN & min, const MAX & max) : f_(f), min_(min), max_(max) { }
152  double operator()(double x) const {
153  return integral_f(f_, min_(x), max_(x));
154  }
155  private:
156  F f_;
157  MIN min_;
158  MAX max_;
159  };
160 
161  }
162 
163 #define NUMERICAL_INTEGRAL(X, F, INTEGRATOR) \
164 namespace funct { \
165  template<typename X> struct Integral<F, X> { \
166  typedef NumericalIntegral<INTEGRATOR, F, X> type; \
167  }; \
168 } \
169 struct __useless_ignoreme
170 
171 #define NUMERICAL_FUNCT_INTEGRAL(F, INTEGRATOR) \
172 namespace funct { \
173  template<> struct Integral<F, no_var> { \
174  typedef NumericalIntegral<INTEGRATOR, F> type; \
175  }; \
176 } \
177 struct __useless_ignoreme
178 
179 #endif
Primitive< F >::type p
Definition: Integral.h:29
DefIntegral(const F &f, const MIN &min, const MAX &max, const Integrator &integrator)
Definition: Integral.h:110
double operator()(double min, double max) const
Definition: Integral.h:36
IntegralStruct(const F &f)
Definition: Integral.h:12
double operator()(double x) const
Definition: Integral.h:42
double operator()(double min, double max) const
Definition: Integral.h:13
Integral< F >::type integral_f(const F &f, const Integrator &integrator)
Definition: Integral.h:79
DefIntegral(const F &f, const MIN &min, const MAX &max)
Definition: Integral.h:151
NumericalIntegral(const F &f, const Integrator &integrator)
Definition: Integral.h:34
IntegralStruct< F, X > type
Definition: Integral.h:65
Primitive< F, X >::type primitive(const F &f)
Definition: Primitive.h:41
DefIntegral(const F &f, const MIN &min, const MAX &max, const Integrator &integrator)
Definition: Integral.h:137
Integrator integrator_
Definition: Integral.h:49
Primitive< F, X >::type p
Definition: Integral.h:19
double operator()(double min, double max) const
Definition: Integral.h:56
static void set(const double &x)
Definition: Variables.h:50
double f[11][100]
double operator()(double min, double max) const
Definition: Integral.h:25
T min(T a, T b)
Definition: MathUtil.h:58
Integral< F, X >::type integral(const F &f)
Definition: Integral.h:69
IntegralStruct(const F &f)
Definition: Integral.h:24
Integrator integrator_
Definition: Integral.h:119
DefIntegral(const F &f, const MIN &min, const MAX &max)
Definition: Integral.h:124
NumericalIntegral(const F &f, const Integrator &integrator)
Definition: Integral.h:54
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
double operator()() const
Definition: Integral.h:112