Class 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 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:
    Serialized Form
    • Constructor Detail

      • DifferentiationVisitor

        public DifferentiationVisitor()
    • 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 interface JepComponent
        Overrides:
        init in class DoNothingVisitor
        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 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
      • 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:
        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