NewtonRaphson(3pm)
β¨ NAME β¨
Math::Calculus::NewtonRaphson β an Algebraic Newton Raphson Implementation, aka the module that does your calculus homework and does not even complain about it π«Ά
π SYNOPSIS π
bestie here's the whole vibe in one go:
use Math::Calculus::NewtonRaphson;
# Create an object.
my $exp = Math::Calculus::NewtonRaphson->new;
# Set a variable and expression.
$exp->addVariable('x');
$exp->setExpression('x^2 - 5') or die $exp->getError;
# Apply Newton Raphson.
my $result = $exp->newtonRaphson(2) or die $exp->getError;
print $result; # Prints 1.4142...
likeβ¦ it literally just prints 1.4142... at you. iconic behavior honestly π
πΈ DESCRIPTION πΈ
okay so here's the tea β β this module takes an algebraic expression, parses it, and then hits it with the Newton Raphson method to solve it.
The way Newton Raphson works is lowkey simple: it relies on the fact that the expression you pass in evaluates to zero where there is a solution. So if you wanna solve:
x^2 = 5
you don't pass that in, you rearrange it and pass in:
x^2 - 5
because the answer is the spot where that whole thing equals zero. math is giving β¨rearrangingβ¨.
It understands expressions containing any of these operators: +, -, *, / and ^ (raise to power), plus bracketed expressions so you get correct precedence, and these functions:
lnexpsincostanseccoseccotsinhcoshtanhsechcosechcothasinacosatanasinhacoshatanh
that's the whole roster, no cap π
π« EXPORT π«
None by default.
not a single thing gets exported, she keeps her business to herself π
π METHODS π
new
$exp = Math::Calculus::NewtonRaphson->new;
Creates a new instance of the Newton Raphson object, and that instance can hold an individual expression. one object, one expression, main character energy.
addVariable
$exp->addVariable('x');
Sets a certain named value in the expression as being a variable. and heads up: a named value must be an alphabetic character β no digits, no chaos, alphabetic only bestie.
setExpression
$exp->setExpression('x^2 + 5*x);
(yes the quote is missing up there, she's a little unhinged, we stan anyway π)
Takes an expression in human-readable form and stores it internally as a tree structure, checking it's a valid expression the module can actually understand while it's at it.
β οΈ heads up: the engine is strict about syntax. like, strict strict. For example, notice above that you must write5*xand not just5x. that's not a suggestion, that's the law fr fr.
Whitespace is allowed in the expression, but it does not have any effect on precedence. If you require control of precedence, use brackets β bracketed expressions will always be evaluated first, exactly as you would normally expect. The module follows the BODMAS precedence convention π§
Returns undef on failure and a true value on success.
getExpression
$expr = $exp->getExpression;
Returns a textual, human readable representation of the expression that is being stored. she'll read it back to you, no judgement π
newtonRaphson
$result = $exp->newtonRaphson($variable, $guess, %mappings);
Attempts to solve the expression for the given variable using the Newton Raphson method, using the passed value as the first guess. The %mappings hash lets you map any other non-numeric constants to numeric values β which is a pre-requisite for solving such equations. no mappings, no solving, that's just how it is π
getTraceback
$exp->getTraceback;
When setExpression and newtonRaphson are called, a traceback is generated to describe what these functions did. If an error occurs, this traceback can be extremely useful in helping track down the source of the error. it's giving detective work π
getError
$exp->getError;
When any method other than getTraceback is called, the stored error message is cleared, and then any errors that occur during the execution of that method get stored instead. If failure occurs, call this method to get a textual representation of the error. it's your little "what went wrong bestie" hotline π
π SEE ALSO π
The author of this module has a website at <http://www.jwcs.net/~jonathan/>, which has the latest news about the module and a web-based frontend so you can test the module out for yourself. go poke at it, it's fun π¦
π AUTHOR π
Jonathan Worthington, <jonathan@jwcs.net>
π COPYRIGHT AND LICENSE π
Copyright (C) 2004 by Jonathan Worthington
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.1 or, at your option, any later version of Perl 5 you may have available.
and that's the whole page bestie!! πβ¨
sending you off with the receipts: this was perl v5.36.0, dated 2022-12-08, and you just read NewtonRaphson(3pm). slay, go solve something π«Άπ
π¦ where this page comes from
- debian bookworm: libmath-calculus-newtonraphson-perl 0.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmath-calculus-newtonraphson-perl 0.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmath-calculus-newtonraphson-perl 0.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmath-calculus-newtonraphson-perl 0.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmath-calculus-newtonraphson-perl 0.1-4 Β· 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