Search in sources :

Example 6 with IPatternObject

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

the class PatternMatcher method matchFlatAndFlatOrderlessAST.

private boolean matchFlatAndFlatOrderlessAST(final ISymbol sym, final IAST lhsPatternAST, final IAST lhsEvalAST, StackMatcher stackMatcher) {
    if ((sym.getAttributes() & ISymbol.ORDERLESS) == ISymbol.ORDERLESS) {
        if (lhsPatternAST.isAST1()) {
            // TODO check for OneIdentity?
            return matchExpr(lhsPatternAST.arg1(), lhsEvalAST, stackMatcher);
        }
        for (int i = 1; i < lhsPatternAST.size(); i++) {
            if (!(lhsPatternAST.get(i) instanceof IPatternObject)) {
                // try to find a matchin sub-expression
                for (int j = 1; j < lhsEvalAST.size(); j++) {
                    if (matchExpr(lhsPatternAST.get(i), lhsEvalAST.get(j))) {
                        if (matchFlatAndFlatOrderlessAST(sym, lhsPatternAST.removeAtClone(i), lhsEvalAST.removeAtClone(j), stackMatcher)) {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
        FlatOrderlessStepVisitor visitor = new FlatOrderlessStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap);
        MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPatternAST.size() - 1);
        return !iter.execute();
    } else {
        if (lhsPatternAST.isAST1()) {
            if (lhsPatternAST.arg1().isPatternSequence()) {
                // TODO only the special case, where the last element is
                // a pattern sequence, is handled here
                IAST seq = F.Sequence();
                seq.appendAll(lhsEvalAST, 1, lhsEvalAST.size());
                if (((IPatternSequence) lhsPatternAST.arg1()).matchPatternSequence(seq, fPatternMap)) {
                    // }
                    return true;
                }
            }
            if (lhsPatternAST.size() == lhsEvalAST.size()) {
                return matchASTSequence(lhsPatternAST, lhsEvalAST, 0, stackMatcher);
            }
            return false;
        }
        FlatStepVisitor visitor = new FlatStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap);
        NumberPartitionsIterator iter = new NumberPartitionsIterator(visitor, lhsEvalAST.size() - 1, lhsPatternAST.size() - 1);
        return !iter.execute();
    }
}
Also used : NumberPartitionsIterator(org.matheclipse.combinatoric.NumberPartitionsIterator) MultisetPartitionsIterator(org.matheclipse.combinatoric.MultisetPartitionsIterator) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST)

Example 7 with IPatternObject

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

the class PatternMap method substitutePatternOrSymbols.

/**
	 * Substitute all patterns and symbols in the given expression with the
	 * current value of the corresponding internal pattern values arrays
	 * 
	 * @param lhsPatternExpr
	 *            left-hand-side expression which may containe pattern objects
	 * 
	 * @return
	 */
protected IExpr substitutePatternOrSymbols(final IExpr lhsPatternExpr) {
    if (fPatternValuesArray != null) {
        IExpr result = lhsPatternExpr.replaceAll((IExpr input) -> {
            if (input instanceof IPatternObject) {
                IPatternObject patternObject = (IPatternObject) input;
                ISymbol sym = patternObject.getSymbol();
                if (sym != null) {
                    for (int i = 0; i < fSymbolsArray.length; i++) {
                        if (sym == fSymbolsArray[i]) {
                            return fPatternValuesArray[i] != null ? fPatternValuesArray[i] : F.NIL;
                        }
                    }
                } else {
                    for (int i = 0; i < fSymbolsArray.length; i++) {
                        if (patternObject == fSymbolsArray[i]) {
                            return fPatternValuesArray[i] != null ? fPatternValuesArray[i] : F.NIL;
                        }
                    }
                }
            }
            return F.NIL;
        });
        if (result != null) {
            return result;
        }
    }
    return lhsPatternExpr;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 8 with IPatternObject

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

the class EvalEngine method evalRules.

/**
	 * Evaluate the rules for an AST.
	 * 
	 * @param symbol
	 * @param ast
	 * @return <code>F.NIL</code> if no evaluation happened
	 */
public IExpr evalRules(ISymbol symbol, IAST ast) {
    IExpr result;
    for (int i = 1; i < ast.size(); i++) {
        if (!(ast.get(i) instanceof IPatternObject)) {
            final IExpr arg = ast.get(i);
            ISymbol lhsSymbol = null;
            if (arg.isSymbol()) {
                lhsSymbol = (ISymbol) arg;
            } else {
                lhsSymbol = arg.topHead();
            }
            if ((result = lhsSymbol.evalUpRule(this, ast)).isPresent()) {
                return result;
            }
        }
    }
    return evalASTBuiltinFunction(symbol, ast);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 9 with IPatternObject

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

the class OutputFormFactory method convert.

public void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        IExpr header = list.head();
        if (!header.isSymbol()) {
            // print expressions like: f(#1, y)& [x]
            IAST[] derivStruct = list.isDerivativeAST1();
            if (derivStruct != null) {
                IAST a1Head = derivStruct[0];
                IAST headAST = derivStruct[1];
                if (a1Head.isAST1() && a1Head.arg1().isInteger() && headAST.isAST1() && headAST.arg1().isSymbol() && derivStruct[2] != null) {
                    try {
                        int n = ((IInteger) a1Head.arg1()).toInt();
                        // IExpr arg1 = listArg1.arg1();
                        if (n == 1 || n == 2) {
                            ISymbol f = (ISymbol) headAST.arg1();
                            convertSymbol(buf, f);
                            if (n == 1) {
                                append(buf, "'");
                            } else if (n == 2) {
                                append(buf, "''");
                            }
                            convertArgs(buf, f, list);
                            return;
                        }
                    } catch (ArithmeticException ae) {
                    }
                }
            }
            convert(buf, header, Integer.MIN_VALUE, true);
            convertFunctionArgs(buf, list);
            return;
        }
        ISymbol head = list.topHead();
        final Operator operator = getOperator(head);
        if (operator != null) {
            if (operator instanceof PostfixOperator) {
                if (list.isAST1()) {
                    convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
                    return;
                }
            } else {
                if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
                    return;
                }
            }
        }
        if (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
            convertList(buf, list);
            return;
        }
        if (head.equals(F.Part) && (list.size() >= 3)) {
            convertPart(buf, list);
            return;
        }
        if (head.equals(F.Slot) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
            convertSlot(buf, list);
            return;
        }
        if (head.equals(F.SlotSequence) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
            convertSlotSequence(buf, list);
            return;
        }
        if ((head.equals(F.HoldForm) || head.equals(F.Defer)) && (list.isAST1())) {
            convert(buf, list.arg1());
            return;
        }
        if (head.equals(F.SeriesData) && (list.size() == 7)) {
            if (convertSeriesData(buf, list, precedence)) {
                return;
            }
        }
        if (list.isDirectedInfinity()) {
            // {
            if (list.isAST0()) {
                append(buf, "ComplexInfinity");
                return;
            }
            if (list.isAST1()) {
                if (list.arg1().isOne()) {
                    append(buf, "Infinity");
                    return;
                } else if (list.arg1().isMinusOne()) {
                    if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
                        append(buf, "(");
                    }
                    append(buf, "-Infinity");
                    if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
                        append(buf, ")");
                    }
                    return;
                } else if (list.arg1().isImaginaryUnit()) {
                    append(buf, "I*Infinity");
                    return;
                } else if (list.arg1().isNegativeImaginaryUnit()) {
                    append(buf, "-I*Infinity");
                    return;
                }
            }
        }
        convertAST(buf, list);
        return;
    }
    if (o instanceof ISignedNumber) {
        convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        return;
    }
    if (o instanceof IPatternObject) {
        convertPattern(buf, (IPatternObject) o);
        return;
    }
    convertString(buf, o.toString());
}
Also used : PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) Operator(org.matheclipse.parser.client.operator.Operator) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator) PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) IComplex(org.matheclipse.core.interfaces.IComplex) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Aggregations

IPatternObject (org.matheclipse.core.interfaces.IPatternObject)9 IExpr (org.matheclipse.core.interfaces.IExpr)7 IAST (org.matheclipse.core.interfaces.IAST)6 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 IPatternSequence (org.matheclipse.core.interfaces.IPatternSequence)2 DoubleUnaryOperator (java.util.function.DoubleUnaryOperator)1 MultisetPartitionsIterator (org.matheclipse.combinatoric.MultisetPartitionsIterator)1 NumberPartitionsIterator (org.matheclipse.combinatoric.NumberPartitionsIterator)1 ASTRealMatrix (org.matheclipse.core.expression.ASTRealMatrix)1 ASTRealVector (org.matheclipse.core.expression.ASTRealVector)1 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)1 IComplex (org.matheclipse.core.interfaces.IComplex)1 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)1 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)1 Operator (org.matheclipse.parser.client.operator.Operator)1 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)1 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)1