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 grow – the escalation signal. Walk leaves->root
choosing the lowest-cost 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).
- class AffineForm(center, terms=None)[source]
Bases:
objectcenter + sum_i coeff_i * eps_iover shared noise symbolseps_i in [-1, 1].- classmethod constant(x)[source]
Create an affine form with no uncertainty terms.
- 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.
- radius()[source]
Half-width =
sum_i |coeff_i|(one outward ULP of slop for the f64 summation).- Return type:
- contains(value)[source]
Return a boolean mask for values contained in the affine enclosure.
- class PrecisionResult(dtype, estimated_abs_error, target_abs_error, status='heuristic_fit')[source]
Bases:
objectResult of
allocate_precision(): a dtype recommendation from an endpoint-magnitude heuristic. This is not an IEEE error certificate.statusis one of:"heuristic_fit": the endpoint estimate is no larger than the target."heuristic_insufficient": even the modeledqdestimate exceeds the target.
Neither status certifies intermediate magnitude, conditioning, reduction order, underflow, or overflow. Use an operation graph with propagated intervals for a sound certificate.
- dtype
the selected dtype name (best-effort, uncertified, when
status == "insufficient").- Type:
- estimated_abs_error
op_count * unit_roundoff(dtype) * center_magnitude– the estimated absolute roundoffdtypeaccumulates (an analytic estimate, not a measurement).- Type:
- target_abs_error
the caller’s requested target, echoed back for convenience.
- Type:
- status
see above; always explicitly heuristic.
- Type:
- property heuristic_met_target: bool
Whether the explicitly non-certifying endpoint estimate fits the requested target.
- property met_target: bool
this result does not carry enough evidence to certify the target.
- Type:
Always false
- allocate_precision(center_magnitude, op_count, target_abs_error)[source]
Suggest the lowest-cost dtype whose endpoint-magnitude error heuristic is under target.
The estimate
op_count*u*center_magnitudedoes not observe intermediate values or an operation graph and is therefore only a planning heuristic. It must never be used as an accuracy certificate.Raises
ValueErrorunlesscenter_magnitudeandop_countare both finite and nonnegative andtarget_abs_erroris finite and strictly positive. Before this validation existed, a negativeop_countflipped the estimated error negative, which spuriously satisfies every dtype’s budget comparison and misselectsfloat16– the least precise dtype – regardless of how large the magnitude or how tight the target; a negativetarget_abs_errormade every dtype’s comparison fail and fell through to silently returningqdeven though no dtype can certify a negative bound (impossible by definition); and a NaN/Inf value anywhere in the formula rode comparison-with-NaN-is-always-False (or Inf’s unpredictable propagation) into whatever the fallthrough dtype happened to be, not a principled decision.