![]() |
OpenMS
|
Natural cubic-spline interpolation of a 2D data set. More...
#include <OpenMS/MATH/MISC/CubicSpline2d.h>
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). | |
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.
| CubicSpline2d | ( | const std::vector< double > & | x, |
| const std::vector< double > & | y | ||
| ) |
Build the spline from parallel x / y vectors.
| [in] | x | Knot abscissae; must contain at least two entries and be sorted in non-decreasing order. |
| [in] | y | Knot ordinates; must have the same length as x. |
| Exception::IllegalArgument | when x and y differ in length, when fewer than two knots are supplied, or when x is not non-decreasing. |
| 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.
| [in] | m | Knots as std::map<x, y>. Must contain at least two entries. |
| Exception::IllegalArgument | when m contains fewer than two entries. |
| double derivative | ( | double | x | ) | const |
First derivative of the spline at x.
Equivalent to derivatives(x, 1).
| [in] | x | Position; must lie in the closed interval [first_knot, last_knot]. |
x.| Exception::IllegalArgument | when x lies outside the knot range. |
| double derivatives | ( | double | x, |
| unsigned | order | ||
| ) | const |
Derivative of the spline at x.
| [in] | x | Position; must lie in the closed interval [first_knot, last_knot]. |
| [in] | order | Derivative 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. |
order -th derivative of the spline at x.| Exception::IllegalArgument | when x lies outside the knot range, or when order is not 1, 2, or 3. |
| double eval | ( | double | x | ) | const |
Evaluate the spline at x.
| [in] | x | Position; must lie in the closed interval [first_knot, last_knot]. |
x.| Exception::IllegalArgument | when x lies outside the knot range. |
|
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.
| [in] | x | Knot abscissae. |
| [in] | y | Knot ordinates. |
|
private |
Per-segment constant coefficient (degree 0); same value as the y at the segment's left knot.
|
private |
Per-segment linear coefficient (degree 1).
|
private |
Per-segment quadratic coefficient (degree 2); contains one extra trailing zero from the natural boundary condition.
|
private |
Per-segment cubic coefficient (degree 3).
|
private |
Sorted knot abscissae (length = number of segments + 1).