Package gov.usgs.earthquake.nshmp.data
Class Interpolator
- java.lang.Object
-
- gov.usgs.earthquake.nshmp.data.Interpolator
-
public abstract class Interpolator extends Object
Utility class to perform linear and log interpolations. The methods of this class are designed to be fast and perform very little argument checking for monotonicity and the like. Results are undefined for x- and y-value arguments wheresize < 2
.Making some assumptions, interpolation is fairly straightforward. Most of the methods implemented here are designed to support interpolation (or derivation) of y-values keyed to monotonically increasing x-values. X-value interpolation requires knowing if y-values are increasing or decreasing. Assumptions and behaviors:
- No error checking for null, empty, single-valued arrays; or arrays of different lengths is performed. Buyer beware.
- X-value arrays are always assumed to be strictly monotonically ascending with no repeated values.
- Internally, binary search is used for y-value interpolation; linear search is used for x-value interpolation.
- Y-value interpolation will always extrapolate off the ends a sequence; this may change, or be configurable, in the future.
- X-value interpolation is predicated on y-values representing some form of cumulative distribution function, either increasing or decreasing (complementary), and must be specified as such. X-values are assumed to be increasing by default.
- X-value interpolation never extrapolates off the ends of a sequence and will always return 0 for out-of-range targets.
Presently, only single value interpolation of x-values is supported. The more common use case is to resample a sequence of y-values, which is supported.
Two static methods,
findX(double, double, double, double, double)
andfindY(double, double, double, double, double)
, are the basis for all interpolation operations in this class. These two methods are point-order agnostic.- Author:
- U.S. Geological Survey
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Interpolator.Builder
An interpolator builder.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static Interpolator.Builder
builder()
Create a new builder instance.abstract double
findX(double[] xs, double[] ys, double y)
Return an interpolated x-value corresponding to the supplied y-value in the supplied x- and y-value arrays.static double
findX(double x1, double y1, double x2, double y2, double y)
Return an interpolated or extrapolated x-value corresponding to the supplied y-value.abstract double
findX(XySequence xy, double y)
Return an interpolated x-value corresponding to the supplied y-value in the supplied xy-sequence.abstract double
findX(List<Double> xs, List<Double> ys, double y)
Return an interpolated x-value corresponding to the supplied y-value in the supplied x- and y-value arrays.abstract double
findY(double[] xs, double[] ys, double x)
Return an interpolated or extrapolated y-value corresponding to the supplied x-value in the supplied x- and y-value arrays.abstract double[]
findY(double[] xs, double[] ys, double[] x)
Return interpolated or extrapolated y-values using the supplied x- and y-value arrays.static double
findY(double x1, double y1, double x2, double y2, double x)
Return an interpolated or extrapolated y-value corresponding to the supplied x-value.abstract double
findY(XySequence xy, double x)
Return an interpolated or extrapolated y-value corresponding to the supplied x-value in the supplied xy-sequence.abstract double[]
findY(XySequence xy, double[] x)
Return interpolated or extrapolated y-values using the supplied x- and y-value arrays.abstract double
findY(List<Double> xs, List<Double> ys, double x)
Return an interpolated or extrapolated y-value corresponding to the supplied x-value in the supplied x- and y-value arrays.abstract double[]
findY(List<Double> xs, List<Double> ys, double[] x)
Return interpolated or extrapolated y-values using the supplied x- and y-value arrays.
-
-
-
Method Detail
-
findX
public static double findX(double x1, double y1, double x2, double y2, double y)
Return an interpolated or extrapolated x-value corresponding to the supplied y-value. If any supplied value isNaN
, returned value will also beNaN
. Method does not perform any input validation such that if the supplied points are coincident or define a horizontal line, the method may returnInfinity
,-Infinity
, orNaN
.- Parameters:
x1
- x-value of first pointy1
- y-value of first pointx2
- x-value of second pointy2
- y-value of second pointy
- value at which to find x- Returns:
- the interpolated x-value
-
findX
public abstract double findX(double[] xs, double[] ys, double y)
Return an interpolated x-value corresponding to the supplied y-value in the supplied x- and y-value arrays.- Parameters:
xs
- x-values of a sequenceys
- y-values of a sequencey
- value at which to find x- Returns:
- an interpolated x-value
-
findX
public abstract double findX(List<Double> xs, List<Double> ys, double y)
Return an interpolated x-value corresponding to the supplied y-value in the supplied x- and y-value arrays.- Parameters:
xs
- x-values of a sequenceys
- y-values of a sequencey
- value at which to find x- Returns:
- an interpolated x-value
-
findX
public abstract double findX(XySequence xy, double y)
Return an interpolated x-value corresponding to the supplied y-value in the supplied xy-sequence.- Parameters:
xy
- an xy-sequencey
- value at which to find x- Returns:
- an interpolated x-value
-
findY
public static double findY(double x1, double y1, double x2, double y2, double x)
Return an interpolated or extrapolated y-value corresponding to the supplied x-value. If any supplied value isNaN
, returned value will also beNaN
. Method does not perform any input validation such that if the supplied points are coincident or define a vertical line, the method may returnInfinity
,-Infinity
, orNaN
.- Parameters:
x1
- x-value of first pointy1
- y-value of first pointx2
- x-value of second pointy2
- y-value of second pointx
- value at which to find y- Returns:
- an interpolated y-value
-
findY
public abstract double findY(double[] xs, double[] ys, double x)
Return an interpolated or extrapolated y-value corresponding to the supplied x-value in the supplied x- and y-value arrays.- Parameters:
xs
- x-values of a sequenceys
- y-values of a sequencex
- value at which to find y- Returns:
- an interpolated y-value
-
findY
public abstract double findY(List<Double> xs, List<Double> ys, double x)
Return an interpolated or extrapolated y-value corresponding to the supplied x-value in the supplied x- and y-value arrays.- Parameters:
xs
- x-values of a sequenceys
- y-values of a sequencex
- value at which to find y- Returns:
- an interpolated y-value
-
findY
public abstract double findY(XySequence xy, double x)
Return an interpolated or extrapolated y-value corresponding to the supplied x-value in the supplied xy-sequence.- Parameters:
xy
- an xy-sequencex
- value at which to find y- Returns:
- an interpolated y-value
-
findY
public abstract double[] findY(double[] xs, double[] ys, double[] x)
Return interpolated or extrapolated y-values using the supplied x- and y-value arrays.- Parameters:
xs
- x-values of a sequenceys
- y-values of a sequencex
- values at which to find y-values- Returns:
- interpolated y-values
-
findY
public abstract double[] findY(List<Double> xs, List<Double> ys, double[] x)
Return interpolated or extrapolated y-values using the supplied x- and y-value arrays.- Parameters:
xs
- x-values of a sequenceys
- y-values of a sequencex
- values at which to find y-values- Returns:
- interpolated y-values
-
findY
public abstract double[] findY(XySequence xy, double[] x)
Return interpolated or extrapolated y-values using the supplied x- and y-value arrays.- Parameters:
xy
- an xy-sequencex
- values at which to find y-values- Returns:
- interpolated y-values
-
builder
public static Interpolator.Builder builder()
Create a new builder instance.
-
-