Class Interpolation

java.lang.Object
cloud.opencode.base.math.interpolation.Interpolation

public final class Interpolation extends Object
Interpolation utility class providing common interpolation algorithms. 插值工具类,提供常见插值算法。

All methods are stateless and thread-safe. Supported algorithms:

所有方法无状态且线程安全。支持的算法:

  • Piecewise linear interpolation — 分段线性插值
  • Lagrange polynomial interpolation — 拉格朗日多项式插值
  • Newton's divided difference interpolation — 牛顿差商插值
  • Natural cubic spline interpolation — 自然三次样条插值
Since:
JDK 25, opencode-base-math V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Precomputed cubic spline coefficients for efficient multi-point evaluation.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    cubicSpline(double[] x, double[] y, double xi)
    Natural cubic spline interpolation (second derivative = 0 at endpoints).
    static double
    lagrange(double[] x, double[] y, double xi)
    Lagrange polynomial interpolation.
    static double
    linear(double[] x, double[] y, double xi)
    Piecewise linear interpolation using binary search.
    static double
    newtonDividedDifference(double[] x, double[] y, double xi)
    Newton's divided difference interpolation.
    precomputeSpline(double[] x, double[] y)
    Precomputes cubic spline coefficients for efficient multi-point evaluation.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • linear

      public static double linear(double[] x, double[] y, double xi)
      Piecewise linear interpolation using binary search. 使用二分查找的分段线性插值。

      The x array must be sorted in strictly ascending order and xi must lie within [x[0], x[x.length - 1]].

      x 数组必须严格升序排列,xi 必须在 [x[0], x[x.length - 1]] 范围内。

      Parameters:
      x - the x-coordinates of the data points (sorted ascending) — 数据点的 x 坐标(升序)
      y - the y-coordinates of the data points — 数据点的 y 坐标
      xi - the x value at which to interpolate — 待插值的 x 值
      Returns:
      the interpolated y value — 插值后的 y 值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出
    • lagrange

      public static double lagrange(double[] x, double[] y, double xi)
      Lagrange polynomial interpolation. 拉格朗日多项式插值。

      The x values must all be distinct. The interpolating polynomial passes through all data points and is exact for polynomials of degree ≤ n-1.

      x 值必须互不相同。插值多项式通过所有数据点,对 ≤ n-1 阶多项式精确。

      Parameters:
      x - the x-coordinates of the data points (must be distinct) — 数据点的 x 坐标(必须互不相同)
      y - the y-coordinates of the data points — 数据点的 y 坐标
      xi - the x value at which to interpolate — 待插值的 x 值
      Returns:
      the interpolated y value — 插值后的 y 值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出
    • newtonDividedDifference

      public static double newtonDividedDifference(double[] x, double[] y, double xi)
      Newton's divided difference interpolation. 牛顿差商插值。

      Constructs the divided difference table and evaluates using the Newton form. The x values must all be distinct.

      构建差商表并使用牛顿形式求值。x 值必须互不相同。

      Parameters:
      x - the x-coordinates of the data points (must be distinct) — 数据点的 x 坐标(必须互不相同)
      y - the y-coordinates of the data points — 数据点的 y 坐标
      xi - the x value at which to interpolate — 待插值的 x 值
      Returns:
      the interpolated y value — 插值后的 y 值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出
    • precomputeSpline

      public static Interpolation.SplineCoefficients precomputeSpline(double[] x, double[] y)
      Precomputes cubic spline coefficients for efficient multi-point evaluation. 预计算三次样条系数,用于高效多点求值。

      Call this once per dataset, then use Interpolation.SplineCoefficients.evaluate(double) for each query point. This avoids redundant O(n) precomputation per query.

      对每个数据集调用一次,然后对每个查询点使用 Interpolation.SplineCoefficients.evaluate(double)。 这避免了每次查询的冗余 O(n) 预计算。

      Parameters:
      x - the x-coordinates of the data points (sorted ascending) — 数据点的 x 坐标(升序)
      y - the y-coordinates of the data points — 数据点的 y 坐标
      Returns:
      precomputed spline coefficients — 预计算的样条系数
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出
    • cubicSpline

      public static double cubicSpline(double[] x, double[] y, double xi)
      Natural cubic spline interpolation (second derivative = 0 at endpoints). 自然三次样条插值(端点处二阶导数为 0)。

      Constructs a natural cubic spline that passes through all data points with continuous first and second derivatives. The x array must be sorted in strictly ascending order and xi must lie within [x[0], x[n-1]].

      构建通过所有数据点的自然三次样条,保证一阶和二阶导数连续。 x 数组必须严格升序排列,xi 必须在 [x[0], x[n-1]] 范围内。

      For multiple evaluations on the same dataset, prefer precomputeSpline(double[], double[]) followed by Interpolation.SplineCoefficients.evaluate(double) for better performance.

      对同一数据集进行多次求值时,建议使用 precomputeSpline(double[], double[])Interpolation.SplineCoefficients.evaluate(double) 以获得更好性能。

      Parameters:
      x - the x-coordinates of the data points (sorted ascending) — 数据点的 x 坐标(升序)
      y - the y-coordinates of the data points — 数据点的 y 坐标
      xi - the x value at which to interpolate — 待插值的 x 值
      Returns:
      the interpolated y value — 插值后的 y 值
      Throws:
      MathException - if inputs are invalid — 输入无效时抛出