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 implementDiffRulesI
A number of inner classes which use this interface are defined for specific function types. In particularMacroDiffRules
allow 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 Node
differentiate(Node node, java.lang.String var)
Differentiates an expression tree wrt a variable var.DiffRulesI
getDiffRule(java.lang.String name)
finds the rule for function with given name.void
init(Jep j)
Initialize the component.boolean
isConstantVar(XVariable var)
void
printDiffRules()
Prints all the differentiation rules for all functions on System.out.void
printDiffRules(java.io.PrintStream out)
Prints all the differentiation 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(ASTOpNode node, java.lang.Object data)
Now the recursive calls to differentiate the treejava.lang.Object
visit(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:JepComponent
Initialize 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:
init
in interfaceJepComponent
- Overrides:
init
in 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:
visit
in interfaceParserVisitor
- Overrides:
visit
in 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:
visit
in interfaceParserVisitor
- Overrides:
visit
in 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:
visit
in interfaceParserVisitor
- Overrides:
visit
in 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:
visit
in interfaceParserVisitor
- Overrides:
visit
in classDeepCopyVisitor
- Returns:
- 0 derivatives of constants are always zero.
- Throws:
ParseException
-
-