org.lsmp.djep.djep
Class DifferentiationVisitor

java.lang.Object
  extended by org.lsmp.djep.xjep.DoNothingVisitor
      extended by org.lsmp.djep.xjep.DeepCopyVisitor
          extended by org.lsmp.djep.djep.DifferentiationVisitor
All Implemented Interfaces:
ParserVisitor

public class DifferentiationVisitor
extends DeepCopyVisitor

A class for performing differentation of an expression. To use do

 JEP j = ...; Node in = ...;
 DifferentiationVisitor dv = new DifferentiationVisitor(jep);
 dv.addStandardDiffRules();
 Node out = dv.differentiate(in,"x");
 
The class follows the visitor pattern described in ParserVisitor. The rules for differentiating specific functions are contained in object which implement DiffRulesI A number of inner classes which use this interface are defined for specific function types. In particular MacroDiffRules allow the rule for differentiation to be specified by strings. New rules can be added using DJep.addDiffRule(org.lsmp.djep.djep.DiffRulesI) method.

Author:
R Morris Created on 19-Jun-2003

Constructor Summary
DifferentiationVisitor(DJep jep)
          Construction with a given set of tree utilities
 
Method Summary
 Node differentiate(Node node, java.lang.String var, DJep djep)
          Differentiates an expression tree wrt a variable var.
 boolean isConstantVar(XVariable var)
           
 void printDiffRules()
          Prints all the differentation rules for all functions on System.out.
 void printDiffRules(java.io.PrintStream out)
          Prints all the differentation rules for all functions on specified stream.
 java.lang.Object visit(ASTConstant node, java.lang.Object data)
          Differentiates a constant.
 java.lang.Object visit(ASTFunNode node, java.lang.Object data)
          Applies differentiation to a function.
 java.lang.Object visit(ASTVarNode node, java.lang.Object data)
          Differentiates a variable.
 
Methods inherited from class org.lsmp.djep.xjep.DeepCopyVisitor
deepCopy
 
Methods inherited from class org.lsmp.djep.xjep.DoNothingVisitor
acceptChildrenAsArray, visit, visit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.nfunk.jep.ParserVisitor
visit, visit
 

Constructor Detail

DifferentiationVisitor

public DifferentiationVisitor(DJep jep)
Construction with a given set of tree utilities

Method Detail

printDiffRules

public void printDiffRules()
Prints all the differentation rules for all functions on System.out.


printDiffRules

public void printDiffRules(java.io.PrintStream out)
Prints all the differentation rules for all functions on specified stream.


differentiate

public Node differentiate(Node node,
                          java.lang.String var,
                          DJep djep)
                   throws ParseException,
                          java.lang.IllegalArgumentException
Differentiates an expression tree wrt a variable var.

Parameters:
node - the top node of the expression tree
var - the variable to differentiate wrt
Returns:
the top node of the differentiated expression
Throws:
ParseException - if some error occurred while trying to differentiate, for instance of no rule supplied for given function.
java.lang.IllegalArgumentException

visit

public java.lang.Object visit(ASTFunNode node,
                              java.lang.Object data)
                       throws ParseException
Applies differentiation to a function. Used the rules specified by objects of type DiffRulesI.

Specified by:
visit in interface ParserVisitor
Overrides:
visit in class DeepCopyVisitor
Parameters:
node - The node of the function.
data - The variable to differentiate wrt.
Throws:
ParseException

isConstantVar

public boolean isConstantVar(XVariable var)

visit

public java.lang.Object visit(ASTVarNode node,
                              java.lang.Object data)
                       throws ParseException
Differentiates a variable. May want to alter behaviour when using multi equation as diff(f,x) might not be zero.

Specified by:
visit in interface ParserVisitor
Overrides:
visit in class DeepCopyVisitor
Returns:
1 if the variable has the same name as data, 0 if the variable has a different name.
Throws:
ParseException

visit

public java.lang.Object visit(ASTConstant node,
                              java.lang.Object data)
                       throws ParseException
Differentiates a constant.

Specified by:
visit in interface ParserVisitor
Overrides:
visit in class DeepCopyVisitor
Returns:
0 derivatives of constants are always zero.
Throws:
ParseException


http://www.singularsys.com/jep Copyright © 2007 Singular Systems