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 is perl v5.42.3, dated 2026-08-21 β€” so like, write that down, she's a Polynomial(3pm) girlie through and through πŸŽ€πŸ’–πŸŒˆ
πŸ“¦ where this page comes from