MathParser\Interpreting\Evaluator Class Reference

Evalutate a parsed mathematical expression. More...

Inheritance diagram for MathParser\Interpreting\Evaluator:
MathParser\Interpreting\Visitors\Visitor

Public Member Functions

 __construct ($variables=null)
 Constructor. More...
 
 setVariables ($variables)
 Update the variables used for evaluating. More...
 
 visitExpressionNode (ExpressionNode $node)
 Evaluate an ExpressionNode. More...
 
 visitNumberNode (NumberNode $node)
 Evaluate a NumberNode. More...
 
 visitIntegerNode (IntegerNode $node)
 Interface function for visiting IntegerNodes. More...
 
 visitRationalNode (RationalNode $node)
 Interface function for visiting RationalNodes. More...
 
 visitVariableNode (VariableNode $node)
 Evaluate a VariableNode. More...
 
 visitFunctionNode (FunctionNode $node)
 Evaluate a FunctionNode. More...
 
 visitConstantNode (ConstantNode $node)
 Evaluate a ConstantNode. More...
 

Private Attributes

 $variables
 mixed[] $variables Key/value pair holding current values of the variables used for evaluating.
 

Detailed Description

Evalutate a parsed mathematical expression.

Implementation of a Visitor, transforming an AST into a floating point number, giving the value of the expression represented by the AST.

The class implements evaluation of all all arithmetic operators as well as every elementary function and predefined constant recognized by StdMathLexer and StdmathParser.

Example:

$parser = new StdMathParser();
$f = $parser->parse('exp(2x)+xy');
$evaluator = new Evaluator();
$evaluator->setVariables([ 'x' => 1, 'y' => '-1' ]);
result = $f->accept($evaluator); // Evaluate $f using x=1, y=-1

TODO: handle user specified functions

Constructor & Destructor Documentation

MathParser\Interpreting\Evaluator::__construct (   $variables = null)

Constructor.

Create an Evaluator with given variable values.

Parameters
mixed$variableskey-value array of variables with corresponding values.

Member Function Documentation

MathParser\Interpreting\Evaluator::setVariables (   $variables)

Update the variables used for evaluating.

Parameters
array$variablesKey/value pair holding current variable values
Return values
void
MathParser\Interpreting\Evaluator::visitConstantNode ( ConstantNode  $node)

Evaluate a ConstantNode.

Returns the value of a ConstantNode recognized by StdMathLexer and StdMathParser.

See Also
MathParser::Lexer::StdMathLexer StdMathLexer
MathParser::StdMathParser StdMathParser
Exceptions
UnknownConstantExceptionif the variable respresented by the ConstantNode is not recognized.
Parameters
ConstantNode$nodeAST to be evaluated
Return values
float

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\Evaluator::visitExpressionNode ( ExpressionNode  $node)

Evaluate an ExpressionNode.

Computes the value of an ExpressionNode x op y where op is one of +, -, *, / or ^

Exceptions
UnknownOperatorExceptionif the operator is something other than +, -, *, / or ^
Parameters
ExpressionNode$nodeAST to be evaluated
Return values
float

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\Evaluator::visitFunctionNode ( FunctionNode  $node)

Evaluate a FunctionNode.

Computes the value of a FunctionNode f(x), where f is an elementary function recognized by StdMathLexer and StdMathParser.

See Also
MathParser::Lexer::StdMathLexer StdMathLexer
MathParser::StdMathParser StdMathParser
Exceptions
UnknownFunctionExceptionif the function respresented by the FunctionNode is not recognized.
Parameters
FunctionNode$nodeAST to be evaluated
Return values
float

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\Evaluator::visitIntegerNode ( IntegerNode  $node)

Interface function for visiting IntegerNodes.

Parameters
IntegerNode$nodeNode to visit.

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\Evaluator::visitNumberNode ( NumberNode  $node)

Evaluate a NumberNode.

Retuns the value of an NumberNode

Parameters
NumberNode$nodeAST to be evaluated
Return values
float

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\Evaluator::visitRationalNode ( RationalNode  $node)

Interface function for visiting RationalNodes.

Parameters
RationalNode$nodeNode to visit.

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\Evaluator::visitVariableNode ( VariableNode  $node)

Evaluate a VariableNode.

Returns the current value of a VariableNode, as defined either by the constructor or set using the Evaluator::setVariables() method.

See Also
Evaluator::setVariables() to define the variables
Exceptions
UnknownVariableExceptionif the variable respresented by the VariableNode is not set.
Parameters
VariableNode$nodeAST to be evaluated
Return values
float

Implements MathParser\Interpreting\Visitors\Visitor.


The documentation for this class was generated from the following file: