com.singularsys.jep.walkers
Class SubstitutionVisitor

java.lang.Object
  extended by com.singularsys.jep.walkers.DoNothingVisitor
      extended by com.singularsys.jep.walkers.SubstitutionVisitor
All Implemented Interfaces:
ParserVisitor

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

Field Summary
protected  Operator assign
           
protected  DeepCopyVisitor dcv
           
 
Fields inherited from class com.singularsys.jep.walkers.DoNothingVisitor
ft, jep, nf, ot, vt
 
Constructor Summary
SubstitutionVisitor(Jep j)
           
 
Method Summary
 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[] names, Node[] replacements)
          Substitutes all occurrences of a set of variable var with a set of replacements.
 Node substitute(Node orig, java.lang.String[] names, java.lang.Object[] values)
          Substitute a set of names with a set of values.
 Node substitute(Node orig, java.lang.String name, Node replacement)
          Substitutes all occurrences of variable var with replacement.
 java.lang.Object visit(ASTVarNode node, java.lang.Object data)
          Visit a variable node.
 
Methods inherited from class com.singularsys.jep.walkers.DoNothingVisitor
copyChildrenIfNeeded, 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)
Method Detail

substitute

public Node substitute(Node orig,
                       java.lang.String name,
                       Node replacement)
                throws JepException
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:
JepException

substitute

public Node substitute(Node orig,
                       Node sub)
                throws JepException
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:
JepException - if sub is of the wrong form

substitute

public Node substitute(Node orig,
                       Node[] subs)
                throws JepException
Throws:
JepException

substitute

public Node substitute(Node orig,
                       java.lang.String[] names,
                       Node[] replacements)
                throws JepException
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
names - the names of the variable
replacements - the expression var is substituted for
Returns:
the tree with variable replace (does not do a DeepCopy)
Throws:
JepException

substitute

public Node substitute(Node orig,
                       java.lang.String[] names,
                       java.lang.Object[] values)
                throws JepException
Substitute a set of names with a set of values.

Parameters:
orig -
names -
values -
Returns:
node with the substitution performed
Throws:
JepException

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 buy sub-classes.

Specified by:
visit in interface ParserVisitor
Overrides:
visit in class DoNothingVisitor
Throws:
JepException


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