npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
GaussLegendreQuadrature.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_GAUSSLEGENDREQUADRATURE_HH_
2 #define NPSTAT_GAUSSLEGENDREQUADRATURE_HH_
3 
4 /*!
5 // \file GaussLegendreQuadrature.hh
6 //
7 // \brief Gauss-Legendre quadratures in long double precision
8 //
9 // Author: I. Volobouev
10 //
11 // April 2010
12 */
13 
14 #include <vector>
15 #include <utility>
16 #include <cassert>
17 
20 
21 namespace npstat {
22  /**
23  // Gauss-Legendre quadrature. Internally, operations are performed
24  // in long double precision.
25  */
27  {
28  public:
29  enum {interval_quadrature = 1};
30 
31  /**
32  // At the moment, the following numbers of points are supported:
33  // 2, 4, 6, 8, 10, 12, 16, 32, 64, 100, 128, 256, 512, 1024.
34  //
35  // If an unsupported number of points is given in the
36  // constructor, std::invalid_argument exception will be thrown.
37  */
38  explicit GaussLegendreQuadrature(unsigned npoints);
39 
40  inline virtual ~GaussLegendreQuadrature() {}
41 
42  /** Return the number of quadrature points */
43  inline unsigned npoints() const {return npoints_;}
44 
45  /**
46  // The abscissae are returned for positive points only,
47  // so the buffer length should be at least npoints/2.
48  */
49  void getAbscissae(long double* abscissae, unsigned len) const;
50 
51  /** Return abscissae for all points */
52  void getAllAbscissae(long double* abscissae, unsigned len) const;
53 
54  /**
55  // The weights are returned for positive points only,
56  // so the buffer length should be at least npoints/2.
57  */
58  void getWeights(long double* weights, unsigned len) const;
59 
60  /** Return weights for all points */
61  void getAllWeights(long double* weights, unsigned len) const;
62 
63  /** Perform the quadrature on the [a, b] interval */
64  template <typename FcnResult, typename FcnArg>
65  long double integrate(const Functor1<FcnResult,FcnArg>& fcn,
66  long double a, long double b) const;
67 
68  /**
69  // This method splits the interval [a, b] into "nsplit"
70  // subintervals of equal length, applies Gauss-Legendre
71  // quadrature to each subinterval, and sums the results.
72  */
73  template <typename FcnResult, typename FcnArg>
74  long double integrate(const Functor1<FcnResult,FcnArg>& fcn,
75  long double a, long double b,
76  unsigned nsplit) const;
77 
78  /** Check if the rule with the given number of points is supported */
79  static bool isAllowed(unsigned npoints);
80 
81  /** The complete set of allowed rules, in the increasing order */
82  static std::vector<unsigned> allowedNPonts();
83 
84  /**
85  // Minimum number of points, among the supported rules, which
86  // integrates a polynomial with the given degree exactly.
87  // Returns 0 if the degree is out of range.
88  */
89  static unsigned minimalExactRule(unsigned polyDegree);
90 
91  private:
93 
94  const long double* a_;
95  const long double* w_;
96  mutable std::vector<std::pair<long double, long double> > buf_;
97  unsigned npoints_;
98 
99 #ifdef SWIG
100  public:
101  inline std::vector<double> abscissae2() const
102  {
103  return std::vector<double>(a_, a_+npoints_/2U);
104  }
105 
106  inline std::vector<double> weights2() const
107  {
108  return std::vector<double>(w_, w_+npoints_/2U);
109  }
110 
111  inline std::vector<double> allabscissae2() const
112  {
113  assert(a_);
114  std::vector<double> abscissae(npoints_);
115  const unsigned halfpoints = npoints_/2;
116  for (unsigned i=0; i<halfpoints; ++i)
117  abscissae[i] = -a_[halfpoints - 1U - i];
118  for (unsigned i=halfpoints; i<npoints_; ++i)
119  abscissae[i] = a_[i - halfpoints];
120  return abscissae;
121  }
122 
123  inline std::vector<double> allweights2() const
124  {
125  assert(w_);
126  std::vector<double> weights(npoints_);
127  const unsigned halfpoints = npoints_/2;
128  for (unsigned i=0; i<halfpoints; ++i)
129  weights[i] = w_[halfpoints - 1U - i];
130  for (unsigned i=halfpoints; i<npoints_; ++i)
131  weights[i] = w_[i - halfpoints];
132  return weights;
133  }
134 
135  template <typename FcnResult, typename FcnArg>
136  inline double integrate2(const Functor1<FcnResult,FcnArg>& fcn,
137  const double a, const double b) const
138  {
139  return integrate(fcn, a, b);
140  }
141 
142  template <typename FcnResult, typename FcnArg>
143  inline double integrate2(const Functor1<FcnResult,FcnArg>& fcn,
144  const double a, const double b,
145  const unsigned nsplit) const
146  {
147  return integrate(fcn, a, b, nsplit);
148  }
149 #endif // SWIG
150  };
151 }
152 
153 #include "npstat/nm/GaussLegendreQuadrature.icc"
154 
155 #endif // NPSTAT_GAUSSLEGENDREQUADRATURE_HH_
Base class for quadratures on the [-1, 1] interval.
Interface definitions and concrete simple functors for a variety of functor-based calculations.
Definition: AbsIntervalQuadrature1D.hh:19
Definition: GaussLegendreQuadrature.hh:27
static std::vector< unsigned > allowedNPonts()
long double integrate(const Functor1< FcnResult, FcnArg > &fcn, long double a, long double b) const
void getAbscissae(long double *abscissae, unsigned len) const
void getAllAbscissae(long double *abscissae, unsigned len) const
static bool isAllowed(unsigned npoints)
long double integrate(const Functor1< FcnResult, FcnArg > &fcn, long double a, long double b, unsigned nsplit) const
void getAllWeights(long double *weights, unsigned len) const
GaussLegendreQuadrature(unsigned npoints)
void getWeights(long double *weights, unsigned len) const
unsigned npoints() const
Definition: GaussLegendreQuadrature.hh:43
static unsigned minimalExactRule(unsigned polyDegree)
Definition: AbsArrayProjector.hh:14
Definition: SimpleFunctors.hh:58