Search in sources :

Example 1 with COMPARE_TERNARY

use of org.matheclipse.core.interfaces.IExpr.COMPARE_TERNARY in project symja_android_library by axkr.

the class PredicateQ method isPossibleZeroQ.

public static boolean isPossibleZeroQ(IAST function, boolean fastTest, EvalEngine engine) {
    try {
        VariablesSet varSet = new VariablesSet(function);
        IAST variables = varSet.getVarList();
        if (function.leafCount() < Config.MAX_POSSIBLE_ZERO_LEAFCOUNT / 5) {
            IExpr expr = F.TrigExpand.of(engine, function);
            expr = F.expandAll(expr, true, true);
            expr = engine.evaluate(expr);
            if (!expr.isAST()) {
                return expr.isZero();
            }
            function = (IAST) expr;
        }
        if (variables.isEmpty()) {
            INumber num = function.isNumericFunction(true) ? function.evalNumber() : null;
            if (num == null || !(F.isZero(num.reDoubleValue(), Config.SPECIAL_FUNCTIONS_TOLERANCE) && F.isZero(num.imDoubleValue(), Config.SPECIAL_FUNCTIONS_TOLERANCE))) {
                return false;
            }
            return true;
        } else {
            if (function.isNumericFunction(varSet)) {
                if (function.isFreeAST(h -> isSpecialNumericFunction(h))) {
                    int trueCounter = 0;
                    // 1. step test some special complex numeric values
                    COMPARE_TERNARY possibeZero = isPossibeZeroFixedValues(F.C0, function, variables, engine);
                    if (possibeZero == IExpr.COMPARE_TERNARY.FALSE) {
                        return false;
                    }
                    if (possibeZero == IExpr.COMPARE_TERNARY.TRUE) {
                        trueCounter++;
                    }
                    possibeZero = isPossibeZeroFixedValues(F.C1, function, variables, engine);
                    if (possibeZero == IExpr.COMPARE_TERNARY.FALSE) {
                        return false;
                    }
                    if (possibeZero == IExpr.COMPARE_TERNARY.TRUE) {
                        trueCounter++;
                    }
                    possibeZero = isPossibeZeroFixedValues(F.CN1, function, variables, engine);
                    if (possibeZero == IExpr.COMPARE_TERNARY.FALSE) {
                        return false;
                    }
                    if (possibeZero == IExpr.COMPARE_TERNARY.TRUE) {
                        trueCounter++;
                    }
                    possibeZero = isPossibeZeroFixedValues(F.CI, function, variables, engine);
                    if (possibeZero == IExpr.COMPARE_TERNARY.FALSE) {
                        return false;
                    }
                    if (possibeZero == IExpr.COMPARE_TERNARY.TRUE) {
                        trueCounter++;
                    }
                    possibeZero = isPossibeZeroFixedValues(F.CNI, function, variables, engine);
                    if (possibeZero == IExpr.COMPARE_TERNARY.FALSE) {
                        return false;
                    }
                    if (possibeZero == IExpr.COMPARE_TERNARY.TRUE) {
                        trueCounter++;
                    }
                    if (trueCounter == 5) {
                        // 2. step test some random complex numeric values
                        for (int i = 0; i < 36; i++) {
                            possibeZero = isPossibeZero(function, variables, engine);
                            if (possibeZero == IExpr.COMPARE_TERNARY.FALSE) {
                                return false;
                            }
                            if (possibeZero == IExpr.COMPARE_TERNARY.TRUE) {
                                trueCounter++;
                            }
                        }
                        if (trueCounter > 28) {
                            return true;
                        }
                    }
                    if (fastTest) {
                        return false;
                    }
                }
            }
        }
        IExpr temp = function.replaceAll(x -> // 
        x.isNumericFunction(true) ? IExpr.ofNullable(x.evalNumber()) : F.NIL);
        if (temp.isPresent()) {
            temp = engine.evaluate(temp);
            if (temp.isZero()) {
                return true;
            }
        }
        return isZeroTogether(function, engine);
    } catch (ValidateException ve) {
        LOGGER.debug("PredicateQ.isPossibleZeroQ() failed", ve);
    }
    return false;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) COMPARE_TERNARY(org.matheclipse.core.interfaces.IExpr.COMPARE_TERNARY) INumber(org.matheclipse.core.interfaces.INumber) VariablesSet(org.matheclipse.core.convert.VariablesSet) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

VariablesSet (org.matheclipse.core.convert.VariablesSet)1 ValidateException (org.matheclipse.core.eval.exception.ValidateException)1 IAST (org.matheclipse.core.interfaces.IAST)1 IExpr (org.matheclipse.core.interfaces.IExpr)1 COMPARE_TERNARY (org.matheclipse.core.interfaces.IExpr.COMPARE_TERNARY)1 INumber (org.matheclipse.core.interfaces.INumber)1