Quick Navigator

 Search Site Miscellaneous Server Agreement Year 2038 Credits

# Manual Reference Pages  -  MATH::ALGEBRA::SYMBOLS (3)

.ds Aq ’

### Symbols

Symbolic Algebra in Pure Perl.

See user manual NAME.

#### Synopsis

This package delivers the public components of package <B>sumB>.

#### import

Export components as requested by caller.

```

use Math::Algebra::Symbols symbols=>s trig=>1 hyper=>1 complex=>0;

```

Valid options are:
symbols=>’s’ Create a function with name <B>s()B> in the callers namespace to create new symbols. The default is <B>B>symbols()<B>B>.

item trig=>0

The default, no trigonometric functions are exported.

item trig=>1

Export trigonometric functions: tan, sec, csc, cot. sin, cos are created by default by overloading the existing Perl sin and cos operators.

hyper=>0 The default, no hyperbolic functions
hyper=>1 Export hyperbolic functions: sinh, cosh, tanh, sech, csch, coth.
complex=>0 The default, no complex functions
complex=>1 Export complex functions: conjugate, cross, dot, im, modulus, re, unit.
Trigonometric can be used instead of trig.

Hyperbolic can be used instead of hyper.

### NAME

Math::Algebra::Symbols - Symbolic Algebra in Pure Perl.

User guide.

### SYNOPSIS

Example symbols.pl

```

#!perl -w -I..
#______________________________________________________________________
# Symbolic algebra.
# PhilipRBrenan@yahoo.com, 2004.
#______________________________________________________________________

use Math::Algebra::Symbols hyper=>1;
use Test::Simple tests=>5;

(\$n, \$x, \$y) = symbols(qw(n x y));

\$a     += (\$x**8 - 1)/(\$x-1);
\$b     +=  sin(\$x)**2 + cos(\$x)**2;
\$c     += (sin(\$n*\$x) + cos(\$n*\$x))->d->d->d->d / (sin(\$n*\$x)+cos(\$n*\$x));
\$d      =  tanh(\$x+\$y) == (tanh(\$x)+tanh(\$y))/(1+tanh(\$x)*tanh(\$y));
(\$e,\$f) =  @{(\$x**2 eq 5*\$x-6) > \$x};

print "\$a\n\$b\n\$c\n\$d\n\$e,\$f\n";

ok("\$a"    eq \$x+\$x**2+\$x**3+\$x**4+\$x**5+\$x**6+\$x**7+1);
ok("\$b"    eq 1);
ok("\$c"    eq \$n**4);
ok("\$d"    eq 1);
ok("\$e,\$f" eq 2,3);

```

### DESCRIPTION

This package supplies a set of functions and operators to manipulate operator expressions algebraically using the familiar Perl syntax.

These expressions are constructed from Symbols, Operators, and Functions, and processed via Methods. For examples, see: Examples.

#### Symbols

Symbols are created with the exported <B>B>symbols()<B>B> constructor routine:

Example t/constants.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: constants.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>1;

my (\$x, \$y, \$i, \$o, \$pi) = symbols(qw(x y i 1 pi));

ok( "\$x \$y \$i \$o \$pi"   eq   \$x \$y i 1 \$pi  );

```

The <B>B>symbols()<B>B> routine constructs references to symbolic variables and symbolic constants from a list of names and integer constants.

The special symbol <B>iB> is recognized as the square root of <B>-1B>.

The special symbol <B>piB> is recognized as the smallest positive real that satisfies:

Example t/ipi.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: constants.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my (\$i, \$pi) = symbols(qw(i pi));

ok(  exp(\$i*\$pi)  ==   -1  );
ok(  exp(\$i*\$pi) <=>  -1 );

```

Constructor Routine Name

If you wish to use a different name for the constructor routine, say <B>SB>:

Example t/ipi2.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: constants.
#______________________________________________________________________

use Math::Algebra::Symbols symbols=>S;
use Test::Simple tests=>2;

my (\$i, \$pi) = S(qw(i pi));

ok(  exp(\$i*\$pi)  ==   -1  );
ok(  exp(\$i*\$pi) <=>  -1 );

```

Big Integers

Symbols automatically uses big integers if needed.

Example t/bigInt.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: bigInt.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>1;

my \$z = symbols(1234567890987654321/1234567890987654321);

ok( eval \$z eq 1);

```

#### Operators

Symbols can be combined with Operators to create symbolic expressions:

Arithmetic operators

Arithmetic Operators: <B>+B> <B>-B> <B>*B> <B>/B> <B>**B>

Example t/x2y2.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: simplification.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$x, \$y) = symbols(qw(x y));

ok(  (\$x**2-\$y**2)/(\$x-\$y)  ==  \$x+\$y  );
ok(  (\$x**2-\$y**2)/(\$x-\$y)  !=  \$x-\$y  );
ok(  (\$x**2-\$y**2)/(\$x-\$y) <=> \$x+\$y );

```

The operators: <B>+=B> <B>-=B> <B>*=B> <B>/=B> are overloaded to work symbolically rather than numerically. If you need numeric results, you can always <B>B>eval()<B>B> the resulting symbolic expression.

Square root Operator: <B>sqrtB>

Example t/ix.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: sqrt(-1).
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my (\$x, \$i) = symbols(qw(x i));

ok(  sqrt(-\$x**2)  ==  \$i*\$x  );
ok(  sqrt(-\$x**2)  <=> i*\$x );

```

The square root is represented by the symbol <B>iB>, which allows complex expressions to be processed by Math::Complex.

Exponential Operator: <B>expB>

Example t/expd.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: exp.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my (\$x, \$i) = symbols(qw(x i));

ok(   exp(\$x)->d(\$x)  ==   exp(\$x)  );
ok(   exp(\$x)->d(\$x) <=>  exp(\$x) );

```

The exponential operator.

Logarithm Operator: <B>logB>

Example t/logExp.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: log: need better example.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>1;

my (\$x) = symbols(qw(x));

ok(   log(\$x) <=>  log(\$x) );

```

Logarithm to base <B>eB>.

Note: the above result is only true for x > 0. <B>SymbolsB> does not include domain and range specifications of the functions it uses.

Sine and Cosine Operators: <B>sinB> and <B>cosB>

Example t/sinCos.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: simplification.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$x) = symbols(qw(x));

ok(  sin(\$x)**2 + cos(\$x)**2  ==  1  );
ok(  sin(\$x)**2 + cos(\$x)**2  !=  0  );
ok(  sin(\$x)**2 + cos(\$x)**2 <=> 1 );

```

This famous trigonometric identity is not preprogrammed into <B>SymbolsB> as it is in commercial products.

Instead: an expression for <B>B>sin()<B>B> is constructed using the complex exponential: exp, said expression is algebraically multiplied out to prove the identity. The proof steps involve large intermediate expressions in each step, as yet I have not provided a means to neatly lay out these intermediate steps and thus provide a more compelling demonstration of the ability of <B>SymbolsB> to verify such statements from first principles.

Relational operators

Relational operators: <B>==B>, <B>!=B>

Example t/x2y2.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: simplification.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$x, \$y) = symbols(qw(x y));

ok(  (\$x**2-\$y**2)/(\$x-\$y)  ==  \$x+\$y  );
ok(  (\$x**2-\$y**2)/(\$x-\$y)  !=  \$x-\$y  );
ok(  (\$x**2-\$y**2)/(\$x-\$y) <=> \$x+\$y );

```

The relational equality operator <B>==B> compares two symbolic expressions and returns TRUE(1) or FALSE(0) accordingly. <B>!=B> produces the opposite result.

Relational operator: <B>eqB>

Example t/eq.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: solving.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$x, \$v, \$t) = symbols(qw(x v t));

ok(  (\$v eq \$x / \$t)->solve(qw(x in terms of v t))  ==  \$v*\$t  );
ok(  (\$v eq \$x / \$t)->solve(qw(x in terms of v t))  !=  \$v+\$t  );
ok(  (\$v eq \$x / \$t)->solve(qw(x in terms of v t)) <=> \$v*\$t );

```

The relational operator <B>eqB> is a synonym for the minus <B>-B> operator, with the expectation that later on the solve() function will be used to simplify and rearrange the equation. You may prefer to use <B>eqB> instead of <B>-B> to enhance readability, there is no functional difference.

Complex operators

Complex operators: the <B>dotB> operator: <B>^B>

Example t/dot.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: dot operator.  Note the low priority
# of the ^ operator.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$a, \$b, \$i) = symbols(qw(a b i));

ok(  ((\$a+\$i*\$b)^(\$a-\$i*\$b))  ==  \$a**2-\$b**2  );
ok(  ((\$a+\$i*\$b)^(\$a-\$i*\$b))  !=  \$a**2+\$b**2  );
ok(  ((\$a+\$i*\$b)^(\$a-\$i*\$b)) <=> \$a**2-\$b**2 );

```

Note the use of brackets: The <B>^B> operator has low priority.

The <B>^B> operator treats its left hand and right hand arguments as complex numbers, which in turn are regarded as two dimensional vectors to which the vector dot product is applied.

Complex operators: the <B>crossB> operator: <B>xB>

Example t/cross.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: cross operator.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$x, \$i) = symbols(qw(x i));

ok(  \$i*\$x x \$x  ==  \$x**2  );
ok(  \$i*\$x x \$x  !=  \$x**3  );
ok(  \$i*\$x x \$x <=> \$x**2 );

```

The <B>xB> operator treats its left hand and right hand arguments as complex numbers, which in turn are regarded as two dimensional vectors defining the sides of a parallelogram. The <B>xB> operator returns the area of this parallelogram.

Note the space before the <B>xB>, otherwise Perl is unable to disambiguate the expression correctly.

Complex operators: the <B>conjugateB> operator: <B>~B>

Example t/conjugate.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: dot operator.  Note the low priority
# of the ^ operator.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$x, \$y, \$i) = symbols(qw(x y i));

ok(  ~(\$x+\$i*\$y)  ==  \$x-\$i*\$y  );
ok(  ~(\$x-\$i*\$y)  ==  \$x+\$i*\$y  );
ok(  ((\$x+\$i*\$y)^(\$x-\$i*\$y)) <=> \$x**2-\$y**2 );

```

The <B>~B> operator returns the complex conjugate of its right hand side.

Complex operators: the <B>modulusB> operator: <B>absB>

Example t/abs.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: dot operator.  Note the low priority
# of the ^ operator.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$x, \$i) = symbols(qw(x i));

ok(  abs(\$x+\$i*\$x)  ==  sqrt(2*\$x**2)  );
ok(  abs(\$x+\$i*\$x)  !=  sqrt(2*\$x**3)  );
ok(  abs(\$x+\$i*\$x) <=> sqrt(2*\$x**2) );

```

The <B>absB> operator returns the modulus (length) of its right hand side.

Complex operators: the <B>unitB> operator: <B>!B>

Example t/unit.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: unit operator.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>4;

my (\$i) = symbols(qw(i));

ok(  !\$i      == \$i                         );
ok(  !\$i     <=> i                        );
ok(  !(\$i+1) <=>  1/(sqrt(2))+i/(sqrt(2)) );
ok(  !(\$i-1) <=> -1/(sqrt(2))+i/(sqrt(2)) );

```

The <B>!B> operator returns a complex number of unit length pointing in the same direction as its right hand side.

Equation Manipulation Operators

Equation Manipulation Operators: <B>SimplifyB> operator: <B>+=B>

Example t/simplify.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: simplify.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my (\$x) = symbols(qw(x));

ok(  (\$x**8 - 1)/(\$x-1)  ==  \$x+\$x**2+\$x**3+\$x**4+\$x**5+\$x**6+\$x**7+1  );
ok(  (\$x**8 - 1)/(\$x-1) <=> \$x+\$x**2+\$x**3+\$x**4+\$x**5+\$x**6+\$x**7+1 );

```

The simplify operator <B>+=B> is a synonym for the simplify() method, if and only if, the target on the left hand side initially has a value of undef.

Admittedly this is very strange behavior: it arises due to the shortage of over-rideable operators in Perl: in particular it arises due to the shortage of over-rideable unary operators in Perl. Never-the-less: this operator is useful as can be seen in the Synopsis, and the desired pre-condition can always achieved by using <B>myB>.

Equation Manipulation Operators: <B>SolveB> operator: <B>>B>

Example t/solve2.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: simplify.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my (\$t) = symbols(qw(t));

my \$rabbit  = 10 + 5 * \$t;
my \$fox     = 7 * \$t * \$t;
my (\$a, \$b) = @{(\$rabbit eq \$fox) > \$t};

ok( "\$a" eq  1/14*sqrt(305)+5/14  );
ok( "\$b" eq -1/14*sqrt(305)+5/14  );

```

The solve operator <B>>B> is a synonym for the solve() method.

The priority of <B>>B> is higher than that of <B>eqB>, so the brackets around the equation to be solved are necessary until Perl provides a mechanism for adjusting operator priority (cf. Algol 68).

If the equation is in a single variable, the single variable may be named after the <B>>B> operator without the use of [...]:

```

use Math::Algebra::Symbols;

my \$rabbit  = 10 + 5 * \$t;
my \$fox     = 7 * \$t * \$t;
my (\$a, \$b) = @{(\$rabbit eq \$fox) > \$t};

print "\$a\n";

# 1/14*sqrt(305)+5/14

```

If there are multiple solutions, (as in the case of polynomials), <B>>B> returns an array of symbolic expressions containing the solutions.

This example was provided by Mike Schilli m@perlmeister.com.

#### Functions

Perl operator overloading is very useful for producing compact representations of algebraic expressions. Unfortunately there are only a small number of operators that Perl allows to be overloaded. The following functions are used to provide capabilities not easily expressed via Perl operator overloading.

These functions may either be called as methods from symbols constructed by the Symbols construction routine, or they may be exported into the user’s namespace as described in EXPORT.

Trigonometric and Hyperbolic functions

Trigonometric functions

Example t/sinCos2.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: methods.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>1;

my (\$x, \$y) = symbols(qw(x y));

ok( (sin(\$x)**2 == (1-cos(2*\$x))/2) );

```

The trigonometric functions <B>cosB>, <B>sinB>, <B>tanB>, <B>secB>, <B>cscB>, <B>cotB> are available, either as exports to the caller’s name space, or as methods.

Hyperbolic functions

Example t/tanh.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: methods.
#______________________________________________________________________

use Math::Algebra::Symbols hyper=>1;
use Test::Simple tests=>1;

my (\$x, \$y) = symbols(qw(x y));

ok( tanh(\$x+\$y)==(tanh(\$x)+tanh(\$y))/(1+tanh(\$x)*tanh(\$y)));

```

The hyperbolic functions <B>coshB>, <B>sinhB>, <B>tanhB>, <B>sechB>, <B>cschB>, <B>cothB> are available, either as exports to the caller’s name space, or as methods.

Complex functions

Complex functions: <B>reB> and <B>imB>

```

use Math::Algebra::Symbols complex=>1;

```

Example t/reIm.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: methods.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my (\$x, \$i) = symbols(qw(x i));

ok( (\$i*\$x)->re   <=>  0    );
ok( (\$i*\$x)->im   <=>  \$x );

```

The <B>reB> and <B>imB> functions return an expression which represents the real and imaginary parts of the expression, assuming that symbolic variables represent real numbers.

Complex functions: <B>dotB> and <B>crossB>

Example t/dotCross.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: methods.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my \$i = symbols(qw(i));

ok( (\$i+1)->cross(\$i-1)   <=>  2 );
ok( (\$i+1)->dot  (\$i-1)   <=>  0 );

```

The <B>dotB> and <B>crossB> operators are available as functions, either as exports to the caller’s name space, or as methods.

Complex functions: <B>conjugateB>, <B>modulusB> and <B>unitB>

Example t/conjugate2.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: methods.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my \$i = symbols(qw(i));

ok( (\$i+1)->unit      <=>  1/(sqrt(2))+i/(sqrt(2)) );
ok( (\$i+1)->modulus   <=>  sqrt(2)                 );
ok( (\$i+1)->conjugate <=>  1-i                     );

```

The <B>conjugateB>, <B>absB> and <B>unitB> operators are available as functions: <B>conjugateB>, <B>modulusB> and <B>unitB>, either as exports to the caller’s name space, or as methods. The confusion over the naming of: the <B>absB> operator being the same as the <B>modulusB> complex function; arises over the limited set of Perl operator names available for overloading.

#### Methods

Methods for manipulating Equations

Simplifying equations: <B>B>simplify()<B>B>

Example t/simplify2.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: simplify.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my (\$x) = symbols(qw(x));

my \$y  = ((\$x**8 - 1)/(\$x-1))->simplify();  # Simplify method
my \$z +=  (\$x**8 - 1)/(\$x-1);               # Simplify via +=

ok( "\$y" eq \$x+\$x**2+\$x**3+\$x**4+\$x**5+\$x**6+\$x**7+1 );
ok( "\$z" eq \$x+\$x**2+\$x**3+\$x**4+\$x**5+\$x**6+\$x**7+1 );

```

<B>B>Simplify()<B>B> attempts to simplify an expression. There is no general simplification algorithm: consequently simplifications are carried out on ad hoc basis. You may not even agree that the proposed simplification for a given expressions is indeed any simpler than the original. It is for these reasons that simplification has to be explicitly requested rather than being performed automagically.

At the moment, simplifications consist of polynomial division: when the expression consists, in essence, of one polynomial divided by another, an attempt is made to perform polynomial division, the result is returned if there is no remainder.

The <B>+=B> operator may be used to simplify and assign an expression to a Perl variable. Perl operator overloading precludes the use of <B>=B> in this manner.

Substituting into equations: <B>B>sub()<B>B>

Example t/sub.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: expression substitution for a variable.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>2;

my (\$x, \$y) = symbols(qw(x y));

my \$e  = 1+\$x+\$x**2/2+\$x**3/6+\$x**4/24+\$x**5/120;

ok(  \$e->sub(x=>\$y**2, z=>2)  <=> \$y**2+1/2*\$y**4+1/6*\$y**6+1/24*\$y**8+1/120*\$y**10+1  );
ok(  \$e->sub(x=>1)            <=>  163/60);

```

The <B>B>sub()<B>B> function example on line <B>#1B> demonstrates replacing variables with expressions. The replacement specified for <B>zB> has no effect as <B>zB> is not present in this equation.

Line <B>#2B> demonstrates the resulting rational fraction that arises when all the variables have been replaced by constants. This package does not convert fractions to decimal expressions in case there is a loss of accuracy, however:

```

my \$e2 = \$e->sub(x=>1);
\$result = eval "\$e2";

```

or similar will produce approximate results.

At the moment only variables can be replaced by expressions. Mike Schilli, m@perlmeister.com, has proposed that substitutions for expressions should also be allowed, as in:

```

\$x/\$y => \$z

```

Solving equations: <B>B>solve()<B>B>

Example t/solve1.t

```

#!perl -w
#______________________________________________________________________
# Symbolic algebra: examples: simplify.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests=>3;

my (\$x, \$v, \$t) = symbols(qw(x v t));

ok(   (\$v eq \$x / \$t)->solve(qw(x in terms of v t))  ==  \$v*\$t  );
ok(   (\$v eq \$x / \$t)->solve(qw(x in terms of v t))  !=  \$v/\$t  );
ok(   (\$v eq \$x / \$t)->solve(qw(x in terms of v t)) <=> \$v*\$t );

```

<B>B>solve()<B>B> assumes that the equation on the left hand side is equal to zero, applies various simplifications, then attempts to rearrange the equation to obtain an equation for the first variable in the parameter list assuming that the other terms mentioned in the parameter list are known constants. There may of course be other unknown free variables in the equation to be solved: the proposed solution is automatically tested against the original equation to check that the proposed solution removes these variables, an error is reported via <B>B>die()<B>B> if it does not.

Example t/solve.t

```

#!perl -w -I..
#______________________________________________________________________
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::Simple tests => 2;

my (\$x) = symbols(qw(x));

my  \$p = \$x**2-5*\$x+6;        # Quadratic polynomial
my (\$a, \$b) = @{(\$p > \$x )};  # Solve for x

print "x=\$a,\$b\n";            # Roots

ok(\$a == 2);
ok(\$b == 3);

```

If there are multiple solutions, (as in the case of polynomials), <B>B>solve()<B>B> returns an array of symbolic expressions containing the solutions.

Methods for performing Calculus

Differentiation: <B>d()B>

Example t/differentiation.t

```

#!perl -w -I..
#______________________________________________________________________
# Symbolic algebra.
#______________________________________________________________________

use Math::Algebra::Symbols;
use Test::More tests => 5;

\$x = symbols(qw(x));

ok(  sin(\$x)    ==  sin(\$x)->d->d->d->d);
ok(  cos(\$x)    ==  cos(\$x)->d->d->d->d);
ok(  exp(\$x)    ==  exp(\$x)->d(\$x)->d(x)->d->d);
ok( (1/\$x)->d   == -1/\$x**2);
ok(  exp(\$x)->d->d->d->d <=> exp(\$x) );

```

<B>d()B> differentiates the equation on the left hand side by the named variable.

The variable to be differentiated by may be explicitly specified, either as a string or as single symbol; or it may be heuristically guessed as follows:

If the equation to be differentiated refers to only one symbol, then that symbol is used. If several symbols are present in the equation, but only one of <B>tB>, <B>xB>, <B>yB>, <B>zB> is present, then that variable is used in honor of Newton, Leibnitz, Cauchy.

#### Example of Equation Solving: the focii of a hyperbola:

```

use Math::Algebra::Symbols;

my (\$a, \$b, \$x, \$y, \$i, \$o) = symbols(qw(a b x y i 1));

print
"Hyperbola: Constant difference between distances from focii to locus of y=1/x",
"\n  Assume by symmetry the focii are on ",
"\n    the line y=x:                     ",  \$f1 = \$x + \$i * \$x,
"\n  and equidistant from the origin:    ",  \$f2 = -\$f1,
"\n  Choose a convenient point on y=1/x: ",  \$a = \$o+\$i,
"\n        and a general point on y=1/x: ",  \$b = \$y+\$i/\$y,
"\n  Difference in distances from focii",
"\n    From convenient point:            ",  \$A = abs(\$a - \$f2) - abs(\$a - \$f1),
"\n    From general point:               ",  \$B = abs(\$b - \$f2) + abs(\$b - \$f1),
"\n\n  Solving for x we get:            x=", (\$A - \$B) > \$x,
"\n                         (should be: sqrt(2))",
"\n  Which is indeed constant, as was to be demonstrated\n";

```

This example demonstrates the power of symbolic processing by finding the focii of the curve <B>y=1/xB>, and incidentally, demonstrating that this curve is a hyperbola.

### EXPORTS

```

use Math::Algebra::Symbols
symbols=>S,
trig   => 1,
hyper  => 1,
complex=> 1;

```
trig=>0 The default, do not export trigonometric functions.
trig=>1 Export trigonometric functions: <B>tanB>, <B>secB>, <B>cscB>, <B>cotB> to the caller’s namespace. <B>sinB>, <B>cosB> are created by default by overloading the existing Perl <B>sinB> and <B>cosB> operators.
<B>trigonometricB> Alias of <B>trigB>
hyperbolic=>0 The default, do not export hyperbolic functions.
hyper=>1 Export hyperbolic functions: <B>sinhB>, <B>coshB>, <B>tanhB>, <B>sechB>, <B>cschB>, <B>cothB> to the caller’s namespace.
<B>hyperbolicB> Alias of <B>hyperB>
complex=>0 The default, do not export complex functions
complex=>1 Export complex functions: <B>conjugateB>, <B>crossB>, <B>dotB>, <B>imB>, <B>modulusB>, <B>reB>, <B>unitB> to the caller’s namespace.

### PACKAGES

The <B>SymbolsB> packages manipulate a sum of products representation of an algebraic equation. The <B>SymbolsB> package is the user interface to the functionality supplied by the <B>Symbols::SumB> and <B>Symbols::TermB> packages.

#### Math::Algebra::Symbols::Term

<B>Symbols::TermB> represents a product term. A product term consists of the number <B>1B>, optionally multiplied by:
Variables any number of variables raised to integer powers,
Coefficient An integer coefficient optionally divided by a positive integer divisor, both represented as BigInts if necessary.
Sqrt The sqrt of of any symbolic expression representable by the <B>SymbolsB> package, including minus one: represented as <B>iB>.
Reciprocal The multiplicative inverse of any symbolic expression representable by the <B>SymbolsB> package: i.e. a <B>SymbolsTermB> may be divided by any symbolic expression representable by the <B>SymbolsB> package.
Exp The number <B>eB> raised to the power of any symbolic expression representable by the <B>SymbolsB> package.
Log The logarithm to base <B>eB> of any symbolic expression representable by the <B>SymbolsB> package.
Thus <B>SymbolsTermB> can represent expressions like:

```

2/3*\$x**2*\$y**-3*exp(\$i*\$pi)*sqrt(\$z**3) / \$x

```

but not:

```

\$x + \$y

```

for which package <B>Symbols::SumB> is required.

#### Math::Algebra::Symbols::Sum

<B>Symbols::SumB> represents a sum of product terms supplied by <B>Symbols::TermB> and thus behaves as a polynomial. Operations such as equation solving and differentiation are applied at this level.

The main benefit of programming <B>Symbols::TermB> and <B>Symbols::SumB> as two separate but related packages is Object Oriented Polymorphism. I.e. both packages need to multiply items together: each package has its own <B>multiplyB> method, with Perl method lookup selecting the appropriate one as required.

#### Math::Algebra::Symbols

Packaging the user functionality alone and separately in package <B>SymbolsB> allows the internal functions to be conveniently hidden from user scripts.

### AUTHOR

Philip R Brenan at philiprbrenan@yahoo.com

#### Credits

Author

philiprbrenan@yahoo.com, 2004