Class GridAxis

  • All Implemented Interfaces:
    Serializable

    public class GridAxis
    extends Object
    implements Serializable
    Helper for finding interpolation nodes along one axis of grid data.

    This class is intended to be used for interpolating inside grids. It works on any sorted data without duplication and size at least n where n is the number of points required for interpolation (i.e. 2 for linear interpolation, 3 for quadratic...)

    The method uses linear interpolation to select the nodes indices. It should be O(1) for sufficiently regular data, therefore much faster than bisection. It also features caching, which improves speed when interpolating several points in raw in the close locations, i.e. when successive calls have a high probability to return the same interpolation nodes. This occurs for example when scanning with small steps a loose grid. The method also works on non-regular grids, but may be slower in this case.

    This class is thread-safe.

    Since:
    1.4
    See Also:
    Serialized Form
    • Constructor Detail

      • GridAxis

        public GridAxis​(double[] grid,
                        int n)
                 throws MathIllegalArgumentException
        Simple constructor.
        Parameters:
        grid - coordinates of the interpolation points, sorted in increasing order
        n - number of points required for interpolation, i.e. 2 for linear, 3 for quadratic...
        Throws:
        MathIllegalArgumentException - if grid size is smaller than n or if the grid is not sorted in strict increasing order
    • Method Detail

      • size

        public int size()
        Get the number of points of the grid.
        Returns:
        number of points of the grid
      • getN

        public int getN()
        Get the number of points required for interpolation.
        Returns:
        number of points required for interpolation
      • node

        public double node​(int index)
        Get the interpolation node at specified index.
        Parameters:
        index - node index
        Returns:
        coordinate of the node at specified index
      • interpolationIndex

        public int interpolationIndex​(double t)
        Get the index of the first interpolation node for some coordinate along the grid.

        The index return is the one for the lowest interpolation node suitable for t. This means that if i is returned the nodes to use for interpolation at coordinate t are at indices i, i+1, ..., i+n-1, where n is the number of points required for interpolation passed at construction.

        The index is selected in order to have the subset of nodes from i to i+n-1 as balanced as possible around t:

        • if t is inside the grid and sufficiently far from the endpoints
          • if n is even, the returned nodes will be perfectly balanced: there will be n/2 nodes smaller than t and n/2 nodes larger than t
          • if n is odd, the returned nodes will be slightly unbalanced by one point: there will be (n+1)/2 nodes smaller than t and (n-1)/2 nodes larger than t
        • if t is inside the grid and close to endpoints, the returned nodes will be unbalanced: there will be less nodes on the endpoints side and more nodes on the interior side
        • if t is outside of the grid, the returned nodes will completely off balance: all nodes will be on the same side with respect to t

        It is not an error to call this method with t outside of the grid, it simply implies that the interpolation will become an extrapolation and accuracy will decrease as t goes farther from the grid points. This is intended so interpolation does not fail near the end of the grid.

        Parameters:
        t - coordinate of the point to interpolate
        Returns:
        index i such node(i), node(i+1), ... node(i+n-1) can be used for interpolating a value at coordinate t
        Since:
        1.4