org.lsmp.djep.xjep
Class SimplificationVisitor

java.lang.Object
  extended by org.lsmp.djep.xjep.DoNothingVisitor
      extended by org.lsmp.djep.xjep.SimplificationVisitor
All Implemented Interfaces:
ParserVisitor

public class SimplificationVisitor
extends DoNothingVisitor

Simplifies an expression. To use

 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.

Author:
Rich Morris Created on 20-Jun-2003 TODO cope with 'a - (-1) * b' TODO cope with '0 - uminus(b)' TODO cope with simplifying complex numbers

Constructor Summary
SimplificationVisitor()
           
 
Method Summary
 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
 java.lang.Object visit(ASTFunNode node, java.lang.Object data)
           
 
Methods inherited from class org.lsmp.djep.xjep.DoNothingVisitor
acceptChildrenAsArray, visit, visit, visit, visit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimplificationVisitor

public SimplificationVisitor()
Method Detail

simplify

public Node simplify(Node node,
                     XJep xjep)
              throws ParseException,
                     java.lang.IllegalArgumentException
must be implemented for subclasses.

Throws:
ParseException
java.lang.IllegalArgumentException

simplifyBuiltOperatorNode

public Node simplifyBuiltOperatorNode(Operator op,
                                      Node lhs,
                                      Node rhs)
                               throws ParseException
First create a new node and then simplify it.

Throws:
ParseException

simplifyTripple

public Node simplifyTripple(XOperator op,
                            Node lhs,
                            Node rhs)
                     throws ParseException
Simplifies expressions like 2+(3+x) or (2+x)+3

Parameters:
op - the root operator
lhs - the left hand side node
rhs - the right hand side node
Returns:
null if no rewrite happens or top node or top node of new tree.
Throws:
ParseException

simplifyAdd

public Node simplifyAdd(Node lhs,
                        Node rhs)
                 throws ParseException
Simplifies an addition. Performs the following rules
 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
 

Throws:
ParseException

simplifySubtract

public Node simplifySubtract(Node lhs,
                             Node rhs)
                      throws ParseException
Simplifies a subtraction. Performs the following rules
 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 side
rhs - the right hand side
Throws:
ParseException

simplifyMultiply

public Node simplifyMultiply(Node child1,
                             Node child2)
                      throws ParseException
Simplifies 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
 and similar.
 

Throws:
ParseException

simplifyDivide

public Node simplifyDivide(Node child1,
                           Node child2)
                    throws ParseException
Simplifies 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

simplifyPower

public Node simplifyPower(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

simplifyOp

public Node simplifyOp(ASTFunNode node,
                       Node[] children)
                throws ParseException
simplifies operators, does not descend into children

Throws:
ParseException

visit

public java.lang.Object visit(ASTFunNode node,
                              java.lang.Object data)
                       throws ParseException
Specified by:
visit in interface ParserVisitor
Overrides:
visit in class DoNothingVisitor
Throws:
ParseException


http://www.singularsys.com/jep Copyright © 2007 Singular Systems