CMS 3D CMS Logo

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