MathParser\Interpreting\Differentiator Class Reference

Differentiate an abstract syntax tree (AST). More...

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

Public Member Functions

 __construct ($variable)
 Class constructor. More...
 
 visitExpressionNode (ExpressionNode $node)
 Differentiate an ExpressionNode. More...
 
 visitNumberNode (NumberNode $node)
 Differentiate a NumberNode. More...
 
 visitIntegerNode (IntegerNode $node)
 Interface function for visiting IntegerNodes. More...
 
 visitRationalNode (RationalNode $node)
 Interface function for visiting RationalNodes. More...
 
 visitVariableNode (VariableNode $node)
 Differentiate a VariableNode. More...
 
 visitFunctionNode (FunctionNode $node)
 Differentiate a FunctionNode. More...
 
 visitConstantNode (ConstantNode $node)
 Differentiate a ConstantNode. More...
 

Protected Attributes

 $variable
 Variable that we differentiate with respect to.
 
 $nodeFactory
 NodeFactory $nodeFactory used for building the resulting AST. More...
 

Detailed Description

Differentiate an abstract syntax tree (AST).

Implementation of a Visitor, transforming an AST into another AST representing the derivative of the original AST.

The class implements differentiation rules for all arithmetic operators as well as every elementary function recognized by StdMathLexer and StdmathParser, handling for example the product rule and the chain rule correctly.

To keep the resulting AST reasonably simple, a number of simplification rules are built in.

Example:

$parser = new StdMathParser();
$f = $parser->parse('exp(2x)+xy');
$ddx = new Differentiator('x'); // Create a d/dx operator
$df = $f->accept($ddx); // $df now contains the AST of '2exp(2x)+y'

TODO: handle user specified functions

Constructor & Destructor Documentation

MathParser\Interpreting\Differentiator::__construct (   $variable)

Class constructor.

Parameters
string$variableDifferentiation variable

Member Function Documentation

MathParser\Interpreting\Differentiator::visitConstantNode ( ConstantNode  $node)

Differentiate a ConstantNode.

Create a NumberNode representing '0'. (The derivative of a constant is indentically 0).

Parameters
ConstantNode$nodeAST to be differentiated
Return values
Node

Implements MathParser\Interpreting\Visitors\Visitor.

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

Differentiate an ExpressionNode.

Using the usual rules for differentiating, create an ExpressionNode giving an AST correctly representing the derivative (x op y)' where op is one of +, -, *, / or ^

Differentiation rules:

  • \( (f+g)' = f' + g' \)
  • \( (f-g) ' = f' - g' \)
  • \( (-f)' = -f' \)
  • \( (f*g)' = f'g + f g' \)
  • \( (f/g)' = (f' g - f g')/g^2 \)
  • \( (f^g)' = f^g (g' \log(f) + g/f) \) with a simpler expression when g is a NumberNode
Exceptions
UnknownOperatorExceptionif the operator is something other than +, -, *, / or ^
Parameters
ExpressionNode$nodeAST to be differentiated
Return values
Node

Implements MathParser\Interpreting\Visitors\Visitor.

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

Differentiate a FunctionNode.

Create an ExpressionNode giving an AST correctly representing the derivative f' where f is an elementary function.

Differentiation rules:

  • \( \sin(f(x))' = f'(x) \cos(f(x)) \)
  • \( \cos(f(x))' = -f'(x) \sin(f(x)) \)
  • \( \tan(f(x))' = f'(x) (1 + \tan(f(x))^2 \)
  • \( \operatorname{cot}(f(x))' = f'(x) (-1 - \operatorname{cot}(f(x))^2 \)
  • \( \arcsin(f(x))' = f'(x) / \sqrt{1-f(x)^2} \)
  • \( \arccos(f(x))' = -f'(x) / \sqrt{1-f(x)^2} \)
  • \( \arctan(f(x))' = f'(x) / (1+f(x)^2) \)
  • \( \operatorname{arccot}(f(x))' = -f'(x) / (1+f(x)^2) \)
  • \( \exp(f(x))' = f'(x) \exp(f(x)) \)
  • \( \log(f(x))' = f'(x) / f(x) \)
  • \( \ln(f(x))' = f'(x) / (\log(10) * f(x)) \)
  • \( \sqrt{f(x)}' = f'(x) / (2 \sqrt{f(x)} \)
  • \( \sinh(f(x))' = f'(x) \cosh(f(x)) \)
  • \( \cosh(f(x))' = f'(x) \sinh(f(x)) \)
  • \( \tanh(f(x))' = f'(x) (1-\tanh(f(x))^2) \)
  • \( \operatorname{coth}(f(x))' = f'(x) (1-\operatorname{coth}(f(x))^2) \)
  • \( \operatorname{arsinh}(f(x))' = f'(x) / \sqrt{f(x)^2+1} \)
  • \( \operatorname{arcosh}(f(x))' = f'(x) / \sqrt{f(x)^2-1} \)
  • \( \operatorname{artanh}(f(x))' = f'(x) (1-f(x)^2) \)
  • \( \operatorname{arcoth}(f(x))' = f'(x) (1-f(x)^2) \)
Exceptions
UnknownFunctionExceptionif the function name doesn't match one of the above
Parameters
FunctionNode$nodeAST to be differentiated
Return values
Node

Implements MathParser\Interpreting\Visitors\Visitor.

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

Interface function for visiting IntegerNodes.

Parameters
IntegerNode$nodeNode to visit.

Implements MathParser\Interpreting\Visitors\Visitor.

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

Differentiate a NumberNode.

Create a NumberNode representing '0'. (The derivative of a constant is indentically 0).

Parameters
NumberNode$nodeAST to be differentiated
Return values
Node

Implements MathParser\Interpreting\Visitors\Visitor.

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

Interface function for visiting RationalNodes.

Parameters
RationalNode$nodeNode to visit.

Implements MathParser\Interpreting\Visitors\Visitor.

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

Differentiate a VariableNode.

Create a NumberNode representing '0' or '1' depending on the differetiation variable.

Parameters
NumberNode$nodeAST to be differentiated
Return values
Node

Implements MathParser\Interpreting\Visitors\Visitor.

Member Data Documentation

MathParser\Interpreting\Differentiator::$nodeFactory
protected

NodeFactory $nodeFactory used for building the resulting AST.


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