/* CubicSpline.cpp Class for generating cubic splines of data in tables or held in memory. Cubic splines are a form of interpolation, somewhat more advanced than linear interpolation, but not so complicated that it significantly slows down a calculation. For more information see Wikipedia or Num. Rec. in C. Gordon M. May 2021 */ #include "CubicSpline.h" #include #include #include namespace PunchTable { CubicSpline::CubicSpline() : m_validFlag(false) { } CubicSpline::CubicSpline(const std::string& filename) : m_validFlag(false) { ReadFile(filename); } CubicSpline::CubicSpline(const std::vector& x, const std::vector& y) : m_validFlag(false) { ReadData(x, y); } CubicSpline::~CubicSpline() {} /* Expected file format is a naked (no header) single space separated table: x y\n */ void CubicSpline::ReadFile(const std::string& filename) { std::ifstream input(filename); if(!input.is_open()) { std::cerr<<"Unable to open input data at CubicSpline::ReadFile from filename: "<>x) { input>>y; m_dataX.push_back(x); m_dataY.push_back(y); } if(m_dataX.size() != m_dataY.size()) { std::cerr<<"Error in CubicSpline::ReadFile! Number of x points not equal to number of y points!"<=0; i--) k[i] = b[i] - a[i][i+1]*k[i+1]; //Fill the spline data for(size_t i=0; i= spline.x1 && x <= spline.x2) { s = spline; break; } else if (i == (m_splines.size() -1)) { //std::cerr<<"Error at CubicSpline::Evaluate! Input x value: "<= spline.x1 && xval <= spline.x2) { s = spline; break; } else if (i == (m_splines.size() -1)) { return 0.0; } } double t = (xval-s.x1)/(s.x2-s.x1); double a = s.k1*(s.x2-s.x1)-(s.y2-s.y1); double b = -s.k2*(s.x2-s.x1)+(s.y2-s.y1); return (1.0-t)*s.y1+t*s.y2+t*(1.0-t)*((1.0-t)*a+t*b); } }