Class Interval

java.lang.Object
io.github.andreipunko.math.space.Interval

public class Interval extends Object
Represents a one-dimensional interval with uniform discretization. This class is used to define spatial or temporal domains with a fixed step size or a specified number of subdivisions. It provides methods for coordinate-to-index and index-to-coordinate conversions.

Step size h vs. subdivision count n: If the interval is built with a step h (constructor Interval(double, double, double) or reborn(double)), then n is computed as (int) ((right - left) / h) — the integer part of the length-to-step ratio. Grid nodes are x(i) = left + i * h for i = 0, 1, …, n. The last node x(n) satisfies x(n) ≤ right, but if (right - left) / h is not an integer, then x(n) < right: the declared right boundary is not necessarily a grid point. To obtain a uniform mesh whose last node coincides exactly with right, use Interval(double, double, int) instead (there h = (right - left) / n).

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a default interval [0,1] with a single step.
    Interval(double left, double right, double h)
    Creates an interval [left,right] with a specified step size.
    Interval(double left, double right, int n)
    Creates an interval [left,right] with a specified number of subdivisions n.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    h()
    Returns the step size between points in the interval.
    int
    i(double x)
    Returns the index of the point closest to the specified coordinate.
    double
    Returns the left boundary of the interval.
    int
    n()
    Returns the number of subdivisions along the interval: valid indices are 0 … n (n + 1 grid nodes).
    void
    reborn(double h)
    Recreates the interval with a new step size while maintaining the same boundaries.
    void
    reborn(int n)
    Recreates the interval with a new subdivision count while maintaining the same boundaries.
    double
    Returns the right boundary of the interval.
    double
    x(int i)
    Returns the coordinate value at the specified index.

    Methods inherited from class java.lang.Object

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

    • Interval

      public Interval()
      Creates a default interval [0,1] with a single step. This constructor is provided for convenience and creates the simplest possible interval.
    • Interval

      public Interval(double left, double right, double h)
      Creates an interval [left,right] with a specified step size. The subdivision count n is (int) ((right - left) / h); the endpoint right may lie beyond the last grid node when the ratio is not integral (see class documentation).
      Parameters:
      left - left boundary of the interval
      right - right boundary of the interval
      h - step size between points
      Throws:
      IllegalArgumentException - if h <= 0, if (right - left) < h, or if left >= right
    • Interval

      public Interval(double left, double right, int n)
      Creates an interval [left,right] with a specified number of subdivisions n. The step size is (right - left) / n, so the last grid node equals right (x(n) == right), unlike construction by a fixed step h.
      Parameters:
      left - left boundary of the interval
      right - right boundary of the interval
      n - number of subdivisions (grid indices 0 … n, i.e. n + 1 nodes)
      Throws:
      IllegalArgumentException - if left >= right or n <= 0
  • Method Details

    • left

      public double left()
      Returns the left boundary of the interval.
      Returns:
      left boundary value
    • right

      public double right()
      Returns the right boundary of the interval.
      Returns:
      right boundary value
    • h

      public double h()
      Returns the step size between points in the interval.
      Returns:
      step size h
    • n

      public int n()
      Returns the number of subdivisions along the interval: valid indices are 0 … n (n + 1 grid nodes). Either supplied to Interval(double, double, int) or derived from step h as described in the class documentation.
      Returns:
      subdivision count n
    • reborn

      public void reborn(double h)
      Recreates the interval with a new step size while maintaining the same boundaries. n is recomputed as (int) ((right - left) / h) (same semantics as Interval(double, double, double); the last node may lie strictly inside right if the length is not a multiple of h).
      Parameters:
      h - new step size
      Throws:
      IllegalArgumentException - if h <= 0 or if (right - left) < h
    • reborn

      public void reborn(int n)
      Recreates the interval with a new subdivision count while maintaining the same boundaries. Same semantics as Interval(double, double, int).
      Parameters:
      n - new subdivision count (must be positive)
      Throws:
      IllegalArgumentException - if n <= 0
    • x

      public double x(int i)
      Returns the coordinate value at the specified index. The coordinate is calculated as left + i * h.
      Parameters:
      i - index of the point (0 <= i <= n)
      Returns:
      coordinate value at index i
      Throws:
      IllegalArgumentException - if i < 0 or i > n
    • i

      public int i(double x)
      Returns the index of the point closest to the specified coordinate. If the coordinate is exactly at a grid point, returns its index. If the coordinate is between grid points, returns the index of the leftmost grid point.
      Parameters:
      x - coordinate value (left <= x <= right)
      Returns:
      index of the closest grid point
      Throws:
      IllegalArgumentException - if x < left or x > right