mixle.engines.affine module

Affine arithmetic – tighter error tracing than intervals, and the precision-allocation dial.

An AffineForm represents a quantity as a center plus a linear combination of shared noise symbols x0 + sum_i x_i * eps_i with eps_i in [-1, 1]. Unlike interval arithmetic, correlated error cancels: (a + b) - a recovers b exactly because a’s symbols subtract out, where an interval would double the width. That tightness is what lets a precision-allocation pass avoid over-spending bits.

The dial: evaluating an operation at dtype d injects a fresh roundoff symbol of radius u(d) * |result| (u = unit roundoff). The affine radius at the root is the certified error bound; a subtraction at a cancellation point makes it blow up – the escalation signal, for free. Walk leaves->root choosing the cheapest dtype whose injected radius keeps the root bound under target.

This is the tighter estimate; the fully IEEE-sound enclosure is mixle.engines.error_tracing (interval, outward-rounded). The radius here is reported with one outward ULP of slop.

unit_roundoff(dtype)[source]

Unit roundoff for a dtype name / numpy dtype ('dd'/'qd' for extended precision).

Parameters:

dtype (Any)

Return type:

float

class AffineForm(center, terms=None)[source]

Bases: object

center + sum_i coeff_i * eps_i over shared noise symbols eps_i in [-1, 1].

Parameters:
  • center (Any)

  • terms (dict[int, np.ndarray] | None)

center
terms
classmethod constant(x)[source]
Parameters:

x (Any)

Return type:

AffineForm

classmethod uncertain(x, radius=0.0)[source]

An input known only to within +/- radius – one fresh noise symbol of that radius.

Parameters:
Return type:

AffineForm

radius()[source]

Half-width = sum_i |coeff_i| (one outward ULP of slop for the f64 summation).

Return type:

ndarray

max_radius()[source]
Return type:

float

to_interval()[source]
Return type:

Any

contains(value)[source]
Parameters:

value (Any)

Return type:

ndarray

inject_roundoff(dtype)[source]

Add the roundoff a dtype-dtype evaluation introduces: a fresh symbol of u*|center|.

Parameters:

dtype (Any)

Return type:

AffineForm

allocate_precision(center_magnitude, op_count, target_abs_error)[source]

Cheapest dtype whose accumulated roundoff over op_count ops keeps error under target.

Each op injects ~``u(d) * |magnitude|``; op_count of them accumulate to op_count*u*|mag|. Walk cheapest-first and return the first dtype that fits the budget – spend minimal precision.

Parameters:
  • center_magnitude (float)

  • op_count (int)

  • target_abs_error (float)

Return type:

str