poly_fit — MATH interp_poly op

Data kinds: signal × signaltable

Call: import mathops; mathops.poly_fit(x, y, degree) (or opsmath.get("poly_fit"))

Usage

Least-squares polynomial fit with its conditioning on the record.

Fits `y ≈ c[0] x^d + ... + c[d]` (coefficients highest-power-first, the

:func:poly_eval / `np.polyval` convention) by SVD least squares on the

Vandermonde matrix. *degree* must be an integer `>= 0` with at least

`degree + 1` samples (fail-closed: an exactly-determined fit is allowed,

an under-determined one is not).

Returns a dict — the fit and its health, inseparable:

`coeffs (degree + 1,) float64 · degree · cond` the Vandermonde

condition number (:func:mat_cond of the design matrix) · `rms_residual`

root-mean-square of `y - p(x)`.

The conditioning mechanism: when `cond > POLY_COND_WARN` (1e10) a

`RuntimeWarning` is emitted *and* the number is in the result — an

equispaced degree-10 fit on raw pixel coordinates is already past it. High

degree on a raw coordinate range is the classic double trap: the

Vandermonde columns become near-collinear (digits lost, coefficients

unstable) and the fit oscillates between nodes (Runge phenomenon, Runge

1901). Centre and scale x to `[-1, 1]` first, or keep degree ≤ ~6.

HALCON: no public polynomial-fitting tuple operator (fitting of this kind

lives inside HALCON's calibration internals).

Family-wide input contract (fail-closed)

Every mathops op validates its input before computing (nothing slips through silently):

• **complex input raises ValueError** — coercing to float64 silently discards the imaginary part (numpy only emits a ComplexWarning and returns a plausible-looking wrong real number). State .real/.imag/abs() explicitly, or use complexops, which handles complex data.

• **masked arrays with masked elements raise ValueError** — the implicit conversion that peels off the mask and uses the raw values underneath is refused. Say explicitly whether to fill or to drop.

• **NaN/Inf raises ValueError on every input** (refused with the count stated — it propagates through the whole result).

Shapes are strict: 1-D and 2-D are never implicitly promoted or broadcast (a matrix in a vector slot, or a vector in a matrix slot, raises ValueError; reshape explicitly).

Size cap: ops that take a matrix, and the stat_histogram bins, raise ValueError beyond mathops.MAX_ELEMENTS (2^26 ≈ 67 million elements).

Detailed usage guide

math_metrology family guide

References (sample data, literature)

• Sample-data catalog (download URLs / licences) — 2-D uses skimage.data (BSD/public domain) plus synthetic images; 3-D lists download URLs for real data sources (Stanford, PDS, …).

• Operator provenance and references — the sources of the research/methods this op family came from.

• The canonical algorithm (author, year) and its uses are named in the family usage guide above.

Runnable examples (verified samples that actually call this op)

math_metrologypy -3.11 examples/math_metrology.py

poc_strain_historypy -3.11 examples/poc_strain_history.py

signal_filterpy -3.11 examples/signal_filter.py

Ops the type connects to (they accept table as input)

Same category (interp_poly)

interp_linear · interp_cubic · poly_eval · poly_roots


*Provenance: mathops.py — MATH operator registry. This per-op note is generated by tools/opdocs.py md (do not hand-edit).*

© 2026 Kazufumi Furuse — Fullseye operator documentation. Licensed under Apache-2.0.