ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ChebyshevApprox.cxx
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Authors: L. Moneta, A. Zsenei 08/2005
3 
4 
5  /**********************************************************************
6  * *
7  * Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
8  * *
9  * This library is free software; you can redistribute it and/or *
10  * modify it under the terms of the GNU General Public License *
11  * as published by the Free Software Foundation; either version 2 *
12  * of the License, or (at your option) any later version. *
13  * *
14  * This library is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
17  * General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this library (see file COPYING); if not, write *
21  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
22  * 330, Boston, MA 02111-1307 USA, or contact the author. *
23  * *
24  **********************************************************************/
25 
26 // Implementation file for class ChebyshevApprox
27 //
28 // Created by: moneta at Thu Dec 2 14:51:15 2004
29 //
30 // Last update: Thu Dec 2 14:51:15 2004
31 //
32 
33 
34 #include "Math/IFunction.h"
35 
36 #include "Math/ChebyshevApprox.h"
37 #include "GSLFunctionWrapper.h"
38 #include "GSLChebSeries.h"
39 
40 #include "gsl/gsl_chebyshev.h"
41 
42 #include <cassert>
43 
44 
45 namespace ROOT {
46 namespace Math {
47 
48 
49 ChebyshevApprox::ChebyshevApprox(const ROOT::Math::IGenFunction & f, double a, double b, size_t n) :
50  fOrder(n) , fSeries(0), fFunction(0)
51 {
52  // constructor from function (IGenFunction type) and interval [a,b] and series size n
53  fSeries = new GSLChebSeries(n);
55  const void * p = &f;
56  Initialize( &adapter.F, const_cast<void *>(p), a, b );
57 }
58 
59 // constructor with GSL function
60 ChebyshevApprox::ChebyshevApprox(GSLFuncPointer f, void * params, double a, double b, size_t n) :
61 fOrder(n) , fSeries(0), fFunction(0)
62 {
63  // constructor from function (GSL type) and interval [a,b] and series size n
64  fSeries = new GSLChebSeries(n);
65  Initialize( f, params, a, b );
66 }
67 
69 {
70  // desctructor (clean up resources)
71  if (fFunction) delete fFunction;
72  if (fSeries) delete fSeries;
73 }
74 
76 fOrder(n) , fSeries(0), fFunction(0)
77 {
78  // constructor passing only size (need to initialize setting the function afterwards)
79  fSeries = new GSLChebSeries(n);
80 }
81 
83 {
84  // cannot copy series because don't know original function
85 }
86 
88 {
89  // dummy assignment
90  if (this == &rhs) return *this; // time saving self-test
91 
92  return *this;
93 }
94 
95 void ChebyshevApprox::Initialize( GSLFuncPointer f, void * params, double a, double b) {
96  // initialize by passing a function and interval [a,b]
97  // delete previous existing function pointer
98  assert(fSeries != 0);
99  if (fFunction) delete fFunction;
100 
103  fFunction->SetParams( params );
104 
105  // check for errors here ???
106  gsl_cheb_init( fSeries->get(), fFunction->GetFunc(), a, b);
107 }
108 
109 double ChebyshevApprox::operator() ( double x ) const {
110  // evaluate the approximation
111  return gsl_cheb_eval(fSeries->get(), x);
112 }
113 
114 std::pair<double, double> ChebyshevApprox::EvalErr( double x) const {
115  // evaluate returning result and error
116  double result, error;
117  gsl_cheb_eval_err(fSeries->get(), x, &result, &error);
118  return std::make_pair( result, error);
119 }
120 
121 double ChebyshevApprox::operator() ( double x, size_t n) const {
122  // evaluate at most order n ( truncate the series)
123  return gsl_cheb_eval_n(fSeries->get(), n, x);
124 }
125 
126 std::pair<double, double> ChebyshevApprox::EvalErr( double x, size_t n) const {
127  // evaluate at most order n ( truncate the series) returning resutl + error
128  double result, error;
129  gsl_cheb_eval_n_err(fSeries->get(), n, x, &result, &error);
130  return std::make_pair( result, error);
131 }
132 
133 
134 
136  // calculate derivative. Returna pointer to a new series
137  // used auto_ptr (supprseed since not good support on some compilers)
138  ChebyshevApprox * deriv = new ChebyshevApprox(fOrder);
139 
140  // check for errors ?
141  gsl_cheb_calc_deriv( (deriv->fSeries)->get(), fSeries->get() );
142  return deriv;
143  // diable auto_ptr to fix AIX compilation
144  // std::auto_ptr<Chebyshev> pDeriv(deriv);
145  // return pDeriv;
146 }
147 
149  // integral (return pointer)
150  ChebyshevApprox * integ = new ChebyshevApprox(fOrder);
151 
152  // check for errors ?
153  gsl_cheb_calc_integ( (integ->fSeries)->get(), fSeries->get() );
154  return integ;
155  // std::auto_ptr<Chebyshev> pInteg(integ);
156  // return pInteg;
157 }
158 
159 } // namespace Math
160 } // namespace ROOT
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...
#define assert(cond)
Definition: unittest.h:542
void Initialize(GSLFuncPointer f, void *params, double a, double b)
Initialize series passing function and range.
TArc * a
Definition: textangle.C:12
wrapper class for C struct gsl_cheb_series
Definition: GSLChebSeries.h:44
TFile * f
double operator()(double x) const
Evaluate the series at a given point x.
GSLFunctionWrapper * fFunction
Double_t x[n]
Definition: legend1.C:17
ChebyshevApprox & operator=(const ChebyshevApprox &)
ChebyshevApprox(const ROOT::Math::IGenFunction &f, double a, double b, size_t n)
Construct a Chebyshev series approximation to a Function f in range [a,b]; constructor based on funct...
double(* GSLFuncPointer)(double, void *)
Function pointer corresponding to gsl_function signature.
Class for adapting any C++ functor class to C function pointers used by GSL.
ChebyshevApprox * Deriv()
Compute the derivative of the series and return a pointer to a new Chebyshev series with the derivati...
void SetFuncPointer(GSLFuncPointer f)
set in the GSL C struct the pointer to the function evaluation
Wrapper class to the gsl_function C structure.
double result[121]
void SetParams(void *p)
set in the GSL C struct the extra-object pointer
gsl_cheb_series * get() const
Definition: GSLChebSeries.h:63
const Int_t n
Definition: legend1.C:16
static double F(double x, void *p)