Package edu.cnu.mdi.splot.fit
Class CubicSpline
java.lang.Object
edu.cnu.mdi.splot.fit.CubicSpline
- All Implemented Interfaces:
Evaluator
Natural cubic spline interpolator over tabulated data
(x[i], y[i]).
This class precomputes the natural cubic spline second derivatives and can:
- Interpolate values
value(double). - Build an approximate derivative spline via numeric differentiation
derivative(). - Find roots in a range using bracket-and-refine utilities
findRoots(double, double). - Find maxima in a range by locating zeros of the first derivative and
testing the second derivative
findXValsOfMaxima(double, double).
Spline details
The spline is "natural": the second derivative is forced to be zero at the endpoints. Between consecutive knotsx[i] <= x <= x[i+1], the interpolant is a cubic polynomial chosen so
that y, y' and y'' are continuous across knots.
Requirements
x[]andy[]must be non-null, same length, and length>= 2.x[]must be strictly increasing (no duplicates).
Out-of-range evaluation
Ifx is outside
[x[0], x[n-1]], this implementation clamps to the nearest endpoint
(returns y[0] or y[n-1]).-
Constructor Summary
ConstructorsConstructorDescriptionCubicSpline(double[] x, double[] y) Create a natural cubic spline through the provided tabulated data. -
Method Summary
Modifier and TypeMethodDescriptionObtain a cubic spline that approximates the derivative of this spline by numeric differentiation at the knots, then spline-interpolating those derivative samples.double[]findRoots(double xmin, double xmax) Find roots of the spline in a given range.findXValsOfMaxima(double xmin, double xmax) Find the(x, y)values of the maxima of the interpolated function in a given range.booleanisValid()Whether the spline is properly initialized.intsize()Number of spline knots.doublevalue(double x) Evaluate the natural cubic spline interpolant.doublexmax()Maximum x value of the spline domain.doublexmin()Minimum x value of the spline domain.
-
Constructor Details
-
CubicSpline
public CubicSpline(double[] x, double[] y) Create a natural cubic spline through the provided tabulated data.- Parameters:
x- strictly increasing x-values (knots)y- y-values at the knots- Throws:
IllegalArgumentException- if inputs are invalid (null, length mismatch, length < 2, non-increasing x)
-
-
Method Details
-
size
public int size()Number of spline knots.- Returns:
- number of x points defining the spline
-
xmin
public double xmin()Minimum x value of the spline domain.- Returns:
- minimum x, or NaN if undefined
-
xmax
public double xmax()Maximum x value of the spline domain.- Returns:
- maximum x, or NaN if undefined
-
isValid
public boolean isValid()Whether the spline is properly initialized.- Returns:
- true if x/y arrays are non-null and have length ≥ 2
-
value
public double value(double x) Evaluate the natural cubic spline interpolant.The interval containing
xis found with a binary search, then the standard cubic-spline interpolation formula is used. -
derivative
Obtain a cubic spline that approximates the derivative of this spline by numeric differentiation at the knots, then spline-interpolating those derivative samples.The derivatives are estimated using:
- Centered differences for interior points
- One-sided differences for the endpoints
- Returns:
- a new
CubicSplineapproximatingdy/dx
-
findRoots
public double[] findRoots(double xmin, double xmax) Find roots of the spline in a given range.The method scans the range with a fixed number of samples to find sign changes, then applies Brent's method to refine each root. The results are deduplicated and returned sorted.
- Parameters:
xmin- minimum x value of the search rangexmax- maximum x value of the search range- Returns:
- array of roots in the range, or
nullif none found - Throws:
IllegalArgumentException- ifxmax <= xmin
-
findXValsOfMaxima
Find the(x, y)values of the maxima of the interpolated function in a given range.Implementation strategy:
- Compute a spline approximation to the first derivative.
- Find its roots (critical points) in
[xmin, xmax]. - Compute a spline approximation to the second derivative.
- Keep only critical points where the second derivative is negative (local maxima).
- Return the corresponding
(x, f(x))pairs, sorted by descendingyif there is more than one.
- Parameters:
xmin- the min value of the rangexmax- the max value of the range- Returns:
- an array of maxima points, or
nullif none are found
-