public class ExpressionCleaner extends DoNothingVisitor
Jep j = new Jep();
TreeUtils tu = new TreeUtils(jep.getNumberFactory());
SimplificationVisitor sv = new SimplificationVisitor(jep, tu);
Node in = jep.parse("1*x^1+0");
Node out = sv.clean(in);
Note this visitor works in-place.
| Constructor and Description |
|---|
ExpressionCleaner()
Constructor to use when used with
Jep.setComponent(JepComponent component). |
ExpressionCleaner(Jep j,
TreeUtils tu)
Constructor with explicit Jep and TreeUtils instances.
|
| Modifier and Type | Method and Description |
|---|---|
Node |
clean(Node node)
Main entry point.
|
Node |
cleanAdd(Node lhs,
Node rhs)
Cleans an addition.
|
Node |
cleanBuildOperatorNode(Operator op,
Node node) |
Node |
cleanBuildOperatorNode(Operator op,
Node lhs,
Node rhs)
First create a new node and then clean it.
|
Node |
cleanDivide(Node child1,
Node child2)
Cleans a division.
|
Node |
cleanMultiply(Node child1,
Node child2)
Cleans a multiplication.
|
Node |
cleanOp(Node node,
Node... children)
simplifies operators, does not descend into children
|
Node |
cleanOp(Operator op,
Node... children)
Attempt to clean an operator.
|
Node |
cleanPower(Node child1,
Node child2)
Simplify a power.
|
Node |
cleanSubtract(Node lhs,
Node rhs)
Cleans a subtraction.
|
Node |
cleanTripple(Operator op,
Node lhs,
Node rhs)
Cleans expressions like 2+(3+x) or (2+x)+3
|
Node |
cleanUMinus(Node node) |
JepComponent |
getLightWeightInstance()
Gets a light-weight instance suitable for using in multiple threads.
|
void |
init(Jep j)
Initialize the component.
|
java.lang.Object |
visit(ASTFunNode node,
java.lang.Object data)
Visit a function node.
|
java.lang.Object |
visit(ASTOpNode node,
java.lang.Object data)
Visit a operator node.
|
childrenHaveChanged, copyChildrenIfNeeded, getFunctionTable, getNodeFactory, getOperatorTable, getVariableTable, visit, visit, visit, visitChildren, visitNodepublic ExpressionCleaner()
Jep.setComponent(JepComponent component). The reference to the
jep instance and TreeUtils are found in init(Jep).public JepComponent getLightWeightInstance()
JepComponentgetLightWeightInstance in interface JepComponentgetLightWeightInstance in class DoNothingVisitorpublic void init(Jep j)
JepComponentinit in interface JepComponentinit in class DoNothingVisitorj - the current Jep instancepublic Node clean(Node node) throws ParseException
node - the base of the tree which will be modified.ParseExceptionpublic Node cleanBuildOperatorNode(Operator op, Node lhs, Node rhs) throws ParseException
ParseExceptionpublic Node cleanBuildOperatorNode(Operator op, Node node) throws ParseException
ParseExceptionpublic Node cleanOp(Node node, Node... children) throws ParseException
ParseExceptionpublic Node cleanOp(Operator op, Node... children) throws ParseException
op - children - ParseExceptionpublic Node cleanTripple(Operator op, Node lhs, Node rhs) throws ParseException
op - the root operatorlhs - the left hand side noderhs - the right hand side nodeParseExceptionpublic Node cleanAdd(Node lhs, Node rhs) throws ParseException
0+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
ParseExceptionpublic Node cleanSubtract(Node lhs, Node rhs) throws ParseException
+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
lhs - the left hand siderhs - the right hand sideParseExceptionpublic Node cleanMultiply(Node child1, Node child2) throws ParseException
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.
ParseExceptionpublic Node cleanDivide(Node child1, Node child2) throws ParseException
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)
ParseExceptionpublic Node cleanPower(Node child1, Node child2) throws ParseException
x^0 -> 1 x^1 -> x 0^0 -> NaN 0^x -> 0 1^x -> 1
ParseExceptionpublic Node cleanUMinus(Node node) throws ParseException
ParseExceptionpublic java.lang.Object visit(ASTOpNode node, java.lang.Object data) throws ParseException
DoNothingVisitorvisit in interface ParserVisitorvisit in class DoNothingVisitorParseExceptionpublic java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws ParseException
DoNothingVisitorvisit in interface ParserVisitorvisit in class DoNothingVisitorParseExceptionCopyright © 2018 Singular Systems http://www.singularsys.com/jep