Package com.singularsys.extensions.djep
Class DifferentiationVisitor
- java.lang.Object
-
- com.singularsys.jep.walkers.DoNothingVisitor
-
- com.singularsys.jep.walkers.DeepCopyVisitor
-
- com.singularsys.extensions.djep.DifferentiationVisitor
-
- All Implemented Interfaces:
JepComponent,ParserVisitor,java.io.Serializable
- Direct Known Subclasses:
MatrixDifferentiationVisitor
public class DifferentiationVisitor extends DeepCopyVisitor
A class for performing differentiation of an expression. To use doJep j = ...; Node in = ...; DifferentiationVisitor dv = new DifferentiationVisitor(jep); dv.addStandardDiffRules(); Node out = dv.differentiate(in,"x");
The class follows the visitor pattern described inParserVisitor. The rules for differentiating specific functions are contained in object which implementDiffRulesIA number of inner classes which use this interface are defined for specific function types. In particularMacroDiffRulesallow the rule for differentiation to be specified by strings. New rules can be added usingDJep.addDiffRule(com.singularsys.extensions.djep.DiffRulesI)method.- Author:
- R Morris Created on 19-Jun-2003
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DifferentiationVisitor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Nodedifferentiate(Node node, java.lang.String var)Differentiates an expression tree wrt a variable var.DiffRulesIgetDiffRule(java.lang.String name)finds the rule for function with given name.voidinit(Jep j)Initialize the component.booleanisConstantVar(XVariable var)voidprintDiffRules()Prints all the differentiation rules for all functions on System.out.voidprintDiffRules(java.io.PrintStream out)Prints all the differentiation rules for all functions on specified stream.java.lang.Objectvisit(ASTConstant node, java.lang.Object data)Differentiates a constant.java.lang.Objectvisit(ASTFunNode node, java.lang.Object data)Applies differentiation to a function.java.lang.Objectvisit(ASTOpNode node, java.lang.Object data)Now the recursive calls to differentiate the treejava.lang.Objectvisit(ASTVarNode node, java.lang.Object data)Differentiates a variable.-
Methods inherited from class com.singularsys.jep.walkers.DeepCopyVisitor
deepCopy
-
Methods inherited from class com.singularsys.jep.walkers.DoNothingVisitor
childrenHaveChanged, copyChildrenIfNeeded, getFunctionTable, getLightWeightInstance, getNodeFactory, getOperatorTable, getVariableTable, visit, visitChildren, visitNode
-
-
-
-
Method Detail
-
init
public void init(Jep j)
Description copied from interface:JepComponentInitialize the component. This methods is called whenever a component is added to Jep. Hence it allows components to keep track of the other components they may rely on.- Specified by:
initin interfaceJepComponent- Overrides:
initin classDoNothingVisitor- Parameters:
j- the current Jep instance
-
getDiffRule
public DiffRulesI getDiffRule(java.lang.String name)
finds the rule for function with given name.
-
printDiffRules
public void printDiffRules()
Prints all the differentiation rules for all functions on System.out.
-
printDiffRules
public void printDiffRules(java.io.PrintStream out)
Prints all the differentiation rules for all functions on specified stream.
-
differentiate
public Node differentiate(Node node, java.lang.String var) throws ParseException
Differentiates an expression tree wrt a variable var.- Parameters:
node- the top node of the expression treevar- 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(ASTOpNode node, java.lang.Object data) throws JepException
Now the recursive calls to differentiate the tree- Specified by:
visitin interfaceParserVisitor- Overrides:
visitin classDeepCopyVisitor- Throws:
JepException
-
visit
public java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws JepException
Applies differentiation to a function. Used the rules specified by objects of typeDiffRulesI.- Specified by:
visitin interfaceParserVisitor- Overrides:
visitin classDeepCopyVisitor- Parameters:
node- The node of the function.data- The variable to differentiate wrt.- Throws:
JepException
-
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:
visitin interfaceParserVisitor- Overrides:
visitin classDeepCopyVisitor- Returns:
- a constant node with value 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:
visitin interfaceParserVisitor- Overrides:
visitin classDeepCopyVisitor- Returns:
- 0 derivatives of constants are always zero.
- Throws:
ParseException
-
-