Search in sources :

Example 31 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class EvalControlledCallable method call.

@Override
public IExpr call() {
    EvalEngine.remove();
    EvalEngine.setReset(fEngine);
    final StringWriter buf = new StringWriter();
    try {
        // fEngine.reset();<>
        IExpr preRead = S.$PreRead.assignedValue();
        IExpr temp;
        try {
            if (preRead != null && preRead.isPresent()) {
                temp = fEngine.evaluate(F.unaryAST1(preRead, fExpr));
            } else {
                temp = fEngine.evaluate(fExpr);
            }
        } catch (final IterationLimitExceeded e) {
            // Iteration limit of `1` exceeded.
            int iterationLimit = fEngine.getIterationLimit();
            IOFunctions.printMessage(S.$IterationLimit, "itlim", F.list(iterationLimit < 0 ? F.CInfinity : F.ZZ(iterationLimit), fExpr), fEngine);
            temp = F.Hold(fExpr);
        } catch (final RecursionLimitExceeded e) {
            // Recursion depth of `1` exceeded during evaluation of `2`.
            int recursionLimit = fEngine.getRecursionLimit();
            IOFunctions.printMessage(S.$RecursionLimit, "reclim2", F.list(recursionLimit < 0 ? F.CInfinity : F.ZZ(recursionLimit), fExpr), fEngine);
            temp = F.Hold(fExpr);
        }
        // IExpr temp = fEngine.evaluate(fExpr);
        if (!fEngine.isOutListDisabled()) {
            fEngine.addInOut(fExpr, temp);
        }
        return temp;
    } catch (org.matheclipse.core.eval.exception.TimeoutException e) {
        return S.$Aborted;
    } catch (final SyntaxError se) {
        LOGGER.error("EvalControlledCallable.call() failed", se);
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        LOGGER.error(buf);
    } catch (final Exception | OutOfMemoryError | StackOverflowError e) {
        LOGGER.debug("EvalControlledCallable.call() failed", e);
        Validate.printException(buf, e);
        LOGGER.error(buf);
    }
    return S.$Aborted;
}
Also used : IterationLimitExceeded(org.matheclipse.core.eval.exception.IterationLimitExceeded) MathException(org.matheclipse.parser.client.math.MathException) RecursionLimitExceeded(org.matheclipse.core.eval.exception.RecursionLimitExceeded) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 32 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class ExprEvaluatorTest method testFactorInteger.

/**
 * Test the <a href=
 * "https://github.com/axkr/symja_android_library/blob/master/symja_android_library/doc/functions/FactorInteger.md">FactorInteger</a>
 * function.
 */
public void testFactorInteger() {
    try {
        ExprEvaluator util = new ExprEvaluator(false, (short) 100);
        IAST function = F.FactorInteger(F.ZZ(new BigInteger("44343535354351600000003434353")));
        IExpr result = util.eval(function);
        assertEquals("{{149,1},{329569479697,1},{903019357561501,1}}", result.toString());
    } catch (SyntaxError e) {
        // catch Symja parser errors here
        assertTrue(false);
    } catch (MathException me) {
        // catch Symja math errors here
        assertTrue(false);
    } catch (final Exception ex) {
        assertTrue(false);
    } catch (final StackOverflowError soe) {
        assertTrue(false);
    } catch (final OutOfMemoryError oome) {
        assertTrue(false);
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) BigInteger(java.math.BigInteger) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) MathException(org.matheclipse.parser.client.math.MathException)

Example 33 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class ExprEvaluatorTest method testStringEval004.

public void testStringEval004() {
    try {
        ExprEvaluator util = new ExprEvaluator();
        util.defineVariable("x", 1.0);
        util.defineVariable("y", 1.0);
        IExpr expr = util.eval("If(x*x+y*y==0,1,Sin(x*x+y*y)/(x*x+y*y))");
        assertEquals("0.454649", expr.toString());
    } catch (SyntaxError e) {
        // catch Symja parser errors here
        System.out.println(e.getMessage());
    } catch (MathException me) {
        // catch Symja math errors here
        System.out.println(me.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } catch (final StackOverflowError soe) {
        System.out.println(soe.getMessage());
    } catch (final OutOfMemoryError oome) {
        System.out.println(oome.getMessage());
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) MathException(org.matheclipse.parser.client.math.MathException)

Example 34 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class ExprEvaluatorTest method testStringEval003.

public void testStringEval003() {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr expr = util.eval("x^2+y+a*x+b*y+c");
        assertEquals("c+a*x+x^2+y+b*y", expr.toString());
        final IAST variables = F.List(F.symbol("x"), F.symbol("y"));
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, variables, variables.argSize(), ExprTermOrderByName.Lexicographic, false);
        ExprPolynomial poly = ring.create(expr);
        assertEquals("x^2 + a x + ( 1+b ) y + c ", poly.toString());
        // x degree
        assertEquals(2, poly.degree(0));
        // y degree
        assertEquals(1, poly.degree(1));
        // show internal structure:
        assertEquals("{{2,0}->1,{1,0}->a,{0,1}->1+b,{0,0}->c}", poly.coefficientRules().toString());
        System.out.println(poly.coefficientRules());
        for (ExprMonomial monomial : poly) {
            System.out.println(monomial.toString());
        }
    } catch (SyntaxError e) {
        // catch Symja parser errors here
        System.out.println(e.getMessage());
    } catch (MathException me) {
        // catch Symja math errors here
        System.out.println(me.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } catch (final StackOverflowError soe) {
        System.out.println(soe.getMessage());
    } catch (final OutOfMemoryError oome) {
        System.out.println(oome.getMessage());
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ExprMonomial(org.matheclipse.core.polynomials.longexponent.ExprMonomial) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial) MathException(org.matheclipse.parser.client.math.MathException)

Example 35 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class ExprEvaluatorTest method testStringVariables001.

public void testStringVariables001() {
    try {
        ExprEvaluator util = new ExprEvaluator(false, (short) 100);
        IExpr result = util.eval("Variables(termquery$$fuchs*(termquery$$huhn*(1-termquery$$schaf)+termquery$$schaf)+(1-termquery$$fuchs)*termquery$$schaf*termquery$$wolf)");
        assertEquals("{termquery$$fuchs,termquery$$huhn,termquery$$schaf,termquery$$wolf}", result.toString());
    } catch (SyntaxError e) {
        // catch Symja parser errors here
        assertTrue(false);
    } catch (MathException me) {
        // catch Symja math errors here
        assertTrue(false);
    } catch (final Exception ex) {
        assertTrue(false);
    } catch (final StackOverflowError soe) {
        assertTrue(false);
    } catch (final OutOfMemoryError oome) {
        assertTrue(false);
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) MathException(org.matheclipse.parser.client.math.MathException)

Aggregations

MathException (org.matheclipse.parser.client.math.MathException)51 IExpr (org.matheclipse.core.interfaces.IExpr)41 SyntaxError (org.matheclipse.parser.client.SyntaxError)37 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)24 IAST (org.matheclipse.core.interfaces.IAST)15 StringWriter (java.io.StringWriter)12 SymjaMathException (org.matheclipse.core.eval.exception.SymjaMathException)8 IOException (java.io.IOException)7 AbortException (org.matheclipse.core.eval.exception.AbortException)7 FailedException (org.matheclipse.core.eval.exception.FailedException)7 ASTNode (org.matheclipse.parser.client.ast.ASTNode)7 EvalControlledCallable (org.matheclipse.core.eval.EvalControlledCallable)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 DoubleEvaluator (org.matheclipse.parser.client.eval.DoubleEvaluator)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)3 OutputFormFactory (org.matheclipse.core.form.output.OutputFormFactory)3 ExprParser (org.matheclipse.core.parser.ExprParser)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2