Search in sources :

Example 56 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine 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 57 with EvalEngine

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

the class QuarticSolver method sortASTArguments.

/**
 * Sort the arguments, which are assumed to be of type <code>List()</code>
 *
 * @param resultList
 * @return
 */
public static IASTMutable sortASTArguments(IASTMutable resultList) {
    if (resultList.isList()) {
        EvalEngine engine = EvalEngine.get();
        IASTAppendable result = F.ListAlloc(resultList.size());
        for (int i = 1; i < resultList.size(); i++) {
            IExpr temp = resultList.get(i);
            // if (temp.isList()) {
            temp = engine.evaluate(temp);
            if (temp.isList()) {
                EvalAttributes.sort((IASTMutable) temp);
                result.append(temp);
            } else {
                result.append(resultList.get(i));
            }
        // } else {
        // result.append(temp);
        // }
        }
        EvalAttributes.sort(result);
        return result;
    }
    return resultList;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 58 with EvalEngine

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

the class ExprEvaluatorTests method builtinFunctionFuzz.

/**
 * Fuzz testing - automated software testing that involves providing random arguments as inputs
 * for the Symja built-in functions.
 */
public static void builtinFunctionFuzz() {
    Config.FILESYSTEM_ENABLED = false;
    EvalEngine engine = new EvalEngine(true);
    engine.setRecursionLimit(256);
    engine.setIterationLimit(1000);
    ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
    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);
    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 = 20000;
    Config.MAX_POLYNOMIAL_DEGREE = 100;
    IAST seedList = // 
    F.List(// 
    ba, // 
    b0a, // 
    nae, // 
    S.$Aborted, // 
    S.False, // 
    S.True, // 
    S.E, // 
    S.Pi, // 
    S.Indeterminate, // 
    F.Missing("test"), // 
    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.ZZ(42), // 
    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.CN2, // 
    F.CN3, // 
    F.CN5, // 
    F.CN7, // 
    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.ZZ(Byte.MIN_VALUE), // 
    F.ZZ(Byte.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))), // 
    S.Graph.of(F.List(F.Rule(F.C1, F.C2), F.Rule(F.C2, F.C3), F.Rule(F.C3, F.C1))), // 
    S.Graph.of(F.List()), S.Graph.of(F.List(F.Rule(F.C1, F.C2), F.Rule(F.C2, F.C3), F.Rule(F.C3, F.C1)), // 
    F.List(F.Rule(S.EdgeWeight, F.List(F.CD0, F.CD1, F.CD1)))), // 
    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();
    String[] functionStrs = AST2Expr.FUNCTION_STRINGS;
    int[] counter = new int[] { 0 };
    for (int loop = 0; loop < 20000; loop++) {
        for (int i = 0; i < functionStrs.length; i++) {
            IBuiltInSymbol sym = (IBuiltInSymbol) F.symbol(functionStrs[i]);
            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.Power || sym == S.OptimizeExpression || sym == S.Share || sym == S.Set || sym == S.SetDelayed || sym == S.UpSet || sym == S.UpSetDelayed) {
                continue;
            }
            IEvaluator evaluator = sym.getEvaluator();
            if (evaluator instanceof IFunctionEvaluator) {
                int[] argSize = ((IFunctionEvaluator) evaluator).expectedArgSize(null);
                if (argSize != null) {
                    int end = argSize[1];
                    if (end <= 10) {
                        int start = argSize[0];
                        if (start == 0) {
                            start = 1;
                        }
                        generateASTs(sym, start, end, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, false);
                        generateASTs(sym, start, end, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, false);
                        if (argSize.length > 2) {
                            generateASTs(sym, start, end, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, true);
                            generateASTs(sym, start, end, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, true);
                        }
                        continue;
                    } else {
                        int start = random.nextInt(argSize[0], 10);
                        generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, false);
                        generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, false);
                        if (argSize.length > 2) {
                            generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, true);
                            generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, true);
                        }
                    }
                } else {
                    int start = random.nextInt(1, 7);
                    generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, false);
                    generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, false, true);
                    generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, false);
                    generateASTs(sym, start, start + 4, seedList, random, counter, (IFunctionEvaluator) evaluator, engine, true, true);
                }
            }
        }
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) NumericArrayExpr(org.matheclipse.core.expression.data.NumericArrayExpr) ByteArrayExpr(org.matheclipse.core.expression.data.ByteArrayExpr) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) EvalEngine(org.matheclipse.core.eval.EvalEngine) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IAST(org.matheclipse.core.interfaces.IAST)

Example 59 with EvalEngine

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

the class ExprEvaluatorTests method checkEvaluator.

void checkEvaluator(IAST ast, String expected) {
    EvalEngine engine = EvalEngine.get();
    try {
        IFunctionEvaluator evaluator = (IFunctionEvaluator) ((IBuiltInSymbol) ast.topHead()).getEvaluator();
        if (evaluator instanceof IFunctionEvaluator) {
            IExpr result = evaluator.evaluate(ast, engine);
            assertEquals(expected, result.toString());
            return;
        }
    } catch (RuntimeException rex) {
        rex.printStackTrace();
    }
    fail();
}
Also used : IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 60 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine 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

EvalEngine (org.matheclipse.core.eval.EvalEngine)131 IExpr (org.matheclipse.core.interfaces.IExpr)71 IAST (org.matheclipse.core.interfaces.IAST)39 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)20 ISymbol (org.matheclipse.core.interfaces.ISymbol)20 IOException (java.io.IOException)13 F (org.matheclipse.core.expression.F)12 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)11 S (org.matheclipse.core.expression.S)11 IInteger (org.matheclipse.core.interfaces.IInteger)11 ASTNode (org.matheclipse.parser.client.ast.ASTNode)11 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 AST2Expr (org.matheclipse.core.convert.AST2Expr)9 ExprParser (org.matheclipse.core.parser.ExprParser)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 MathException (org.matheclipse.parser.client.math.MathException)8 ArrayList (java.util.ArrayList)7 Config (org.matheclipse.core.basic.Config)7 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)7