Search in sources :

Example 51 with IASTMutable

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

the class TeXTests method testSmartFuzz.

public void testSmartFuzz() {
    boolean quietMode = true;
    EvalEngine engine = EvalEngine.get();
    List<ASTNode> node = parseFileToList();
    IExpr temp;
    TeXFormFactory fTeXFactory = new TeXFormFactory();
    OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
    fInputFactory.setInputForm(true);
    AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
    IAST seedList = // 
    F.List(// 
    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.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), // 
    S.True, // 
    S.False, // 
    F.assoc(F.CEmptyList), // 
    F.assoc(F.List(F.Rule(F.x, F.y))), // 
    F.CEmptyList, // 
    F.List(F.Rule(F.C1, F.C0)), // 
    F.List(F.Rule(F.x, F.CN1)), // 
    F.C0, // 
    F.C1, // 
    F.CN1, // 
    F.C2, // 
    F.CN2, // 
    F.CN10, // 
    F.CN1D2, // 
    F.C1D2, // 
    F.CNI, // 
    F.CI, // 
    F.CInfinity, // 
    F.CNInfinity, // 
    F.Null, // 
    F.Power(F.x, F.C2), // 
    F.ComplexInfinity, // 
    F.x_, // 
    F.y_, // 
    F.C1DSqrt5, // 
    F.Slot1, // 
    F.stringx(""), // 
    F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
    int counter = 0;
    ThreadLocalRandom random = ThreadLocalRandom.current();
    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) {
                final StringBuilder buf = new StringBuilder();
                int seedIndex = random.nextInt(1, seedList.size());
                IExpr seed = seedList.get(seedIndex);
                IASTMutable mutant = ((IAST) temp).copy();
                int randomIndex = random.nextInt(1, mutant.size());
                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());
                    mutant.set(randomIndex, seed);
                }
                engine.init();
                engine.setQuietMode(quietMode);
                engine.setRecursionLimit(256);
                engine.setIterationLimit(1000);
                final String mutantStr = fInputFactory.toString(mutant);
                try {
                    // System.out.print(".");
                    if (counter++ > 80) {
                        // System.out.println("");
                        counter = 0;
                        System.out.flush();
                        System.err.flush();
                    }
                    // eval.eval(mutantStr);
                    fTeXFactory.convert(buf, mutant, 0);
                    System.out.println(buf.toString());
                } catch (FlowControlException mex) {
                    if (!quietMode) {
                        System.err.println(mutantStr);
                        mex.printStackTrace();
                        System.err.println();
                    }
                } catch (SyntaxError se) {
                    if (!quietMode) {
                        System.err.println(mutantStr);
                        se.printStackTrace();
                        System.err.println();
                    }
                // fail();
                } catch (MathException mex) {
                    System.err.println(mutantStr);
                    mex.printStackTrace();
                    System.err.println();
                    fail();
                } catch (RuntimeException rex) {
                    System.err.println(mutantStr);
                    rex.printStackTrace();
                    fail();
                } catch (Error rex) {
                    System.err.println(mutantStr);
                    if (rex instanceof StackOverflowError) {
                        System.err.println("java.lang.StackOverflowError");
                        rex.printStackTrace();
                    } else {
                        rex.printStackTrace();
                        fail();
                    }
                }
            }
        }
    }
// return result;
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) AST2Expr(org.matheclipse.core.convert.AST2Expr) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) TeXFormFactory(org.matheclipse.core.form.tex.TeXFormFactory) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 52 with IASTMutable

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

the class EvalEngine method evalAttributes.

/**
 * Evaluate an AST according to the attributes set in the header symbol. The evaluation steps are
 * controlled by the header attributes.
 *
 * @param symbol the header symbol
 * @param mutableAST the AST which should be evaluated. If <code>symbol</code> has attribute
 *        {@link ISymbol#ORDERLESS} the mutableAST will be modified.
 * @return <code>F.NIL</code> if no evaluation was possible
 */
public IExpr evalAttributes(ISymbol symbol, IASTMutable mutableAST) {
    final int astSize = mutableAST.size();
    if (astSize == 2) {
        return evalASTArg1(mutableAST);
    }
    IExpr result = mutableAST.head().evaluateHead(mutableAST, this);
    if (result.isPresent()) {
        return result;
    }
    if (astSize != 1) {
        IASTMutable returnResult = F.NIL;
        final int attributes = symbol.getAttributes();
        if ((attributes & ISymbol.SEQUENCEHOLD) != ISymbol.SEQUENCEHOLD) {
            if ((result = F.flattenSequence(mutableAST)).isPresent()) {
                return result;
            }
        }
        IASTMutable resultList = evalArgs(mutableAST, attributes);
        if (resultList.isPresent()) {
            return resultList;
        }
        // ONEIDENTITY is checked in the evalASTArg1() method!
        if (ISymbol.hasFlatAttribute(attributes)) {
            // associative symbol
            IASTAppendable flattened;
            if ((flattened = EvalAttributes.flatten(mutableAST)).isPresent()) {
                returnResult = flattened;
                mutableAST = returnResult;
            }
        }
        result = evalTagSetPlusTimes(mutableAST);
        if (result.isPresent()) {
            return result;
        }
        if ((ISymbol.LISTABLE & attributes) == ISymbol.LISTABLE && !((mutableAST.getEvalFlags() & IAST.IS_LISTABLE_THREADED) == IAST.IS_LISTABLE_THREADED)) {
            // thread over the lists
            resultList = threadASTListArgs(mutableAST, S.Thread, "tdlen");
            if (resultList.isPresent()) {
                return evalArgs(resultList, ISymbol.NOATTRIBUTE).orElse(resultList);
            }
            int indx = mutableAST.indexOf(x -> x.isAssociation());
            if (indx > 0) {
                return ((IAssociation) mutableAST.get(indx)).mapThread(mutableAST, indx);
            }
        }
        if ((ISymbol.NUMERICFUNCTION & attributes) == ISymbol.NUMERICFUNCTION) {
            if (!((ISymbol.HOLDALL & attributes) == ISymbol.HOLDALL)) {
                if (mutableAST.exists(x -> x.isIndeterminate())) {
                    return S.Indeterminate;
                }
                IExpr temp = mutableAST.extractConditionalExpression(false);
                if (temp.isPresent()) {
                    return temp;
                }
            }
        } else if (mutableAST.isBooleanFunction() || mutableAST.isComparatorFunction()) {
            IExpr temp = mutableAST.extractConditionalExpression(false);
            if (temp.isPresent()) {
                return temp;
            }
        }
        if (astSize > 2 && ISymbol.hasOrderlessAttribute(attributes)) {
            // commutative symbol
            EvalAttributes.sortWithFlags(mutableAST);
        }
        return returnResult;
    }
    return F.NIL;
}
Also used : IAssociation(org.matheclipse.core.interfaces.IAssociation) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 53 with IASTMutable

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

the class EvalEngine method evalFlatOrderlessAttributesRecursive.

/**
 * Evaluate the Flat and Orderless attributes of the given <code>ast</code> recursively.
 *
 * @param ast
 * @return <code>F.NIL</code> if no evaluation was possible
 */
public IAST evalFlatOrderlessAttributesRecursive(final IAST ast) {
    if (ast.isEvalFlagOn(IAST.IS_FLAT_ORDERLESS_EVALED)) {
        return F.NIL;
    }
    final ISymbol symbol = ast.topHead();
    final int attributes = symbol.getAttributes();
    // final Predicate<IExpr> isPattern = Predicates.isPattern();
    IASTMutable resultList = F.NIL;
    if ((ISymbol.HOLDALL & attributes) != ISymbol.HOLDALL) {
        final int astSize = ast.size();
        if ((ISymbol.HOLDFIRST & attributes) == ISymbol.NOATTRIBUTE) {
            // the HoldFirst attribute isn't set here
            if (astSize > 1 && ast.arg1().isAST()) {
                IExpr expr = ast.arg1();
                if (ast.arg1().isAST()) {
                    IAST temp = (IAST) ast.arg1();
                    expr = evalFlatOrderlessAttributesRecursive(temp);
                    if (expr.isPresent()) {
                        resultList = ast.setAtCopy(1, expr);
                    } else {
                        expr = ast.arg1();
                    }
                }
            }
        }
        if (astSize > 2) {
            if ((ISymbol.HOLDREST & attributes) == ISymbol.NOATTRIBUTE) {
                // the HoldRest attribute isn't set here
                for (int i = 2; i < astSize; i++) {
                    if (ast.get(i).isAST()) {
                        IAST temp = (IAST) ast.get(i);
                        IExpr expr = evalFlatOrderlessAttributesRecursive(temp);
                        if (expr.isPresent()) {
                            if (!resultList.isPresent()) {
                                resultList = ast.copy();
                            }
                            resultList.set(i, expr);
                        }
                    }
                }
            }
        }
    }
    if (resultList.isPresent()) {
        if (resultList.size() > 2) {
            if (ISymbol.hasFlatAttribute(attributes)) {
                // associative
                IASTAppendable result;
                if ((result = EvalAttributes.flattenDeep(resultList)).isPresent()) {
                    resultList = result;
                    if (ISymbol.hasOrderlessAttribute(attributes)) {
                        EvalAttributes.sortWithFlags(resultList);
                    }
                    resultList.addEvalFlags(IAST.IS_FLAT_ORDERLESS_EVALED);
                    return resultList;
                }
            }
            if (ISymbol.hasOrderlessAttribute(attributes)) {
                EvalAttributes.sortWithFlags(resultList);
            }
        }
        resultList.addEvalFlags(IAST.IS_FLAT_ORDERLESS_EVALED);
        return resultList;
    }
    if (ISymbol.hasFlatAttribute(attributes)) {
        // associative
        IASTAppendable result;
        if ((result = EvalAttributes.flattenDeep(ast)).isPresent()) {
            resultList = result;
            if (ISymbol.hasOrderlessAttribute(attributes)) {
                EvalAttributes.sortWithFlags(resultList);
            }
            resultList.addEvalFlags(IAST.IS_FLAT_ORDERLESS_EVALED);
            return resultList;
        }
    }
    if (ISymbol.hasOrderlessAttribute(attributes)) {
        if (EvalAttributes.sortWithFlags((IASTMutable) ast)) {
            ast.addEvalFlags(IAST.IS_FLAT_ORDERLESS_EVALED);
            return ast;
        }
        return ast;
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 54 with IASTMutable

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

the class EvalAttributes method threadList.

/**
 * Thread through all (sub-)lists in the arguments of the IAST (i.e. typically the ASTs head has
 * the attribute ISymbol.LISTABLE) example: <code>Sin[{2,x,Pi}] ==> {Sin[2],Sin[x],Sin[Pi]}</code>
 *
 * @param ast
 * @param listHead the lists head (typically <code>F.List</code>)
 * @param argHead the arguments head (typically <code>ast.head()</code>)
 * @param listLength the length of the list
 * @return the resulting ast with the <code>argHead</code> threaded into each ast argument.
 */
public static IASTMutable threadList(final IAST ast, final IExpr listHead, final IExpr argHead, final int listLength) {
    if (listLength == 0) {
        return F.headAST0(listHead);
    }
    IASTMutable result = F.NIL;
    final int listSize = ast.size();
    for (int j = 1; j < listLength + 1; j++) {
        final IASTMutable subResult = F.astMutable(argHead, listSize - 1);
        for (int i = 1; i < listSize; i++) {
            if (// 
            listHead == S.List && (ast.get(i).isList() || ast.get(i).isSparseArray())) {
                if (ast.get(i).isList()) {
                    final IAST arg = (IAST) ast.get(i);
                    subResult.set(i, arg.get(j));
                } else if (ast.get(i).isSparseArray()) {
                    final ISparseArray arg = (ISparseArray) ast.get(i);
                    subResult.set(i, arg.get(j));
                // subResult.set(i, Programming.sparsePart(arg, F.Part(arg, F.ZZ(j)), 2,
                // EvalEngine.get()));
                }
            } else if (listHead == S.SparseArray) {
                if (ast.get(i).isList()) {
                    final IAST arg = (IAST) ast.get(i);
                    if (j >= arg.size()) {
                        return F.NIL;
                    }
                    subResult.set(i, arg.get(j));
                } else if (ast.get(i).isSparseArray()) {
                    final ISparseArray arg = (ISparseArray) ast.get(i);
                    if (j >= arg.size()) {
                        return F.NIL;
                    }
                    subResult.set(i, arg.get(j));
                } else {
                    subResult.set(i, ast.get(i));
                }
            } else if (ast.get(i).isAST(listHead)) {
                final IAST arg = (IAST) ast.get(i);
                subResult.set(i, arg.get(j));
            } else {
                subResult.set(i, ast.get(i));
            }
        }
        if (!result.isPresent()) {
            IExpr head = listHead == S.SparseArray ? S.List : listHead;
            switch(listLength) {
                case 1:
                    result = F.unaryAST1(head, F.Slot1);
                    break;
                case 2:
                    result = F.binaryAST2(head, F.Slot1, F.Slot2);
                    break;
                case 3:
                    result = F.ternaryAST3(head, F.Slot1, F.Slot2, F.Slot3);
                    break;
                default:
                    result = F.astMutable(head, listLength);
            }
        }
        result.set(j, subResult);
    }
    if (listHead == S.SparseArray) {
        return F.unaryAST1(S.SparseArray, result);
    }
    return result;
}
Also used : ISparseArray(org.matheclipse.core.interfaces.ISparseArray) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 55 with IASTMutable

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

the class EvalAttributes method copySort.

/**
 * Copy the <code>ast</code> and return the sorted copy using function <code>Order(a, b)</code>.
 *
 * @param ast
 * @return return the sorted copy
 */
public static final IAST copySort(final IAST ast) {
    final IASTMutable sortedList = ast.copy();
    sort(sortedList);
    return sortedList;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Aggregations

IASTMutable (org.matheclipse.core.interfaces.IASTMutable)92 IExpr (org.matheclipse.core.interfaces.IExpr)60 IAST (org.matheclipse.core.interfaces.IAST)34 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)26 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 IInteger (org.matheclipse.core.interfaces.IInteger)5 Map (java.util.Map)4 IComplex (org.matheclipse.core.interfaces.IComplex)4 IRational (org.matheclipse.core.interfaces.IRational)4 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)3 ValidateException (org.matheclipse.core.eval.exception.ValidateException)3 INumber (org.matheclipse.core.interfaces.INumber)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 ISparseArray (org.matheclipse.core.interfaces.ISparseArray)3 ASTNode (org.matheclipse.parser.client.ast.ASTNode)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2