MUQ  0.4.3
OrthogonalPolynomial.cpp
Go to the documentation of this file.
2 
4 
5 #include <cstdlib>
6 #include <typeinfo>
7 #include <memory>
8 #include <cxxabi.h>
9 
10 using namespace muq::Modeling;
11 using namespace muq::Approximation;
12 
13 
14 std::shared_ptr<OrthogonalPolynomial> OrthogonalPolynomial::Construct(std::string const& polyName)
15 {
16  return std::dynamic_pointer_cast<OrthogonalPolynomial>(IndexedScalarBasis::Construct(polyName));
17 }
18 
19 
20 double OrthogonalPolynomial::Normalization(unsigned int polyOrder) const {
21 
22  std::string rawName = typeid(*this).name();
23 
24  int status = -4; // some arbitrary value to eliminate the compiler warning
25 
26  // enable c++11 by passing the flag -std=c++11 to g++
27  std::unique_ptr<char, void(*)(void*)> res {
28  abi::__cxa_demangle(rawName.c_str(), NULL, NULL, &status),
29  std::free
30  };
31 
32  std::string className = (status==0) ? res.get() : rawName;
33 
34  throw muq::NotImplementedError("The Normalization function has not been implemented for the class \"" + className + "\". Is this polynomial family orthogonal?");
35 
36  return std::numeric_limits<double>::quiet_NaN();
37 };
38 
39 double OrthogonalPolynomial::BasisEvaluate(int const order, double const x) const {
40 
41  if(order==0){
42  return phi0(x);
43  }else if(order==1){
44  return phi1(x);
45  }else{
46 
47  // "Downward" Clenshaw algorithm http://mathworld.wolfram.com/ClenshawRecurrenceFormula.html
48  double yk2 = 0.0;
49  double yk1 = 0.0;
50  double yk = 1.0;
51  double alpha, beta;
52 
53  for( int k=order-1; k>=0; k-- ) {
54  yk2 = yk1;
55  yk1 = yk;
56 
57  alpha = ak(k+1)*x + bk(k+1);
58  beta = -ck(k+2);
59  yk = alpha*yk1 + beta*yk2;
60  }
61  beta = -ck(2);
62  return yk1*phi1(x) + beta * phi0(x)*yk2;
63  }
64 }
65 
66 Eigen::VectorXd OrthogonalPolynomial::EvaluateAllTerms(int const maxOrder,
67  double const x) const {
68 
69  Eigen::VectorXd output(maxOrder+1);
70  output(0) = phi0(x);
71  if(maxOrder>0)
72  output(1) = phi1(x);
73 
74  for(int i=2; i<=maxOrder; ++i)
75  output(i) = (ak(i)*x + bk(i))*output(i-1) - ck(i)*output(i-2);
76 
77  return output;
78 }
Class for virtual base functions that are not implemented.
Definition: Exceptions.h:22