Search in sources :

Example 81 with ISymbol

use of org.matheclipse.core.interfaces.ISymbol 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 attr = symbol.getAttributes();
    // final Predicate<IExpr> isPattern = Predicates.isPattern();
    IAST resultList = F.NIL;
    if ((ISymbol.HOLDALL & attr) != ISymbol.HOLDALL) {
        final int astSize = ast.size();
        if ((ISymbol.HOLDFIRST & attr) == 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 & attr) == 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.FLAT & attr) == ISymbol.FLAT) {
                // associative
                IAST result;
                if ((result = EvalAttributes.flatten(resultList)).isPresent()) {
                    resultList = result;
                    if ((ISymbol.ORDERLESS & attr) == ISymbol.ORDERLESS) {
                        EvalAttributes.sort(resultList);
                    }
                    resultList.addEvalFlags(IAST.IS_FLAT_ORDERLESS_EVALED);
                    return resultList;
                }
            }
            if ((ISymbol.ORDERLESS & attr) == ISymbol.ORDERLESS) {
                EvalAttributes.sort(resultList);
            }
        }
        resultList.addEvalFlags(IAST.IS_FLAT_ORDERLESS_EVALED);
        return resultList;
    }
    if ((ISymbol.FLAT & attr) == ISymbol.FLAT) {
        // associative
        IAST result;
        if ((result = EvalAttributes.flatten(ast)).isPresent()) {
            resultList = result;
            if ((ISymbol.ORDERLESS & attr) == ISymbol.ORDERLESS) {
                EvalAttributes.sort(resultList);
            }
            resultList.addEvalFlags(IAST.IS_FLAT_ORDERLESS_EVALED);
            return resultList;
        }
    }
    if ((ISymbol.ORDERLESS & attr) == ISymbol.ORDERLESS) {
        if (EvalAttributes.sort(ast)) {
            ast.addEvalFlags(IAST.IS_FLAT_ORDERLESS_EVALED);
            return ast;
        }
        return ast;
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 82 with ISymbol

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

the class PatternMatcherAndEvaluator method getAsAST.

public IAST getAsAST() {
    ISymbol setSymbol = getSetSymbol();
    IExpr condition = getCondition();
    if (condition != null) {
        return F.binaryAST2(setSymbol, getLHS(), F.Condition(getRHS(), condition));
    // ast = F.ast(setSymbol);
    // ast.add(getLHS());
    // ast.add(F.Condition(getRHS(), condition));
    }
    return F.binaryAST2(setSymbol, getLHS(), getRHS());
// ast = F.ast(setSymbol);
// ast.add(getLHS());
// ast.add(getRHS());
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 83 with ISymbol

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

the class PatternMatcherEquals method getAsAST.

public IAST getAsAST() {
    ISymbol setSymbol;
    // IAST ast;
    setSymbol = getSetSymbol();
    return F.binaryAST2(setSymbol, fLhsPatternExpr, getRHS());
//
// ast = F.ast(setSymbol);
// ast.add(fLhsPatternExpr);
// ast.add(getRHS());
// return ast;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol)

Example 84 with ISymbol

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

the class RulesData method putDownRule.

public PatternMatcher putDownRule(final PatternMatcherAndInvoker pmEvaluator) {
    final IExpr leftHandSide = pmEvaluator.getLHS();
    Set<ISymbol> headerSymbols = new HashSet<ISymbol>();
    if (!isComplicatedPatternRule(leftHandSide, headerSymbols)) {
        fSimplePatternDownRules = getSimplePatternDownRules();
        return addSimplePatternDownRule(leftHandSide, pmEvaluator);
    } else {
        if (headerSymbols.size() > 0) {
            fSimpleOrderlesPatternDownRules = getSimpleOrderlessPatternDownRules();
            return addSimpleOrderlessPatternDownRule(headerSymbols, leftHandSide, pmEvaluator);
        }
        fPatternDownRules = getPatternDownRules();
        fPatternDownRules.remove(pmEvaluator);
        fPatternDownRules.add(pmEvaluator);
        return pmEvaluator;
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr) HashSet(java.util.HashSet)

Example 85 with ISymbol

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

the class ExprParser method getFactor.

private IExpr getFactor() throws SyntaxError {
    IExpr temp;
    if (fToken == TT_PRECEDENCE_OPEN) {
        fRecursionDepth++;
        try {
            getNextToken();
            temp = parseExpression();
            if (fToken != TT_PRECEDENCE_CLOSE) {
                throwSyntaxError("\')\' expected.");
            }
        } finally {
            fRecursionDepth--;
        }
        getNextToken();
        if (fToken == TT_PRECEDENCE_OPEN) {
            return getTimes(temp);
        }
        if (fToken == TT_ARGUMENTS_OPEN) {
            return getFunctionArguments(temp);
        }
        return temp;
    } else if (fToken == TT_LIST_OPEN) {
        return getList();
    } else if (fToken == TT_IDENTIFIER) {
        final IExpr head = getSymbol();
        if (head.isSymbol()) {
            final ISymbol symbol = (ISymbol) head;
            temp = symbol;
            if (fToken == TT_BLANK) {
                // read '_'
                if (isWhitespace()) {
                    temp = F.$p(symbol, null);
                    getNextToken();
                } else {
                    getNextToken();
                    if (fToken == TT_IDENTIFIER) {
                        final IExpr check = getSymbol();
                        temp = F.$p(symbol, check);
                    } else {
                        temp = F.$p(symbol, null);
                    }
                }
            } else if (fToken == TT_BLANK_BLANK) {
                // read '__'
                if (isWhitespace()) {
                    temp = F.$ps(symbol, null);
                    getNextToken();
                } else {
                    getNextToken();
                    if (fToken == TT_IDENTIFIER) {
                        final IExpr check = getSymbol();
                        temp = F.$ps(symbol, check);
                    } else {
                        temp = F.$ps(symbol, null);
                    }
                }
            } else if (fToken == TT_BLANK_BLANK_BLANK) {
                // read '___'
                if (isWhitespace()) {
                    temp = F.$ps(symbol, null, false, true);
                    getNextToken();
                } else {
                    getNextToken();
                    if (fToken == TT_IDENTIFIER) {
                        final IExpr check = getSymbol();
                        temp = F.$ps(symbol, check, false, true);
                    } else {
                        temp = F.$ps(symbol, null, false, true);
                    }
                }
            } else if (fToken == TT_BLANK_OPTIONAL) {
                // read '_.'
                if (isWhitespace()) {
                    temp = F.$p(symbol, null, true);
                    getNextToken();
                } else {
                    getNextToken();
                    if (fToken == TT_IDENTIFIER) {
                        final IExpr check = getSymbol();
                        temp = F.$p(symbol, check, true);
                    } else {
                        temp = F.$p(symbol, null, true);
                    }
                }
            } else if (fToken == TT_BLANK_COLON) {
                // read '_:'
                getNextToken();
                IExpr defaultValue = parseExpression();
                temp = F.$p(symbol, null, defaultValue);
            }
        } else {
            temp = head;
        }
        return parseArguments(temp);
    } else if (fToken == TT_BLANK) {
        if (isWhitespace()) {
            getNextToken();
            temp = F.$b();
        // temp = fFactory.createPattern(null, null);
        } else {
            getNextToken();
            if (fToken == TT_IDENTIFIER) {
                final IExpr check = getSymbol();
                temp = F.$b(check);
            // temp = fFactory.createPattern(null, check);
            } else {
                temp = F.$b();
            // temp = fFactory.createPattern(null, null);
            }
        }
        return parseArguments(temp);
    } else if (fToken == TT_BLANK_BLANK) {
        // read '__'
        if (isWhitespace()) {
            getNextToken();
            temp = F.$ps(null, null);
        // temp = fFactory.createPattern2(null, null);
        } else {
            getNextToken();
            if (fToken == TT_IDENTIFIER) {
                final IExpr check = getSymbol();
                temp = F.$ps(null, check);
            // temp = fFactory.createPattern2(null, check);
            } else {
                temp = F.$ps(null, null);
            // temp = fFactory.createPattern2(null, null);
            }
        }
        return parseArguments(temp);
    } else if (fToken == TT_BLANK_BLANK_BLANK) {
        // read '___'
        if (isWhitespace()) {
            getNextToken();
            temp = F.$ps(null, null, false, true);
        // temp = fFactory.createPattern3(null, null);
        } else {
            getNextToken();
            if (fToken == TT_IDENTIFIER) {
                final IExpr check = getSymbol();
                temp = F.$ps(null, check, false, true);
            // temp = fFactory.createPattern3(null, check);
            } else {
                temp = F.$ps(null, null, false, true);
            // temp = fFactory.createPattern3(null, null);
            }
        }
        return parseArguments(temp);
    } else if (fToken == TT_BLANK_OPTIONAL) {
        // read '_.'
        if (isWhitespace()) {
            getNextToken();
            temp = F.$b(null, true);
        // temp = fFactory.createPattern(null, null, true);
        } else {
            getNextToken();
            if (fToken == TT_IDENTIFIER) {
                final IExpr check = getSymbol();
                temp = F.$b(check, true);
            // temp = fFactory.createPattern(null, check, true);
            } else {
                temp = F.$b(null, true);
            // temp = fFactory.createPattern(null, null, true);
            }
        }
        return parseArguments(temp);
    } else if (fToken == TT_BLANK_COLON) {
        // read '_:'
        getNextToken();
        IExpr defaultValue = parseExpression();
        temp = F.$b(null, defaultValue);
        return parseArguments(temp);
    } else if (fToken == TT_DIGIT) {
        return getNumber(false);
    } else if (fToken == TT_STRING) {
        return getString();
    } else if (fToken == TT_PERCENT) {
        final IAST out = F.ast(F.Out);
        int countPercent = 1;
        getNextToken();
        if (fToken == TT_DIGIT) {
            countPercent = getIntegerNumber();
            out.append(F.integer(countPercent));
            return out;
        }
        while (fToken == TT_PERCENT) {
            countPercent++;
            getNextToken();
        }
        out.append(F.integer(-countPercent));
        return parseArguments(out);
    } else if (fToken == TT_SLOT) {
        getNextToken();
        if (fToken == TT_DIGIT) {
            final IAST slot = F.ast(F.Slot);
            slot.append(getNumber(false));
            return parseArguments(slot);
        } else {
            return parseArguments(F.Slot1);
        }
    } else if (fToken == TT_SLOTSEQUENCE) {
        getNextToken();
        final IAST slotSequencce = F.ast(F.SlotSequence);
        if (fToken == TT_DIGIT) {
            slotSequencce.append(getNumber(false));
        } else {
            slotSequencce.append(F.C1);
        }
        return parseArguments(slotSequencce);
    // final FunctionNode slotSequencce =
    // fFactory.createFunction(fFactory.createSymbol(IConstantOperators.SlotSequence));
    // if (fToken == TT_DIGIT) {
    // slotSequencce.add(getNumber(false));
    // } else {
    // slotSequencce.add(fFactory.createInteger(1));
    // }
    // return parseArguments(slotSequencce);
    }
    switch(fToken) {
        case TT_PRECEDENCE_CLOSE:
            throwSyntaxError("Too much closing ) in factor.");
            break;
        case TT_LIST_CLOSE:
            throwSyntaxError("Too much closing } in factor.");
            break;
        case TT_ARGUMENTS_CLOSE:
            throwSyntaxError("Too much closing ] in factor.");
            break;
    }
    throwSyntaxError("Error in factor at character: '" + fCurrentChar + "' (" + fToken + ")");
    return null;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) Apint(org.apfloat.Apint)

Aggregations

ISymbol (org.matheclipse.core.interfaces.ISymbol)117 IExpr (org.matheclipse.core.interfaces.IExpr)79 IAST (org.matheclipse.core.interfaces.IAST)76 IInteger (org.matheclipse.core.interfaces.IInteger)18 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)12 INum (org.matheclipse.core.interfaces.INum)10 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 ArrayList (java.util.ArrayList)6 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)6 HashSet (java.util.HashSet)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 Num (org.matheclipse.core.expression.Num)5 GenPolynomial (edu.jas.poly.GenPolynomial)4 Options (org.matheclipse.core.eval.util.Options)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 ExpVector (edu.jas.poly.ExpVector)3