/**Evaluate a point on the spline. Includes basic error handling * * @param x :: Point to evaluate * @return :: the value of the spline at the given point */ double CubicSpline::splineEval(const double x) const { // calculate the y value double y = gsl_spline_eval(m_spline.get(), x, m_acc.get()); int errorCode = gsl_spline_eval_e(m_spline.get(), x, m_acc.get(), &y); // check if GSL function returned an error checkGSLError(errorCode, GSL_EDOM); return y; }
/** Calculate the derivatives of each of the supplied points * * @param out :: The array to store the calculated derivatives * @param xValues :: The array of x values we wish to calculate derivatives at * @param nData :: The size of the arrays * @param order :: The order of derivatives to calculate too */ void CubicSpline::calculateDerivative(double* out, const double* xValues, const size_t nData, const size_t order) const { double xDeriv = 0; int errorCode = 0; bool outOfRange(false); //throw error if the order is not the 1st or 2nd derivative if(order < 1) throw std::invalid_argument( "CubicSpline: order of derivative must be 1 or greater"); for(size_t i = 0; i < nData; ++i) { if(checkXInRange(xValues[i])) { //choose the order of the derivative if(order == 1) { xDeriv = gsl_spline_eval_deriv(m_spline.get(),xValues[i],m_acc.get()); errorCode = gsl_spline_eval_deriv_e (m_spline.get(),xValues[i],m_acc.get(),&xDeriv); } else if (order == 2) { xDeriv = gsl_spline_eval_deriv2(m_spline.get(),xValues[i],m_acc.get()); errorCode = gsl_spline_eval_deriv2_e (m_spline.get(),xValues[i],m_acc.get(),&xDeriv); } } else { //if out of range, just set it to zero outOfRange = true; xDeriv = 0; } //check GSL functions didn't return an error checkGSLError(errorCode, GSL_EDOM); //record the value out[i] = xDeriv; } //warn user that some values weren't calculated if(outOfRange) { g_log.warning() << "Some x values where out of range and will not be calculated." << std::endl; } }
/** Initilize the GSL spline with the given points * * @param x :: The x points defining the spline * @param y :: The y points defining the spline * @param n :: The size of the arrays */ void CubicSpline::initGSLObjects(boost::scoped_array<double>& x, boost::scoped_array<double>& y, int n) const { int status = gsl_spline_init(m_spline.get(), x.get(), y.get(), n); checkGSLError(status, GSL_EINVAL); }