Package com.singularsys.extensions.djep
Class DifferentiationVisitor
java.lang.Object
com.singularsys.jep.walkers.GenericDoNothingVisitor<String>
com.singularsys.jep.walkers.GenericDeepCopyVisitor<String>
com.singularsys.extensions.djep.DifferentiationVisitor
- All Implemented Interfaces:
GenericVisitor<Node,,String, ParseException> JepComponent,Serializable
- Direct Known Subclasses:
MatrixDifferentiationVisitor
A class for performing differentiation 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(com.singularsys.extensions.djep.DiffRulesI) method.- Author:
- R Morris Created on 19-Jun-2003
- See Also:
-
Field Summary
FieldsFields inherited from class com.singularsys.jep.walkers.GenericDoNothingVisitor
ft, nf, ot -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondifferentiate(Node node, String varName) Differentiates an expression tree wrt a variable var.getDiffRule(String name) finds the rule for function with given name.voidInitialize the component.booleanisConstantVar(XVariable var) voidPrints all the differentiation rules for all functions on System.out.voidPrints all the differentiation rules for all functions on specified stream.visit(ASTConstant node, String varName) Differentiates a constant.visit(ASTFunNode node, String varName) Applies differentiation to a function.Now the recursive calls to differentiate the treevisit(ASTVarNode node, String varName) Differentiates a variable.Methods inherited from class com.singularsys.jep.walkers.GenericDeepCopyVisitor
getLightWeightInstance, visitMethods inherited from class com.singularsys.jep.walkers.GenericDoNothingVisitor
childrenHaveChanged, copyChildrenIfNeeded, copyChildrenIfNeeded, makeExceptionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.singularsys.jep.GenericVisitor
makeException, visitChildren
-
Field Details
-
djep
-
tu
-
-
Constructor Details
-
DifferentiationVisitor
public DifferentiationVisitor()
-
-
Method Details
-
init
Description copied from interface:JepComponentInitialize the component. This method 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 classGenericDoNothingVisitor<String>- Parameters:
j- the current Jep instance
-
getDiffRule
finds the rule for function with given name. -
printDiffRules
public void printDiffRules()Prints all the differentiation rules for all functions on System.out. -
printDiffRules
Prints all the differentiation rules for all functions on specified stream. -
differentiate
Differentiates an expression tree wrt a variable var.- Parameters:
node- the top node of the expression treevarName- the name of 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.IllegalArgumentException
-
visit
Now the recursive calls to differentiate the tree- Specified by:
visitin interfaceGenericVisitor<Node,String, ParseException> - Overrides:
visitin classGenericDeepCopyVisitor<String>- Parameters:
node- current nodevar- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Applies differentiation to a function. Used the rules specified by objects of typeDiffRulesI.- Specified by:
visitin interfaceGenericVisitor<Node,String, ParseException> - Overrides:
visitin classGenericDeepCopyVisitor<String>- Parameters:
node- The node of the function.varName- The variable to differentiate wrt.- Returns:
- the result of visiting the node
- Throws:
ParseException
-
isConstantVar
-
visit
Differentiates a variable. May want to alter behaviour when using multi equation as diff(f,x) might not be zero.- Specified by:
visitin interfaceGenericVisitor<Node,String, ParseException> - Overrides:
visitin classGenericDeepCopyVisitor<String>- Parameters:
node- current nodevarName- context data- 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
Differentiates a constant.- Specified by:
visitin interfaceGenericVisitor<Node,String, ParseException> - Overrides:
visitin classGenericDeepCopyVisitor<String>- Parameters:
node- current nodevarName- context data- Returns:
- 0 derivatives of constants are always zero.
- Throws:
ParseException
-