Chebyshev.cxx

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: Chebyshev.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Authors: L. Moneta, A. Zsenei   08/2005 
00003 
00004 
00005  /**********************************************************************
00006   *                                                                    *
00007   * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
00008   *                                                                    *
00009   * This library is free software; you can redistribute it and/or      *
00010   * modify it under the terms of the GNU General Public License        *
00011   * as published by the Free Software Foundation; either version 2     *
00012   * of the License, or (at your option) any later version.             *
00013   *                                                                    *
00014   * This library is distributed in the hope that it will be useful,    *
00015   * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00016   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00017   * General Public License for more details.                           *
00018   *                                                                    *
00019   * You should have received a copy of the GNU General Public License  *
00020   * along with this library (see file COPYING); if not, write          *
00021   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00022   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00023   *                                                                    *
00024   **********************************************************************/
00025 
00026 // Implementation file for class Chebyshev
00027 // 
00028 // Created by: moneta  at Thu Dec  2 14:51:15 2004
00029 // 
00030 // Last update: Thu Dec  2 14:51:15 2004
00031 // 
00032 
00033 
00034 #include "Math/IFunction.h"
00035 
00036 #include "Math/Chebyshev.h"
00037 #include "GSLFunctionWrapper.h"
00038 #include "GSLChebSeries.h"
00039 
00040 #include "gsl/gsl_chebyshev.h"
00041 
00042 #include <cassert>
00043 
00044 
00045 namespace ROOT {
00046 namespace Math {
00047 
00048 
00049 Chebyshev::Chebyshev(const ROOT::Math::IGenFunction & f, double a, double b, size_t n) : 
00050    fOrder(n) , fSeries(0), fFunction(0)
00051 {
00052    // constructor from function (IGenFunction type) and interval [a,b] and series size n
00053    fSeries = new GSLChebSeries(n); 
00054    GSLFunctionAdapter<ROOT::Math::IGenFunction> adapter;
00055    const void * p = &f; 
00056    Initialize(  &adapter.F, const_cast<void *>(p), a, b );     
00057 }
00058 
00059 // constructor with GSL function
00060 Chebyshev::Chebyshev(GSLFuncPointer f, void * params, double a, double b, size_t n) :
00061 fOrder(n) , fSeries(0), fFunction(0)
00062 {
00063    // constructor from function (GSL type) and interval [a,b] and series size n  
00064    fSeries = new GSLChebSeries(n); 
00065    Initialize(  f, params, a, b ); 
00066 }
00067 
00068 Chebyshev::~Chebyshev() 
00069 {
00070    // desctructor (clean up resources)
00071    if (fFunction) delete fFunction;
00072    if (fSeries) delete fSeries;
00073 }
00074 
00075 Chebyshev::Chebyshev(size_t n) : 
00076 fOrder(n) , fSeries(0), fFunction(0)
00077 {
00078    // constructor passing only size (need to initialize setting the function afterwards) 
00079    fSeries = new GSLChebSeries(n); 
00080 }
00081 
00082 Chebyshev::Chebyshev(const Chebyshev & /*cheb */ )  
00083 {
00084    // cannot copy series because don't know original function
00085 }
00086 
00087 Chebyshev & Chebyshev::operator = (const Chebyshev &rhs) 
00088 {
00089    // dummy assignment
00090    if (this == &rhs) return *this;  // time saving self-test
00091    
00092    return *this;
00093 }
00094 
00095 void Chebyshev::Initialize( GSLFuncPointer f, void * params, double a, double b) { 
00096    // initialize by passing a function and interval [a,b] 
00097    // delete previous existing function pointer
00098    assert(fSeries != 0); 
00099    if (fFunction) delete fFunction;
00100    
00101    fFunction = new GSLFunctionWrapper(); 
00102    fFunction->SetFuncPointer( f ); 
00103    fFunction->SetParams( params ); 
00104    
00105    // check for errors here ???
00106    gsl_cheb_init( fSeries->get(), fFunction->GetFunc(), a, b); 
00107 }
00108 
00109 double Chebyshev::operator() ( double x ) const {
00110    // evaluate the approximation
00111    return gsl_cheb_eval(fSeries->get(), x);
00112 } 
00113 
00114 std::pair<double, double>  Chebyshev::EvalErr( double x) const { 
00115    // evaluate returning result and error
00116    double result, error; 
00117    gsl_cheb_eval_err(fSeries->get(), x, &result, &error);
00118    return std::make_pair( result, error); 
00119 }
00120 
00121 double Chebyshev::operator() ( double x, size_t n) const {
00122    // evaluate at most order n ( truncate the series)
00123    return gsl_cheb_eval_n(fSeries->get(), n, x);
00124 }
00125 
00126 std::pair<double, double>  Chebyshev::EvalErr( double x, size_t n) const { 
00127    // evaluate at most order n ( truncate the series) returning resutl + error
00128    double result, error; 
00129    gsl_cheb_eval_n_err(fSeries->get(), n, x, &result, &error);
00130    return std::make_pair( result, error); 
00131 }
00132 
00133 
00134 
00135 Chebyshev *   Chebyshev::Deriv() { 
00136    // calculate derivative. Returna pointer to a new series 
00137    // used auto_ptr (supprseed since not good support on some compilers)
00138    Chebyshev * deriv = new Chebyshev(fOrder); 
00139    
00140    // check for errors ? 
00141    gsl_cheb_calc_deriv( (deriv->fSeries)->get(), fSeries->get() );
00142    return deriv;
00143    // diable auto_ptr to fix AIX compilation
00144    //   std::auto_ptr<Chebyshev> pDeriv(deriv);
00145    //   return pDeriv;  
00146 }
00147 
00148 Chebyshev * Chebyshev::Integral() { 
00149    // integral (return pointer)
00150    Chebyshev * integ = new Chebyshev(fOrder); 
00151    
00152    // check for errors ? 
00153    gsl_cheb_calc_integ( (integ->fSeries)->get(), fSeries->get() );
00154    return integ;
00155    //   std::auto_ptr<Chebyshev> pInteg(integ);
00156    //   return pInteg;  
00157 }
00158 
00159 } // namespace Math
00160 } // namespace ROOT

Generated on Tue Jul 5 14:34:31 2011 for ROOT_528-00b_version by  doxygen 1.5.1