Class OptimizeExpression

  • All Implemented Interfaces:
    IFunctionEvaluator, IEvaluator

    public class OptimizeExpression
    extends AbstractFunctionEvaluator
     OptimizeExpression(function)
     

    common subexpressions elimination for a complicated function by generating “dummy” variables for these subexpressions.

     >> OptimizeExpression( Sin(x) + Cos(Sin(x)) )
     {v1+Cos(v1),{v1->Sin(x)}}
    
     >> OptimizeExpression((3 + 3*a^2 + Sqrt(5 + 6*a + 5*a^2) + a*(4 + Sqrt(5 + 6*a + 5*a^2)))/6)
     {1/6*(3+3*v1+v2+a*(4+v2)),{v1->a^2,v2->Sqrt(5+6*a+5*v1)}}
     

    Create the original expression:

     >> ReplaceRepeated(1/6*(3+3*v1+v2+a*(4+v2)), {v1->a^2, v2->Sqrt(5+6*a+5*v1)})
     1/6*(3+3*a^2+Sqrt(5+6*a+5*a^2)+a*(4+Sqrt(5+6*a+5*a^2)))
     
    • Constructor Detail

      • OptimizeExpression

        public OptimizeExpression()
    • Method Detail

      • cseArray

        public static IAST cseArray​(IAST ast,
                                    int minReferences,
                                    int minLeafCounter)
      • cse

        public static IAST cse​(IASTMutable ast)
        Perform common subexpression elimination on an expression.Try to optimize/extract common sub-IASTMutable expressions to minimize the number of operations
        Parameters:
        ast - the ast whose internal memory consumption should be minimized
        Returns:
        the pair of {shared-expressions, recursive-replacement-rules}
      • cse

        public static IAST cse​(IASTMutable ast,
                               Supplier<String> variablePrefix)
        Perform common subexpression elimination on an expression.Try to optimize/extract common sub-IASTMutable expressions to minimize the number of operations
        Parameters:
        ast - the ast whose internal memory consumption should be minimized
        variablePrefix - the prefix string, which should be used for the variable names
        Returns:
        the pair of {shared-expressions, recursive-replacement-rules}
      • evaluate

        public IExpr evaluate​(IAST ast,
                              EvalEngine engine)
        Description copied from class: AbstractFunctionEvaluator
        Symbolic evaluation of a function. The method IExpr.head() contains the head (i.e. the function symbol) of this abstract syntax tree (AST).

        From ast.get(1) to ast.get(n) the ast contains the first to n-th argument of the function (alternatively you get the first to fifth argument with the methods IAST.arg1(), IAST.arg2(), IAST.arg3() ... IAST.arg5().

        Example: the expression Binomial(n,m) is represented as AST with ast.head() <=> F.Binomial, ast.arg1() <=> n and ast.arg2() <=> m

        If necessary use the methods from the Validate class to check the number or types of arguments in the evaluate method.

        Note: if the symbolic evaluation isn't possible or no result is found the evaluate method returns with a F.NIL value without throwing an exception!

        Specified by:
        evaluate in interface IFunctionEvaluator
        Specified by:
        evaluate in class AbstractFunctionEvaluator
        Parameters:
        ast - the abstract syntax tree (AST) which should be evaluated
        engine - the users current evaluation engine
        Returns:
        the evaluated object or F#NIL, if evaluation isn't possible
        See Also:
        Validate
      • expectedArgSize

        public int[] expectedArgSize​(IAST ast)
        Description copied from interface: IFunctionEvaluator
        At index 0 return the "from", at index 1 return the "to" number of arguments, which are expected by this function. If the returned int array has length 3, the function allows headers unequal to the built-in function name (i.e. the function can be in "operator form").
        Parameters:
        ast - the abstract syntax tree (AST) those arguments should be checked
        Returns:
        null if no range for the number of arguments is specified.
      • csePairAsJava

        public static void csePairAsJava​(IAST csePair,
                                         StringBuilder buf)