CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
InterpolatingPolynomial.cc
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id:
3#include "CLHEP/GenericFunctions/InterpolatingPolynomial.hh"
4#include <cassert>
5#include <cmath>
6#include <cfloat>
7#include <iostream>
8#include <vector>
9
10namespace Genfun {
11 FUNCTION_OBJECT_IMP(InterpolatingPolynomial)
12
15 {}
16
18 :Genfun::AbsFunction(),xPoints(right.xPoints)
19 {}
20
22 }
23
24 double InterpolatingPolynomial::operator() (double x) const {
25 double y=0.0;
26 double deltay=0; // also gives error;
27 double dif = fabs(x-xPoints[0].first),dift;
28 const unsigned int _K = (unsigned int)xPoints.size(),_KP=_K+1;
29 std::vector<double>c(_KP),d(_KP);
30 int ns=0;
31 for (unsigned int i=0;i<_K;i++) {
32 dift=fabs(x-xPoints[i].first);
33 if (dift<dif) {
34 ns=i;
35 dif=dift;
36 }
37 c[i]=d[i]=xPoints[i].second;
38 }
39 y = xPoints[ns--].second;
40 for (unsigned int m=0;m<_K-1;m++) {
41 for (unsigned int i=0;i<_K-m-1;i++) {
42 double ho = xPoints[i].first-x;
43 double hp= xPoints[i+m+1].first-x;
44 double w=c[i+1]-d[i];
45 double den=ho-hp;
46 if (den==0)
47 std::cerr
48 << "Error in polynomial extrapolation"
49 << std::endl;
50 den=w/den;
51 d[i]=hp*den;
52 c[i]=ho*den;
53 }
54 deltay = 2*(ns+1) < (int)(_K-m-1) ? c[ns+1]: d[ns--];
55 y += deltay;
56 }
57 return y;
58 }
59
60 void InterpolatingPolynomial::addPoint( double x, double y) {
61 xPoints.push_back(std::make_pair(x,y));
62 }
63
64 void InterpolatingPolynomial::getRange( double & min, double & max) const {
65 min=DBL_MAX, max=-DBL_MAX;
66 for (unsigned int i=0;i<xPoints.size();i++) {
67 min = std::min(min,xPoints[i].first);
68 max = std::max(max,xPoints[i].first);
69 }
70 }
71} // namespace Genfun
#define FUNCTION_OBJECT_IMP(classname)
Definition: AbsFunction.hh:149
void getRange(double &min, double &max) const
virtual double operator()(double argument) const override
Definition: Abs.hh:14