ROOT  6.06/09
Reference Guide
testChebyshev.cxx
Go to the documentation of this file.
1 
2 #include "Math/ChebyshevApprox.h"
3 #include "Math/IFunction.h"
4 #include "Math/Functor.h"
5 #include "Math/SpecFunc.h"
6 
7 //#include "MathCore/GSLIntegrator.h"
8 
9 #include <iostream>
10 #include <cmath>
11 
12 
13 typedef double ( * FP ) ( double, void * );
14 
15 // function is a step function
16 
17 double myfunc ( double x, void * /* params */) {
18  //double * p = reinterpret_cast<double *>( params);
19  if (x < 0.5)
20  return 0.25;
21  else
22  return 0.75;
23 }
24 
25 double gamma_func( double x, void *)
26 {
27  return ROOT::Math::tgamma(x);
28 }
29 
30 // gamma function
31 class GammaFunction : public ROOT::Math::IGenFunction {
32 
33 public:
34 
35 
37  return new GammaFunction();
38  }
39 
40 private:
41 
42  double DoEval ( double x) const {
43  return ROOT::Math::tgamma(x);
44  }
45 
46 };
47 
48 
49 int printCheb( const ROOT::Math::ChebyshevApprox & c, double x0, double x1, FP func = 0 ) {
50 
51  double dx = (x1-x0)/10;
52  for ( double x = x0; x < x1; x+= dx ) {
53 
54  double y = c(x);
55  double ey = c.EvalErr(x).second;
56  double y10 = c(x,10);
57  double ey10 = c.EvalErr(x,10).second;
58  double fVal = 0;
59  if (func) fVal = func(x,0);
60  std::cout << " x = " << x << " true Val = " << fVal << " y = " << y << " +/- " << ey << " y@10 = " << y10 << " +/- " << ey10 << std::endl;
61  }
62 
63 
64 
65  return 0;
66 }
67 
68 
69 
70 int main() {
71 
72 
73  // test with cos(x) + 1.0
74  std::cout << "Test Cheb approx to step function :" << std::endl;
75  ROOT::Math::ChebyshevApprox c(myfunc, 0, 0., 1.0, 40);
76  printCheb(c, 0, 1, myfunc);
77  std::cout << "Test integral of step function :" << std::endl;
79  printCheb(*cInteg, 0., 1);
80  delete cInteg;
81 
82 
83  std::cout << "Test Cheb approx to Gamma function :" << std::endl;
84  GammaFunction gf;
85  ROOT::Math::ChebyshevApprox c2(gf, 1.0, 2.0, 40);
86  printCheb(c2, 1.0, 2.0, gamma_func);
87  std::cout << "Test derivative of gammma :" << std::endl;
88  ROOT::Math::ChebyshevApprox * cDeriv = c2.Deriv();
89  printCheb(*cDeriv, 1.0, 2.0);
90  delete cDeriv;
91 
92 
93 
94  return 0;
95 
96 }
97 
Class describing a Chebyshev series which can be used to approximate a function in a defined range [a...
std::pair< double, double > EvalErr(double x) const
Evaluate the series at a given point x estimating both the series result and its absolute error...
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
ChebyshevApprox * Integral()
Compute the integral of the series and return a pointer to a new Chebyshev series with the integral c...
double gamma_func(double x, void *)
double myfunc(double x, void *)
double tgamma(double x)
The gamma function is defined to be the extension of the factorial to real numbers.
Double_t x[n]
Definition: legend1.C:17
double(* FP)(double, void *)
ChebyshevApprox * Deriv()
Compute the derivative of the series and return a pointer to a new Chebyshev series with the derivati...
return c2
Definition: legend2.C:14
static const double x1[5]
Double_t y[n]
Definition: legend1.C:17
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Double_t ey[n]
Definition: legend1.C:17
virtual double DoEval(double x) const =0
implementation of the evaluation function. Must be implemented by derived classes ...
int printCheb(const ROOT::Math::ChebyshevApprox &c, double x0, double x1, FP func=0)
int main()
virtual IBaseFunctionOneDim * Clone() const =0
Clone a function.