public class SimplificationVisitor extends DoNothingVisitor
JEP j = ...; Node in = ...; SimplificationVisitor sv = new SimplificationVisitor(tu); Node out = sv.simplify(in);
Its intended to completly rewrite this class to that simplification rules can be specified by strings in a way similar to DiffRulesI. It also would be nice to change the rules depending on the type of arguments, for example matrix multiplication is not commutative. But some of the in built rules exploit commutativity.
Constructor and Description |
---|
SimplificationVisitor() |
Modifier and Type | Method and Description |
---|---|
Node |
simplify(Node node,
XJep xjep)
must be implemented for subclasses.
|
Node |
simplifyAdd(Node lhs,
Node rhs)
Simplifies an addition.
|
Node |
simplifyBuiltOperatorNode(Operator op,
Node lhs,
Node rhs)
First create a new node and then simplify it.
|
Node |
simplifyDivide(Node child1,
Node child2)
Simplifies a division.
|
Node |
simplifyMultiply(Node child1,
Node child2)
Simplifies a multiplication.
|
Node |
simplifyOp(ASTFunNode node,
Node[] children)
simplifies operators, does not descend into children
|
Node |
simplifyPower(Node child1,
Node child2)
Simplify a power.
|
Node |
simplifySubtract(Node lhs,
Node rhs)
Simplifies a subtraction.
|
Node |
simplifyTripple(XOperator op,
Node lhs,
Node rhs)
Simplifies expressions like 2+(3+x) or (2+x)+3
|
Object |
visit(ASTFunNode node,
Object data) |
visit, visit, visit, visit
public Node simplify(Node node, XJep xjep) throws ParseException, IllegalArgumentException
public Node simplifyBuiltOperatorNode(Operator op, Node lhs, Node rhs) throws ParseException
ParseException
public Node simplifyTripple(XOperator op, Node lhs, Node rhs) throws ParseException
op
- the root operatorlhs
- the left hand side noderhs
- the right hand side nodeParseException
public Node simplifyAdd(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 simplifySubtract(Node lhs, Node rhs) throws ParseException
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 simplifyMultiply(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 and similar.
ParseException
public Node simplifyDivide(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 simplifyPower(Node child1, Node child2) throws ParseException
x^0 -> 1 x^1 -> x 0^0 -> NaN 0^x -> 0 1^x -> 1
ParseException
public Node simplifyOp(ASTFunNode node, Node[] children) throws ParseException
ParseException
public Object visit(ASTFunNode node, Object data) throws ParseException
visit
in interface ParserVisitor
visit
in class DoNothingVisitor
ParseException
Jas4pp 1.5 © Java Analysis Studio for Particle Physics