Math::Derivative(3pm)
β¨ Math::Derivative β¨ (that's the Math::Derivative(3pm) page, bestie β user contributed Perl docs, and yes we're doing this π)
okay so picture this: you've got data, you want derivatives, and you do NOT wanna do calculus by hand. enter this little Perl package. numeric first and second order differentiation, that's the whole vibe.
πΈ NAME πΈ
Math::Derivative β numeric 1st and 2nd order differentiation π
π¦ SYNOPSIS π¦
use Math::Derivative qw(:all);
@dydx = forwarddiff(\@x, \@y);
@dydx = centraldiff(\@x, \@y);
@dydx = Derivative1(\@x, \@y); # A synonym for centraldiff()
@d2ydx2 = Derivative2(\@x, \@y, $yd0, $ydn);
look at that. four whole ways to get your derivative on. iconic behavior honestly.
π DESCRIPTION π
This Perl package exports functions that numerically approximate first and second order differentiation on vectors of data. literally that's it, that's the job.
heads up tho: how accurate your approximation is depends on the differences between the successive values in the X array. small gaps = happy math. big weird gaps = your results are gonna be giving β¨chaosβ¨.
π FUNCTIONS π
You can import these functions by name, or just grab the whole squad with the :all tag. no cap.
π forwarddiff()
@dydx = forwarddiff(\@x, \@y);
Take the references to two arrays containing the x and y ordinates of the data, and it hands you back an array of approximate first derivatives at the given x ordinates, using the forward difference approximation. slay.
now the tea β: that last term is actually formed using a backward difference formula, because there is no array item to subtract from at the end of the array. poor thing is at the edge of the party with nobody to subtract.
so if you want derivatives strictly formed from the forward difference formula, use only the values from [0 .. #y-1], like:
@dydx = (forwarddiff(\@x, \@y))[0 .. $#y-1];
or, more simply:
@dydx = forwarddiff(\@x, \@y);
pop @dydx;
pop it like it's hot π₯
π§ centraldiff()
@dydx = centraldiff(\@x, \@y);
Take the references to two arrays containing the x and y ordinates of the data, and it returns an array of approximate first derivatives at the given x ordinates. bestie behavior.
the algorithm uses three data points to calculate the derivative β except at the end points, where by necessity the forward difference algorithm is used instead. the ends are just doing their best okay π
if you want derivatives strictly formed from the central difference formula, use only the values from [1 .. #y-1], like:
@dydx = (centraldiff(\@x, \@y))[1 .. $#y-1];
slicing off the ends so they can't ruin the vibe π
π Derivative2()
@d2ydx2 = Derivative2(\@x, \@y);
or
@d2ydx2 = Derivative2(\@x, \@y, $yp0, $ypn);
Take references to two arrays containing the x and y ordinates of the data and it returns an array of approximate second derivatives at the given x ordinates. second derivative era fr fr.
you may optionally give values to use as the first derivatives at the start and end points of the data. if you don't, first derivative values will be assumed to be zero. so like, speak now or forever hold your zeros.
π Derivative1()
A synonym for centraldiff(). same function, second name, main character energy. π
β REFERENCES β
- <http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/central-differences/>
- <http://www.robots.ox.ac.uk/~sjrob/Teaching/EngComp/ecl6.pdf>
the receipts, the lore, the required reading πβ¨
π AUTHOR π
- John A.R. Williams β
J.A.R.Williams@aston.ac.uk - John M. Gamble β
jgamble@cpan.org(current maintainer)
shoutout to the maintainers, holding it down π«Ά
and that's the whole page, bestie! go forth and differentiate. numerically. responsibly. π β¨
this was Math::Derivative(3pm) from perl v5.36.0, dated 2022-10-13 β mwah, kisses, bye π
π¦ where this page comes from
- ubuntu noble: libmath-derivative-perl 1.01-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmath-derivative-perl 1.01-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmath-derivative-perl 1.01-3 Β· 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