Skip to content

About

Dependency-free Python interval arithmetic with outward-rounded binary64 bounds, polynomial evaluation, recursive subdivision, exact-reference checks, and SVG visualizations.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Interval arithmetic bounds

A small, dependency-free Python experiment in numerical enclosures. It shows how outward rounding, repeated variables, algebraic rearrangement, and recursive subdivision affect the bounds of a mathematical expression.

Run

Python 3.11 or newer is required. From the repository root, no install is needed:

python -m interval_bounds demo --output artifacts
python -m interval_bounds compare --example parabola --depths 0 1 2 4 8
python -m unittest discover -s tests -v

The demo writes dependency.svg, parabola.svg, and cancellation.svg, plus a JSON report for each. Open an SVG in a browser. Generated artifacts/ files are ignored by Git. A checked-in example is available below.

Parabola enclosure comparison

Optional package installation: python -m pip install .; this installs the interval-bounds command. The build uses setuptools; runtime and tests use only the Python standard library. No GitHub Actions or workflows are included.

What the examples demonstrate

Example Domain Direct expression Rearranged expression True range
dependency [1, 2] x - x 0 [0, 0]
parabola [0, 1] x(1 - x) 1/4 - (x - 1/2)(x - 1/2) [0, 1/4]
cancellation [1, 2] xx - 2x + 1 (x - 1)(x - 1) [0, 1]

The true ranges follow respectively from cancellation, the vertex and endpoints of the parabola, and monotonicity of (x-1)^2 on [1,2]. They are analytic reference ranges, not estimates obtained by sampling. All constants in these examples are exactly representable as binary64.

Rounded display values from the default run:

Example Direct width, 1 leaf Direct width, 256 leaves Rearranged width, 1 leaf
dependency 2 0.0078125 0
parabola 1 0.251953125 0.5
cancellation 5 1.015625 1

Actual bounds include small additional outward-rounding expansion. JSON retains the floating-point endpoints; the console rounds numbers for readability.

Each occurrence of a variable uses the full input interval independently. For example, [1,2] - [1,2] encloses [-1,1] even though the real function x-x is zero. Rearrangement can reduce that dependency. Subdivision reduces the input uncertainty in each evaluation. Neither a particular expression form nor a fixed subdivision budget guarantees a requested tightness for arbitrary expressions.

The cancellation example also compares Horner and expanded polynomial evaluation. Coefficients are ordered from constant to highest degree. Neither evaluation form is claimed to be universally tighter.

Arithmetic contract

from interval_bounds import Interval
from interval_bounds.polynomial import horner
from interval_bounds.subdivision import subdivide

x = Interval(0, 1)
print(x * (1 - x))
print(horner((0, 1, -1), x))
print(subdivide(lambda t: t * (1 - t), x, max_depth=8).enclosure)

Interval(lo, hi) is immutable. Endpoints must be finite floats or exactly representable integers, with lo <= hi. A float denotes its exact stored binary value: Interval.point(0.1) does not enclose the exact decimal rational 1/10. Large integers that would change during float conversion are rejected. Decimal strings and implicit decimal enclosures are outside the MVP.

Addition uses [a+c, b+d]; subtraction uses [a-d, b-c]. Multiplication and division take the minimum and maximum of the four endpoint combinations. Division rejects any divisor interval containing zero, including an endpoint equal to zero. Scalar operands are promoted to point intervals. Squares use multiplication; there is no dependency-aware square or symbolic simplification.

Each scalar endpoint result is expanded with math.nextafter toward negative or positive infinity. Under correctly rounded binary64 arithmetic, this encloses the exact scalar operation, including subnormal results. Exact operations are also expanded, so these are conservative bounds, not the tightest directed-rounding results. For example, even [0,0] + [0,0] expands by the smallest subnormal. See the Python nextafter documentation.

The implementation assumes IEEE-754 binary64, round-to-nearest, and gradual underflow. It checks the float format at import; it does not control or fully detect external changes to the hardware rounding mode or flush-to-zero behavior.

NaN and infinite input endpoints are rejected. Any nonfinite arithmetic result or outward step to infinity raises OverflowError, even if a tighter specialized implementation could represent that exact result. No unbounded or empty intervals, transcendental functions, or arbitrary expression parser are implemented.

width is an ordinary floating-point diagnostic, not a certified bound on the width; it can overflow for extremely wide finite intervals. Midpoint construction avoids overflow, and subdivision stops if no strictly interior float exists.

Subdivision and metrics

The recursive algorithm evaluates each visited node, bisects at its floating-point midpoint, and takes the hull of the leaf enclosures. Children share the midpoint, so the input range has no gaps. The leaf budget is divided deterministically between siblings; unused budget is not reallocated. Parent evaluations count as work, but only leaves contribute to the final hull.

python -m interval_bounds compare --example cancellation --depths 0 2 6 10 --max-leaves 1000 --min-width 0.001 --samples 2001 --output artifacts/cancellation

Limits: depth 0 through 64, positive leaf budget (default 4096), nonnegative finite minimum input width (default 0), and at least two samples. Reported leaf stopping reasons are max_depth, min_width, no_representable_midpoint, or leaf_budget, in that precedence order. These limits bound work; they are not output-error tolerances. Very large user-supplied budgets can still require substantial memory.

The console prints widths, inflation, leaves, interval operation counts, and exact sampled violations. Each JSON report includes the full metrics and every leaf domain/enclosure:

Metric Meaning
width Floating-point upper minus lower endpoint
inflation Enclosure width divided by known true-range width
sample_relative_inflation Width divided by sampled floating-point range width
excess_width Enclosure width minus known true-range width
splits, leaf_count, evaluations Bisections, leaves, and all visited nodes
interval_operations Interval arithmetic operator calls across all nodes
endpoint_operations Scalar endpoint arithmetic: 2 for +/- and 4 for multiply/divide
arithmetic_seconds One wall-clock measurement of subdivision/evaluation, including counting
float_containment_violations Floating-point grid results outside the global enclosure
exact_containment_violations Exact rational polynomial grid results outside the enclosure

Inflation is null when its denominator is zero. Construction, comparisons, midpoint calculation, hulls, and nextafter calls are not counted as endpoint arithmetic operations. Timings exclude sampling, JSON, and SVG work; they are single-run observations, not statistically controlled benchmarks.

SVGs contain whole-domain bounds, the direct expression's leaf rectangles at the largest requested depth, and a width-versus-leaf-count comparison of all variants. The curve is sampled for display. The JSON report is the source for full precision.

Verification

The standard-library unittest suite checks arithmetic against independent exact Fraction corner results, using deterministic random ranges and boundary cases. It checks polynomial evaluation against an exact power sum, leaf coverage and exact sampled containment, stopping limits, metrics, CLI error handling, JSON output, and SVG XML structure.

For the default demo, 40 variant/depth combinations each check 1001 points with both ordinary floating-point evaluation and exact rational evaluation. Sampling is only a sanity check: it does not prove containment between points. The enclosure argument comes from outward-rounded elementary operations composed over intervals, subject to the stated runtime assumptions. The tests are evidence of implementation correctness, not a machine-checked proof.

CLI exit codes: 0 for successful reports with no exact sampled violations, 1 when exact sampled violations are found, and 2 for invalid arguments or unsupported numeric evaluation. Floating-point sampled violations remain diagnostic metrics.

Layout

  • interval_bounds/interval.py: finite binary64 arithmetic and operation counters.
  • polynomial.py, examples.py, validation.py: evaluators and independent references.
  • subdivision.py, metrics.py: recursive partitioning and measurements.
  • svg.py, __main__.py: SVG/JSON reports and CLI.
  • tests/: deterministic checks; docs/: checked-in sample SVG reports.

The package lives directly in the repository root so python -m interval_bounds works without installation or a PYTHONPATH setting.

About

Dependency-free Python interval arithmetic with outward-rounded binary64 bounds, polynomial evaluation, recursive subdivision, exact-reference checks, and SVG visualizations.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages