CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

InterpolatingPolynomial.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: 
00003 #include "CLHEP/GenericFunctions/InterpolatingPolynomial.hh"
00004 #include <cassert>
00005 #include <cmath>
00006 #include <cfloat>
00007 namespace Genfun {
00008   FUNCTION_OBJECT_IMP(InterpolatingPolynomial)
00009   
00010   InterpolatingPolynomial::InterpolatingPolynomial()
00011   :Genfun::AbsFunction()
00012   {}
00013   
00014   InterpolatingPolynomial::InterpolatingPolynomial(const InterpolatingPolynomial & right)
00015     :Genfun::AbsFunction(),xPoints(right.xPoints)
00016   {}
00017   
00018   InterpolatingPolynomial::~InterpolatingPolynomial() {
00019   }
00020   
00021   double InterpolatingPolynomial::operator() (double x) const {
00022     double y=0.0;
00023     double deltay=0;  // also gives error;
00024     double dif = fabs(x-xPoints[0].first),dift;
00025     const unsigned int _K=xPoints.size(),_KP=_K+1;
00026     std::vector<double>c(_KP),d(_KP);
00027     int ns=0;
00028     for (unsigned int i=0;i<_K;i++) {
00029       dift=fabs(x-xPoints[i].first);
00030       if (dift<dif) {
00031         ns=i;
00032         dif=dift;
00033       }
00034       c[i]=d[i]=xPoints[i].second;
00035     }
00036     y = xPoints[ns--].second;
00037     for (unsigned int m=0;m<_K-1;m++) {
00038       for (unsigned int i=0;i<_K-m-1;i++) {
00039         double ho = xPoints[i].first-x;
00040         double hp=  xPoints[i+m+1].first-x;
00041         double w=c[i+1]-d[i];
00042         double den=ho-hp;
00043         if (den==0)
00044           std::cerr
00045             << "Error in polynomial extrapolation"
00046             << std::endl;
00047         den=w/den;
00048         d[i]=hp*den;
00049         c[i]=ho*den;
00050       }
00051       deltay = 2*(ns+1) < (int)(_K-m-1) ? c[ns+1]: d[ns--];
00052       y += deltay;
00053     }
00054     return y;
00055   }
00056   
00057   void InterpolatingPolynomial::addPoint( double x, double y) {
00058     xPoints.push_back(std::make_pair(x,y));
00059   }
00060 
00061   void InterpolatingPolynomial::getRange( double & min, double & max) const {
00062     min=DBL_MAX, max=-DBL_MAX;
00063     for (unsigned int i=0;i<xPoints.size();i++) {
00064       min = std::min(min,xPoints[i].first);
00065       max = std::max(max,xPoints[i].first);
00066     }
00067   }
00068 } // namespace Genfun

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7