Search in sources :

Example 16 with IPatternObject

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

the class AbstractAST method isFreeOfPatterns.

/**
 * {@inheritDoc}
 */
@Override
public final boolean isFreeOfPatterns() {
    final int evalFlags = getEvalFlags();
    if ((evalFlags & IAST.CONTAINS_NO_PATTERN) == IAST.CONTAINS_NO_PATTERN) {
        return true;
    }
    if ((evalFlags & IAST.CONTAINS_PATTERN_EXPR) != IAST.NO_FLAG) {
        return false;
    }
    if (isPatternMatchingFunction()) {
        addEvalFlags(IAST.CONTAINS_PATTERN);
        return false;
    }
    boolean isFreeOfPatterns = true;
    for (int i = 0; i < size(); i++) {
        // all elements including head element
        IExpr temp = get(i);
        if (temp.isAST() && !temp.isFreeOfPatterns()) {
            isFreeOfPatterns = false;
            addEvalFlags(((IAST) temp).getEvalFlags() & IAST.CONTAINS_PATTERN_EXPR);
            continue;
        } else if (temp instanceof IPatternObject) {
            isFreeOfPatterns = false;
            if (temp instanceof IPatternSequence) {
                if (temp.isPatternDefault()) {
                    addEvalFlags(IAST.CONTAINS_DEFAULT_PATTERN);
                }
                addEvalFlags(IAST.CONTAINS_PATTERN_SEQUENCE);
            } else {
                if (temp.isPatternDefault()) {
                    addEvalFlags(IAST.CONTAINS_DEFAULT_PATTERN);
                }
                addEvalFlags(IAST.CONTAINS_PATTERN);
            }
        }
    }
    if (isFreeOfPatterns) {
        addEvalFlags(IAST.CONTAINS_NO_PATTERN);
    }
    return isFreeOfPatterns;
}
Also used : IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 17 with IPatternObject

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

the class ComplexFormFactory method convertInternal.

private void convertInternal(final StringBuilder buf, final IExpr o, final int precedence, boolean isASTHead) {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        if (list.head().isSymbol()) {
            ISymbol head = (ISymbol) list.head();
            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;
                    }
                }
            }
        }
        convertAST(buf, list);
        return;
    }
    if (o instanceof ISignedNumber) {
        double d = o.evalDouble();
        if (fPackagePrefix) {
            buf.append("org.hipparchus.complex.");
        }
        buf.append("Complex.valueOf(" + d + ")");
        return;
    }
    if (o instanceof INumber) {
        Complex c = o.evalComplex();
        if (c != null) {
            if (fPackagePrefix) {
                buf.append("org.hipparchus.complex.");
            }
            buf.append("Complex.valueOf(" + c.getReal() + ", " + c.getImaginary() + ")");
        } else {
            buf.append("Complex.valueOf(" + o.toString() + ")");
        }
        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) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) INumber(org.matheclipse.core.interfaces.INumber) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST) Complex(org.hipparchus.complex.Complex)

Example 18 with IPatternObject

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

the class OutputFormFactory method convert.

private void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        if (!list.isPresent()) {
            append(buf, "NIL");
            return;
        }
        if (o.isDataset()) {
            // TODO improve output
            buf.append(o.toString());
            return;
        } else if (o.isAssociation()) {
            convertAssociation(buf, (IAssociation) o);
            return;
        } else if (o.isAST(S.Association, 1)) {
            buf.append("<||>");
            return;
        }
        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() || headAST.arg1().isAST()) && derivStruct[2] != null) {
                    try {
                        int n = ((IInteger) a1Head.arg1()).toInt();
                        if (n == 1 || n == 2) {
                            IExpr symbolOrAST = headAST.arg1();
                            convert(buf, symbolOrAST, Integer.MIN_VALUE, false);
                            if (n == 1) {
                                append(buf, "'");
                            } else if (n == 2) {
                                append(buf, "''");
                            }
                            convertArgs(buf, symbolOrAST, list);
                            return;
                        }
                    } catch (ArithmeticException ae) {
                    }
                }
            }
            convert(buf, header, Integer.MIN_VALUE, true);
            // avoid fast StackOverflow
            append(buf, "[");
            for (int i = 1; i < list.size(); i++) {
                convert(buf, list.get(i), Integer.MIN_VALUE, false);
                if (i < list.argSize()) {
                    append(buf, ",");
                }
            }
            append(buf, "]");
            return;
        }
        if (header.isSymbol()) {
            ISymbol head = (ISymbol) header;
            int functionID = head.ordinal();
            if (functionID > ID.UNKNOWN) {
                switch(functionID) {
                    case ID.TwoWayRule:
                    case ID.UndirectedEdge:
                        if (list.isAST2()) {
                            convert(buf, list.arg1(), Integer.MIN_VALUE, false);
                            buf.append("<->");
                            convert(buf, list.arg2(), Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                    case ID.DirectedEdge:
                        if (list.isAST2()) {
                            convert(buf, list.arg1(), Integer.MIN_VALUE, false);
                            buf.append("->");
                            convert(buf, list.arg2(), Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                }
            }
            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 (functionID > ID.UNKNOWN) {
                switch(functionID) {
                    case ID.Inequality:
                        if (list.size() > 3 && convertInequality(buf, list, precedence)) {
                            return;
                        }
                        break;
                    case ID.Quantity:
                        // if (head.equals(F.SeriesData) && (list.size() == 7)) {
                        if (list instanceof IQuantity) {
                            if (convertQuantityData(buf, (IQuantity) list, precedence)) {
                                return;
                            }
                        }
                        break;
                    case ID.SeriesData:
                        // if (head.equals(F.SeriesData) && (list.size() == 7)) {
                        if (list instanceof ASTSeriesData) {
                            if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
                                return;
                            }
                        }
                        break;
                    case ID.SparseArray:
                        if (list.isSparseArray()) {
                            buf.append(list.toString());
                            return;
                        }
                        break;
                    case ID.Parenthesis:
                        convertArgs(buf, S.Parenthesis, list);
                        return;
                    case ID.List:
                        convertList(buf, list, false);
                        return;
                    case ID.MatrixForm:
                        if (list.isASTOrAssociation() && list.size() > 1) {
                            // see also MatrixForm in MathML or TeX format for "graphical representation".
                            IExpr normal = list.arg1().normal(false);
                            if (normal.isList()) {
                                // && normal.isMatrix() != null) {
                                IntList dims = LinearAlgebra.dimensions((IAST) normal, S.List);
                                convertList(buf, (IAST) normal, dims.size() >= 2);
                                return;
                            }
                            convert(buf, normal, Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                    case ID.Out:
                        if (list.isAST1() && list.arg1().isInteger()) {
                            int lineNumber = list.arg1().toIntDefault();
                            if (lineNumber == -1) {
                                buf.append("%");
                                return;
                            } else if (lineNumber == -2) {
                                buf.append("%%");
                                return;
                            }
                        }
                        break;
                    case ID.Part:
                        if (list.size() >= 3) {
                            convertPart(buf, list);
                            return;
                        }
                        break;
                    case ID.Slot:
                        if (list.isAST1() && list.arg1().isInteger()) {
                            convertSlot(buf, list);
                            return;
                        }
                        break;
                    case ID.SlotSequence:
                        if (list.isAST1() && list.arg1().isInteger()) {
                            convertSlotSequence(buf, list);
                            return;
                        }
                        break;
                    case ID.Defer:
                    case ID.HoldForm:
                        if (list.isAST1()) {
                            convert(buf, list.arg1(), Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                    case ID.DirectedInfinity:
                        if (list.isDirectedInfinity()) {
                            // head.equals(F.DirectedInfinity))
                            if (list.isAST0()) {
                                append(buf, "ComplexInfinity");
                                return;
                            }
                            if (list.isAST1()) {
                                if (list.arg1().isOne()) {
                                    append(buf, "Infinity");
                                    return;
                                } else if (list.arg1().isMinusOne()) {
                                    if (Precedence.PLUS < precedence) {
                                        append(buf, "(");
                                    }
                                    append(buf, "-Infinity");
                                    if (Precedence.PLUS < precedence) {
                                        append(buf, ")");
                                    }
                                    return;
                                } else if (list.arg1().isImaginaryUnit()) {
                                    append(buf, "I*Infinity");
                                    return;
                                } else if (list.arg1().isNegativeImaginaryUnit()) {
                                    append(buf, "-I*Infinity");
                                    return;
                                }
                            }
                        }
                        break;
                    case ID.Optional:
                        if (list.isAST2() && (list.arg1().isBlank() || list.arg1().isPattern())) {
                            convert(buf, list.arg1(), Integer.MIN_VALUE, false);
                            buf.append(":");
                            convert(buf, list.arg2(), Integer.MIN_VALUE, false);
                            return;
                        }
                        break;
                    case ID.Complex:
                        if (list.isAST2()) {
                            // used for visual comparison of steps
                            boolean isZeroRealPart = list.arg1().isZero();
                            final int prec = isZeroRealPart ? Precedence.TIMES : Precedence.PLUS;
                            if (prec < precedence) {
                                append(buf, "(");
                            }
                            if (isZeroRealPart) {
                                buf.append("I*");
                                convert(buf, list.arg2(), Precedence.TIMES, false);
                            } else {
                                convert(buf, list.arg1(), Precedence.PLUS, false);
                                buf.append("+I*");
                                convert(buf, list.arg2(), Precedence.TIMES, false);
                            }
                            if (prec < precedence) {
                                append(buf, ")");
                            }
                            return;
                        }
                        break;
                    case ID.Rational:
                        if (list.isAST2()) {
                            // used for visual comparison of steps
                            IExpr numerator = list.arg1();
                            final boolean isNegative = numerator.isNegative();
                            final int prec = isNegative ? Precedence.PLUS : Precedence.TIMES;
                            if (prec < precedence) {
                                append(buf, "(");
                            }
                            convert(buf, list.arg1(), Precedence.DIVIDE, false);
                            buf.append("/");
                            convert(buf, list.arg2(), Precedence.DIVIDE, false);
                            if (prec < precedence) {
                                append(buf, ")");
                            }
                            return;
                        }
                        break;
                }
            } else {
                if (list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
                    convertList(buf, list, false);
                    return;
                }
            }
        }
        convertAST(buf, list);
    } else if (o instanceof ISignedNumber) {
        convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
    } else if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
    } else if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
    } else if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
    } else if (o instanceof IPatternObject) {
        convertPattern(buf, (IPatternObject) o);
    } else if (o instanceof IStringX) {
        convertString(buf, ((IStringX) o).toString());
    } else {
        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) IAssociation(org.matheclipse.core.interfaces.IAssociation) ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IntList(it.unimi.dsi.fastutil.ints.IntList) IQuantity(org.matheclipse.core.tensor.qty.IQuantity) 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) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) IStringX(org.matheclipse.core.interfaces.IStringX) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Example 19 with IPatternObject

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

the class IPatternMap 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 contain pattern objects
 * @param onlyNamedPatterns TODO
 * @return <code>F.NIL</code> if substitutions isn't possible
 */
default IExpr substitutePatternOrSymbols(final IExpr lhsPatternExpr, boolean onlyNamedPatterns) {
    VisitorReplaceAllWithPatternFlags visitor = new VisitorReplaceAllWithPatternFlags(input -> {
        if (input instanceof IPatternObject) {
            if (onlyNamedPatterns && !(input instanceof Pattern)) {
                return F.NIL;
            }
            IExpr symbolOrPatternObject = ((IPatternObject) input).getSymbol();
            if (symbolOrPatternObject == null) {
                if (onlyNamedPatterns) {
                    return F.NIL;
                }
                symbolOrPatternObject = input;
            }
            return substitute(symbolOrPatternObject);
        }
        return F.NIL;
    }, onlyNamedPatterns);
    IExpr result = lhsPatternExpr.accept(visitor);
    if (result.isPresent()) {
        // set the eval flags
        result.isFreeOfPatterns();
        return result;
    }
    return lhsPatternExpr;
}
Also used : OptionsPattern(org.matheclipse.core.expression.OptionsPattern) Pattern(org.matheclipse.core.expression.Pattern) IPatternMap.addOptionsPattern(org.matheclipse.core.patternmatching.IPatternMap.addOptionsPattern) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) VisitorReplaceAllWithPatternFlags(org.matheclipse.core.visit.VisitorReplaceAllWithPatternFlags) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 20 with IPatternObject

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

the class IPatternMap method determinePatternsRecursive.

/**
 * Determine all patterns (i.e. all objects of instance IPattern) in the given expression
 *
 * <p>
 * Increments this classes pattern counter.
 *
 * @param patternIndexMap
 * @param lhsPatternExpr the (left-hand-side) expression which could contain pattern objects.
 * @param treeLevel the level of the tree where the patterns are determined
 */
static int determinePatternsRecursive(List<Pair<IExpr, IPatternObject>> patternIndexMap, final IAST lhsPatternExpr, int[] priority, boolean[] ruleWithoutPattern, int treeLevel) {
    int[] listEvalFlags = new int[] { IAST.NO_FLAG };
    if (lhsPatternExpr.isAlternatives() || lhsPatternExpr.isExcept()) {
        ruleWithoutPattern[0] = false;
    }
    lhsPatternExpr.forEach(x -> {
        if (x.isASTOrAssociation()) {
            final IAST lhsPatternAST = (IAST) x;
            if (lhsPatternAST.isPatternMatchingFunction()) {
                listEvalFlags[0] |= IAST.CONTAINS_PATTERN;
            }
            listEvalFlags[0] |= determinePatternsRecursive(patternIndexMap, lhsPatternAST, priority, ruleWithoutPattern, treeLevel + 1);
            priority[0] -= 11;
            if (x.isPatternDefault()) {
                listEvalFlags[0] |= IAST.CONTAINS_DEFAULT_PATTERN;
            }
        } else if (x instanceof IPatternObject) {
            ruleWithoutPattern[0] = false;
            int[] result = ((IPatternObject) x).addPattern(patternIndexMap);
            listEvalFlags[0] |= result[0];
            priority[0] -= result[1];
            if (x instanceof PatternNested) {
                IExpr patternExpr = ((PatternNested) x).getPatternExpr();
                if (patternExpr.isASTOrAssociation()) {
                    listEvalFlags[0] |= determinePatternsRecursive(patternIndexMap, (IAST) patternExpr, priority, ruleWithoutPattern, treeLevel + 1);
                    priority[0] -= 11;
                    if (x.isPatternDefault()) {
                        listEvalFlags[0] |= IAST.CONTAINS_DEFAULT_PATTERN;
                    }
                }
            }
        } else {
            priority[0] -= (50 - treeLevel);
        }
    }, 0);
    lhsPatternExpr.setEvalFlags(listEvalFlags[0]);
    // listEvalFlags &= IAST.CONTAINS_NO_DEFAULT_PATTERN_MASK;
    return listEvalFlags[0];
}
Also used : IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST) PatternNested(org.matheclipse.core.expression.PatternNested) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IPatternObject (org.matheclipse.core.interfaces.IPatternObject)22 IExpr (org.matheclipse.core.interfaces.IExpr)17 IAST (org.matheclipse.core.interfaces.IAST)16 ISymbol (org.matheclipse.core.interfaces.ISymbol)8 IPatternSequence (org.matheclipse.core.interfaces.IPatternSequence)6 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)5 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)4 PatternNested (org.matheclipse.core.expression.PatternNested)3 IAssociation (org.matheclipse.core.interfaces.IAssociation)3 INumber (org.matheclipse.core.interfaces.INumber)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 OptionsPattern (org.matheclipse.core.expression.OptionsPattern)2 Pattern (org.matheclipse.core.expression.Pattern)2 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)2 IComplex (org.matheclipse.core.interfaces.IComplex)2 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)2 IFraction (org.matheclipse.core.interfaces.IFraction)2 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)2 Operator (org.matheclipse.parser.client.operator.Operator)2 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)2