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, visitNode
public ExpressionCleaner()
Jep.setComponent(JepComponent component)
. The reference to the
jep instance and TreeUtils are found in init(Jep)
.public JepComponent getLightWeightInstance()
JepComponent
getLightWeightInstance
in interface JepComponent
getLightWeightInstance
in class DoNothingVisitor
public void init(Jep j)
JepComponent
init
in interface JepComponent
init
in class DoNothingVisitor
j
- the current Jep instancepublic Node clean(Node node) throws ParseException
node
- the base of the tree which will be modified.ParseException
public Node cleanBuildOperatorNode(Operator op, Node lhs, Node rhs) throws ParseException
ParseException
public Node cleanBuildOperatorNode(Operator op, Node node) throws ParseException
ParseException
public Node cleanOp(Node node, Node... children) throws ParseException
ParseException
public Node cleanOp(Operator op, Node... children) throws ParseException
op
- children
- ParseException
public Node cleanTripple(Operator op, Node lhs, Node rhs) throws ParseException
op
- the root operatorlhs
- the left hand side noderhs
- the right hand side nodeParseException
public 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
ParseException
public 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 sideParseException
public 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.
ParseException
public 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)
ParseException
public Node cleanPower(Node child1, Node child2) throws ParseException
x^0 -> 1 x^1 -> x 0^0 -> NaN 0^x -> 0 1^x -> 1
ParseException
public Node cleanUMinus(Node node) throws ParseException
ParseException
public java.lang.Object visit(ASTOpNode node, java.lang.Object data) throws ParseException
DoNothingVisitor
visit
in interface ParserVisitor
visit
in class DoNothingVisitor
ParseException
public java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws ParseException
DoNothingVisitor
visit
in interface ParserVisitor
visit
in class DoNothingVisitor
ParseException
Copyright © 2018 Singular Systems http://www.singularsys.com/jep