com.singularsys.jep.walkers

## Class SubstitutionVisitor

- java.lang.Object
- - com.singularsys.jep.walkers.DoNothingVisitor
  - - com.singularsys.jep.walkers.SubstitutionVisitor

- All Implemented Interfaces:
    JepComponent, ParserVisitor, java.io.Serializable

      public class SubstitutionVisitor
      extends DoNothingVisitor

  Allows substitution of a given variable with an expression tree. For example

       Jep jep = new Jep();
       Node node = jep.parse("x^2+x");
       SubstitutionVisitor sv = new SubstitutionVisitor(jep);
       Node sub = jep.parse("sin(y)");
       Node res = sv.substitute(node,"x",sub);

  Will give the expression "(sin(y))^2+sin(y)".
  Author:
    Rich Morris Created on 16-Nov-2003
  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Operator` | `assign`
    `protected DeepCopyVisitor` | `dcv`

    - ### Fields inherited from class com.singularsys.jep.walkers.DoNothingVisitor

      `ft, jep, nf, ot, vt`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SubstitutionVisitor()` Constructor to uses as a JepComponent.
    `SubstitutionVisitor(Jep j)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `init(Jep j)` Initialize the component.
    `Node` | `substitute(Node orig, Node sub)` Substitutes into orig the equation given by sub
    `Node` | `substitute(Node orig, Node[] subs)`
    `Node` | `substitute(Node orig, java.lang.String[] names1, Node[] replacements1)` Substitutes all occurrences of a set of variable var with a set of replacements.
    `Node` | `substitute(Node orig, java.lang.String[] names1, java.lang.Object[] values)` Substitute a set of names with a set of values.
    `Node` | `substitute(Node orig, java.lang.String[] oldNames, java.lang.String[] newNames)` Substitute a set of variable names with a new set of names.
    `Node` | `substitute(Node orig, java.lang.String name, Node replacement)` Substitutes all occurrences of variable var with replacement.
    `Node` | `substitute(Node orig, java.lang.String name, java.lang.Object value)` Substitute a single variable with a given value.
    `java.lang.Object` | `visit(ASTVarNode node, java.lang.Object data)` Visit a variable node.

    - ### Methods inherited from class com.singularsys.jep.walkers.DoNothingVisitor

      `childrenHaveChanged, copyChildrenIfNeeded, getFunctionTable, getLightWeightInstance, getNodeFactory, getOperatorTable, getVariableTable, visit, visit, visit, visit, visitChildren, visitNode`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### assign

          protected Operator assign

    - #### dcv

          protected DeepCopyVisitor dcv

  - ### Constructor Detail

    - #### SubstitutionVisitor

          public SubstitutionVisitor(Jep j)

    - #### SubstitutionVisitor

          public SubstitutionVisitor()

      Constructor to uses as a JepComponent. `init(Jep)` method must be called to set the jep instance.
      Since:
        3.4.0

  - ### 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

    - #### substitute

          public Node substitute(Node orig,
                                 java.lang.String name,
                                 Node replacement)
                          throws ParseException

      Substitutes all occurrences of variable var with replacement. Does not do a DeepCopy.
      Parameters:
        `orig` - the expression we wish to perform the substitution on
        `name` - the name of the variable
        `replacement` - the expression var is substituted for
      Returns:
        the tree with variable replace (does not do a DeepCopy)
      Throws:
        `ParseException`

    - #### substitute

          public Node substitute(Node orig,
                                 Node sub)
                          throws ParseException

      Substitutes into orig the equation given by sub
      Parameters:
        `orig` - the equation to substitute into
        `sub` - and equation of the form x=....
      Returns:
        orig after substitution
      Throws:
        `ParseException` - if sub is of the wrong form

    - #### substitute

          public Node substitute(Node orig,
                                 Node[] subs)
                          throws ParseException

      Throws:
        `ParseException`

    - #### substitute

          public Node substitute(Node orig,
                                 java.lang.String[] names1,
                                 Node[] replacements1)
                          throws ParseException

      Substitutes all occurrences of a set of variable var with a set of replacements. Does not do a DeepCopy.
      Parameters:
        `orig` - the expression we wish to perform the substitution on
        `names1` - the names of the variable
        `replacements1` - the expression var is substituted for
      Returns:
        the tree with variable replace (does not do a DeepCopy)
      Throws:
        `ParseException`

    - #### substitute

          public Node substitute(Node orig,
                                 java.lang.String[] names1,
                                 java.lang.Object[] values)
                          throws ParseException

      Substitute a set of names with a set of values.
      Parameters:
        `orig` -
        `names1` -
        `values` -
      Returns:
        node with the substitution performed
      Throws:
        `ParseException`

    - #### substitute

          public Node substitute(Node orig,
                                 java.lang.String name,
                                 java.lang.Object value)
                          throws ParseException

      Substitute a single variable with a given value.
      Parameters:
        `orig` - equation to perform substitution on
        `name` - variable name
        `value` - value to use
      Returns:
        node with the substitution performed
      Throws:
        `ParseException`
      Since:
        3.4.0

    - #### substitute

          public Node substitute(Node orig,
                                 java.lang.String[] oldNames,
                                 java.lang.String[] newNames)
                          throws ParseException

      Substitute a set of variable names with a new set of names.
      Parameters:
        `orig` -
        `oldNames` - the old variable name
        `newNames` - the new variable names
      Returns:
        node with the substitution performed
      Throws:
        `ParseException`

    - #### visit

          public java.lang.Object visit(ASTVarNode node,
                                        java.lang.Object data)
                                 throws JepException

      Description copied from class: `DoNothingVisitor`
      Visit a variable node. Can be overridden by sub-classes.
      Specified by:
        `visit` in interface `ParserVisitor`
      Overrides:
        `visit` in class `DoNothingVisitor`
      Throws:
        `JepException`

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