Package com.singularsys.jep.walkers
Class SubstitutionVisitor
java.lang.Object
com.singularsys.jep.walkers.GenericDoNothingVisitor<Void>
com.singularsys.jep.walkers.SubstitutionVisitor
- All Implemented Interfaces:
GenericVisitor<Node,,Void, ParseException> JepComponent,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
FieldsFields inherited from class com.singularsys.jep.walkers.GenericDoNothingVisitor
ft, nf, ot -
Constructor Summary
ConstructorsConstructorDescriptionConstructor to uses as a JepComponent. -
Method Summary
Modifier and TypeMethodDescriptionvoidInitialize the component.substitute(Node orig, Node sub) Substitutes intoorigthe 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 variablevarwithreplacement.substitute(Node orig, String name, Object value) Substitute a single variable with a given value.visit(ASTVarNode node, Void data) Visit a variable nodeMethods inherited from class com.singularsys.jep.walkers.GenericDoNothingVisitor
childrenHaveChanged, copyChildrenIfNeeded, copyChildrenIfNeeded, getLightWeightInstance, makeException, visit, visit, visitMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.singularsys.jep.GenericVisitor
makeException, visit, visitChildren
-
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:JepComponentInitialize 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:
initin interfaceJepComponent- Overrides:
initin classGenericDoNothingVisitor<Void>- Parameters:
j- the current Jep instance
-
substitute
Substitutes all occurrences of variablevarwithreplacement. Does not do a DeepCopy.- Parameters:
orig- the expression we wish to perform the substitution onname- the name of the variablereplacement- the expressionvaris substituted for- Returns:
- the tree with variable replace (does not do a DeepCopy)
- Throws:
ParseException
-
substitute
Substitutes intoorigthe equation given by sub- Parameters:
orig- the equation to substitute intosub- and equation of the formx=....- Returns:
- a copy of
origafter 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
origafter 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 interface:GenericVisitorVisit a variable node- Specified by:
visitin interfaceGenericVisitor<Node,Void, ParseException> - Overrides:
visitin classGenericDoNothingVisitor<Void>- Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-