3 #include "CLHEP/Matrix/Matrix.h" 4 #include "CLHEP/Matrix/Vector.h" 9 FUNCTION_OBJECT_IMP(CubicSplinePolynomial)
15 mutable CLHEP::HepMatrix
A;
16 mutable CLHEP::HepVector
V;
17 mutable CLHEP::HepVector
Q;
18 std::vector<std::pair<double,double> >
xPoints;
21 unsigned int N=xPoints.size()-1;
22 A=CLHEP::HepMatrix(N+1,N+1,0);
23 V=CLHEP::HepVector(N+1,0);
26 for (
unsigned int i=1;i<N;i++) {
28 double dxPlus = xPoints[i+1].first -xPoints[i].first;
29 double dyPlus = xPoints[i+1].second-xPoints[i].second;
30 double mPlus = dyPlus/dxPlus;
32 double dx = xPoints[i].first -xPoints[i-1].first;
33 double dy = xPoints[i].second-xPoints[i-1].second;
37 A[i][i] = 2*(1/dxPlus + 1/dx);
40 V[i] = 3*(m/dx+mPlus/dxPlus);
45 double dx = xPoints[1].first -xPoints[0].first;
46 double dy = xPoints[1].second-xPoints[0].second;
54 double dx = xPoints[N].first -xPoints[N-1].first;
55 double dy = xPoints[N].second-xPoints[N-1].second;
66 inline CubicSplinePolynomial::CubicSplinePolynomial()
67 :
Genfun::AbsFunction(),c(new Clockwork())
72 inline CubicSplinePolynomial::CubicSplinePolynomial(
const CubicSplinePolynomial & right)
73 :
Genfun::AbsFunction(),c(new Clockwork)
78 inline CubicSplinePolynomial::~CubicSplinePolynomial() {
82 inline double CubicSplinePolynomial::operator() (
double x)
const {
83 unsigned int N=c->xPoints.size()-1;
85 if (c->xPoints.size()==0)
return 0;
86 if (c->xPoints.size()==1)
return c->xPoints[0].second;
91 std::pair<double,double> fk(x,0);
92 std::vector<std::pair<double,double> >::const_iterator
93 n=std::lower_bound(c->xPoints.begin(),c->xPoints.end(),fk);
94 unsigned int i = n-c->xPoints.begin();
96 double x0=c->xPoints[0].first;
97 double y0=c->xPoints[0].second;
101 else if (i==c->xPoints.size()) {
102 double x0=c->xPoints[N].first;
103 double y0=c->xPoints[N].second;
105 return y0 + m*(x-x0);
108 double x0=c->xPoints[i-1].first;
109 double x1=c->xPoints[i].first;
110 double y0=c->xPoints[i-1].second;
111 double y1=c->xPoints[i].second;
115 double Q0 = c->Q[i-1];
117 double a = (Q0-m)*dx;
118 double b = (m-Q1)*dx;
119 double t = (x-x0)/dx;
121 return u*y0+t*y1 + t*u*(u*a + t*b);
125 inline void CubicSplinePolynomial::addPoint(
double x,
double y) {
126 c->xPoints.push_back(std::make_pair(x,y));
127 std::sort(c->xPoints.begin(),c->xPoints.end());
131 inline void CubicSplinePolynomial::getRange(
double & min,
double & max)
const {
132 min=DBL_MAX, max=-DBL_MAX;
133 for (
unsigned int i=0;i<c->xPoints.size();i++) {
134 min = std::min(min,c->xPoints[i].first);
135 max = std::max(max,c->xPoints[i].first);
void computeSlopes() const
std::vector< std::pair< double, double > > xPoints