Package com.singularsys.extensions.xjep
Class ExpressionCleaner
java.lang.Object
com.singularsys.extensions.xjep.ExpressionCleaner
- All Implemented Interfaces:
GenericVisitor<Node,,Void, ParseException> JepComponent,Serializable
- Direct Known Subclasses:
DExpressionCleaner
public class ExpressionCleaner
extends Object
implements GenericVisitor<Node,Void,ParseException>, JepComponent
Cleans up expressions, removing redundant parts. To use
Jep 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 DirtyFunction interface
so functions like rand() are no cleaned.
Since Jep 4.1, extension 2.2, this now implements GenericVisitor
some method signatures have changed to be more specific.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor to use when used withJep.setComponent(JepComponent component).ExpressionCleaner(Jep j, TreeUtils tu) Constructor with explicit Jep and TreeUtils instances. -
Method Summary
Modifier and TypeMethodDescriptionMain entry point.Cleans an addition.cleanBuildOperatorNode(Operator op, Node node) cleanBuildOperatorNode(Operator op, Node lhs, Node rhs) First create a new node and then clean it.cleanDivide(Node child1, Node child2) Cleans a division.cleanMultiply(Node child1, Node child2) Cleans a multiplication.Attempt to clean an operator.simplifies operators, does not descend into childrencleanPower(Node child1, Node child2) Simplify a power.cleanSubtract(Node lhs, Node rhs) Cleans a subtraction.cleanTriple(Operator op, Node lhs, Node rhs) Cleans expressions like 2+(3+x) or (2+x)+3.cleanUMinus(Node child) Gets a light-weight instance suitable for using in multiple threads.voidInitialize the component.makeException(String message) Create an Exception of the correct type.visit(ASTConstant node, Void data) Visit a constant nodevisit(ASTFunNode node, Void data) Visit a function nodeVisit an operator nodevisit(ASTVarNode node, Void data) Visit a variable nodeMethods 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
-
Constructor Details
-
ExpressionCleaner
public ExpressionCleaner()Constructor to use when used withJep.setComponent(JepComponent component). The reference to the jep instance and TreeUtils are found ininit(Jep). -
ExpressionCleaner
Constructor with explicit Jep and TreeUtils instances. Can be used in a standalone context.- Parameters:
j-tu- TreeUtils class to use
-
-
Method Details
-
getLightWeightInstance
Description copied from interface:JepComponentGets a light-weight instance suitable for using in multiple threads.- Specified by:
getLightWeightInstancein interfaceJepComponent- Returns:
- either a new instance, null or 'this'.
-
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- Parameters:
j- the current Jep instance
-
clean
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
First create a new node and then clean it.- Throws:
ParseException
-
cleanBuildOperatorNode
- Throws:
ParseException
-
cleanOp
simplifies operators, does not descend into children- Throws:
ParseException
-
cleanOp
Attempt to clean an operator.- Parameters:
op-children-- Returns:
- a simplified expression or null if it could be simplified.
- Throws:
ParseException
-
cleanTriple
Cleans expressions like 2+(3+x) or (2+x)+3.- 2 + ~( 3 + ~x ) -> (2+~3) + ~~x
- 2 * (3 + ~x) -> (2 * 3) + ~(2 @ x)
- 2 + ~( x + ~3 ) -> (2 + ~~3) + ~x
- 2 * (x + ~3) -> (2 * x) + ~(2 * 3)
- (2 + ~x) + ~3 -> (2 + ~3) + ~x
- (2 + ~x) * 3 --> (2*3) +~ (x*3)
- (x + ~2) + !3 -> x + (~2 + !3) -> x + ~(2+~!3)
- (x*2)*3 -> x*(2*3), (x/2)*3 -> x/(2/3)
- (x*2)/3 -> x*(2/3), (x/2)/3 -> x/(2*3)
- (x + ~2) * 3 -> (x*3) + ~(2*3)
- x * (y * z) -> (x * y) * z where * is an associative left binding operator
- (x ! y) ! z -> x ! (y ! z) where ! is an associative right binding operator
- (x * y) * 2 -> (2 * x) * y
- (x-y)+z -> (x+z)-y
- (x/y)*z -> (x*z)/y
- 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
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
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
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
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
Simplify a power.x^0 -> 1 x^1 -> x 0^0 -> NaN 0^x -> 0 1^x -> 1
- Throws:
ParseException
-
cleanUMinus
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit an operator node- Specified by:
visitin interfaceGenericVisitor<Node,Void, ParseException> - Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit a function node- Specified by:
visitin interfaceGenericVisitor<Node,Void, ParseException> - Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit a constant node- Specified by:
visitin interfaceGenericVisitor<Node,Void, ParseException> - Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit a variable node- Specified by:
visitin interfaceGenericVisitor<Node,Void, ParseException> - Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
makeException
Description copied from interface:GenericVisitorCreate an Exception of the correct type. A typical implementation is@Override public JepException makeException(String message) { return new JepException(message); }- Specified by:
makeExceptionin interfaceGenericVisitor<Node,Void, ParseException> - Parameters:
message- error message- Returns:
- an Exception
-