Polynomial(3pm)
π Polynomial(3pm) β PDL::Fit::Polynomial π
okay bestie, so this is the Polynomial(3pm) page from the Perl docs, and it's literally the PDL polynomial fitting module. it's giving math girlie energy and honestly? kinda iconic.
β¨ NAME β¨
PDL::Fit::Polynomial β routines for fitting with polynomials π
πΈ DESCRIPTION πΈ
bestie. this module is just a lil collection of routines that do simple polynomial fits to your data. that's the whole vibe. no cap. π¦
π SYNOPSIS π
here's how you actually use it, copied exactly so you don't get it twisted:
$x = sequence(20)-10;
$coeff_orig = cdouble(30,-2,3,-2); # order used in this module
$y = 30-2*$x+3*$x**2-2*$x**3; # or: $y = polyval($coeff_orig->slice("-1:0"), $x->r2C);
$y += ($x->grandom - 0.5) * 100;
($yfit, $coeff) = fitpoly1d($x,$y,4);
use PDL::Graphics::Simple;
$w = pgswin();
$xi = zeroes(100)->xlinvals(-10,9);
$yi = polyval($coeff->r2C->slice("-1:0"), $xi->r2C);
$w->plot(with=>'points',$x,$y,
with=>'points',$x,$yfit, with=>'line',$xi,$yi);
$yfit = fitpoly1d $data,2; # Least-squares line fit
($yfit, $coeffs) = fitpoly1d $x, $y, 4; # Fit a cubic
π₯ FUNCTIONS π₯
π fitpoly1d π
okay so this one Fits 1D polynomials to data using min chi^2 (least squares). slay. β¨
Usage: ($yfit, [$coeffs]) = fitpoly1d [$xdata], $data, $order, [Options...]
Signature: (x(n); y(n); [o]yfit(n); [o]coeffs(order))
so the way it works: it uses a standard matrix inversion method to do a least squares / min chi^2 polynomial fit to your data. and heads up β Order=2 is a linear fit (two parameters). so like, order 2 β quadratic here, iykyk. it's lowkey a trap if you don't read. π
it returns the fitted data, and optionally the coefficients β and those coeffs come in ascending order of degree, which is the opposite of "polyval" in PDL::Math. so highkey don't mix them up bestie. π
you can also broadcast over extra dimensions to do multiple fits at once β with ONE caveat: the order can not be broadcasted over. it has to be one fixed scalar number, like "4". one order, always. periodt. π
there's some internal magic where the data gets normalised internally to avoid overflows (it uses the mean of the abs value) β and those overflows are super common in large polynomial series, so this is doing you a solid fr fr. BUT the returned fit and coeffs are in unnormalised units. so don't come for me when the numbers look different than you expected. π
here are the examples, verbatim:
$yfit = fitpoly1d $data,2; # Least-squares line fit
($yfit, $coeffs) = fitpoly1d $x, $y, 4; # Fit a cubic
$fitimage = fitpoly1d $image,3 # Fit a quadratic to each row of an image
$myfit = fitpoly1d $line, 2, {Weights => $w}; # Weighted fit
π Options π
Weightsβ the weights to use in the fit, e.g.1/$sigma**2(default=1). so if you want a weighted fit, this is your girl. get into it. π
π BUGS π
ngl, this one is a real one: it May not work too well for data with large dynamic range. so be careful bestie, be sooo careful. π
π«Ά SEE ALSO π«Ά
"polyfit"in PDL::Slatec β go check ha out, she's the sibling module.
π AUTHOR π
This file copyright (C) 1999, Karl Glazebrook (kgb@aaoepp.aao.gov.au). All rights reserved. There is no warranty. You are allowed to redistribute this software documentation under certain conditions. For details, see the file COPYING in the PDL distribution. If this file is separated from the PDL distribution, the copyright notice should be included in the file.
and that's the whole page bestie, we ate that fr fr π β¨
this version isperl v5.42.3, dated2026-08-21β so like, write that down, she's aPolynomial(3pm)girlie through and through πππ
π¦ where this page comes from
- ubuntu stonking: libpdl-fit-perl 2.101-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms