Evalutate a parsed mathematical expression. More...
Public Member Functions | |
__construct ($variables=null) | |
Constructor. More... | |
parseRational ($value) | |
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... | |
Static Public Member Functions | |
static | ifactor ($n) |
Integer factorization. More... | |
static | powerFreeFactorization ($n, $d) |
Compute a power free integer factorization: n = pq^d, where p is d-power free. More... | |
Private Member Functions | |
isInteger ($value) | |
isSignedInteger ($value) | |
rpow ($a, $b) | |
Private Attributes | |
$variables | |
mixed[] $variables Key/value pair holding current values of the variables used for evaluating. | |
Static Private Attributes | |
static | $sieve = array() |
Private cache for prime sieve. | |
Evalutate a parsed mathematical expression.
Implementation of a Visitor, transforming an AST into a rational 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.
TODO: handle user specified functions
MathParser\Interpreting\RationalEvaluator::__construct | ( | $variables = null | ) |
Constructor.
Create an Evaluator with given variable values.
mixed | $variables | key-value array of variables with corresponding values. |
|
static |
Integer factorization.
Computes an integer factorization of $n using trial division and a cached sieve of computed primes
type | var Description |
|
static |
Compute a power free integer factorization: n = pq^d, where p is d-power free.
The function returns an array: [ 'square' => q, 'nonSquare' => p ]
int | $n | input |
MathParser\Interpreting\RationalEvaluator::setVariables | ( | $variables | ) |
Update the variables used for evaluating.
array | $variables | Key/value pair holding current variable values |
void |
MathParser\Interpreting\RationalEvaluator::visitConstantNode | ( | ConstantNode | $node | ) |
Evaluate a ConstantNode.
Returns the value of a ConstantNode recognized by StdMathLexer and StdMathParser.
UnknownConstantException | if the variable respresented by the ConstantNode is not recognized. |
ConstantNode | $node | AST to be evaluated |
float |
Implements MathParser\Interpreting\Visitors\Visitor.
MathParser\Interpreting\RationalEvaluator::visitExpressionNode | ( | ExpressionNode | $node | ) |
Evaluate an ExpressionNode.
Computes the value of an ExpressionNode x op y
where op
is one of +
, -
, *
, /
or ^
UnknownOperatorException | if the operator is something other than + , - , * , / or ^ |
ExpressionNode | $node | AST to be evaluated |
float |
Implements MathParser\Interpreting\Visitors\Visitor.
MathParser\Interpreting\RationalEvaluator::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.
UnknownFunctionException | if the function respresented by the FunctionNode is not recognized. |
FunctionNode | $node | AST to be evaluated |
float |
Implements MathParser\Interpreting\Visitors\Visitor.
MathParser\Interpreting\RationalEvaluator::visitIntegerNode | ( | IntegerNode | $node | ) |
Interface function for visiting IntegerNodes.
IntegerNode | $node | Node to visit. |
Implements MathParser\Interpreting\Visitors\Visitor.
MathParser\Interpreting\RationalEvaluator::visitNumberNode | ( | NumberNode | $node | ) |
Evaluate a NumberNode.
Retuns the value of an NumberNode
NumberNode | $node | AST to be evaluated |
float |
Implements MathParser\Interpreting\Visitors\Visitor.
MathParser\Interpreting\RationalEvaluator::visitRationalNode | ( | RationalNode | $node | ) |
Interface function for visiting RationalNodes.
RationalNode | $node | Node to visit. |
Implements MathParser\Interpreting\Visitors\Visitor.
MathParser\Interpreting\RationalEvaluator::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.
UnknownVariableException | if the variable respresented by the VariableNode is not set. |
VariableNode | $node | AST to be evaluated |
float |
Implements MathParser\Interpreting\Visitors\Visitor.