DefiniteIntegral.cc 7.36 KB
Newer Older
1
// -*- C++ -*-
Lynn Garren's avatar
Lynn Garren committed
2
// $Id: DefiniteIntegral.cc,v 1.6 2010/06/16 18:22:01 garren Exp $
3
4

#include <cmath>
5
6
#include <vector>
#include <stdexcept>
7
8
#include "CLHEP/GenericFunctions/DefiniteIntegral.hh"
#include "CLHEP/GenericFunctions/AbsFunction.hh"
9
10


11
12
13
namespace Genfun {


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  class DefiniteIntegral::Clockwork {
    
    //
    // This class has limited visibility its functions, data,
    // and nested classes are all public:
    // 
    
  public:
    
    class QuadratureRule {
      
    public:
      
      // Constructorctor:
      QuadratureRule() {};
      
      // Destructor:
Lynn Garren's avatar
Lynn Garren committed
31
      virtual ~QuadratureRule() {};
32
33
34
35
36
37
38
39
40
      
      // Integrate at the j^th level of refinement:
      virtual double integrate(const AbsFunction & function, 
			       double a,
			       double b,
			       unsigned int j) const=0;
      
      // Return the step multiplier:
      virtual unsigned int stepMultiplier () const=0;
41

42
43
      // Return the number of function calls:
      virtual unsigned int numFunctionCalls() const =0;
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    };
    
    class TrapezoidQuadratureRule:public QuadratureRule {
      
    public:
      
      // Constructor:
      TrapezoidQuadratureRule():retVal(0),nFunctionCalls(0) {};
      
      // Destructor:
      ~TrapezoidQuadratureRule() {};
      
      // Integrate at the j^th level of refinement:
      virtual double integrate(const AbsFunction & function, 
			       double a,
			       double b,
			       unsigned int j) const;
      
      // The step is doubled at each level of refinement:
      virtual unsigned int stepMultiplier () const {return 2;}
65

66
67
68
69
      // Returns number of function calls:
      virtual unsigned int numFunctionCalls() const {return nFunctionCalls;};
      
    private:
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
      mutable double retVal;
      mutable unsigned int nFunctionCalls;

    };
    
    class XtMidpointQuadratureRule:public QuadratureRule {
      
    public:
      
      // Constructor:
      XtMidpointQuadratureRule():retVal(0),nFunctionCalls(0) {};
      
      // Destructorctor:
      ~XtMidpointQuadratureRule() {};
      
      // Integrate at the j^th level of refinement:
      virtual double integrate(const AbsFunction & function, 
			       double a,
			       double b,
			       unsigned int j) const;
      
      // The step is tripled at each level of refinement:
      virtual unsigned int stepMultiplier () const {return 3;}

      // Returns number of function calls:
      virtual unsigned int numFunctionCalls() const {return nFunctionCalls;};
      
    private:

      mutable double retVal;
      mutable unsigned int nFunctionCalls;
    };
    
    double                              a;              // lower limit of integration
    double                              b;              // upper limit of integration
    DefiniteIntegral::Type              type;           // open or closed
    mutable unsigned int                nFunctionCalls; // bookkeeping
    unsigned int                        MAXITER;        // Max no of step doubling, tripling, etc.
    double                              EPS;            // Target precision
    unsigned int                        K;              // Minimum order =2*5=10

    // Polynomial interpolation with Neville's method:
    void polint(std::vector<double>::iterator xArray, std::vector<double>::iterator yArray, double x, double & y, double & deltay) const;
    
  };

  
  DefiniteIntegral::DefiniteIntegral(double a, double b, Type type):
    c(new Clockwork()) {
    c->a              = a;
    c->b              = b;
    c->type           = type;
    c->nFunctionCalls = 0;
    c->MAXITER        = type==OPEN ? 20 : 14; 
    c->EPS            = 1.0E-6;
    c->K              = 5;
127
  }
128
129
130
  
  DefiniteIntegral::~DefiniteIntegral() {
    delete c;
131
  }
132

Lynn Garren's avatar
Lynn Garren committed
133
134
  DefiniteIntegral::DefiniteIntegral(const DefiniteIntegral & right) 
  :AbsFunctional(right), c(new Clockwork(*right.c)) {
135
  }
136
137
138
139
140

  DefiniteIntegral & DefiniteIntegral::operator = (const DefiniteIntegral & right) {
    if (this!=&right) {
      delete c;
      c = new Clockwork(*right.c);
141
    }
142
143
144
145
146
    return *this;
  }
  
  void DefiniteIntegral::setEpsilon(double eps) {
    c->EPS=eps;
147
148
  }

149
150
151
  void DefiniteIntegral::setMaxIter(unsigned int maxIter) {
    c->MAXITER=maxIter;
  }
152

153
154
155
  void DefiniteIntegral::setMinOrder(unsigned int minOrder) {
    c->K=(minOrder+1)/2;
  }
156

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  double DefiniteIntegral::operator [] (const AbsFunction & function) const {

    const Clockwork::QuadratureRule * rule = c->type==OPEN ? 
      (Clockwork::QuadratureRule *) new Clockwork::XtMidpointQuadratureRule() : 
      (Clockwork::QuadratureRule *) new Clockwork::TrapezoidQuadratureRule();
    double xMult=rule->stepMultiplier();

    c->nFunctionCalls=0;
    std::vector<double> s(c->MAXITER+2),h(c->MAXITER+2);
    h[1]=1.0;
    for (unsigned int j=1;j<=c->MAXITER;j++) {
      s[j]=rule->integrate(function, c->a, c->b, j);
      c->nFunctionCalls=rule->numFunctionCalls();
      if (j>=c->K) {
	double ss, dss;
	c->polint(h.begin()+j-c->K,s.begin()+j-c->K,0.0,ss, dss);
	if (fabs(dss) <= c->EPS*fabs(ss)) {
	  delete rule;
	  return ss;
	}
      }
      s[j+1]=s[j];
      h[j+1]=h[j]/xMult/xMult;
180
    }
181
182
183
    delete rule;
    throw std::runtime_error("DefiniteIntegral:  too many steps.  No convergence");
    return 0.0;                   // Never get here.
184
  }
185
186
187
188
  
  void DefiniteIntegral::Clockwork::polint(std::vector<double>::iterator xArray, std::vector<double>::iterator yArray, double x, double & y, double & deltay) const {
    double dif = fabs(x-xArray[1]),dift;
    std::vector<double> c(K+1),d(K+1);
Lynn Garren's avatar
Lynn Garren committed
189
    unsigned int ns=1;
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    for (unsigned int i=1;i<=K;i++) {
      dift=fabs(x-xArray[i]);
      if (dift<dif) {
	ns=i;
	dif=dift;
      }
      c[i]=d[i]=yArray[i];
    }
    y = yArray[ns--];
    for (unsigned int m=1;m<K;m++) {
      for (unsigned int i=1;i<=K-m;i++) {
	double ho = xArray[i]-x;
	double hp=  xArray[i+m]-x;
	double w=c[i+1]-d[i];
	double den=ho-hp;
	if (den==0)
	  std::cerr
	    << "Error in polynomial extrapolation"
	    << std::endl;
	den=w/den;
	d[i]=hp*den;
	c[i]=ho*den;
      }
      deltay = 2*ns < (K-m) ? c[ns+1]: d[ns--];
      y += deltay;
215
216
    }
  }
217
  
218
  unsigned int DefiniteIntegral::numFunctionCalls() const {
219
    return c->nFunctionCalls;
220
  }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

  // Quadrature rules:
  double DefiniteIntegral::Clockwork::TrapezoidQuadratureRule::integrate(const AbsFunction & function, double a, double b, unsigned int n) const {
    unsigned int it, j;
    if (n==1) {
      retVal = 0.5*(b-a)*(function(a)+function(b));
      nFunctionCalls+=2;
    }
    else { 
      for (it=1,j=1;j<n-1;j++)  it <<=1;
      double tnm=it;
      double del = (b-a)/tnm;
      double x=a+0.5*del;
      double sum;
      for (sum=0.0,j=1;j<=it;j++,x+=del) {
	sum +=function(x);
	nFunctionCalls++;
      }
      retVal = 0.5*(retVal+(b-a)*sum/tnm);
    }
    return retVal;
  }

  // Quadrature rules:
  double DefiniteIntegral::Clockwork::XtMidpointQuadratureRule::integrate(const AbsFunction & function, double a, double b, unsigned int n) const {
    unsigned int it, j;
    if (n==1) {
      retVal = (b-a)*(function((a+b)/2.0));
      nFunctionCalls+=1;
    }
    else { 
      for (it=1,j=1;j<n-1;j++)  it *=3;
      double tnm=it;
      double del  = (b-a)/(3.0*tnm);
      double ddel = del+del;
      double x=a+0.5*del;
      double sum=0;
      for (j=1;j<=it;j++) {
	sum +=function(x);
	x+=ddel;
	sum +=function(x);
	x+=del;
	nFunctionCalls+=2;
      }
      retVal = (retVal+(b-a)*sum/tnm)/3.0;
    }
    return retVal;
  }
  
  

272
} // namespace Genfun