Math::...erator(3pm)

๐ŸŒธ NAME ๐ŸŒธ

Math::Symbolic::Operator โ€” operators in symbolic calculations, bestie ๐Ÿ’–

Okay so this is the Math::Symbolic::Operator(3pm) page, straight out of the User Contributed Perl Documentation, and it's literally the module that implements all the operator objects over in Math::Symbolic land. Slay. ๐Ÿ‘‘


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

  use Math::Symbolic::Operator;

  my $sum = Math::Symbolic::Operator->new('+', $term1, $term2);

  # or:
  my $division =
    Math::Symbolic::Operator->new(
      {
        type     => B_DIVISON,
        operands => [$term1, $term2],
      }
    );

  my $derivative =
    Math::Symbolic::Operator->new(
      {
        type     => U_P_DERIVATIVE,
        operands => [$term],
      }
    );

๐Ÿ’ซ DESCRIPTION ๐Ÿ’ซ

This module implements all the Math::Symbolic::Operator objects, no cap. These objects are overloaded, which is a fancy way of saying: in stringification-context they call the to_string() method on themselves ๐Ÿช„, and in numeric and boolean context they evaluate to their numerical representation. It's giving dual citizenship fr fr.

Want the full list of supported operators? Scroll down to the docs for the new() constructor, because that's where the whole menu lives ๐Ÿ“.

Math::Symbolic::Operator inherits from Math::Symbolic::Base. Family tree realness. ๐ŸŒณ

โœจ EXPORT โœจ

None. That's it. Nothing gets exported. The vibe is minimal. ๐Ÿ’…


๐Ÿ”ฎ CLASS DATA ๐Ÿ”ฎ

Math::Symbolic::Operator contains several class data structures. Lowkey you should not worry about dealing with any of them, because they're mostly an implementation detail โ€” but for the sake of completeness, here's the gist. Feel free to skip this whole section though, bestie, no hard feelings ๐Ÿซถ

One of them is the %Op_Symbols hash, which associates operator (and function) symbols with the corresponding constant as exported by Math::Symbolic or Math::Symbolic::ExportConstants. For example, '+' => B_SUM which in turn is 0, if I recall correctly ๐Ÿ‘€. But I didn't tell you that. Because you're supposed to use the supplied constants (they're inlined and hence fast ๐ŸŽ๏ธ) so I can change their internal order if I deem it necessary. Respect the system, okay?

The array @Op_Types associates operator indices (yes, those nifty constants you just met โœจ) with anonymous hash datastructures that contain some info on the operator โ€” stuff like its arity, the rule used to derive it, its infix string, its prefix string, and information on how to actually apply it to numbers. So much lore in one array.


๐Ÿงš METHODS ๐Ÿงš

๐Ÿ’– Constructor new ๐Ÿ’–

Expects a hash reference as first argument. That hash's contents get treated as key-value pairs of object attributes. The important attributes are 'type' => OPERATORTYPE (and yes, please use the constants as exported by Math::Symbolic::ExportConstants!) and 'operands=>[op1,op2,...]'. Where the operands themselves may either be valid Math::Symbolic::* objects or strings that will be parsed as such. Flexible queen ๐Ÿ’…

Special case, and this one's iconic: if no hash reference was found, the first argument is assumed to be the operator's symbol and the operator is assumed to be binary. The following 2 arguments will be treated as operands. This special case will ignore attempts to clone objects, but if the operands are no valid Math::Symbolic::* objects, they will be sent through a Math::Symbolic::Parser to construct Math::Symbolic trees. ๐Ÿง 

Returns a Math::Symbolic::Operator.

Supported operator symbols: (number of operands and their function in parens, so you know what you're getting)

  +                  => sum (2)
  -                  => difference (2)
  *                  => product (2)
  /                  => division (2)
  log                => logarithm (2: base, function)
  ^                  => exponentiation (2: base, exponent)
  neg                => unary minus (1)
  partial_derivative => partial derivative (2: function, var)
  total_derivative   => total derivative (2: function, var)
  sin                => sine (1)
  cos                => cosine (1)
  tan                => tangent (1)
  cot                => cotangent (1)
  asin               => arc sine (1)
  acos               => arc cosine (1)
  atan               => arc tangent (1)
  atan2              => arc tangent of y/x (2: y, x)
  acot               => arc cotangent (1)
  sinh               => hyperbolic sine (1)
  cosh               => hyperbolic cosine (1)
  asinh              => hyperbolic area sine (1)
  acosh              => hyperbolic area cosine (1)

โญ Method arity โญ

Returns the operator's arity as an integer. That's the number of operands it eats, if you were wondering ๐Ÿฝ๏ธ

โญ Method type โญ

Optional integer argument that sets the operator's type. Returns the operator's type as an integer. Getter AND setter, we love a multitasker ๐Ÿ˜Œ

โญ Method to_string โญ

Returns a string representation of the operator and its operands. Optional argument: 'prefix' or 'infix'. Defaults to 'infix'. Pick your aesthetic, bestie ๐ŸŽ€

โญ Method term_type โญ

Returns the type of the term. ( T_OPERATOR ) โ€” that's the answer, every single time, look at it go โœจ

โญ Method simplify โญ

Term simpilification. First argument: Boolean indicating that the tree does not need to be cloned, but can be restructured instead. While this is faster, you might not be able to use the old tree any more. ๐Ÿšจ

Example:

  my $othertree = $tree->simplify();
  # can use $othertree and $tree now.

  my $yetanothertree = $tree->simplify(1);
  # must not use $tree any more because its internal
  # representation might have been destroyed.

If you want to optimize a routine and you're sure that you won't need the unsimplified tree any more, go ahead and use the first parameter. In all other cases, you should go the safe route. ๐Ÿ›Ÿ

Bestie, fr, do not touch $tree after that simplify(1) call. It's gone. It's in the wind. The internal representation is cooked ๐Ÿ’€

โญ Methods op1 and op2 โญ

Returns first/second operand of the operator if it exists, or undef. Sometimes the answer is just undef and that's valid ๐Ÿ™ˆ

โญ Method apply โญ

Applies the operation to its operands' value() and returns the result as a constant (-object).

Without arguments, all variables in the tree are required to have a value. If any don't, the call to apply() returns undef. So cold. โ„๏ธ

To (temorarily, for this single method call) assign values to variables in the tree, you may provide key/value pairs of variable names and values. Instead of passing a list of key/value pairs, you may also pass a single hash reference containing the variable mappings.

You usually want to call the value() instead of this. Just saying. ๐Ÿ‘€

โญ Method value โญ

value() evaluates the Math::Symbolic tree to its numeric representation. This is the one you actually want.

value() without arguments requires that every variable in the tree contains a defined value attribute. Please note that this refers to every variable object, not just every named variable. Yeah, every. Don't come crying to me when it returns undef ๐Ÿ˜ญ

value() with one argument sets the object's value if you're dealing with Variables or Constants. In case of operators, a call with one argument will assume that the argument is a hash reference. (see next paragraph ๐Ÿ‘‡)

value() with named arguments (key/value pairs) associates variables in the tree with the value-arguments if the corresponging key matches the variable name. (Can one say this any more complicated? Apparently yes, we did it, iconic of the docs tbh.) Since version 0.132, an equivalent and valid syntax is to pass a single hash reference instead of a list.

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.

If a variable in the tree has no value set (and no argument of value sets it temporarily), the call to value() returns undef. That's the rules. ๐Ÿซฅ

โญ Method signature โญ

signature() returns a tree's signature.

In the context of Math::Symbolic, signatures are the list of variables any given tree depends on. That means the tree "v*t+x" depends on the variables v, t, and x. Thus, applying signature() on the tree that would be parsed from above example yields the sorted list ('t', 'v', 'x'). Sorted, because we're nothing if not tidy ๐Ÿ’…

Constants do not depend on any variables and therefore return the empty list. Obviously, operators' dependencies vary.

Math::Symbolic::Variable objects, however, may have a slightly more involved signature. By convention, Math::Symbolic variables depend on themselves. That means 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 compicated 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'). The way that variable said "it's complicated" ๐Ÿ˜ญ๐Ÿ’–

If you're just looking for a list of the names of all variables in the tree, you should use the explicit_signature() method instead.

โญ Method explicit_signature โญ

explicit_signature() returns a lexicographically sorted list of variable names in the tree. No drama, just names. โœจ

See also: signature().


๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Please send feedback, bug reports, and support requests to the Math::Symbolic support mailing list: math-symbolic-support at lists dot sourceforge dot net. Please consider letting us know how you use Math::Symbolic. Thank you. ๐Ÿซถ

If you're interested in helping with the development or extending the module's functionality, please contact the developers' mailing list: math-symbolic-develop at lists dot sourceforge dot net.

List of contributors:

  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 can be found on http://steffen-mueller.net or CPAN. The module development takes place on Sourceforge at http://sourceforge.net/projects/math-symbolic/ โ€” go be nosy, bestie, it's free ๐Ÿฆ‹

Math::Symbolic


And that's a wrap on the whole entire page ๐Ÿ’– this absolute banger of a man page shipped with perl v5.40.0 on 2024-09-16, so go forth and do symbolic math, you main character. Periodt. ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from