Search in sources :

Example 46 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class MainTestCase method testSystem400.

public void testSystem400() {
    EvalEngine engine = EvalEngine.get();
    IExpr exprNumerator = engine.parse("8+12*x+20*x^2+12*x^3+8*x^4+3*x^5");
    IExpr exprDenominator = engine.parse("8*x+12*x^3+6*x^5+x^7");
    IExpr[] result = Algebra.cancelGCD(exprNumerator, exprDenominator);
    assertEquals(result[0].toString(), "1");
    assertEquals(result[1].toString(), "4+6*x+8*x^2+3*x^3");
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 47 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class MMAFormTestCase method testWLForm006.

public void testWLForm006() {
    EvalEngine engine = new EvalEngine("", 256, 256, System.out, System.err, true);
    ExprParser parser = new ExprParser(engine, true);
    IExpr expr = parser.parse("a+i*b^2+k*c^3+d");
    assertEquals(expr.toMMA(), "a + i*b^2 + k*c^3 + d");
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser)

Example 48 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class PatternMatcher method matchFlatOrderless.

/**
 * Match <code>Flat</code> and <code>Orderless</code> LHS pattern expressions. It's assumed that
 * the headers of the expressions already matched.
 *
 * @param sym
 * @param lhsPattern
 * @param lhsEval
 * @param engine
 * @param stackMatcher
 * @return
 */
private boolean matchFlatOrderless(final ISymbol sym, IAST lhsPattern, IAST lhsEval, EvalEngine engine, StackMatcher stackMatcher) {
    if (lhsPattern.isAST1()) {
        return matchExpr(lhsPattern.arg1(), lhsEval, engine, stackMatcher);
    }
    IAST lhsPatternAST = lhsPattern;
    IAST lhsEvalAST = lhsEval;
    // removeOrderless already called a level up
    boolean matched = false;
    IExpr[] patternValues = fPatternMap.copyPattern();
    if (lhsPatternAST.size() <= 2) {
        try {
            if (lhsPatternAST.isAST1()) {
                matched = matchExpr(lhsPatternAST.arg1(), lhsEvalAST, engine, stackMatcher);
                return matched;
            }
            if (lhsPatternAST.isEmpty() && lhsEvalAST.size() > 1) {
                matched = false;
                return matched;
            }
            matched = stackMatcher.matchRest();
            return matched;
        } finally {
            if (!matched) {
                fPatternMap.resetPattern(patternValues);
            }
        }
    }
    lhsPattern = lhsPatternAST;
    lhsEval = lhsEvalAST;
    final IAST lhsPatternFinal = lhsPattern;
    final IAST lhsEvalFinal = lhsEval;
    for (int i = 1; i < lhsPatternFinal.size(); i++) {
        IExpr patternArg = lhsPatternFinal.get(i);
        if (!(patternArg instanceof IPatternObject)) {
            final int index = i;
            IAST reduced = lhsPatternFinal.splice(index);
            boolean evaled = false;
            for (int k = 1; k < lhsEvalFinal.size(); k++) {
                try {
                    IExpr evalArg = lhsEvalFinal.get(k);
                    if (!(patternArg.head() instanceof IPatternObject)) {
                        if (patternArg.isASTOrAssociation()) {
                            if ((((IAST) patternArg).getEvalFlags() & IAST.CONTAINS_DEFAULT_PATTERN) == IAST.CONTAINS_DEFAULT_PATTERN) {
                                continue;
                            }
                        }
                        if (patternArg.head().equals(evalArg.head()) && patternArg.isFree(x -> x.isOrderlessAST(), true)) {
                            evaled = true;
                            matched = matchExpr(patternArg, evalArg, engine, stackMatcher);
                        }
                        if (matched) {
                            matched = matchFlatAndFlatOrderless(sym, reduced, lhsEvalFinal.removeAtCopy(k), engine, stackMatcher);
                            if (matched) {
                                return true;
                            }
                        }
                    }
                } finally {
                    if (!matched) {
                        fPatternMap.resetPattern(patternValues);
                    }
                }
            }
            if (evaled) {
                return false;
            }
        }
    }
    FlatOrderlessStepVisitor visitor = new FlatOrderlessStepVisitor(sym, lhsPatternFinal, lhsEvalFinal, stackMatcher, fPatternMap, sym.hasFlatAttribute());
    MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPattern.argSize());
    return !iter.execute();
}
Also used : ResultException(org.matheclipse.core.eval.exception.ResultException) ObjectOutput(java.io.ObjectOutput) MultisetPartitionsIterator(org.matheclipse.core.combinatoric.MultisetPartitionsIterator) IAssociation(org.matheclipse.core.interfaces.IAssociation) IFraction(org.matheclipse.core.interfaces.IFraction) INumber(org.matheclipse.core.interfaces.INumber) NumberPartitionsIterator(org.matheclipse.core.combinatoric.NumberPartitionsIterator) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IPattern(org.matheclipse.core.interfaces.IPattern) ReturnException(org.matheclipse.core.eval.exception.ReturnException) ConditionException(org.matheclipse.core.eval.exception.ConditionException) EvalEngine(org.matheclipse.core.eval.EvalEngine) ID(org.matheclipse.core.expression.ID) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) Externalizable(java.io.Externalizable) IOException(java.io.IOException) ISymbol(org.matheclipse.core.interfaces.ISymbol) S(org.matheclipse.core.expression.S) List(java.util.List) PatternNested(org.matheclipse.core.expression.PatternNested) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) Logger(org.apache.logging.log4j.Logger) IExpr(org.matheclipse.core.interfaces.IExpr) ObjectInput(java.io.ObjectInput) ArrayDeque(java.util.ArrayDeque) LogManager(org.apache.logging.log4j.LogManager) MultisetPartitionsIterator(org.matheclipse.core.combinatoric.MultisetPartitionsIterator) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 49 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class MatrixD method evaluate.

/**
 * For the referenced formula numbers (XX) see:
 * <a href="https://archive.org/details/imm3274/">Internet Archive - The Matrix Cookbook</a>
 */
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    IOFunctions.printExperimental(S.MatrixD);
    if (ast.size() < 3) {
        return F.NIL;
    }
    final IExpr fx = ast.arg1();
    if (fx.isIndeterminate()) {
        return S.Indeterminate;
    }
    if (ast.size() > 3) {
        // reduce arguments by folding MatrixD[fxy, x, y] to MatrixD[ MatrixD[fxy, x], y] ...
        return ast.foldLeft((x, y) -> engine.evaluateNIL(F.MatrixD(x, y)), fx, 2);
    }
    IExpr x = ast.arg2();
    if (!(x.isVariable() || x.isList())) {
        // `1` is not a valid variable.
        return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
    }
    if (fx.isList()) {
        IAST list = (IAST) fx;
        // thread over first list
        return list.mapThreadEvaled(engine, F.ListAlloc(list.size()), ast, 1);
    }
    if (x.isList()) {
        // MatrixD[fx_, {...}]
        IAST xList = (IAST) x;
        if (xList.isAST1() && xList.arg1().isListOfLists()) {
            IAST subList = (IAST) xList.arg1();
            IASTAppendable result = F.ListAlloc(subList.size());
            result.appendArgs(subList.size(), i -> F.MatrixD(fx, F.list(subList.get(i))));
            return result;
        } else if (xList.isAST1() && xList.arg1().isList()) {
            IAST subList = (IAST) xList.arg1();
            return subList.mapLeft(F.ListAlloc(), (a, b) -> engine.evaluateNIL(F.MatrixD(a, b)), fx);
        } else if (xList.isAST2()) {
            if (xList.arg1().isList()) {
                x = F.list(xList.arg1());
            } else {
                x = xList.arg1();
            }
            IExpr arg2 = xList.arg2();
            int n = arg2.toIntDefault();
            if (n >= 0) {
                IExpr temp = fx;
                for (int i = 0; i < n; i++) {
                    temp = S.MatrixD.ofNIL(engine, temp, x);
                    if (!temp.isPresent()) {
                        return F.NIL;
                    }
                }
                return temp;
            }
            if (arg2.isFree(num -> num.isNumber(), false)) {
                if (fx.equals(x)) {
                    return S.$SingleEntryMatrix;
                }
                if (fx.isAST()) {
                    final IAST function = (IAST) fx;
                    if (function.isPlus()) {
                        // (35)
                        return function.mapThread(F.MatrixD(F.Slot1, xList), 1);
                    }
                }
                return F.NIL;
            }
            if (!x.isVariable()) {
                // `1` is not a valid variable.
                return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
            }
            if (arg2.isAST()) {
                return F.NIL;
            }
            // symbolic expression or a non-negative integer.
            return IOFunctions.printMessage(ast.topHead(), "dvar", F.list(xList), engine);
        }
        return F.NIL;
    }
    if (!x.isVariable()) {
        // `1` is not a valid variable.
        return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
    }
    return binaryMatrixD(fx, x);
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) S(org.matheclipse.core.expression.S) Logger(org.apache.logging.log4j.Logger) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) MatrixDRules(org.matheclipse.core.reflection.system.rules.MatrixDRules) AbstractFunctionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator) IExpr(org.matheclipse.core.interfaces.IExpr) IOFunctions(org.matheclipse.core.builtin.IOFunctions) LogManager(org.apache.logging.log4j.LogManager) BinaryBindIth1st(org.matheclipse.core.generic.BinaryBindIth1st) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 50 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class TrigToExp method evaluate.

/**
 * Exponential definitions for trigonometric functions
 *
 * <p>
 * See <a href=
 * "http://en.wikipedia.org/wiki/List_of_trigonometric_identities#Exponential_definitions"> List
 * of trigonometric identities - Exponential definitions</a>,<br>
 * <a href="http://en.wikipedia.org/wiki/Hyperbolic_function">Hyperbolic function</a>
 */
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    IExpr temp = StructureFunctions.threadLogicEquationOperators(ast.arg1(), ast, 1);
    if (temp.isPresent()) {
        return temp;
    }
    IExpr arg1 = ast.arg1();
    Function<IExpr, IExpr> fun = x -> {
        IExpr t = x.rewrite(ID.Exp);
        if (!t.isPresent()) {
            return x.rewrite(ID.Log);
        }
        return t.rewrite(ID.Log).orElse(t);
    };
    VisitorReplaceAllDFS dfs = new VisitorReplaceAllDFS(fun, 1);
    return arg1.accept(dfs).orElse(arg1);
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) ID(org.matheclipse.core.expression.ID) VisitorReplaceAllDFS(org.matheclipse.core.visit.VisitorReplaceAllDFS) AbstractEvaluator(org.matheclipse.core.eval.interfaces.AbstractEvaluator) StructureFunctions(org.matheclipse.core.builtin.StructureFunctions) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ISymbol(org.matheclipse.core.interfaces.ISymbol) Function(java.util.function.Function) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) VisitorReplaceAllDFS(org.matheclipse.core.visit.VisitorReplaceAllDFS) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

EvalEngine (org.matheclipse.core.eval.EvalEngine)131 IExpr (org.matheclipse.core.interfaces.IExpr)71 IAST (org.matheclipse.core.interfaces.IAST)39 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)20 ISymbol (org.matheclipse.core.interfaces.ISymbol)20 IOException (java.io.IOException)13 F (org.matheclipse.core.expression.F)12 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)11 S (org.matheclipse.core.expression.S)11 IInteger (org.matheclipse.core.interfaces.IInteger)11 ASTNode (org.matheclipse.parser.client.ast.ASTNode)11 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 AST2Expr (org.matheclipse.core.convert.AST2Expr)9 ExprParser (org.matheclipse.core.parser.ExprParser)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 MathException (org.matheclipse.parser.client.math.MathException)8 ArrayList (java.util.ArrayList)7 Config (org.matheclipse.core.basic.Config)7 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)7