Search in sources :

Example 76 with IASTMutable

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

the class PatternMatcher method matchASTSpecialBuiltIn.

/**
 * Test first if <code>functionID = lhsPatternAST.headID()</code> is a special pattern-matching
 * construct (i.e. <code>
 * Association, HoldPattern, Literal, Condition, Alternatives, Except, Complex, Rational, Optional, PatternTest, Verbatim
 * </code>). If <code>true</code> evaluate the special pattern-matching construct otherwise
 * continue with <code>lhsPatternAST</code> pattern matching.
 *
 * @param lhsPatternAST left-hand-side pattern AST
 * @param lhsEvalExpr left-hand-side expression which should be matched by the pattern expression
 * @param engine the evaluation engine
 * @param stackMatcher a stack matcher
 * @return
 */
private boolean matchASTSpecialBuiltIn(IAST lhsPatternAST, final IExpr lhsEvalExpr, EvalEngine engine, StackMatcher stackMatcher) {
    int functionID = lhsPatternAST.headID();
    if (functionID >= ID.Association && functionID <= ID.Verbatim) {
        boolean matched = false;
        if (lhsPatternAST.size() == 2) {
            final IExpr[] patternValues;
            switch(functionID) {
                case ID.Association:
                    patternValues = fPatternMap.copyPattern();
                    try {
                        if (lhsEvalExpr.isAssociation()) {
                            IAST lhsPatternAssociation = lhsPatternAST;
                            // TODO set/determine pattern matching flags?
                            IASTMutable lhsPatternList = (IASTMutable) lhsPatternAssociation.normal(false);
                            lhsPatternList.set(0, S.Association);
                            IAssociation lhsEvalAssociation = (IAssociation) lhsEvalExpr;
                            IASTMutable lhsEvalList = lhsEvalAssociation.normal(false);
                            lhsEvalList.set(0, S.Association);
                            matched = matchExpr(lhsPatternList, lhsEvalList, engine, stackMatcher);
                            return matched;
                        }
                        matched = matchASTExpr(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
                    } finally {
                        if (!matched) {
                            fPatternMap.resetPattern(patternValues);
                        }
                    }
                    return matched;
                case ID.Except:
                    patternValues = fPatternMap.copyPattern();
                    try {
                        matched = !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, engine, stackMatcher);
                    } finally {
                        if (!matched) {
                            fPatternMap.resetPattern(patternValues);
                        }
                    }
                    return matched;
                case ID.HoldPattern:
                case ID.Literal:
                    patternValues = fPatternMap.copyPattern();
                    try {
                        matched = matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, engine, stackMatcher);
                    } finally {
                        if (!matched) {
                            fPatternMap.resetPattern(patternValues);
                        }
                    }
                    return matched;
                case ID.Optional:
                    return matchOptional(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
                case ID.Verbatim:
                    return lhsPatternAST.arg1().equals(lhsEvalExpr);
                default:
            }
        } else if (lhsPatternAST.size() == 3) {
            if (functionID >= ID.Complex && functionID <= ID.Rational) {
                final IExpr[] patternValues;
                switch(functionID) {
                    case ID.Complex:
                        patternValues = fPatternMap.copyPattern();
                        try {
                            if (lhsEvalExpr.isNumber()) {
                                INumber number = (INumber) lhsEvalExpr;
                                matched = matchExpr(lhsPatternAST.arg1(), number.re(), engine, stackMatcher) && matchExpr(lhsPatternAST.arg2(), number.im(), engine, stackMatcher);
                                return matched;
                            }
                            matched = matchASTExpr(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
                        } finally {
                            if (!matched) {
                                fPatternMap.resetPattern(patternValues);
                            }
                        }
                        return matched;
                    case ID.Condition:
                        return matchCondition(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
                    case ID.Except:
                        patternValues = fPatternMap.copyPattern();
                        try {
                            matched = !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, engine, stackMatcher) && matchExpr(lhsPatternAST.arg2(), lhsEvalExpr, engine, stackMatcher);
                        } finally {
                            if (!matched) {
                                fPatternMap.resetPattern(patternValues);
                            }
                        }
                        return matched;
                    case ID.Optional:
                        return matchOptional(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
                    case ID.PatternTest:
                        patternValues = fPatternMap.copyPattern();
                        try {
                            final IExpr lhsPatternExpr = lhsPatternAST.arg1();
                            final IExpr patternTest = lhsPatternAST.arg2();
                            if (lhsPatternExpr instanceof IPatternObject && patternTest.isFreeOfPatterns()) {
                                // other pattern symbol
                                if (matchPattern((IPatternObject) lhsPatternExpr, lhsEvalExpr, engine, stackMatcher)) {
                                    if (fPatternMap.isPatternTest(lhsPatternExpr, patternTest, engine)) {
                                        matched = stackMatcher.matchRest();
                                    }
                                }
                            } else if (matchExpr(lhsPatternExpr, lhsEvalExpr, engine, stackMatcher)) {
                                matched = fPatternMap.isPatternTest(lhsPatternExpr, patternTest, engine);
                            }
                        } finally {
                            if (!matched) {
                                fPatternMap.resetPattern(patternValues);
                            }
                        }
                        return matched;
                    case ID.Rational:
                        patternValues = fPatternMap.copyPattern();
                        try {
                            // check for fractions (and no integers) here to be compatible with MMA
                            if (lhsEvalExpr.isFraction()) {
                                IFraction rational = (IFraction) lhsEvalExpr;
                                matched = matchExpr(lhsPatternAST.arg1(), rational.numerator(), engine, stackMatcher) && matchExpr(lhsPatternAST.arg2(), rational.denominator(), engine, stackMatcher);
                                return matched;
                            }
                            matched = matchASTExpr(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
                            return matched;
                        } finally {
                            if (!matched) {
                                fPatternMap.resetPattern(patternValues);
                            }
                        }
                    default:
                }
            }
        }
    } else {
        if (lhsPatternAST.isAlternatives()) {
            return matchAlternatives(lhsPatternAST, lhsEvalExpr, engine);
        }
    }
    return matchASTExpr(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IAssociation(org.matheclipse.core.interfaces.IAssociation) INumber(org.matheclipse.core.interfaces.INumber) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 77 with IASTMutable

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

the class PolynomialHomogenizationNew method replaceTimes.

private IExpr replaceTimes(final IAST ast, final IExpr base, IExpr exp) {
    IExpr first = exp.first();
    if (first.isComplex() && ((IComplex) first).reRational().isZero()) {
        IRational imPart = ((IComplex) first).imRational();
        int exponent = imPart.toIntDefault();
        if (exponent == Integer.MIN_VALUE) {
            return replaceExpression(ast).orElse(ast);
        } else if (exponent > 0) {
            IASTMutable restExponent = ((IAST) exp).setAtCopy(1, F.CI);
            return F.Power(replaceExpression(base.power(restExponent)), exponent);
        }
        return replaceExpression(ast);
    }
    int exponent = first.toIntDefault();
    if (exponent == Integer.MIN_VALUE) {
        return replaceExpression(ast);
    } else if (exponent > 0) {
        IExpr rest = exp.rest().oneIdentity1();
        return F.Power(replaceExpression(base.power(rest)), exponent);
    }
    return replaceExpression(ast).orElse(ast);
}
Also used : IComplex(org.matheclipse.core.interfaces.IComplex) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 78 with IASTMutable

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

the class PolynomialHomogenizationNew method determineTimes.

private void determineTimes(final IAST ast, final IExpr base, IAST timesExponent) {
    IExpr first = timesExponent.first();
    if (first.isComplex() && ((IComplex) first).reRational().isZero()) {
        IRational pureImPart = ((IComplex) first).imRational();
        int exponent = pureImPart.toIntDefault();
        if (exponent == Integer.MIN_VALUE) {
            replaceExpressionLCM(ast, F.C1);
            return;
        } else if (exponent > 0) {
            IASTMutable restExponent = timesExponent.setAtCopy(1, F.CI);
            replaceExpressionLCM(base.power(restExponent), F.C1);
            return;
        }
        replaceExpressionLCM(ast, F.C1);
        return;
    }
    int exponent = first.toIntDefault();
    if (exponent == Integer.MIN_VALUE) {
        replaceExpressionLCM(ast, F.C1);
        return;
    } else if (exponent > 0) {
        IExpr rest = timesExponent.rest().oneIdentity1();
        replaceExpressionLCM(base.power(rest), F.C1);
        return;
    }
    replaceExpressionLCM(ast, F.C1);
}
Also used : IComplex(org.matheclipse.core.interfaces.IComplex) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 79 with IASTMutable

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

the class PolynomialHomogenization method replaceTimes.

private IExpr replaceTimes(final IAST ast, final IExpr base, IExpr exp) {
    IExpr first = exp.first();
    if (first.isComplex() && ((IComplex) first).reRational().isZero()) {
        IRational imPart = ((IComplex) first).imRational();
        int exponent = imPart.toIntDefault();
        if (exponent == Integer.MIN_VALUE) {
            return replaceExpression(ast).orElse(ast);
        } else if (exponent > 0) {
            IASTMutable restExponent = ((IAST) exp).setAtCopy(1, F.CI);
            return F.Power(replaceExpression(base.power(restExponent)), exponent);
        }
        return replaceExpression(ast);
    }
    int exponent = first.toIntDefault();
    if (exponent == Integer.MIN_VALUE) {
        return replaceExpression(ast);
    } else if (exponent > 0) {
        IExpr rest = exp.rest().oneIdentity1();
        return F.Power(replaceExpression(base.power(rest)), exponent);
    }
    return replaceExpression(ast).orElse(ast);
}
Also used : IComplex(org.matheclipse.core.interfaces.IComplex) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 80 with IASTMutable

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

the class PolynomialHomogenization method determineTimes.

private void determineTimes(final IAST ast, final IExpr base, IAST timesExponent) {
    IExpr first = timesExponent.first();
    if (first.isComplex() && ((IComplex) first).reRational().isZero()) {
        IRational pureImPart = ((IComplex) first).imRational();
        int exponent = pureImPart.toIntDefault();
        if (exponent == Integer.MIN_VALUE) {
            replaceExpressionLCM(ast, F.C1);
            return;
        } else if (exponent > 0) {
            IASTMutable restExponent = timesExponent.setAtCopy(1, F.CI);
            replaceExpressionLCM(base.power(restExponent), F.C1);
            return;
        }
        replaceExpressionLCM(ast, F.C1);
        return;
    }
    int exponent = first.toIntDefault();
    if (exponent == Integer.MIN_VALUE) {
        replaceExpressionLCM(ast, F.C1);
        return;
    } else if (exponent > 0) {
        IExpr rest = timesExponent.rest().oneIdentity1();
        replaceExpressionLCM(base.power(rest), F.C1);
        return;
    }
    replaceExpressionLCM(ast, F.C1);
}
Also used : IComplex(org.matheclipse.core.interfaces.IComplex) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr) 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