Package com.singularsys.extensions.xjep
Class XJep
- java.lang.Object
-
- com.singularsys.jep.Jep
-
- com.singularsys.extensions.xjep.XJep
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
DJep
public class XJep extends Jep
An extended version of Jep adds various routines for working with trees. Has a NodeFactory, and OperatorTable, TreeUtils and Visitors DoNothingVisitor,- Author:
- Rich Morris Created on 16-Nov-2003
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected ExpressionCleaner
cleaner
protected CommandVisitor
commandv
protected DeepCopyVisitor
copier
protected EquationExtractor
extractor
protected PolynomialCreator
polyv
protected RewriteVisitor
rewritev
protected SubstitutionVisitor
subv
protected TreeUtils
tu
A few utility functions.-
Fields inherited from class com.singularsys.jep.Jep
additionalComponents, allowAssignment, allowUndeclared, evaluator, funTab, implicitMul, lastRootNode, nodeFac, numFac, opTab, parser, pv, varFac, varTab
-
-
Constructor Summary
Constructors Constructor Description XJep()
Create a new XJep with all the function of Jep plus simplification and other features.XJep(ComponentSet components)
Create a new XJep instance using specified components set and default components if necessary.XJep(JepComponent... components)
Create a new XJep instance using specified components and default components if necessary.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addRewriteRule(RewriteRuleI rule)
java.lang.Object
calcVarValue(java.lang.String name, boolean force)
Calculates the value for the variables equation and returns that value.Node
clean(Node node)
Returns a cleaned up version of an expression tree.Node
deepCopy(Node node)
Returns a deep copy of an expression tree.boolean
equals(Node lhs, Node rhs)
Compare two expressions symbolically.Node
expand(Node node)
Expand brackets in an expressionCommandVisitor
getCommandVisitor()
DeepCopyVisitor
getCopier()
ExpressionCleaner
getExpressionCleaner()
EquationExtractor
getExtractor()
PolynomialCreator
getPolynomialCreator()
RewriteVisitor
getRewriteVisitor()
SubstitutionVisitor
getSubstitutionVisitor()
TreeUtils
getTreeUtils()
Returns the TreeUtilitities, used for examining properties of nodes.java.util.Set<Variable>
getVarsInEquation(Node n)
Finds all the variables in an equation.java.util.Set<Variable>
getVarsInEquation(Node n, java.util.Set<Variable> vars)
Finds all the variables in an equation.Node
preprocess(Node node)
Pre-processes an equation to allow the diff and eval operators to be used.java.util.List<XVariable>
recursiveGetVarsInEquation(Node n, java.util.List<XVariable> vars)
Finds all the variables in an equation and if any of those variables are defined by equations find the variables in those equations as well.Node
replaceRHSVariablesByEquations(Node node)
Replace variables on the right had side of an assignment by their equationsNode
replaceVariableByExpressions(Node node)
Replace all symbolic variable by their equationsNode
rewrite(Node node, boolean clean)
protected void
setXComponentInternal(JepComponent comp)
Node
simplify(Node node)
Returns a simplification of an expression tree.Node
substitute(Node orig, Node sub)
Substitute all occurrences of the variable on the left hand side of sub with the expression tree on the right hand side.Node
substitute(Node orig, Node[] subs)
Substitute a set of expressions into the original.Node
substitute(Node orig, java.lang.String[] names, Node[] replacements)
Substitute all occurrences of a set of named variable with a set of expression tree.Node
substitute(Node orig, java.lang.String[] names, java.lang.Object[] values)
Substitute all occurrences of the variables with their values.Node
substitute(Node orig, java.lang.String name, Node replacement)
Substitute all occurrences of a named variable with an expression.Node
substitute(Node orig, java.lang.String name, java.lang.Object value)
Substitute all occurrences of a named variable with a constant value.Node
substituteConstantVariables(Node orig)
Replace all constant variables likepi
by their values.-
Methods inherited from class com.singularsys.jep.Jep
addConstant, addDefaultComponents, addFunction, addStandardConstants, addVariable, addVariable, addVariable, addVariable, continueParsing, evaluate, evaluate, evaluateD, getAdditionalComponent, getAdditionalComponents, getAllowAssignment, getAllowUndeclared, getDefaultValue, getEvaluator, getFunctionTable, getImplicitMul, getLastRootNode, getNodeFactory, getNumberFactory, getOperatorTable, getParser, getPrintVisitor, getVariable, getVariableFactory, getVariableTable, getVariableValue, initMultiParse, initMultiParse, parse, parse, print, print, print, println, println, println, reinitializeComponents, rootNodeToString, setAllowAssignment, setAllowUndeclared, setComponent, setComponentInternal, setComponents, setComponentsInternal, setDefaultValue, setImplicitMul, setVariable, toString, tryAddConstant
-
-
-
-
Field Detail
-
tu
protected TreeUtils tu
A few utility functions.
-
copier
protected DeepCopyVisitor copier
-
subv
protected SubstitutionVisitor subv
-
cleaner
protected ExpressionCleaner cleaner
-
commandv
protected CommandVisitor commandv
-
polyv
protected PolynomialCreator polyv
-
rewritev
protected RewriteVisitor rewritev
-
extractor
protected EquationExtractor extractor
-
-
Constructor Detail
-
XJep
public XJep()
Create a new XJep with all the function of Jep plus simplification and other features.
-
XJep
public XJep(JepComponent... components)
Create a new XJep instance using specified components and default components if necessary.- Parameters:
components
-
-
XJep
public XJep(ComponentSet components)
Create a new XJep instance using specified components set and default components if necessary.- Parameters:
components
-
-
-
Method Detail
-
setXComponentInternal
protected void setXComponentInternal(JepComponent comp)
-
preprocess
public Node preprocess(Node node) throws ParseException
Pre-processes an equation to allow the diff and eval operators to be used. Uses theCommandVisitor
- Throws:
ParseException
-
deepCopy
public Node deepCopy(Node node) throws ParseException
Returns a deep copy of an expression tree.- Throws:
ParseException
- See Also:
DeepCopyVisitor
-
clean
public Node clean(Node node) throws ParseException
Returns a cleaned up version of an expression tree. First does a deep copy of the tree.- Throws:
ParseException
- See Also:
ExpressionCleaner
-
simplify
public Node simplify(Node node) throws ParseException
Returns a simplification of an expression tree.- Throws:
ParseException
- See Also:
PolynomialCreator.simplify(Node)
-
expand
public Node expand(Node node) throws ParseException
Expand brackets in an expression- Parameters:
node
- expression to expand- Returns:
- expanded version of expression
- Throws:
ParseException
- See Also:
PolynomialCreator.expand(Node)
-
equals
public boolean equals(Node lhs, Node rhs) throws ParseException
Compare two expressions symbolically.- Parameters:
lhs
-rhs
-- Returns:
- true if symbolically equal after rearrangement
- Throws:
ParseException
- See Also:
PolynomialCreator.equals(Node, Node)
-
substitute
public Node substitute(Node orig, java.lang.String name, java.lang.Object value) throws ParseException
Substitute all occurrences of a named variable with a constant value.- Parameters:
orig
- expression to substitute intoname
- name of variable to substitutevalue
- its replacement value- Returns:
- expression after substitution
- Throws:
ParseException
-
substitute
public Node substitute(Node orig, java.lang.String name, Node replacement) throws ParseException
Substitute all occurrences of a named variable with an expression.- Parameters:
orig
- expression to substitute intoname
- name of variable to substitutereplacement
- expression to- Returns:
- expression after substitution
- Throws:
ParseException
-
substitute
public Node substitute(Node orig, Node sub) throws ParseException
Substitute all occurrences of the variable on the left hand side of sub with the expression tree on the right hand side.- Parameters:
orig
- expression to substitute intosub
- expression of the form x=....- Returns:
- expression after substitution
- Throws:
ParseException
-
substitute
public Node substitute(Node orig, java.lang.String[] names, java.lang.Object[] values) throws ParseException
Substitute all occurrences of the variables with their values.- Throws:
ParseException
-
substitute
public Node substitute(Node orig, Node[] subs) throws ParseException
Substitute a set of expressions into the original.- Parameters:
orig
- expression to substitute intosubs
- array of expressions of the formx=....
- Returns:
- expression after substitution
- Throws:
ParseException
-
substitute
public Node substitute(Node orig, java.lang.String[] names, Node[] replacements) throws ParseException
Substitute all occurrences of a set of named variable with a set of expression tree.- Throws:
ParseException
-
substituteConstantVariables
public Node substituteConstantVariables(Node orig) throws ParseException
Replace all constant variables likepi
by their values.- Parameters:
orig
- the input- Returns:
- a copy of the input with all constant variables substituted by their values.
- Throws:
ParseException
-
replaceRHSVariablesByEquations
public Node replaceRHSVariablesByEquations(Node node) throws JepException
Replace variables on the right had side of an assignment by their equations- Parameters:
node
- an equation of the formg:=f^2-1
- Returns:
- a copy of the node
- Throws:
JepException
-
replaceVariableByExpressions
public Node replaceVariableByExpressions(Node node) throws JepException
Replace all symbolic variable by their equations- Parameters:
node
- root of expression- Returns:
- copy of node with variables replaced by their equations
- Throws:
JepException
-
calcVarValue
public java.lang.Object calcVarValue(java.lang.String name, boolean force) throws EvaluationException
Calculates the value for the variables equation and returns that value. If the variable does not have an equation just return its value. Recursively calculates the values of any variable with equations which appear.- Parameters:
name
- name of variableforce
- whether to force re-evaluation if the variable has a valid value- Returns:
- value of variable
- Throws:
EvaluationException
-
getVarsInEquation
public java.util.Set<Variable> getVarsInEquation(Node n)
Finds all the variables in an equation.- Parameters:
n
- the top node of the expression- Returns:
- v
-
getVarsInEquation
public java.util.Set<Variable> getVarsInEquation(Node n, java.util.Set<Variable> vars)
Finds all the variables in an equation.- Parameters:
n
- the top node of the expressionvars
- the list of variables (new variables will be added on end)- Returns:
- v
-
recursiveGetVarsInEquation
public java.util.List<XVariable> recursiveGetVarsInEquation(Node n, java.util.List<XVariable> vars) throws ParseException
Finds all the variables in an equation and if any of those variables are defined by equations find the variables in those equations as well. The result is an ordered sequence, evaluating each variable in turn will correctly allow the final equation to be evaluated.For example if the equation is
a+b
anda=c+d
,b=c+e
then the result will be the sequence(c,d,a,e,b)
For this method to work the equations must be preprocessed usingpreprocess(com.singularsys.jep.parser.Node)
. From version 2.1 returns a sorted list rather than an ordered set.- Parameters:
n
- top nodevars
- list of variables, new variables will be added on the end.- Returns:
- v, the ordered sequence of variables
- Throws:
ParseException
- if equation is recursive i.e.x=y; y=x+1;
-
rewrite
public Node rewrite(Node node, boolean clean) throws ParseException
- Throws:
ParseException
-
addRewriteRule
public boolean addRewriteRule(RewriteRuleI rule)
-
getTreeUtils
public TreeUtils getTreeUtils()
Returns the TreeUtilitities, used for examining properties of nodes.
-
getCopier
public DeepCopyVisitor getCopier()
-
getSubstitutionVisitor
public SubstitutionVisitor getSubstitutionVisitor()
-
getExpressionCleaner
public ExpressionCleaner getExpressionCleaner()
-
getCommandVisitor
public CommandVisitor getCommandVisitor()
-
getPolynomialCreator
public PolynomialCreator getPolynomialCreator()
-
getRewriteVisitor
public RewriteVisitor getRewriteVisitor()
-
getExtractor
public EquationExtractor getExtractor()
-
-