Expression(3pm)

πŸ“¦
version 0.2.2.ds Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 write 5*x, not just 5x. 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:

  1. the first letter of the name of the method invoked is capitalized πŸ” 
  2. a module by that name is loaded (if it exists) πŸ“¦
  3. 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 (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