Math::...::Base(3pm)
π NAME π
Math::Symbolic::Base β bestie, this is literally the base class for symbols in symbolic calculations. That's the whole vibe. It's the mothership that all the other Math::Symbolic terms inherit from. π
π SYNOPSIS π
use Math::Symbolic::Base;
π¦ DESCRIPTION π¦
Okay so picture this: every single term in the Math::Symbolic::* family is built on this class. We're talking Math::Symbolic::Operator, Math::Symbolic::Variable, and Math::Symbolic::Constant objects. They all descend from this one. It's giving founding mother energy, fr fr. πΈ
β¨ EXPORT β¨
None by default. Nope, nothing gets exported, girl. You want stuff, you go get it yourself. π
π₯ METHODS π₯
β¨ Method to_string β¨
The default method for stringification. It just returns the object's value. That's it. No drama, no plot twist, just straight up returns the value. Simple queen behavior. π
π Method value π
value() evaluates the Math::Symbolic tree into its numeric representation. Like, it actually does the math. Slay.
value()with zero arguments requires that every variable in the tree already has a definedvalueattribute. And bestie, hear me when I say this: that means every variable object, not just every named variable. Don't get cute and try to shortcut it. πvalue()with one argument sets the object's value β this is the move for a variable or a constant.value()with named arguments (key/value pairs) associates variables in the tree with the value-arguments if the corresponding key matches the variable name. (Can one say this any more complicated? π) Since version 0.132, there's an alternative syntax where you just pass a single hash reference instead. Love that for us.
Example: $tree->value(x => 1, y => 2, z => 3, t => 0) assigns the value 1 to any occurrances of variables of the name "x", aso.
Heads up: if a variable in the tree has no value set β and no argument ofvaluesets it temporarily β then the call tovalue()returnsundef. That's the deal. π
π Method signature π
signature() returns a tree's signature.
In the Math::Symbolic context, signatures are the list of variables any given tree depends on. So like, the tree "v*t+x" depends on the variables v, t, and x. Therefore, if you apply signature() on the tree that would be parsed from that example, you get the sorted list ('t', 'v', 'x'). Chef's kiss. π
Constants don't depend on any variables, so they return the empty list. Obviously. Operators' dependencies vary, because operators are complicated like that.
Math::Symbolic::Variable objects, though? They can have a slightly more involved signature. By convention, Math::Symbolic variables depend on themselves β meaning their signature contains their own name. But they can also depend on various other variables, because variables themselves can be viewed as placeholders for more complicated terms. For example, in mechanics, the acceleration of a particle depends on its mass and the sum of all forces acting on it. So the variable 'acceleration' would have the signature:
('acceleration', 'force1', 'force2', ..., 'mass', 'time')
If you're just looking for a list of the names of all the variables in the tree, you should use the explicit_signature() method instead. Different tool, different job. π§
π¦ Method explicit_signature π¦
explicit_signature() returns a lexicographically sorted list of the variable names in the tree. Just names. Clean and simple.
See also: signature().
π Method set_signature π
set_signature expects any number of variable identifiers as arguments. It sets a variable's signature to this list of identifiers. Feed it names, it remembers them. Periodt.
β Method implement β
implement() works in-place! Yes bestie, in-place, it mutates the thing you called it on. Be aware.
It takes key/value pairs as arguments. The keys are to be variable names, and the values must be valid Math::Symbolic trees. All occurrances of the variables will be replaced with their implementation. Swap in, swap out, done. π₯
π
Method replace π
First argument must be a valid Math::Symbolic tree.
replace() modifies the object it is called on in-place, in that it replaces it with its first argument. In doing so, it retains the original object reference. This destroys the object it is called on. Rip to that object, honestly. π
However β and this is the chaotic part β this also means you can create recursive trees of objects if the new tree is to contain the old tree. So make sure you clone the old tree using the new() method before using it in the replacement tree, or you will end up with a program that eats your memory fast. Not a vibe. Not iconic. Clone first. π
Seriously: clone with new() before you use the old tree in the replacement, or your memory is gone.
π fill_in_vars π
This method returns a modified copy of the tree it was called on. (Copy! Not in-place! We love a non-destructive queen.)
It walks the tree and replaces all variables whose value attribute is defined (either done at the time of object creation or using set_value()) with the corresponding constant objects. Variables whose value is not defined are left unaffected. Take, for example, the following code:
$tree = parse_from_string('a*b+a*c');
$tree->set_value(a => 4, c => 10); # value of b still not defined.
print $tree->fill_in_vars();
# prints "(4 * b) + (4 * 10)"
See how b stayed as b? Because b had no value. Respect. πΈ
π Method simplify π
Minimum method for term simpilification just clones. That's the whole thing. It's a stub doing the bare minimum and honestly, same.
π§ Method descending_operands π§
When called on an operator, descending_operands tries hard to determine which operands to descend into. (Which usually means all operands.) A list of these is returned.
When called on a constant or a variable, it returns the empty list. Nothing to descend into, bestie, it's a leaf. π
Of course, some routines may have to descend into different branches of the Math::Symbolic tree, but this routine returns the default operands.
The first argument to this method may control its behaviour. If it is any of the following key-words, behaviour is modified accordingly:
defaultβ obvious. Use default heuristics.
These are all supersets of 'default':
allβ returns ALL operands. Use with caution. πall_varsβ returns all operands that may contain vars.
π Method descend π
The method takes named arguments (key/value pairs). descend() descends (who would have guessed? π) into the Math::Symbolic tree recursively, and for each node, it calls code references with a copy of the current node as argument. That copy may be modified and will be used for the construction of the returned tree. The automatic copying behaviour may be turned off.
Returns a (modified) copy of the original tree. If in-place modification is turned on, the returned tree will not be a copy. Note the difference, it matters. π
Available parameters are:
beforeβ A code reference to be used as a callback that will be invoked before descent. Depending on whether or not the"in_place"option is set, the callback will be passed a copy of the current node (default) or the original node itself.
The callback may modify the tree node, and the modified node will be used to construct descend()'s return value.
The return value of this callback describes the way descend() handles the descent into the current node's operands.
- If it returns the empty list, the (possibly modified) copy of the current node that was passed to the callback is used as the return value of
descend(), but the recursive descent is continued for all of the current node's operands which may or may not be modified by the callback. The"after"callback will be called on the node after descent into the operands. (This is the normal behavior.) - If the callback returns
undef, the descent is stopped for the current branch and an exact copy of the current branch's children will be used fordescend()'s return value. The"after"callback will be called immediately. - If the callback returns a list of integers, these numbers are assumed to be the indexes of the current node's operands that are to be descended into. That means if the callback returns
(1),descendwill be called for the second operand and only the second. All other children/operands will be cloned. As usual, the"after"callback will be called after descent. - Any other return lists will lead to hard-to-debug errors. Tough luck. π
- Returning a hash reference from the callback allows for complete control over the
descend()routine. The hash may contain the following elements:operandsβ This is a referenced array that will be put in place of the previous operands. It is the callback's job to make sure the number of operands stays correct. The"operands"entry is evaluated before the"descend_into"entry.descend_intoβ This is a referenced array of integers and references. The integers are assumed to be indices of the array of operands. Returning(1)results in descent into the second operand and only the second. References are assumed to be operands to descend into.descend()will be directly called on them. If the array is empty,descend()will act just as if an empty list had been returned.in_placeβ Boolean indicating whether or not to modify the operands in-place or not. If this is true,descend()will be called with the"in_place => 1"parameter. If false, it will be called with"in_place => 0"instead. Defaults to false. (Cloning) This does not affect the call to the"after"callback but only the descent into operands.skip_afterβ If this option exists and is set to true, the"after"callback will not be invoked. This only applies to the current node, not to its children/operands.
- The list of options may grow in future versions. π
afterβ This is a code reference which will be invoked as a callback after the descent into the operands.in_placeβ Controls whether or not to modify the current tree node in-place. Defaults to false - cloning.operand_finderβ This option controls how the descend routine chooses which operands to recurse into by default. That means it controls which operandsdescend()recurses into if the'before'routine returned the empty list or if no'before'routine was specified.
The option may either be a code reference or a string. If it is a code reference, this code reference will be called with the current node as argument. If it is a string, the method with that name will be called on the current node object.
By default, descend() calls the 'descending_operands()' method on the current node to determine the operands to descend into.
π Method term_type π
Returns the type of the term. This is a stub to be overridden. It's giving placeholder energy β the subclasses are supposed to come through and actually do something here. π
β Method set_value β
set_value() returns the tree it modifies, but acts in-place on the Math::Symbolic tree it was called on.
set_value() requires named arguments (key/value pairs) that associate variable names of variables in the tree with the value-arguments if the corresponding key matches the variable name. (Can one say this any more complicated? π) Since version 0.132, an alternative syntax is to pass a single hash reference to the method.
Example: $tree->set_value(x => 1, y => 2, z => 3, t => 0) assigns the value 1 to any occurrances of variables of the name "x", aso.
As opposed to value(), set_value() assigns to the variables permanently and does not evaluate the tree. Different method, different behavior, don't mix them up bestie. π«Ά
When called on constants, set_value() sets their value to its first argument, but only if there is only one argument. One arg or no deal. π
π AUTHOR π
okay so bestie, if you got feedback, found a bug, or you're just out here needing support β send it all to the Math::Symbolic support mailing list: math-symbolic-support at lists dot sourceforge dot net. and fr fr, they'd love to hear how you're using Math::Symbolic, so please consider letting them know. thank youuu π«Ά
if you're the type of main character who wants to help with development or extend the module's functionality, then hit up the developers' mailing list instead: math-symbolic-develop at lists dot sourceforge dot net. go off π
and here's the full list of contributors, absolute icons the whole bunch:
Steffen MΓΌller, symbolic-module at steffen-mueller dot net
Stray Toaster, mwk at users dot sourceforge dot net
Oliver EbenhΓΆh
π SEE ALSO π
new versions of this module? they're living at http://steffen-mueller.net or on CPAN. and the module development happens over on Sourceforge at http://sourceforge.net/projects/math-symbolic/ β¨
also go check out Math::Symbolic while you're at it, iykyk π
aaaand that's a wrap on Math::...::Base(3pm), bestie π this one's perl v5.40.0 and it's dated 2024-09-16 β so like, shoutout to that version and that date, they ate and left no crumbs π go forth and base your maths, you got this πΈπ¦
π¦ where this page comes from
- ubuntu resolute: libmath-symbolic-perl 0.613-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmath-symbolic-perl 0.613-1 Β· 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