Package 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 variable with values or expressions. For exampleJep jep = new Jep(); SubstitutionVisitor sv = new SubstitutionVisitor(jep); Node node = jep.parse("x^2+x"); Node sub = jep.parse("sin(y)"); Node res = sv.substitute(node,"x",sub);
Will give the expression "(sin(y))^2+sin(y)".Various different types of substitution are available.
- Substitute a variable by a value.
substitute(Node, String, Object)
- Substitute a variable by an expression
substitute(Node, String, Node)
- Substitute a set of variables by a set of values
substitute(Node, String[], Object[])
- Substitute a set of variables by a set of expressions
substitute(Node, String[], String[])
- Substitute using an expression of the form
x=z+3
substitute(Node, Node)
- Substitute using a set of expressions of the form
x=z+3
substitute(Node, Node[])
The methods which do multiple substitutions at the same time do not apply substitutions to the expressions which have been substituted. Hence if we try to use two substitutions
x=y+2
,y=z+3
the result of substituting intox^2+y^2
will be(y+2)^2+(z+3)^2
and not(z+3+2)^2+(z+1)^2
. This prevents possible infinite recursion. To completely substitute a set of expressions a loop could be used.Node base = jep.parse("x^2+y^2"); Node sub1 = jep.parse("x=y+2"); Node sub2 = jep.parse("y=z+3"); Node subs[] = new Node[] {sub1,sub2}; for(Node sub:subs) { base = sv.substitute(base, sub); }
Note that the order of substitution matters.- Author:
- Rich Morris Created on 16-Nov-2003
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected Operator
assign
protected DeepCopyVisitor
dcv
-
Constructor Summary
Constructors Constructor Description SubstitutionVisitor()
Constructor to uses as a JepComponent.SubstitutionVisitor(Jep j)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
init(Jep j)
Initialize the component.Node
substitute(Node orig, Node sub)
Substitutes intoorig
the equation given by subNode
substitute(Node orig, Node[] subs)
Substitute a set of equations into the original.Node
substitute(Node orig, java.lang.String[] names, Node[] replacements)
Substitutes all occurrences of a set of variables var with a set of replacements.Node
substitute(Node orig, java.lang.String[] names, java.lang.Object[] values)
Substitute all occurrences of variables with matching name with corresponding values.Node
substitute(Node orig, java.lang.String[] oldNames, java.lang.String[] newNames)
Renames a set of variables with a new set of names.Node
substitute(Node orig, java.lang.String name, Node replacement)
Substitutes all occurrences of variablevar
withreplacement
.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
-
-
-
-
Field Detail
-
assign
protected Operator assign
-
dcv
protected DeepCopyVisitor dcv
-
-
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
-
substitute
public Node substitute(Node orig, java.lang.String name, Node replacement) throws ParseException
Substitutes all occurrences of variablevar
withreplacement
. Does not do a DeepCopy.- Parameters:
orig
- the expression we wish to perform the substitution onname
- the name of the variablereplacement
- the expressionvar
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 intoorig
the equation given by sub- Parameters:
orig
- the equation to substitute intosub
- and equation of the formx=....
- Returns:
- a copy of
orig
after substitution - Throws:
ParseException
- if sub is of the wrong form
-
substitute
public Node substitute(Node orig, Node[] subs) throws ParseException
Substitute a set of equations into the original. For exampleNode base = jep.parse("x^2+y^2"); Node sub1 = jep.parse("x=y+2"); Node sub2 = jep.parse("y=z+3"); Node res = sv.substitute(base, new Node[] {sub1,sub2});
Node this method does not do two levels of substitution, so the result here is(y+2)^2+(z+3)^2
.- Parameters:
orig
- the expression to substitute intosubs
- a set of equations of the formx=...
- Returns:
- a copy of
orig
after substitution - Throws:
ParseException
-
substitute
public Node substitute(Node orig, java.lang.String[] names, Node[] replacements) throws ParseException
Substitutes all occurrences of a set of variables var with a set of replacements. Does not do a DeepCopy. For example
Node this method does not do two levels of substitution, so the result here isNode base = jep.parse("x^2+y^2"); Node sub1 = jep.parse("y+2"); Node sub2 = jep.parse("z+3"); Node res = sv.substitute(base, new String[]{"x","y"},new Node[] {sub1,sub2});
(y+2)^2+(z+3)^2
.- Parameters:
orig
- the expression we wish to perform the substitution onnames
- the names of the variablereplacements
- 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[] names, java.lang.Object[] values) throws ParseException
Substitute all occurrences of variables with matching name with corresponding values. For examplesubstitute(node, new String[]{"x","y"}, new Double[]{3.0,2.0});
- Parameters:
orig
- the equation to substitute intonames
- set of variable namesvalues
- set of 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. For examplesubstitute(node, "x", 3.0);
- Parameters:
orig
- equation to perform substitution onname
- variable namevalue
- 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
Renames a set of variables with a new set of names.- Parameters:
orig
- equation to perform substitution onoldNames
- the old variable namenewNames
- 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 interfaceParserVisitor
- Overrides:
visit
in classDoNothingVisitor
- Throws:
JepException
-
-