The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Math::Symbolic::Operator - Operators in symbolic calculations

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 Math::Symbolic::Operator objects. These objects are overloaded in stringification-context to call the to_string() method on the object. In numeric and boolean context, they evaluate to their numerical representation.

For a list of supported operators, please refer to the list found below, in the documentation for the new() constructor.

Math::Symbolic::Operator inherits from Math::Symbolic::Base.

EXPORT

None.

CLASS DATA

Math::Symbolic::Operator contains several class data structures. Usually, you should not worry about dealing with any of them because they are mostly an implementation detail, but for the sake of completeness, here's the gist, but feel free to skip this section of the docs:

One of these is the %Op_Symbols hash that 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 (inlined and hence fast) constants so I can change their internal order if I deem it necessary.)

The array @Op_Types associates operator indices (recall those nifty constants?) with anonymous hash datastructures that contain some info on the operator such as its arity, the rule used to derive it, its infix string, its prefix string, and information on how to actually apply it to numbers.

METHODS

Constructor new

Expects a hash reference as first argument. That hash's contents will be treated as key-value pairs of object attributes. Important attributes are 'type' => OPERATORTYPE (use 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.

Special case: if no hash reference was found, 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)

  +                  => 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)
  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.

Method type

Optional integer argument that sets the operator's type. Returns the operator's type as an integer.

Method to_string

Returns a string representation of the operator and its operands. Optional argument: 'prefix' or 'infix'. Defaults to 'infix'.

Method term_type

Returns the type of the term. ( T_OPERATOR )

Method simplify

Term simpilification.

Method op1 and op2

Returns first/second operand of the operator if it exists or undef.

Method apply

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

Method value

For operators, value() is just a wrapper around apply().

Method apply_derivatives

If the operator is a derivative, this applies the derivative to its first operand. Regardless what kind of operator this is called on, apply_derivatives will be applied recursively on its operands.

If the first parameter to this function is an integer, at maximum that number of derivatives are applied (from top down the tree if possible).

AUTHOR

Steffen Mueller, <symbolic-module at steffen-mueller dot net>

New versions of this module can be found on http://steffen-mueller.net or CPAN.

SEE ALSO

Math::Symbolic