Search in sources :

Example 6 with MathException

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

the class MathUtils method integrate.

/**
	 * Integrate a function numerically.
	 * 
	 * @param method
	 *            the following methods are possible: LegendreGauss, Simpson,
	 *            Romberg, Trapezoid
	 * @param fun
	 *            the function which should be integrated
	 * @param v
	 *            the variable
	 * @param aS
	 *            lower bound double value string for integration
	 * @param bS
	 *            upper bound double value string for integration
	 * @return
	 * @throws MathException
	 */
public static double integrate(String method, String fun, String v, String aS, String bS) throws MathException {
    ExprEvaluator parser = new ExprEvaluator();
    double a, b;
    try {
        a = parser.evaluateDoube(aS);
    } catch (MathException e) {
        // e.getMessage(), e.context);
        throw e;
    }
    try {
        // b = parser.parse(bS).getVal();
        b = parser.evaluateDoube(bS);
    } catch (MathException e) {
        // e.getMessage(), e.context);
        throw e;
    }
    IExpr function = parse(fun, null);
    IExpr var = parse(v, null);
    IAST list = F.List();
    list.append(var);
    list.append(F.num(a));
    list.append(F.num(b));
    return NIntegrate.integrate("LegendreGauss", list, a, b, function, NIntegrate.DEFAULT_MAX_POINTS, NIntegrate.DEFAULT_MAX_ITERATIONS);
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 7 with MathException

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

the class Console method interpreter.

/**
	 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
	 * 
	 * @param inputExpression
	 * @return
	 */
public String interpreter(final String inputExpression) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (fSeconds <= 0) {
            result = fEvaluator.evaluate(inputExpression);
        } else {
            result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true);
        }
        if (result != null) {
            if (result.equals(F.Null)) {
                return "";
            }
            StringBuilder strBuffer = new StringBuilder();
            fOutputFactory.convert(strBuffer, result);
            return strBuffer.toString();
        }
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println();
        System.err.println(msg);
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        return "";
    }
    return buf.toString();
}
Also used : StringWriter(java.io.StringWriter) 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) IOException(java.io.IOException)

Example 8 with MathException

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

the class TimeConstrained method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    IExpr arg2 = engine.evaluate(ast.arg2());
    long seconds = 0L;
    try {
        if (arg2.isSignedNumber()) {
            seconds = ((ISignedNumber) arg2).toLong();
        } else {
            engine.printMessage("TimeConstrained: " + ast.arg2().toString() + " is not a Java long value.");
            return F.NIL;
        }
    } catch (ArithmeticException ae) {
        engine.printMessage("TimeConstrained: " + ast.arg2().toString() + " is not a Java long value.");
        return F.NIL;
    }
    if (Config.JAS_NO_THREADS) {
        // no thread can be spawned
        try {
            return engine.evaluate(ast.arg1());
        } catch (final MathException e) {
            throw e;
        } catch (final Throwable th) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
        }
        return F.Aborted;
    } else {
        TimeLimiter timeLimiter = new SimpleTimeLimiter();
        Callable<IExpr> work = new EvalCallable(ast.arg1(), engine);
        try {
            return timeLimiter.callWithTimeout(work, seconds, TimeUnit.SECONDS, true);
        } catch (java.util.concurrent.TimeoutException e) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
            return F.Aborted;
        } catch (com.google.common.util.concurrent.UncheckedTimeoutException e) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
            return F.Aborted;
        } catch (Exception e) {
            if (Config.DEBUG) {
                e.printStackTrace();
            }
            return F.Null;
        }
    }
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) MathException(org.matheclipse.parser.client.math.MathException) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 9 with MathException

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

the class PolynomialExample method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr expr = util.evaluate("x^2+y+a*x+b*y+c");
        System.out.println(expr.toString());
        final IAST variables = F.List(F.x, F.y);
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, variables, variables.size() - 1, ExprTermOrderByName.Lexicographic, false);
        ExprPolynomial poly = ring.create(expr);
        System.out.println(poly.toString());
        // x degree
        System.out.println(poly.degree(0));
        // y degree
        System.out.println(poly.degree(1));
        // show internal structure:
        System.out.println(poly.coefficientRules());
        System.out.println();
        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.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.ExprMonomial) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial) MathException(org.matheclipse.parser.client.math.MathException)

Example 10 with MathException

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

the class SolveExample method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr result = util.evaluate("Solve(2*x==5 + 4*x,x)");
        // print: {{x->-5/2}}
        System.out.println(result.toString());
        result = util.evaluate("Roots(2*x==5+4*x, x)");
        // print: x==-5/2
        System.out.println(result.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)

Aggregations

MathException (org.matheclipse.parser.client.math.MathException)23 IExpr (org.matheclipse.core.interfaces.IExpr)16 SyntaxError (org.matheclipse.parser.client.SyntaxError)11 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)6 IAST (org.matheclipse.core.interfaces.IAST)6 ASTNode (org.matheclipse.parser.client.ast.ASTNode)6 IOException (java.io.IOException)4 StringWriter (java.io.StringWriter)4 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ExprParser (org.matheclipse.core.parser.ExprParser)2 DoubleEvaluator (org.matheclipse.parser.client.eval.DoubleEvaluator)2 SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)1 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Bindings (javax.script.Bindings)1 ScriptException (javax.script.ScriptException)1 AST2Expr (org.matheclipse.core.convert.AST2Expr)1 ASCIIPrettyPrinter3 (org.matheclipse.core.form.output.ASCIIPrettyPrinter3)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1