OpenMS
Loading...
Searching...
No Matches
CubicSpline2d Class Reference

Natural cubic-spline interpolation of a 2D data set. More...

#include <OpenMS/MATH/MISC/CubicSpline2d.h>

Collaboration diagram for CubicSpline2d:
[legend]

Public Member Functions

 CubicSpline2d (const std::vector< double > &x, const std::vector< double > &y)
 Build the spline from parallel x / y vectors.
 
 CubicSpline2d (const std::map< double, double > &m)
 Build the spline from a map of (x, y) knots.
 
double eval (double x) const
 Evaluate the spline at x.
 
double derivative (double x) const
 First derivative of the spline at x.
 
double derivatives (double x, unsigned order) const
 Derivative of the spline at x.
 

Private Member Functions

void init_ (const std::vector< double > &x, const std::vector< double > &y)
 Initialise the spline coefficients from the supplied knots.
 

Private Attributes

std::vector< double > a_
 Per-segment constant coefficient (degree 0); same value as the y at the segment's left knot.
 
std::vector< double > b_
 Per-segment linear coefficient (degree 1).
 
std::vector< double > c_
 Per-segment quadratic coefficient (degree 2); contains one extra trailing zero from the natural boundary condition.
 
std::vector< double > d_
 Per-segment cubic coefficient (degree 3).
 
std::vector< double > x_
 Sorted knot abscissae (length = number of segments + 1).
 

Detailed Description

Natural cubic-spline interpolation of a 2D data set.

Fits a piecewise cubic polynomial through the supplied (x, y) knots so that the resulting spline is twice continuously differentiable. The construction follows R.L. Burden, J.D. Faires, Numerical Analysis, 4th ed., PWS-Kent 1989, ISBN 0-53491-585-X, pp. 126-131.

Construction is the expensive step; subsequent evaluations are roughly an order of magnitude (~50x) faster than construction in typical cases. After construction, the spline can be sampled at any x in the closed interval [x_first, x_last] via eval, and its first three derivatives are available via derivative / derivatives.

Constructor & Destructor Documentation

◆ CubicSpline2d() [1/2]

CubicSpline2d ( const std::vector< double > &  x,
const std::vector< double > &  y 
)

Build the spline from parallel x / y vectors.

Parameters
[in]xKnot abscissae; must contain at least two entries and be sorted in non-decreasing order.
[in]yKnot ordinates; must have the same length as x.
Exceptions
Exception::IllegalArgumentwhen x and y differ in length, when fewer than two knots are supplied, or when x is not non-decreasing.

◆ CubicSpline2d() [2/2]

CubicSpline2d ( const std::map< double, double > &  m)

Build the spline from a map of (x, y) knots.

The map is automatically sorted by x; passing an empty or single-entry map is an error.

Parameters
[in]mKnots as std::map<x, y>. Must contain at least two entries.
Exceptions
Exception::IllegalArgumentwhen m contains fewer than two entries.

Member Function Documentation

◆ derivative()

double derivative ( double  x) const

First derivative of the spline at x.

Equivalent to derivatives(x, 1).

Parameters
[in]xPosition; must lie in the closed interval [first_knot, last_knot].
Returns
First derivative at x.
Exceptions
Exception::IllegalArgumentwhen x lies outside the knot range.

◆ derivatives()

double derivatives ( double  x,
unsigned  order 
) const

Derivative of the spline at x.

Parameters
[in]xPosition; must lie in the closed interval [first_knot, last_knot].
[in]orderDerivative order. Cubic splines provide meaningful first, second, and third derivatives; this method accepts order in {1, 2, 3}. Higher orders are zero everywhere and are rejected with an exception.
Returns
order -th derivative of the spline at x.
Exceptions
Exception::IllegalArgumentwhen x lies outside the knot range, or when order is not 1, 2, or 3.

◆ eval()

double eval ( double  x) const

Evaluate the spline at x.

Parameters
[in]xPosition; must lie in the closed interval [first_knot, last_knot].
Returns
Interpolated value at x.
Exceptions
Exception::IllegalArgumentwhen x lies outside the knot range.

◆ init_()

void init_ ( const std::vector< double > &  x,
const std::vector< double > &  y 
)
private

Initialise the spline coefficients from the supplied knots.

Called from both public constructors after their argument validation; assumes the caller has already verified that the inputs are well-formed.

Parameters
[in]xKnot abscissae.
[in]yKnot ordinates.

Member Data Documentation

◆ a_

std::vector<double> a_
private

Per-segment constant coefficient (degree 0); same value as the y at the segment's left knot.

◆ b_

std::vector<double> b_
private

Per-segment linear coefficient (degree 1).

◆ c_

std::vector<double> c_
private

Per-segment quadratic coefficient (degree 2); contains one extra trailing zero from the natural boundary condition.

◆ d_

std::vector<double> d_
private

Per-segment cubic coefficient (degree 3).

◆ x_

std::vector<double> x_
private

Sorted knot abscissae (length = number of segments + 1).