Search in sources :

Example 41 with MathException

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

the class Issue212 method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator(false, (short) 100);
        ISymbol a = F.Dummy("a");
        ISymbol b = F.Dummy("b");
        ISymbol c = F.Dummy("c");
        IAST compound = F.CompoundExpression(F.Set(a, F.ZZ(13)), F.Set(b, F.ZZ(11)), F.Set(c, F.ZZ(12)), F.Plus(a, b, c));
        IExpr result = util.eval(compound);
        // print 36
        if (result.isInteger()) {
            IInteger n = (IInteger) result;
            System.out.println(n.toString());
        }
        // solve({x==y,y==2},{x,y})
        ISymbol x = F.Dummy("x");
        ISymbol y = F.Dummy("y");
        IAST solve = // 
        F.Solve(// 
        F.List(F.Equal(x, y), F.Equal(y, F.ZZ(2))), F.List(x, y));
        result = util.eval(solve);
        if (result.isListOfLists()) {
            IAST list = (IAST) result;
            for (int i = 1; i < list.size(); i++) {
                IExpr arg = list.get(i);
                if (arg.isListOfRules()) {
                    IAST listOfRules = (IAST) arg;
                    for (int j = 1; j < listOfRules.size(); j++) {
                        IAST rule = (IAST) listOfRules.get(j);
                        System.out.println(rule.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 (final Exception ex) {
        System.out.println(ex.getMessage());
    } 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) ISymbol(org.matheclipse.core.interfaces.ISymbol) SyntaxError(org.matheclipse.parser.client.SyntaxError) IInteger(org.matheclipse.core.interfaces.IInteger) MathException(org.matheclipse.parser.client.math.MathException) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) MathException(org.matheclipse.parser.client.math.MathException)

Example 42 with MathException

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

the class QuantityExample method main.

public static void main(String[] args) {
    try {
        F.initSymbols();
        EvalEngine engine = new EvalEngine(false);
        // ExprEvaluator engine = new ExprEvaluator(false, 100);
        IAST function = F.Times(F.C7, F.Quantity(F.C2, F.$str("ft")));
        IExpr result = engine.evaluate(function);
        // print: 14[ft]
        System.out.println("Out[1]: " + 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 (final Exception ex) {
        System.out.println(ex.getMessage());
    } catch (final StackOverflowError soe) {
        System.out.println(soe.getMessage());
    } catch (final OutOfMemoryError oome) {
        System.out.println(oome.getMessage());
    }
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) EvalEngine(org.matheclipse.core.eval.EvalEngine) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) MathException(org.matheclipse.parser.client.math.MathException)

Example 43 with MathException

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

the class SolveIssue171 method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        // IExpr exp1 = util.evaluate("x=985.3698808807793");
        IExpr exp2 = util.eval("y=89.73311105248706");
        IExpr exp3 = util.eval("z=30.358930164378133");
        IExpr exp4 = util.eval("w=143.26674070021394");
        IExpr exp5 = util.eval("q=923.282853878973");
        IExpr exp6 = util.eval("j=955.7677262340256");
        // IExpr exp = util.evaluate(" N(Solve({q/w==(m+2.0*y)/(m+z+x+j)},m))");
        IExpr exp = util.eval("Solve(4.027433300199394==(110.70534+x)/(1015.3739400000001+x),x)");
        // -1314.2
        System.out.println(exp.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 44 with MathException

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

the class SymbolicSO43739728 method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        // (3/4)*(4/3)*Sqrt(2)*I
        IExpr formula = F.Times(F.QQ(3, 4), F.QQ(4, 3), F.Sqrt(F.ZZ(2)), F.CI);
        // symbolic evaluation
        IExpr result = util.eval(formula);
        // print: I*Sqrt(2)
        System.out.println(result.toString());
        // numerical evaluations
        result = util.eval(F.N(formula));
        // I*1.4142135623730951
        System.out.println(result.toString());
        // print result in decimal format
        final StringWriter buf = new StringWriter();
        OutputFormFactory.get(true, false, 5, 7).convert(buf, result);
        // I*1.41421
        System.out.println(buf.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) 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)

Example 45 with MathException

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

the class ImportJSON method main.

public static void main(String[] args) {
    try {
        Config.FILESYSTEM_ENABLED = true;
        IOInit.init();
        ExprEvaluator util = new ExprEvaluator();
        IExpr expr = util.eval("Import(\"https://json-stat.org/samples/oecd.json\",\"JSON\")");
        StringBuilder buf = new StringBuilder();
        OutputFormFactory off = OutputFormFactory.get(false, false);
        off.setIgnoreNewLine(true);
        off.setInputForm(true);
        if (off.convert(buf, expr)) {
            System.out.println(buf);
        }
    } 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) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) 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