poly_fit — MATH interp_poly op

資料種類:signal × signaltable

呼叫:import mathops; mathops.poly_fit(x, y, degree)(或 opsmath.get("poly_fit"))

用法

最小平方多項式擬合,並記錄其條件數

> 以下的詳細說明為原文 —— 摘要與標題已翻譯。

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).

該族通用的輸入契約(fail-closed)

mathops 的每個運算子都先檢驗輸入再計算(不讓任何東西無聲通過):

• **complex 輸入一律 ValueError** —— 強制轉成 float64 會無聲丟掉虛部(numpy 只發一個 ComplexWarning,然後回傳一個「看似合理卻是錯的」實數)。請明確寫出 .real/.imag/abs(),或改用支援複數的 complexops。

• **含被遮罩元素的 masked array 一律 ValueError** —— 拒絕「剝掉遮罩直接使用下面原值」的隱式轉換。請明確選擇填補還是丟棄。

• **所有輸入中的 NaN/Inf 一律 ValueError**(明確給出個數後拒絕 —— 它會汙染整個結果)。

形狀嚴格:不對 1-D 與 2-D 做隱式提升或廣播(向量槽位收到矩陣、矩陣槽位收到向量都是 ValueError;請明確 reshape)。

尺寸上限:接受矩陣的運算子與 stat_histogram 的 bins,超過 mathops.MAX_ELEMENTS(2^26 ≈ 6700 萬個元素)即 ValueError

詳細使用指南

math_metrology 族使用指南

參考(範例資料・文獻)

• 範例資料目錄(下載 URL / 授權) —— 2-D 用 skimage.data(BSD/公有領域)加合成圖,3-D 給出真實資料源(Stanford/PDS 等)的下載 URL。

• 運算子來歷與參考文獻 —— 該運算子族所依據的研究/方法出處。

• 演算法的正典(作者・年份)與用途見上面的族使用指南

可執行的範例(實際呼叫該運算子並已驗證的樣例)

math_metrologypy -3.11 examples/math_metrology.py

signal_filterpy -3.11 examples/signal_filter.py

型別可銜接的下一個運算子(可接受 table 作為輸入)

同類別(interp_poly)

interp_linear · interp_cubic · poly_eval · poly_roots


*Provenance: mathops.py — MATH 運算子登記表。本條目由 tools/opdocs.py md 自動產生(請勿手動編輯)。*

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