com.singularsys.aa.manipulation
Class Simplifier

java.lang.Object
  extended by com.singularsys.aa.manipulation.Simplifier
All Implemented Interfaces:
org.nfunk.jep.ParserVisitor

public class Simplifier
extends java.lang.Object
implements org.nfunk.jep.ParserVisitor

The simplifier class simplifies expression trees. With the simplify() method, a set of simplification rules is repeatedly applied until the expression can not be simplified any more.

The simplification rules are:

 Transforming negatives
       a - b        ->  a + ((-1) * b)
       -a           ->  (-1) * a

 Leveling Operators
       a + (b + c)  ->  sum(a, b, c)
       (a + b) + c  ->  sum(a, b, c)
       a * (b * c)  ->  mul(a, b, c)
       (a * b) * c  ->  mul(a, b, c)

 Simplifying Rational Expressions
       (a / b) / c  ->  a / (b * c)
       a / (b / c)  ->  (a * c) / b
       a * (b / c)  ->  (a * b) / c

 Expanding
       a * (b + c)  ->  a * b + a * c

 Collecting like terms
       a + (2 * a)  ->  (1 * a) + (2 * a)
         a * (a^2)  ->  (a^1) * (a^2)
   (a*C1) + (a*C2)  ->  a * (C1 + C2)
   (a^C1) * (a^C2)  ->  a ^ (C1 + C2)

 Folding Constants
   fun(C1, C2, ...) ->  C3
 


Constructor Summary
Simplifier()
          Creates new Simplifier
 
Method Summary
 org.nfunk.jep.Node simplify(org.nfunk.jep.Node topNode)
          Simplifies an expression, and returns the resulting tree.
 java.lang.Object visit(org.nfunk.jep.ASTConstant node, java.lang.Object data)
           
 java.lang.Object visit(org.nfunk.jep.ASTFunNode node, java.lang.Object data)
           
 java.lang.Object visit(org.nfunk.jep.ASTStart node, java.lang.Object data)
           
 java.lang.Object visit(org.nfunk.jep.ASTVarNode node, java.lang.Object data)
           
 java.lang.Object visit(org.nfunk.jep.SimpleNode node, java.lang.Object data)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Simplifier

public Simplifier()
Creates new Simplifier

Method Detail

simplify

public org.nfunk.jep.Node simplify(org.nfunk.jep.Node topNode)
                            throws java.lang.Exception
Simplifies an expression, and returns the resulting tree. This is done by repeatedly applying simplification rules to the expression tree until no changes take place.

The tree under topNode is not changed. A new tree is created and returned.

Throws:
java.lang.Exception

visit

public java.lang.Object visit(org.nfunk.jep.ASTFunNode node,
                              java.lang.Object data)
Specified by:
visit in interface org.nfunk.jep.ParserVisitor

visit

public java.lang.Object visit(org.nfunk.jep.ASTConstant node,
                              java.lang.Object data)
Specified by:
visit in interface org.nfunk.jep.ParserVisitor

visit

public java.lang.Object visit(org.nfunk.jep.ASTVarNode node,
                              java.lang.Object data)
Specified by:
visit in interface org.nfunk.jep.ParserVisitor

visit

public java.lang.Object visit(org.nfunk.jep.ASTStart node,
                              java.lang.Object data)
Specified by:
visit in interface org.nfunk.jep.ParserVisitor

visit

public java.lang.Object visit(org.nfunk.jep.SimpleNode node,
                              java.lang.Object data)
Specified by:
visit in interface org.nfunk.jep.ParserVisitor