QNANO
Integrator.h
1 #ifndef QNANO_NEW_INTEGRATOR_DEFINED_H
2 #define QNANO_NEW_INTEGRATOR_DEFINED_H
3 
4 #include "tools/Integrable_Function.h"
5 #include "tools/List_Class.h"
6 
9 public:
10  double lim_a, lim_b;
11  size_t N;
12 };
13 class Integrator_Discretization: public List_Class<Integrator_Discretization_Parameter>{
14 public:
15 
16  Integrator_Discretization(size_t D, double lim_a, double lim_b, size_t N){
17  list.resize(D);
18  for(size_t i=0; i<D; i++){
19  list[i].lim_a=lim_a;
20  list[i].lim_b=lim_b;
21  list[i].N=N;
22  }
23  }
24 };
25 
26 class Integrator{
27 public:
28  virtual double integrate(const Integrable_Function& ifct, const Integrator_Discretization& discr)const=0;
29 };
30 
32 template<int D> class Integrator_Riemann: public Integrator{
33 public:
34  virtual double integrate(const Integrable_Function& ifct, const Integrator_Discretization& discr, std::vector<double> &x)const{
35 
36  if(discr.size() < D){
37  std::cerr<<"Integrator_Riemann: discr.size()="<<discr.size()<<" < D="<<D<<std::endl;
38  exit(1);
39  }
40  if(x.size() <D){
41  std::cerr<<"Integrator_Riemann: x.size()="<<x.size()<<" < D="<<D<<std::endl;
42  exit(1);
43  }
44 
45 
46  double h=(discr[D-1].lim_b-discr[D-1].lim_a)/discr[D-1].N;
47  double res=0.;
48  for(size_t i=0; i<discr[D-1].N; i++){
49  x[D-1] = discr[D-1].lim_a + i * h;
50  res += h * ( Integrator_Riemann<D-1>().integrate(ifct, discr, x) );
51  }
52  return res;
53  }
54  virtual double integrate(const Integrable_Function& ifct, const Integrator_Discretization& discr)const{
55  std::vector<double> x(D,0);
56  return integrate(ifct, discr, x);
57  }
58 };
59 
60 template<> class Integrator_Riemann<0>{
61 public:
62  double integrate(const Integrable_Function& ifct, const Integrator_Discretization& discr, std::vector<double> &x)const{
63  return ifct.evaluate(x);
64  }
65 };
66 
68 template<int D> class Integrator_Simpson: public Integrator{
69 public:
70  virtual double integrate(const Integrable_Function& ifct, const Integrator_Discretization& discr, std::vector<double> &x)const{
71 
72  if(discr.size() < D){
73  std::cerr<<"Integrator_Riemann: discr.size()="<<discr.size()<<" < D="<<D<<std::endl;
74  exit(1);
75  }
76  if(x.size() <D){
77  std::cerr<<"Integrator_Riemann: x.size()="<<x.size()<<" < D="<<D<<std::endl;
78  exit(1);
79  }
80 
81  double h=(discr[D-1].lim_b-discr[D-1].lim_a)/discr[D-1].N;
82  double res=0.;
83 
84  x[D-1]=discr[D-1].lim_a;
85  res+=h/3*Integrator_Simpson<D-1>().integrate(ifct, discr, x);
86 
87  x[D-1]=discr[D-1].lim_b;
88  res+=h/3*Integrator_Simpson<D-1>().integrate(ifct, discr, x);
89 
90  double pref=4./3.*h;
91  for(size_t i=1; i<discr[D-1].N; i+=2){
92  x[D-1]=discr[D-1].lim_a+h*i;
93  res+=pref*Integrator_Simpson<D-1>().integrate(ifct, discr, x);
94  }
95  pref=2./3.*h;
96  for(size_t i=2; i<discr[D-1].N-1; i+=2){
97  x[D-1]=discr[D-1].lim_a+h*i;
98  res+=pref*Integrator_Simpson<D-1>().integrate(ifct, discr, x);
99  }
100  return res;
101  }
102 
103  virtual double integrate(const Integrable_Function& ifct, const Integrator_Discretization& discr)const{
104  std::vector<double> x(D,0);
105  return integrate(ifct, discr, x);
106  }
107 };
108 template <> class Integrator_Simpson<0>{
109 public:
110  double integrate(const Integrable_Function& ifct, const Integrator_Discretization& discr, std::vector<double> &x) const{
111  return ifct.evaluate(x);
112  }
113 };
114 
115 
116 
117 
118 
119 #endif
Definition: List_Class.h:8
Definition: Integrator.h:26
Integrator using Riemann sums.
Definition: Integrator.h:32
Definition: Integrator.h:13
Interface for integrable functions.
Definition: Integrable_Function.h:10
Integrator using Simpson&#39;s rule.
Definition: Integrator.h:68
Interface for integrators.
Definition: Integrator.h:8