MUQ  0.4.3
ClenshawCurtisQuadrature.cpp
Go to the documentation of this file.
2 
3 using namespace muq::Approximation;
4 
5 
7  nested(nestedIn) {}
8 
9 unsigned int ClenshawCurtisQuadrature::Exactness(unsigned int index) const
10 {
11  return IndexToNumPoints(index)-1;
12 }
13 
14 unsigned int ClenshawCurtisQuadrature::IndexToNumPoints(unsigned int index) const
15 {
16  if(nested){
17  switch (index) {
18  case 0: return 1;
19  case 1: return 3;
20  case 2: return 5;
21  case 3: return 9;
22  case 4: return 17;
23  case 5: return 33;
24  case 6: return 65;
25  case 7: return 129;
26  case 8: return 257;
27  case 9: return 513;
28  case 10: return 1025;
29  case 11: return 2049;
30  case 12: return 4097;
31  case 13: return 8193;
32  case 14: return 16385;
33  case 15: return 32769;
34  default: {
35  std::stringstream msg;
36  msg << "Requested a nested Clenshaw-Curtis rule with index " << index << ", which is not defined. "
37  << "The maximum nested index allowed is 15, which already has 32769 points. "
38  << "Do you really need more than that?" << std::endl;
39 
40  throw std::runtime_error(msg.str());
41  return 0;
42  }
43  }
44  }else{
45  return index + 1;
46  }
47 }
48 
49 void ClenshawCurtisQuadrature::Compute(unsigned int index) {
50 
51  unsigned int numPoints = IndexToNumPoints(index);
52 
53  pts.resize(1,numPoints);
54  wts.resize(numPoints);
55 
56  if(numPoints==1){
57  pts(0,0) = 0.0;
58  wts(0) = 2.0;
59  return;
60  }
61 
62  for(int i=0; i<numPoints; ++i)
63  pts(0,i) = std::cos( double(numPoints-i-1) * pi / double(numPoints - 1));
64 
65  pts(0,0) = -1.0;
66  if((numPoints%2)==1)
67  pts((numPoints+1)/2-1) = 0.0;
68  pts(numPoints-1) = 1.0;
69 
70  wts = Eigen::VectorXd::Ones(numPoints);
71 
72  for(int i=0; i<numPoints; ++i) {
73  double theta = double(i) * pi / double(numPoints-1);
74 
75  for(int j=0; j<(numPoints-1)/2; ++j){
76 
77  double b;
78  if(2*(j+1)==(numPoints-1)){
79  b = 1.0;
80  }else{
81  b = 2.0;
82  }
83 
84  wts(i) -= b*std::cos(2.0*(j+1)*theta) / (4.0*std::pow(j+1,2) - 1);
85  }
86  }
87 
88  wts(0) /= double(numPoints-1);
89  wts.segment(1,numPoints-2) *= 2.0/(numPoints-1);
90  wts(numPoints-1) /= double(numPoints-1);
91 
92 }
virtual unsigned int Exactness(unsigned int quadOrder) const override
unsigned int IndexToNumPoints(unsigned int index) const
virtual void Compute(unsigned int index) override
Base class for multivariate quadrature rules. @detail An abstract class for computing nodes and weigh...
Definition: Quadrature.h:124