Source code for suboptimumg.loganalysis.fitted_curve

"""Polynomial curve fitting with Horner evaluation."""

from __future__ import annotations

import numpy as np


[docs] class FittedCurve: """Polynomial fit of tabulated [x, y] data with Horner evaluation. Parameters ---------- data : list of [x, y] pairs Raw tabulated input data. poly_order : int Polynomial degree for the least-squares fit. Attributes ---------- x_raw, y_raw : np.ndarray Original input arrays. poly_order : int coeffs : np.ndarray Polynomial coefficients in **descending** degree order ``[c_n, c_{n-1}, ..., c_1, c_0]``, matching ``np.polyfit`` output and the order Horner's method consumes directly. """ def __init__(self, data: list[list[float]], poly_order: int): print( """ DEPRECATION WARNING: FittedCurve from suboptimumg.loganalysis is deprecated and will be removed in a future release. Please use suboptimumg.vehicle.FittedCurve instead. """ ) self.x_raw = np.array([p[0] for p in data], dtype=np.float64) self.y_raw = np.array([p[1] for p in data], dtype=np.float64) self.poly_order = poly_order # np.polyfit returns highest-degree-first: [c_n, ..., c_1, c_0] # This is exactly the order Horner's method iterates over. self.coeffs = np.polyfit(self.x_raw, self.y_raw, poly_order) def __call__(self, x): return self.evaluate(x)
[docs] def evaluate(self, x): """Evaluate the fitted polynomial via Horner's method. Parameters ---------- x : float or array-like Input value(s) at which to evaluate. Returns ------- np.ndarray or np.floating Evaluated polynomial value(s). """ x = np.asarray(x, dtype=np.float64) result = np.full_like(x, self.coeffs[0]) for c in self.coeffs[1:]: result = result * x + c return result
def __repr__(self): return ( f"FittedCurve(order={self.poly_order}, " f"{len(self.x_raw)} points, " f"x=[{self.x_raw[0]:.4g}..{self.x_raw[-1]:.4g}])" )