Search in sources :

Example 1 with VisitorReplaceAllWithPatternFlags

use of org.matheclipse.core.visit.VisitorReplaceAllWithPatternFlags in project symja_android_library by axkr.

the class IPatternMap method substituteASTPatternOrSymbols.

/**
 * Substitute all already find matchings in <code>lhsPatternExpr</code> and return the new pattern
 * expression.
 *
 * @param lhsPatternExpr
 * @param onlyNamedPatterns
 * @return {@link F#NIL} if no substitution can be found.
 */
default IExpr substituteASTPatternOrSymbols(final IAST 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);
    IASTMutable result = F.NIL;
    for (int i = 1; i < lhsPatternExpr.size(); i++) {
        IExpr temp = lhsPatternExpr.get(i).accept(visitor);
        if (temp.isPresent()) {
            if (!result.isPresent()) {
                result = lhsPatternExpr.setAtCopy(i, temp);
            // result.setEvalFlags(lhsPatternExpr.getEvalFlags());
            } else {
                result.set(i, temp);
            }
        }
    }
    if (result.isPresent()) {
        return EvalAttributes.simpleEval(result);
    // if (result.isFlatAST()) {
    // IASTMutable temp = EvalAttributes.flattenDeep((IAST) result);
    // if (temp.isPresent()) {
    // result = temp;
    // }
    // }
    // // don't test for OneIdentity attribute here !
    // if (result.isOrderlessAST()) {
    // EvalAttributes.sort(result);
    // }
    // // set the eval flags
    // result.isFreeOfPatterns();
    // return result;
    }
    return F.NIL;
}
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) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 2 with VisitorReplaceAllWithPatternFlags

use of org.matheclipse.core.visit.VisitorReplaceAllWithPatternFlags 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)

Aggregations

OptionsPattern (org.matheclipse.core.expression.OptionsPattern)2 Pattern (org.matheclipse.core.expression.Pattern)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)2 IPatternMap.addOptionsPattern (org.matheclipse.core.patternmatching.IPatternMap.addOptionsPattern)2 VisitorReplaceAllWithPatternFlags (org.matheclipse.core.visit.VisitorReplaceAllWithPatternFlags)2 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)1