polyfit method
Fits polynomial coefficients to x and y, stores them in this instance,
and returns the fitted coefficient vector.
Implementation
Float64List polyfit(
Float64List x,
Float64List y, {
CoeffMode mode = CoeffMode.scaled,
}) {
if (x.length != y.length) {
throw ArgumentError('x and y must have the same length');
}
final n = x.length;
_ensureCapacity(n);
ffi.Pointer<ffi.Double> xPtr = ffi.nullptr;
ffi.Pointer<ffi.Double> yPtr = ffi.nullptr;
ffi.Pointer<ffi.Double> coeffPtr = ffi.nullptr;
try {
xPtr = calloc<ffi.Double>(n);
yPtr = calloc<ffi.Double>(n);
coeffPtr = calloc<ffi.Double>(deg + 1);
xPtr.asTypedList(n).setAll(0, x);
yPtr.asTypedList(n).setAll(0, y);
final rc = yl_polyfit_ctx_fit_f64(
res.ctx,
xPtr,
yPtr,
n,
mode.value,
coeffPtr,
ffi.nullptr,
);
if (rc != 0) {
throw StateError('yl_polyfit_ctx_fit_f64 failed with code $rc');
}
final out = Float64List.fromList(coeffPtr.asTypedList(deg + 1));
_coeffs = out;
_coeffMode = mode;
return Float64List.fromList(out);
} finally {
if (xPtr != ffi.nullptr) calloc.free(xPtr);
if (yPtr != ffi.nullptr) calloc.free(yPtr);
if (coeffPtr != ffi.nullptr) calloc.free(coeffPtr);
}
}