Expression(3pm)
π Expression π
okay bestie, so you know Math::Calculus::Expression? that's the Algebraic Calculus Tools Expression Class, and she's lowkey a whole icon β¨ she eats algebraic expressions for breakfast. i'm gonna walk you through her the way i'd walk you through a group chat β but fr fr, every single flag and fact is staying, no cap.
β¨ NAME β¨
Math::Calculus::Expression β Algebraic Calculus Tools Expression Class π
πΈ SYNOPSIS πΈ
use Math::Calculus::Expression;
# Create an expression object.
my $exp = Math::Calculus::Expression->new;
# Set a variable and expression.
$exp->addVariable('x');
$exp->setExpression('x^(2+1) + 6*5*x') or die $exp->getError;
# Simplify
$exp->simplify or die $exp->getError;;
# Print the result.
print $exp->getExpression; # Prints x^3 + 30*x
look at her go π
note the or die $exp->getError β that's the vibe check, if she fails she tells you why.
π DESCRIPTION π
so basically, this module takes an algebraic expression and parses it into a tree structure. then she can:
- simplify that tree π³
- substitute variables and named constants for other variables or constants β and yes, those can be numeric π’
- numerically evaluate the tree
- turn the tree back into output of the same form as the input π
the way she supports so many expressions... obsessed. she handles the +, -, *, / and ^ (raise to power) operators, bracketed expressions (so precedence actually behaves correctly, thank u), and these functions:
ln, exp, sin, cos, tan, sec, cosec, cot, sinh, cosh, tanh, sech, cosech, coth, asin, acos, atan, asinh, acosh and atanh π¦
like. that's a list. she's so versatile it's giving main character energy.
π EXPORT π
None by default. that's it, that's the whole tea π nothing gets exported unless you ask.
π METHODS π
π new
$exp = Math::Calculus::Expression->new;
creates a new instance of the expression object β¨ she can hold an individual expression and do basic operations on it. your brand new baby.
π addVariable
$exp->addVariable('x');
sets a certain named value in the expression as being a variable. a named value has to be an alphabetic character, periodt. π€
π setExpression
$exp->setExpression('x^2 + 5*x);
takes an expression in human-readable form and stores it internally as a tree structure π³ β and while she's at it she checks that it's a valid expression she can actually understand.
bestie pls pay attention: she is strict about syntax. look at the example above β you write5*x, not just5x. don't do it. she will not forgive you. π
whitespace is allowed in the expression, but it does not have any effect on precedence. if you want control over precedence, use brackets β bracketed expressions always get evaluated first, exactly as you'd normally expect π«Ά
she 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's currently being stored. it's giving "show me what you're holding, queen" π
π simplify
$exp->simplify;
attempts to simplify the expression stored internally. glow-up time β¨
π evaluate
$exp->evaluate(x => 0.5, a => 4);
this one takes a hash mapping any variables and named constants (represented by letters) in the expression to numerical values, and attempts to evaluate the expression and return a numerical value π’
she fails and returns undef if:
- she finds letters that have no mapping π
- an error such as division by zero happens during the evaluation π
π sameRepresentation
$same = $exp->sameRepresentation($exp2);
takes another expression object as its parameter and returns true if that expression has the same internal representation as the expression the method was invoked on.
β οΈ be careful here bestie: while it's true that if two expressions have the same representation they are equal, it would be wrong to say that if they have different representations they're not equal. case in point:"x + 2"and"2 + x"are clearly equal, but their internal representation may well differ. don't be fooled π
π clone
$expCopy = $exp->clone;
returns a deep copy of the expression object. deep copy meaning: if the original gets modified, the copy will not be affected β and vice versa. two independent queens, no codependency π¦
π getTraceback
$exp->getTraceback;
when setExpression and differentiate are called, a traceback is generated to describe what those functions did. if an error occurs, this traceback can be extremely useful in helping track down the source of the error π like a little receipt of everything that went down.
π getError
$exp->getError;
when any method other than getTraceback is called, the stored error message is cleared first, and then any errors that happen during the execution of the method get stored. if failure occurs, call this method to get a textual representation of the error π
π Other Methods
any other method call is taken to refer to a subclass of Expression. here's the ritual:
- the first letter of the name of the method invoked is capitalized π
- a module by that name is loaded (if it exists) π¦
- the method is called on it π
this works for, for example, the Differentiate module β calling the differentiate method on an Expression will load the Differentiate module and call the differentiate method.
π if a module cannot be loaded or the method cannot be called, then this module will die. no soft launch, just death.
π¦ SEE ALSO π¦
the author of this module has a website at <http://www.jwcs.net/~jonathan/> π which has the latest news about the module, plus a web-based frontend so you can try out this module and β more specifically β its subclasses. very "come hang out in the browser" of him π«Ά
π 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, from perl v5.36.0 dated 2022-10-13 β go simplify something iconic, ily, bye πβ¨
π¦ where this page comes from
- ubuntu noble: libmath-calculus-expression-perl 0.2.2.ds-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmath-calculus-expression-perl 0.2.2.ds-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmath-calculus-expression-perl 0.2.2.ds-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