Package com.singularsys.extensions.xjep
Class ExpressionCleaner
- java.lang.Object
-
- com.singularsys.jep.walkers.DoNothingVisitor
-
- com.singularsys.extensions.xjep.ExpressionCleaner
-
- All Implemented Interfaces:
JepComponent,ParserVisitor,java.io.Serializable
- Direct Known Subclasses:
DExpressionCleaner
public class ExpressionCleaner extends DoNothingVisitor
Cleans up expressions, removing redundant parts. To useJep j = new Jep(); TreeUtils tu = new TreeUtils(jep.getNumberFactory()); ExpressionCleaner sv = new ExpressionCleaner(jep, tu); Node in = jep.parse("1*x^1+0"); Node out = sv.clean(in);Note this visitor works in-place.
Since Jep 4.0/Extensions 2.1 respects the
DirtyFunctioninterface so functions likerand()are no cleaned.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ExpressionCleaner()Constructor to use when used withJep.setComponent(JepComponent component).ExpressionCleaner(Jep j, TreeUtils tu)Constructor with explicit Jep and TreeUtils instances.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Nodeclean(Node node)Main entry point.NodecleanAdd(Node lhs, Node rhs)Cleans an addition.NodecleanBuildOperatorNode(Operator op, Node node)NodecleanBuildOperatorNode(Operator op, Node lhs, Node rhs)First create a new node and then clean it.NodecleanDivide(Node child1, Node child2)Cleans a division.NodecleanMultiply(Node child1, Node child2)Cleans a multiplication.NodecleanOp(Operator op, Node... children)Attempt to clean an operator.NodecleanOp(Node node, Node... children)simplifies operators, does not descend into childrenNodecleanPower(Node child1, Node child2)Simplify a power.NodecleanSubtract(Node lhs, Node rhs)Cleans a subtraction.NodecleanTripple(Operator op, Node lhs, Node rhs)Cleans expressions like 2+(3+x) or (2+x)+3NodecleanUMinus(Node node)JepComponentgetLightWeightInstance()Gets a light-weight instance suitable for using in multiple threads.voidinit(Jep j)Initialize the component.java.lang.Objectvisit(ASTFunNode node, java.lang.Object data)Visit a function node.java.lang.Objectvisit(ASTOpNode node, java.lang.Object data)Visit a operator node.-
Methods inherited from class com.singularsys.jep.walkers.DoNothingVisitor
childrenHaveChanged, copyChildrenIfNeeded, getFunctionTable, getNodeFactory, getOperatorTable, getVariableTable, visit, visit, visit, visitChildren, visitNode
-
-
-
-
Constructor Detail
-
ExpressionCleaner
public ExpressionCleaner()
Constructor to use when used withJep.setComponent(JepComponent component). The reference to the jep instance and TreeUtils are found ininit(Jep).
-
-
Method Detail
-
getLightWeightInstance
public JepComponent getLightWeightInstance()
Description copied from interface:JepComponentGets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstancein interfaceJepComponent- Overrides:
getLightWeightInstancein classDoNothingVisitor- Returns:
- this
-
init
public void init(Jep j)
Description copied from interface:JepComponentInitialize 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:
initin interfaceJepComponent- Overrides:
initin classDoNothingVisitor- Parameters:
j- the current Jep instance
-
clean
public Node clean(Node node) throws ParseException
Main entry point. Cleans the node in place.- Parameters:
node- the base of the tree which will be modified.- Returns:
- simplified version of the expression tree, no necessarily the same object as the input.
- Throws:
ParseException
-
cleanBuildOperatorNode
public Node cleanBuildOperatorNode(Operator op, Node lhs, Node rhs) throws ParseException
First create a new node and then clean it.- Throws:
ParseException
-
cleanBuildOperatorNode
public Node cleanBuildOperatorNode(Operator op, Node node) throws ParseException
- Throws:
ParseException
-
cleanOp
public Node cleanOp(Node node, Node... children) throws ParseException
simplifies operators, does not descend into children- Throws:
ParseException
-
cleanOp
public Node cleanOp(Operator op, Node... children) throws ParseException
Attempt to clean an operator.- Parameters:
op-children-- Returns:
- a simplified expression or null if it could be simplified.
- Throws:
ParseException
-
cleanTripple
public Node cleanTripple(Operator op, Node lhs, Node rhs) throws ParseException
Cleans expressions like 2+(3+x) or (2+x)+3- Parameters:
op- the root operatorlhs- the left hand side noderhs- the right hand side node- Returns:
- null if no rewrite happens or top node or top node of new tree.
- Throws:
ParseException
-
cleanAdd
public Node cleanAdd(Node lhs, Node rhs) throws ParseException
Cleans an addition. Performs the following rules0+x -> x x+0 -> x m+n -> (m+n) where m,n are numbers x - (-2) -> x + 2 for any negative number -2 x + (-2) -> x - 2 for any negative number -2 2 +/- ( 3 +/- x ) -> (2 +/- 3 ) +/- x and similar
- Throws:
ParseException
-
cleanSubtract
public Node cleanSubtract(Node lhs, Node rhs) throws ParseException
Cleans a subtraction. Performs the following rules+Infinity-x -> +Infinity -Infinity-x -> -Infinity x - +Infinity -> -Infinity x - -Infinity -> +Infinity 0-x -> 0-x x-0 -> x m-n -> (m-n) where m,n are numbers x - (-2) -> x + 2 for any negative number -2 x + (-2) -> x - 2 for any negative number -2 2 +/- ( 3 +/- x ) -> (2 +/- 3 ) +/- x and similar
- Parameters:
lhs- the left hand siderhs- the right hand side- Throws:
ParseException
-
cleanMultiply
public Node cleanMultiply(Node child1, Node child2) throws ParseException
Cleans a multiplication.0 * Inf -> NaN 0 * x -> 0 x * 0 -> 0 1 * x -> x x * 1 -> x Inf * x -> Inf x * Inf -> Inf 2 * ( 3 * x) -> (2*3) * x 2 * (-x) -> -2 * x and similar.
- Throws:
ParseException
-
cleanDivide
public Node cleanDivide(Node child1, Node child2) throws ParseException
Cleans a division.0/0 -> NaN 0/Inf -> Inf 0/x -> Inf x/0 -> Inf x/1 -> x Inf / x -> Inf x / Inf -> 0 2 / ( 3 * x) -> (2/3) / x 2 / ( x * 3) -> (2/3) / x 2 / ( 3 / x) -> (2/3) * x 2 / ( x / 3) -> (2*3) / x (2 * x) / 3 -> (2/3) * x (x * 2) / 3 -> x * (2/3) (2 / x) / 3 -> (2/3) / x (x / 2) / 3 -> x / (2*3)
- Throws:
ParseException
-
cleanPower
public Node cleanPower(Node child1, Node child2) throws ParseException
Simplify a power.x^0 -> 1 x^1 -> x 0^0 -> NaN 0^x -> 0 1^x -> 1
- Throws:
ParseException
-
cleanUMinus
public Node cleanUMinus(Node node) throws ParseException
- Throws:
ParseException
-
visit
public java.lang.Object visit(ASTOpNode node, java.lang.Object data) throws ParseException
Description copied from class:DoNothingVisitorVisit a operator node. Can be overridden by sub-classes.- Specified by:
visitin interfaceParserVisitor- Overrides:
visitin classDoNothingVisitor- Throws:
ParseException
-
visit
public java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws ParseException
Description copied from class:DoNothingVisitorVisit a function node. Can be overridden by sub-classes.- Specified by:
visitin interfaceParserVisitor- Overrides:
visitin classDoNothingVisitor- Throws:
ParseException
-
-