Package io.github.andreipunko.math.space
Class Interval
java.lang.Object
io.github.andreipunko.math.space.Interval
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
ConstructorsConstructorDescriptionInterval()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 subdivisionsn. -
Method Summary
Modifier and TypeMethodDescriptiondoubleh()Returns the step size between points in the interval.inti(double x) Returns the index of the point closest to the specified coordinate.doubleleft()Returns the left boundary of the interval.intn()Returns the number of subdivisions along the interval: valid indices are0 … n(n + 1grid nodes).voidreborn(double h) Recreates the interval with a new step size while maintaining the same boundaries.voidreborn(int n) Recreates the interval with a new subdivision count while maintaining the same boundaries.doubleright()Returns the right boundary of the interval.doublex(int i) Returns the coordinate value at the specified index.
-
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 countnis(int) ((right - left) / h); the endpointrightmay lie beyond the last grid node when the ratio is not integral (see class documentation).- Parameters:
left- left boundary of the intervalright- right boundary of the intervalh- 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 subdivisionsn. The step size is(right - left) / n, so the last grid node equalsright(x(n) == right), unlike construction by a fixed steph.- Parameters:
left- left boundary of the intervalright- right boundary of the intervaln- number of subdivisions (grid indices0 … n, i.e.n + 1nodes)- 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 are0 … n(n + 1grid nodes). Either supplied toInterval(double, double, int)or derived from stephas 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.nis recomputed as(int) ((right - left) / h)(same semantics asInterval(double, double, double); the last node may lie strictly insiderightif the length is not a multiple ofh).- 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 asInterval(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
-