Class ExpressionCleaner

  • All Implemented Interfaces:
    JepComponent, ParserVisitor, java.io.Serializable
    Direct Known Subclasses:
    DExpressionCleaner

    public class ExpressionCleaner
    extends DoNothingVisitor
    Cleans up expressions, removing redundant parts. To use
     Jep j = new Jep();
     TreeUtils tu = new TreeUtils(jep.getNumberFactory());
     ExpressionCleaner sv = new ExpressionCleaner(jep, tu);
     
     Node in = jep.parse("1*x^1+0");
     Node out = sv.clean(in);
     

    Note this visitor works in-place.

    Since Jep 4.0/Extensions 2.1 respects the DirtyFunction interface so functions like rand() are no cleaned.

    See Also:
    Serialized Form
    • Constructor Detail

      • ExpressionCleaner

        public ExpressionCleaner​(Jep j,
                                 TreeUtils tu)
        Constructor with explicit Jep and TreeUtils instances. Can be used in a stand alone context.
        Parameters:
        j -
        tu - TreeUtils class to use
    • Method Detail

      • init

        public void init​(Jep j)
        Description copied from interface: JepComponent
        Initialize the component. This methods is called whenever a component is added to Jep. Hence it allows components to keep track of the other components they may rely on.
        Specified by:
        init in interface JepComponent
        Overrides:
        init in class DoNothingVisitor
        Parameters:
        j - the current Jep instance
      • clean

        public Node clean​(Node node)
                   throws ParseException
        Main entry point. Cleans the node in place.
        Parameters:
        node - the base of the tree which will be modified.
        Returns:
        simplified version of the expression tree, no necessarily the same object as the input.
        Throws:
        ParseException
      • cleanOp

        public Node cleanOp​(Operator op,
                            Node... children)
                     throws ParseException
        Attempt to clean an operator.
        Parameters:
        op -
        children -
        Returns:
        a simplified expression or null if it could be simplified.
        Throws:
        ParseException
      • cleanTripple

        public Node cleanTripple​(Operator op,
                                 Node lhs,
                                 Node rhs)
                          throws ParseException
        Cleans 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
      • cleanAdd

        public Node cleanAdd​(Node lhs,
                             Node rhs)
                      throws ParseException
        Cleans 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
      • cleanSubtract

        public Node cleanSubtract​(Node lhs,
                                  Node rhs)
                           throws ParseException
        Cleans a subtraction. Performs the following rules
         +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
         
        Parameters:
        lhs - the left hand side
        rhs - the right hand side
        Throws:
        ParseException
      • cleanMultiply

        public Node cleanMultiply​(Node child1,
                                  Node child2)
                           throws ParseException
        Cleans 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
         2 * (-x) -> -2 * x
         and similar.
         
        Throws:
        ParseException
      • cleanDivide

        public Node cleanDivide​(Node child1,
                                Node child2)
                         throws ParseException
        Cleans 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