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
,Serializable
Allows substitution of variable with values or expressions.
For example
Jep 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 into x^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:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructor to uses as a JepComponent. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Initialize the component.substitute
(Node orig, Node sub) Substitutes intoorig
the equation given by subsubstitute
(Node orig, Node[] subs) Substitute a set of equations into the original.substitute
(Node orig, String[] names, Node[] replacements) Substitutes all occurrences of a set of variables var with a set of replacements.substitute
(Node orig, String[] names, Object[] values) Substitute all occurrences of variables with matching name with corresponding values.substitute
(Node orig, String[] oldNames, String[] newNames) Renames a set of variables with a new set of names.substitute
(Node orig, String name, Node replacement) Substitutes all occurrences of variablevar
withreplacement
.substitute
(Node orig, String name, Object value) Substitute a single variable with a given value.visit
(ASTVarNode node, 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 Details
-
assign
-
dcv
-
-
Constructor Details
-
SubstitutionVisitor
-
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 Details
-
init
Description copied from interface:JepComponent
Initialize 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:
init
in interfaceJepComponent
- Overrides:
init
in classDoNothingVisitor
- Parameters:
j
- the current Jep instance
-
substitute
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
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
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
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
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
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
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
Description copied from class:DoNothingVisitor
Visit a variable node. Can be overridden by subclasses.- Specified by:
visit
in interfaceParserVisitor
- Overrides:
visit
in classDoNothingVisitor
- Throws:
JepException
-