Search in sources :

Example 1 with ValidateException

use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.

the class EvalEngine method evalASTBuiltinFunction.

/**
 * @param symbol
 * @param ast
 * @return <code>F.NIL</code> if no evaluation happened
 */
private IExpr evalASTBuiltinFunction(final ISymbol symbol, IAST ast) {
    final int attributes = symbol.getAttributes();
    if (fEvalLHSMode) {
        if ((ISymbol.HOLDALL & attributes) == ISymbol.HOLDALL) {
            // (i.e. Sin, Cos,...)
            if (!(symbol.equals(S.Set) || symbol.equals(S.SetDelayed) || symbol.equals(S.UpSet) || symbol.equals(S.UpSetDelayed))) {
                return F.NIL;
            }
        } else {
            if ((ISymbol.NUMERICFUNCTION & attributes) != ISymbol.NUMERICFUNCTION) {
                return F.NIL;
            }
        }
    }
    if (!symbol.equals(S.Integrate)) {
        IExpr result;
        if ((result = symbol.evalDownRule(this, ast)).isPresent()) {
            return result;
        }
    }
    if (symbol.isBuiltInSymbol()) {
        final IEvaluator evaluator = ((IBuiltInSymbol) symbol).getEvaluator();
        if (evaluator instanceof IFunctionEvaluator) {
            if (ast.isEvalFlagOn(IAST.BUILT_IN_EVALED) && isSymbolicMode(attributes)) {
                return F.NIL;
            }
            // evaluate a built-in function.
            final IFunctionEvaluator functionEvaluator = (IFunctionEvaluator) evaluator;
            OptionsResult opres = checkBuiltinArguments(ast, functionEvaluator);
            if (opres == null) {
                return F.NIL;
            }
            ast = opres.result;
            try {
                if (evaluator instanceof AbstractFunctionOptionEvaluator) {
                    AbstractFunctionOptionEvaluator optionsEvaluator = (AbstractFunctionOptionEvaluator) evaluator;
                    IExpr result = optionsEvaluator.evaluate(ast, opres.argSize, opres.options, this);
                    if (result.isPresent()) {
                        return result;
                    }
                } else {
                    IExpr result = fNumericMode ? functionEvaluator.numericEval(ast, this) : functionEvaluator.evaluate(ast, this);
                    if (result.isPresent()) {
                        return result;
                    }
                }
            } catch (ValidateException ve) {
                return IOFunctions.printMessage(ast.topHead(), ve, this);
            } catch (FlowControlException e) {
                throw e;
            } catch (SymjaMathException ve) {
                LOGGER.log(getLogLevel(), ast.topHead(), ve);
                return F.NIL;
            }
        // cannot generally set the result as evaluated in built-in function. Especially problems in
        // `togetherMode`
        // if (isSymbolicMode(attributes) && !isTogetherMode()) {
        // ast.addEvalFlags(IAST.BUILT_IN_EVALED);
        // }
        }
    }
    return F.NIL;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) ValidateException(org.matheclipse.core.eval.exception.ValidateException) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) AbstractFunctionOptionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) IExpr(org.matheclipse.core.interfaces.IExpr) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

Example 2 with ValidateException

use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.

the class PlusOp method plus.

/**
 * Add an argument <code>arg</code> to this <code>Plus()</code> expression.
 *
 * @param arg
 * @return <code>F.Indeterminate</code> if the result is indeterminated, <code>F.NIL</code>
 *         otherwise.
 */
public IExpr plus(final IExpr arg) {
    // }
    if (arg.isIndeterminate()) {
        return S.Indeterminate;
    }
    try {
        if (numberValue.isPresent() && numberValue.isDirectedInfinity()) {
            if (numberValue.isComplexInfinity()) {
                if (arg.isDirectedInfinity()) {
                    return S.Indeterminate;
                }
                numberValue = F.CComplexInfinity;
                evaled = true;
                return F.NIL;
            } else if (numberValue.isInfinity()) {
                if (arg.isInfinity()) {
                    evaled = true;
                    return F.NIL;
                }
                if (arg.isDirectedInfinity()) {
                    return S.Indeterminate;
                }
                if (arg.isRealResult()) {
                    evaled = true;
                    return F.NIL;
                }
            } else if (numberValue.isNegativeInfinity()) {
                if (arg.isNegativeInfinity()) {
                    evaled = true;
                    return F.NIL;
                }
                if (arg.isDirectedInfinity()) {
                    // Indeterminate expression `1` encountered.
                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(numberValue, arg)), EvalEngine.get());
                    return S.Indeterminate;
                }
                if (arg.isRealResult()) {
                    evaled = true;
                    return F.NIL;
                }
            }
        }
        if (arg.isNumber()) {
            if (arg.isZero()) {
                evaled = true;
                return F.NIL;
            }
            if (!numberValue.isPresent()) {
                numberValue = arg;
                return F.NIL;
            }
            if (numberValue.isNumber()) {
                numberValue = numberValue.plus(arg);
                evaled = true;
                return F.NIL;
            }
            if (numberValue.isInfinity()) {
                if (arg.isNegativeInfinity()) {
                    // Indeterminate expression `1` encountered.
                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(numberValue, arg)), EvalEngine.get());
                    return S.Indeterminate;
                }
                numberValue = F.CInfinity;
                evaled = true;
                return F.NIL;
            }
            if (numberValue.isNegativeInfinity()) {
                numberValue = negativeInfinityPlus(arg);
                if (numberValue.isIndeterminate()) {
                    return S.Indeterminate;
                }
                evaled = true;
                return F.NIL;
            }
            return F.NIL;
        } else if (arg.isQuantity()) {
            // if (arg.isQuantity()) {
            if (!numberValue.isPresent()) {
                numberValue = arg;
                return F.NIL;
            }
            IQuantity q = (IQuantity) arg;
            IExpr temp = q.plus(numberValue, true);
            if (temp.isPresent()) {
                evaled = true;
                numberValue = temp;
            } else {
                if (addMerge(q, F.C1)) {
                    evaled = true;
                }
            }
            return F.NIL;
        // }
        } else if (arg.isAST()) {
            final IAST ast = (IAST) arg;
            final int headID = ((IAST) arg).headID();
            if (headID >= ID.DirectedInfinity) {
                switch(headID) {
                    case ID.DirectedInfinity:
                        if (arg.isDirectedInfinity()) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                if (arg.isComplexInfinity()) {
                                    if (plusMap != null && plusMap.size() > 0) {
                                        evaled = true;
                                    }
                                } else {
                                    if (plusMap != null) {
                                        Iterator<Entry<IExpr, IExpr>> iterator = plusMap.entrySet().iterator();
                                        while (iterator.hasNext()) {
                                            Entry<IExpr, IExpr> entry = iterator.next();
                                            if (entry.getKey().isRealResult()) {
                                                iterator.remove();
                                                evaled = true;
                                            }
                                        }
                                    }
                                }
                                return F.NIL;
                            }
                            if (arg.isInfinity()) {
                                if (numberValue.isNegativeInfinity()) {
                                    // Indeterminate expression `1` encountered.
                                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(arg, numberValue)), EvalEngine.get());
                                    return S.Indeterminate;
                                }
                                numberValue = F.CInfinity;
                                evaled = true;
                                return F.NIL;
                            } else if (arg.isNegativeInfinity()) {
                                numberValue = negativeInfinityPlus(numberValue);
                                if (numberValue.isIndeterminate()) {
                                    return S.Indeterminate;
                                }
                                evaled = true;
                                return F.NIL;
                            } else if (arg.isComplexInfinity()) {
                                if (numberValue.isDirectedInfinity()) {
                                    // Indeterminate expression `1` encountered.
                                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(arg, numberValue)), EvalEngine.get());
                                    return S.Indeterminate;
                                }
                                numberValue = F.CComplexInfinity;
                                evaled = true;
                                return F.NIL;
                            }
                        }
                        break;
                    case ID.Times:
                        if (ast.size() > 1) {
                            if (ast.arg1().isNumber()) {
                                if (addMerge(ast.rest().oneIdentity1(), ast.arg1())) {
                                    evaled = true;
                                }
                                return F.NIL;
                            }
                            if (addMerge(ast, F.C1)) {
                                evaled = true;
                            }
                        }
                        return F.NIL;
                    case ID.Interval:
                        if (arg.isInterval()) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                return F.NIL;
                            }
                            IExpr temp;
                            if (numberValue.isInterval()) {
                                temp = IntervalSym.plus((IAST) numberValue, (IAST) arg);
                            } else {
                                temp = IntervalSym.plus(numberValue, (IAST) arg);
                            }
                            if (temp.isPresent()) {
                                numberValue = temp;
                                evaled = true;
                            } else {
                                if (addMerge(arg, F.C1)) {
                                    evaled = true;
                                }
                            }
                            return F.NIL;
                        }
                        break;
                    // break;
                    case ID.SeriesData:
                        if (arg instanceof ASTSeriesData) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                return F.NIL;
                            }
                            numberValue = ((ASTSeriesData) arg).plus(numberValue);
                            evaled = true;
                            return F.NIL;
                        }
                        break;
                }
            }
        // } else if (!numberValue.isPresent() && arg.isRealResult()) {
        // numberValue = arg;
        // return F.NIL;
        }
        if (addMerge(arg, F.C1)) {
            evaled = true;
        }
    } catch (ValidateException | LimitException e) {
        LOGGER.debug("PlusOp.plus() failed", e);
        throw e;
    } catch (SymjaMathException sme) {
        LOGGER.debug("PlusOp.plus() failed", sme);
    }
    return F.NIL;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) IQuantity(org.matheclipse.core.tensor.qty.IQuantity) Entry(java.util.Map.Entry) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) LimitException(org.matheclipse.core.eval.exception.LimitException) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

Example 3 with ValidateException

use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.

the class AST2Expr method evaluateOnInput.

/**
 * Try some &quot;evaluations&quot; for special expressions directly from the &quot;input
 * form&quot;.
 *
 * @param functionID an id <code>&gt; ID.UNKNOWN</code> i.e. a built-in function ID
 * @param ast
 * @param functionNode
 * @return
 */
private IExpr evaluateOnInput(int functionID, IASTMutable ast, final FunctionNode functionNode) {
    try {
        IExpr expr;
        switch(functionID) {
            case ID.Association:
                if (ast.isAST1() && ast.arg1().isList()) {
                    IExpr arg1 = ast.arg1();
                    if (arg1.isListOfRules(true)) {
                        return F.assoc((IAST) arg1);
                    } else if (arg1.isList1()) {
                        arg1 = arg1.first();
                        if (arg1.isListOfRules(true)) {
                            return F.assoc((IAST) arg1);
                        }
                    }
                }
                break;
            case ID.Get:
                if (ast.isAST1() && ast.arg1().isString()) {
                    return S.Get.of(ast.arg1());
                }
                break;
            case ID.Import:
                if (ast.isAST1() && ast.arg1().isString()) {
                    return S.Import.of(ast.arg1());
                }
                break;
            case ID.N:
                if (ast.isAST2() && ast.arg2().isInteger()) {
                    try {
                        int precision = ast.arg2().toIntDefault();
                        if (EvalEngine.isApfloat(precision)) {
                            fPrecision = precision;
                            ast.set(1, convertNode(functionNode.get(1)));
                        }
                        return ast;
                    } catch (ValidateException ve) {
                    }
                }
                break;
            case ID.Sqrt:
                if (ast.isAST1()) {
                    // rewrite from input: Sqrt(x) => Power(x, 1/2)
                    return F.Power(ast.getUnevaluated(1), F.C1D2);
                }
                break;
            case ID.Exp:
                if (ast.isAST1()) {
                    // rewrite from input: Exp(x) => E^x
                    return F.Power(S.E, ast.getUnevaluated(1));
                }
                break;
            case ID.Power:
                if (ast.isPower() && ast.base().isPower() && ast.exponent().isMinusOne()) {
                    IAST arg1Power = (IAST) ast.base();
                    if (arg1Power.exponent().isNumber()) {
                        // Power(x, - <number>)
                        return F.Power(arg1Power.getUnevaluated(1), ((INumber) arg1Power.getUnevaluated(2)).negate());
                    }
                }
                break;
            case ID.Blank:
                expr = PatternMatching.Blank.CONST.evaluate(ast, fEngine);
                if (expr.isPresent()) {
                    return expr;
                }
                break;
            case ID.BlankSequence:
                expr = PatternMatching.BlankSequence.CONST.evaluate(ast, fEngine);
                if (expr.isPresent()) {
                    return expr;
                }
                break;
            case ID.BlankNullSequence:
                expr = PatternMatching.BlankNullSequence.CONST.evaluate(ast, fEngine);
                if (expr.isPresent()) {
                    return expr;
                }
                break;
            case ID.Pattern:
                expr = PatternMatching.Pattern.CONST.evaluate(ast, fEngine);
                if (expr.isPresent()) {
                    return expr;
                }
                break;
            case ID.Optional:
                expr = PatternMatching.Optional.CONST.evaluate(ast, fEngine);
                if (expr.isPresent()) {
                    return expr;
                }
                break;
            // break;
            case ID.Repeated:
                expr = PatternMatching.Repeated.CONST.evaluate(ast, fEngine);
                if (expr.isPresent()) {
                    return expr;
                }
                break;
            case ID.Complex:
                expr = Arithmetic.CONST_COMPLEX.evaluate(ast, fEngine);
                if (expr.isPresent()) {
                    return expr;
                }
                break;
            case ID.Rational:
                expr = Arithmetic.CONST_RATIONAL.evaluate(ast, fEngine);
                if (expr.isPresent()) {
                    return expr;
                }
                break;
        }
    } catch (ValidateException ve) {
        LOGGER.debug("AST2Expr.evaluateOnInput() failed", ve);
    }
    return F.NIL;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) Apint(org.apfloat.Apint)

Example 4 with ValidateException

use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.

the class D method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (ast.isAST1()) {
        return ast.arg1();
    }
    try {
        final IExpr fx = ast.arg1();
        if (fx.isIndeterminate()) {
            return S.Indeterminate;
        }
        if (ast.size() > 3) {
            // reduce arguments by folding D[fxy, x, y] to D[ D[fxy, x], y] ...
            return ast.foldLeft((x, y) -> engine.evaluateNIL(F.D(x, y)), fx, 2);
        }
        IExpr x = ast.arg2();
        if (!(x.isVariable() || x.isList())) {
            // `1` is not a valid variable.
            return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
        }
        if (fx.isList()) {
            IAST list = (IAST) fx;
            // thread over first list
            return list.mapThreadEvaled(engine, F.ListAlloc(list.size()), ast, 1);
        }
        if (x.isList()) {
            // D[fx_, {...}]
            IAST xList = (IAST) x;
            if (xList.isAST1() && xList.arg1().isListOfLists()) {
                IAST subList = (IAST) xList.arg1();
                IASTAppendable result = F.ListAlloc(subList.size());
                result.appendArgs(subList.size(), i -> F.D(fx, F.list(subList.get(i))));
                return result;
            } else if (xList.isAST1() && xList.arg1().isList()) {
                IAST subList = (IAST) xList.arg1();
                return subList.mapLeft(F.ListAlloc(), (a, b) -> engine.evaluateNIL(F.D(a, b)), fx);
            } else if (xList.isAST2()) {
                if (ast.isEvalFlagOn(IAST.IS_DERIVATIVE_EVALED)) {
                    return F.NIL;
                }
                if (xList.arg1().isList()) {
                    x = F.list(xList.arg1());
                } else {
                    x = xList.arg1();
                }
                IExpr arg2 = xList.arg2();
                int n = arg2.toIntDefault();
                if (n >= 0) {
                    IExpr temp = fx;
                    for (int i = 0; i < n; i++) {
                        temp = S.D.ofNIL(engine, temp, x);
                        if (!temp.isPresent()) {
                            return F.NIL;
                        }
                    }
                    return temp;
                }
                if (arg2.isFree(num -> num.isNumber(), false)) {
                    if (fx instanceof ASTSeriesData) {
                        return F.NIL;
                    }
                    if (fx.isFree(x, true)) {
                        // Piecewise({{fx, arg2 == 0}}, 0)
                        return F.Piecewise(F.list(F.list(fx, F.Equal(arg2, F.C0))), F.C0);
                    }
                    if (fx.equals(x)) {
                        // Piecewise({{fx, arg2 == 0}, {1, arg2 == 1}}, 0)
                        return F.Piecewise(F.list(F.list(fx, F.Equal(arg2, F.C0)), F.list(F.C1, F.Equal(arg2, F.C1))), F.C0);
                    }
                    if (fx.isAST()) {
                        final IAST function = (IAST) fx;
                        // final IExpr header = function.head();
                        if (function.isPlus()) {
                            // D(a_+b_+c_,x_) -> D(a,x)+D(b,x)+D(c,x)
                            return function.mapThread(F.D(F.Slot1, xList), 1);
                        }
                    }
                    return F.NIL;
                }
                if (!x.isVariable()) {
                    // `1` is not a valid variable.
                    return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
                }
                if (arg2.isAST()) {
                    return F.NIL;
                }
                // symbolic expression or a non-negative integer.
                return IOFunctions.printMessage(ast.topHead(), "dvar", F.list(xList), engine);
            }
            return F.NIL;
        }
        if (!x.isVariable()) {
            // `1` is not a valid variable.
            return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
        }
        return binaryD(fx, x, ast, engine);
    } catch (final ValidateException ve) {
        // int number validation
        LOGGER.log(engine.getLogLevel(), ve.getMessage(ast.topHead()), ve);
        return F.NIL;
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) IInteger(org.matheclipse.core.interfaces.IInteger) ISymbol(org.matheclipse.core.interfaces.ISymbol) IOFunctions(org.matheclipse.core.builtin.IOFunctions) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) S(org.matheclipse.core.expression.S) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) Logger(org.apache.logging.log4j.Logger) IRational(org.matheclipse.core.interfaces.IRational) AbstractFunctionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator) DRules(org.matheclipse.core.reflection.system.rules.DRules) IExpr(org.matheclipse.core.interfaces.IExpr) LogManager(org.apache.logging.log4j.LogManager) ValidateException(org.matheclipse.core.eval.exception.ValidateException) BinaryBindIth1st(org.matheclipse.core.generic.BinaryBindIth1st) ValidateException(org.matheclipse.core.eval.exception.ValidateException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 5 with ValidateException

use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.

the class ExprEvaluatorTests method smartFuzz.

/**
 * Fuzz testing - automated software testing that involves providing random arguments as inputs
 * for the input expressions in file <code>./data/harvest.sym</code> harvested from existing JUnit
 * tests of built-in functions.
 */
public static void smartFuzz() {
    Config.MAX_AST_SIZE = 10000;
    Config.MAX_OUTPUT_SIZE = 10000;
    Config.MAX_INPUT_LEAVES = 100L;
    Config.MAX_MATRIX_DIMENSION_SIZE = 100;
    Config.MAX_PRECISION_APFLOAT = 100;
    Config.MAX_BIT_LENGTH = 200000;
    Config.MAX_POLYNOMIAL_DEGREE = 100;
    Config.FILESYSTEM_ENABLED = false;
    boolean quietMode = true;
    EvalEngine engine = new EvalEngine(true);
    engine.setRecursionLimit(256);
    engine.setIterationLimit(1000);
    ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
    List<ASTNode> node = parseFileToList();
    IExpr temp;
    OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
    fInputFactory.setInputForm(true);
    AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
    byte[] bArray = new byte[0];
    ByteArrayExpr ba = ByteArrayExpr.newInstance(bArray);
    byte[] b0Array = new byte[] { 0 };
    ByteArrayExpr b0a = ByteArrayExpr.newInstance(b0Array);
    F.x.setAttributes(ISymbol.PROTECTED);
    F.y.setAttributes(ISymbol.PROTECTED);
    double[] doubleArr = new double[] { 1.0, -1.0, 0.0, 2.0, 100.0, 200.0 };
    int[] dims = new int[] { 2, 3 };
    NumericArrayExpr nae = new NumericArrayExpr(doubleArr, dims, NumericArrayExpr.Real64);
    IAST seedList = // 
    F.List(// 
    ba, // 
    b0a, // 
    nae, // 
    F.complex(-0.5, 0.5), // 
    F.complex(0.0, 0.5), // 
    F.complex(0.0, -1.0), // 
    F.complex(0.0, 1.0), // 
    F.complex(2.0, -1.0), // 
    F.complex(2.0, 1.0), // 
    F.complex(-2.0, -2.0), // 
    F.complex(-2.0, 2.0), // 
    F.complexNum("-0.8", "1.2", 30), // 
    F.num(0.5), // 
    F.num(-0.5), // 
    F.num(Math.PI * (-0.5)), // 
    F.num(Math.PI * 0.5), // 
    F.num(-Math.PI), // 
    F.num(Math.PI), // 
    F.num(-Math.E), // 
    F.num(Math.E), // 
    F.num("-0.8", 30), // 
    F.C0, // 
    F.C1, // 
    F.CN1, // 
    F.CN1D2, // 
    F.C1D2, // 
    F.CNI, // 
    F.CI, // 
    F.CC(Long.MAX_VALUE, Long.MIN_VALUE, Long.MIN_VALUE, Long.MAX_VALUE), // 
    F.QQ(Long.MAX_VALUE, Long.MIN_VALUE), // 
    F.QQ(Long.MIN_VALUE, Long.MAX_VALUE), // 
    F.Slot2, // some primes
    F.C2, F.C3, F.C5, F.C7, F.ZZ(11), F.ZZ(13), F.ZZ(17), F.ZZ(19), F.ZZ(101), F.ZZ(1009), // 
    F.ZZ(10007), // 
    F.ZZ(Integer.MIN_VALUE), // 
    F.ZZ(Integer.MAX_VALUE), // 
    F.CInfinity, // 
    F.CNInfinity, // 
    F.Null, // 
    F.Power(F.x, F.C2), // 
    F.Indeterminate, // 
    F.ComplexInfinity, // 
    F.x_, // 
    F.y_, // any sequence of one or more expressions
    F.x__, // any sequence of one or more expressions
    F.y__, // any sequence of zero or more expressions
    F.x___, // any sequence of zero or more expressions
    F.y___, // 
    F.CEmptyList, // 
    F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1))), // 
    F.assoc(F.List()), // 
    F.assoc(F.List(F.Rule(F.stringx("s1"), F.C0), F.RuleDelayed(F.stringx("s2"), F.C1))), F.assoc(F.List(F.Rule(F.stringx("s1"), F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1)))), F.RuleDelayed(F.stringx("s2"), // 
    F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1)))))), // 
    SparseArrayExpr.newDenseList(F.List(F.C0, F.C0), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.C0, F.C1, F.C0, F.C2), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), F.C0), // 
    F.Function(F.EvenQ(F.Slot1)), // 
    F.Function(F.Expand(F.Power(F.Plus(F.C2, F.Slot1), F.C3))), // 
    F.Graph(F.List(F.Rule(F.C1, F.C2), F.Rule(F.C2, F.C3), F.Rule(F.C3, F.C1))), // 
    F.Graph(F.List()), // 
    F.CEmptySequence, // 
    F.CEmptyList, // 
    F.List(F.List(F.C0)), // 
    F.List(F.List(F.C1)), // 
    F.List(F.List(F.CN1)), // 
    F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), // 
    F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), // 
    F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1), F.C0), // 
    F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0), F.C0), F.List(F.num("-3.1415", 30), F.num("2.987", 30), F.num("-1", 30), F.num("0.0", 30), // 
    F.num("1", 30)), // 
    F.List(F.CN1, F.CN2, F.C3), // 
    F.List(F.CN1D2, F.CN2, F.C3), // 
    F.List(F.x, F.CN2, F.C3), // 
    F.List(F.x, F.C5, F.CN3), // 
    F.List(F.x, F.CN3, F.CN1D2), // 
    F.List(F.x, F.CN1D2, F.C1D2, F.C1D4), // 
    F.List(F.C0, F.C0), // 
    F.List(F.C0, F.C0, F.C0), // 
    F.List(F.C1, F.C2, F.C3), // 
    F.List(F.C1, F.C1, F.C1), // 
    F.List(F.C1, F.C2, F.C3, F.a), // 
    F.List(F.C0, F.C0, F.C0, F.C0), // 
    F.List(F.C1, F.C1, F.C1, F.C1), // 
    F.List(F.x, F.CN1, F.C1, F.C1), // 
    F.List(F.x, F.C0, F.C0, F.C0), // 
    F.List(F.x, F.C1, F.CN1, F.CN1), // 
    F.List(F.CN1), // 
    F.List(F.C0), // 
    F.List(F.C1), // simulate level spec
    F.List(F.CN5), // simulate level spec
    F.List(F.C7), // 
    F.List(F.complex(0.0, -1.0)), // 
    F.List(F.complex(0.0, 1.0)), // 
    F.List(F.x), // 
    F.List(F.CN3D2), // 
    F.List(F.C3D2), // 
    F.List(F.C3D4), // 
    F.Part(F.x, F.C1), // 
    F.Part(F.x, F.C2), // 
    F.Part(F.x, F.ZZ(Integer.MAX_VALUE)), // 
    F.Part(F.x, F.CN1, F.C1, F.C1), // 
    F.Part(F.x, F.C1, F.C1, F.C1, F.C1), // 
    F.C1DSqrt5, // GoldenRatio
    F.Divide(F.Plus(F.C1, F.Sqrt(5)), F.C2), // 1/GoldenRatio
    F.Divide(F.C2, F.Plus(F.C1, F.Sqrt(5))), // 
    F.Negate(F.Sqrt(2)), // 
    F.Divide(F.Sqrt(2), F.C2), // 
    F.Negate(F.Divide(F.Sqrt(2), F.C2)), // 
    F.Plus(F.Sqrt(2), F.C1), // 
    F.Plus(F.Sqrt(2), F.CN1), // 
    F.Exp(F.Times(F.Pi, F.CI, F.C1D3)), // 
    F.Plus(F.C1, F.CI), // 
    F.Plus(F.CN1, F.CI), // 
    F.Times(F.Sqrt(2), F.C7), // 
    F.Times(F.Sqrt(2), F.Sqrt(5)), // 
    F.CSqrt2, // 
    F.C2Pi, // 
    F.CN3D2, // 
    F.C3D2, // 
    F.C3D4, // 
    F.QQ(Long.MAX_VALUE, 7L), // 
    F.QQ(Long.MIN_VALUE, 11L), // 
    F.QQ(7, Long.MAX_VALUE), // 
    F.QQ(11, Long.MAX_VALUE), // 
    F.QQ(Long.MAX_VALUE, Long.MAX_VALUE), // 
    F.QQ(Long.MIN_VALUE, Long.MAX_VALUE), // 
    F.Slot2, // 
    F.Slot(Integer.MAX_VALUE), // 
    IQuantity.of(1.2, "m"), // 
    F.RegularExpression("?i)"), // 
    F.CEmptyString, // 
    F.stringx("\\"), // 
    F.stringx("\r"), // 
    F.stringx("\t"), // 
    F.stringx("\n"), // 
    F.stringx("\r\n"), // 
    F.stringx("\n   "), // 
    F.stringx("\uffff"), // division by zero problem
    F.Power(F.C0, F.CN1), // 
    F.Subtract(F.C1, F.C1), // 
    F.Rule(S.Modulus, F.C2), // 
    F.Rule(S.Modulus, F.C10), // 
    F.Rule(S.Heads, S.True), // 
    F.Rule(S.Heads, S.False), // 
    F.$OptionsPattern(), // 
    F.OptionValue(F.a), // 
    F.OptionValue(F.b), // 
    F.OptionValue(F.x), F.OptionValue(F.y));
    ThreadLocalRandom random = ThreadLocalRandom.current();
    SlowComputationThread thread = null;
    for (int j = 1; j < 10000; j++) {
        int i = 0;
        while (i < node.size()) {
            temp = ast2Expr.convert(node.get(i++));
            if (temp.isAST() && temp.size() > 1) {
                int seedIndex = random.nextInt(1, seedList.size());
                IExpr seed = seedList.get(seedIndex);
                String mutantStr = "initial";
                IASTMutable mutant = ((IAST) temp).copy();
                try {
                    ISymbol sym = mutant.topHead();
                    if (sym == S.PolynomialGCD || sym == S.TestReport || sym == S.VerificationTest || sym == S.On || sym == S.Off || sym == S.Compile || sym == S.CompiledFunction || sym == S.FactorialPower || sym == S.Pause || sym == S.OptimizeExpression || sym == S.Share || sym == S.Set || sym == S.SetDelayed || sym == S.UpSet || sym == S.UpSetDelayed) {
                        continue;
                    }
                    int randomIndex = random.nextInt(1, mutant.size());
                    if (mutant.isAssociation()) {
                        mutant.set(randomIndex, F.Rule(F.ZZ(randomIndex), seed));
                    } else {
                        mutant.set(randomIndex, seed);
                    }
                    for (int k = 0; k < 1; k++) {
                        seedIndex = random.nextInt(1, seedList.size());
                        seed = seedList.get(seedIndex);
                        randomIndex = random.nextInt(1, mutant.size());
                        if (mutant.isAssociation()) {
                            mutant.set(randomIndex, F.Rule(F.ZZ(randomIndex), seed));
                        } else {
                            mutant.set(randomIndex, seed);
                        }
                    }
                    engine.init();
                    engine.setQuietMode(quietMode);
                    engine.setRecursionLimit(256);
                    engine.setIterationLimit(1000);
                    // mutantStr = fInputFactory.toString(mutant);
                    // System.out.println(">> " + mutantStr);
                    // if (counter++ > 80) {
                    // System.out.println("");
                    // counter = 0;
                    // System.out.flush();
                    // System.err.flush();
                    // }
                    thread = new SlowComputationThread(">> " + mutant.toString(), engine);
                    thread.start();
                    engine.evaluate(mutant);
                } catch (FlowControlException mex) {
                    if (!quietMode) {
                        System.err.println(mutant.toString());
                        mex.printStackTrace();
                        System.err.println();
                    }
                } catch (SyntaxError se) {
                    System.err.println(mutant.toString());
                    se.printStackTrace();
                    System.err.println();
                // fail();
                } catch (ValidateException ve) {
                    System.err.println(mutant.toString());
                    ve.printStackTrace();
                    System.err.println();
                // fail();
                } catch (MathException mex) {
                    System.err.println(mutant.toString());
                    mex.printStackTrace();
                    System.err.println();
                    fail();
                } catch (RuntimeException rex) {
                    System.err.println(mutant.toString());
                    rex.printStackTrace();
                    fail();
                } catch (Error rex) {
                    System.err.println(mutant.toString());
                    if (rex instanceof StackOverflowError) {
                        System.err.println("java.lang.StackOverflowError");
                        rex.printStackTrace();
                        fail();
                    } else {
                        System.err.println(mutantStr);
                        rex.printStackTrace();
                        fail();
                    }
                } finally {
                    thread.terminate();
                    thread.interrupt();
                }
            }
        }
    }
// return result;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) SyntaxError(org.matheclipse.parser.client.SyntaxError) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IAST(org.matheclipse.core.interfaces.IAST) ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) ISymbol(org.matheclipse.core.interfaces.ISymbol) NumericArrayExpr(org.matheclipse.core.expression.data.NumericArrayExpr) SyntaxError(org.matheclipse.parser.client.SyntaxError) AST2Expr(org.matheclipse.core.convert.AST2Expr) ByteArrayExpr(org.matheclipse.core.expression.data.ByteArrayExpr) MathException(org.matheclipse.parser.client.math.MathException) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

ValidateException (org.matheclipse.core.eval.exception.ValidateException)16 IAST (org.matheclipse.core.interfaces.IAST)12 IExpr (org.matheclipse.core.interfaces.IExpr)12 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)5 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 Complex (org.hipparchus.complex.Complex)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 SymjaMathException (org.matheclipse.core.eval.exception.SymjaMathException)3 VariablesSet (org.matheclipse.core.convert.VariablesSet)2 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)2 LimitException (org.matheclipse.core.eval.exception.LimitException)2 ASTSeriesData (org.matheclipse.core.expression.ASTSeriesData)2 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)2 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)2 SyntaxError (org.matheclipse.parser.client.SyntaxError)2 MathException (org.matheclipse.parser.client.math.MathException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1