Jonathan Worthington > Math-Calculus-Expression-0.2.2 > Math::Calculus::Expression

Dependencies

Annotate this POD

# CPAN RT

 New 2 Open 0
View/Report Bugs
Module Version: 0.2.2

# 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```

# DESCRIPTION

This module can take an algebraic expression, parse it into a tree structure, simplify the tree, substitute variables and named constants for other variables or constants (which may be numeric), numerically evaluate the tree and turn the tree back into an output of the same form as the input.

It supports a wide range of expressions including the +, -, *, / and ^ (raise to power) operators, bracketed expressions to enable correct precedence and the functions ln, exp, sin, cos, tan, sec, cosec, cot, sinh, cosh, tanh, sech, cosech, coth, asin, acos, atan, asinh, acosh and atanh.

None by default.

# METHODS

new
`  \$exp = Math::Calculus::Expression->new;`

Creates a new instance of the expression object, which can hold an individual expression and perform basic operations on it.

addVariable
`  \$exp->addVariable('x');`

Sets a certain named value in the expression as being a variable. A named value must be an alphabetic chracter.

setExpression
`  \$exp->setExpression('x^2 + 5*x);`

Takes an expression in human-readable form and stores it internally as a tree structure, checking it is a valid expression that the module can understand in the process. Note that the module is strict about syntax. For example, note above that you must write 5*x and not just 5x. Whitespace is allowed in the expression, but does not have any effect on precedence. If you require control of precedence, use brackets; bracketed expressions will always be evaluated first, as you would normally expect. The module follows the BODMAS precedence convention. Returns undef on failure and a true value on success.

getExpression
`  \$expr = \$exp->getExpression;`

Returns a textaul, human readable representation of the expression that is being stored.

simplify
`  \$exp->simplify;`

Attempts to simplify the expression that is stored internally.

evaluate
`  \$exp->evaluate(x => 0.5, a => 4);`

This method 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. It fails and returns undef if it finds letters that have no mapping or an error such as division by zero occurs during the evaluation.

sameRepresentation
`  \$same = \$exp->sameRepresentation(\$exp2);`

The sameRepresentation method takes another expression object as its parameter and returns true if that expression has the same internal representation as the expression the method is invoked on. Be careful – while it can be said that if two expressions have the same representation they are equal, it would be wrong to say that if they have different representations they are not equal. It is clear to see that "x + 2" and "2 + x" are equal, but their internal representation may well differ.

clone
`  \$expCopy = \$exp->clone;`

The clone method returns a deep copy of the expression object (deep copy meaning that if the original is modified the copy will not be affected and vice versa).

getTraceback
`  \$exp->getTraceback;`

When setExpression and differentiate are called, a traceback is generated to describe what these functions did. If an error occurs, this traceback can be extremely useful in helping track down the source of the error.

getError
`  \$exp->getError;`

When any method other than getTraceback is called, the error message stored is cleared, and then any errors that occur during the execution of the method are 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. The first letter of the name of the method invoked is capitalized, then a module by that name is loaded (if it exists) and 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.

# SEE ALSO

The author of this module has a website at http://www.jwcs.net/~jonathan/, which has the latest news about the module and a web-based frontend to allow you to try out this module and, more specifically, its subclasses.

# AUTHOR

Jonathan Worthington, <jonathan@jwcs.net>

# COPYRIGHT AND LICENSE

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.

syntax highlighting: