Search in sources :

Example 46 with MathException

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

the class SolveIssue176 method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        util.eval("x0=20.796855124168776");
        IExpr result = util.eval("Solve(x0==(-1.0000000000000002)*Sqrt(y^2.0),y)");
        // {}
        System.out.println(result.toString());
        result = util.eval("Solve(x1==(-1.0000000000000002)*Sqrt(y^2.0),y)");
        // {{y->-x1},{y->x1}}
        System.out.println(result.toString());
        IExpr exp1 = util.eval("x2=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");
        result = util.eval(" N(Solve(q/w==(m+2.0*y)/(m+z+x2+j),m))");
        // {{m->-2300.6414589715228}}
        System.out.println(result.toString());
        result = util.eval("Solve(923.282853878973/143.26674070021394==(m+2.0*89.73311105248706)/(m+30.358930164378133+985.3698808807793+955.7677262340256),m)");
        // {{m->-2300.6414589715228}}
        System.out.println(result.toString());
        result = util.eval("Solve(4.027433300199394==(110.70534+x3)/(1015.3739400000001+x3),x3)");
        // {{x3->-1314.197567242396}}
        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)

Example 47 with MathException

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

the class MathUtils method getFunctionVal.

public static String getFunctionVal(String fun, String[] var, String resp, String[] vals) throws MathException {
    // return evaluate(command.toString(), null);
    try {
        EvalDouble parParser = new EvalDouble(true);
        double[] values = new double[vals.length];
        for (int i = 0; i < vals.length; i++) {
            values[i] = parParser.evaluate(vals[i]);
        }
        String respVar = null;
        for (int i = 0; i < var.length; i++) {
            if (var[i].equals(resp)) {
                respVar = resp;
                // parParser.add(respVar);
                // respVar.setVal(values[i]);
                parParser.defineVariable(respVar, values[i]);
            } else {
                String temp = var[i];
                parParser.defineVariable(temp, values[i]);
            }
        }
        if (respVar != null) {
            try {
                ASTNode f = parParser.parse(fun);
                return parParser.evaluateNode(f) + "";
            } catch (MathException e) {
                // e.getMessage(), e.context);
                throw e;
            }
        }
    } catch (MathException e) {
        // ParserContext(resp, 0, null));
        throw e;
    }
    throw new SymjaMathException("MathUtils:getFunctionVal - cannot compute function values");
}
Also used : SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException) MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

Example 48 with MathException

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

the class MathUtils method getFunctionVal.

public static double getFunctionVal(String f, String v, String x) {
    // StringBuilder command = new StringBuilder();
    // command.append("ReplaceAll(");
    // command.append(f);
    // command.append(",");
    // command.append(v);
    // command.append("-> (");
    // command.append(x);
    // command.append(")");
    // command.append(")");
    // String result = evaluate(command.toString(), "N");
    // 
    // EvalDouble dEval = new EvalDouble(true);
    // return dEval.evaluate(result);
    // Variable var = new Variable(v);
    // Expression fun,val;
    // Parser parser = new Parser(Parser.STANDARD_FUNCTIONS |
    // Parser.OPTIONAL_PARENS
    // | Parser.OPTIONAL_STARS | Parser.OPTIONAL_SPACES
    // | Parser.BRACES | Parser.BRACKETS| Parser.BOOLEANS);
    // parser.add(var);
    // setUpParser(parser);
    EvalDouble parser = new EvalDouble(true);
    String var = v;
    ASTNode fun, val;
    parser.defineVariable(var);
    try {
        fun = parser.parse(f);
    } catch (MathException e) {
        // + e.getMessage(), e.context);
        throw e;
    }
    try {
        val = parser.parse(x);
    } catch (MathException e) {
        // e.getMessage(), e.context);
        throw e;
    }
    // var.setVal(val.getVal());
    parser.defineVariable(var, parser.evaluateNode(val));
    return parser.evaluateNode(fun);
}
Also used : SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException) MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Example 49 with MathException

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

the class SymjaInterpreter method interpreter.

/**
 * Parse the <code>codeString</code> into an <code>IExpr</code> and if <code>function</code>
 * unequals <code>null</code>, replace all occurences of slot <code>#</code> in the function with
 * the parsed expression. After that evaluate the given expression.
 *
 * @param function
 * @return
 */
public String interpreter(IAST function) {
    String evalStr = codeString;
    IExpr expr;
    EvalEngine engine = EvalEngine.get();
    EvalEngine.setReset(engine);
    try {
        ExprParser p = new ExprParser(engine, true);
        // throws SyntaxError exception, if syntax isn't valid
        expr = p.parse(evalStr);
    } catch (SyntaxError e1) {
        try {
            ExprParser p = new ExprParser(engine);
            // throws SyntaxError exception, if syntax isn't valid
            expr = p.parse(evalStr);
        } catch (Exception e2) {
            outStream.println(e2.getMessage());
            return "";
        }
    }
    IExpr result;
    final StringBuilder buf = new StringBuilder();
    try {
        if (function != null) {
            expr = function.replaceAll(F.Rule(F.Slot1, expr));
        }
        if (expr.isPresent()) {
            result = evaluate(expr);
            if (result.isPresent()) {
                if (result.equals(S.Null)) {
                    return buf.toString();
                }
                if (OutputFormFactory.get(engine.isRelaxedSyntax()).convert(buf, result)) {
                    return buf.toString();
                }
            }
            return "ERROR-IN-OUTPUTFORM";
        }
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
    } catch (final Exception e) {
        Validate.printException(buf, e);
    }
    return buf.toString();
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException) MathException(org.matheclipse.parser.client.math.MathException)

Example 50 with MathException

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

the class MMAConsole method interpreter.

/**
 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
 *
 * @param trimmedInput a trimmed input string
 * @return
 */
/* package private */
String interpreter(final String trimmedInput) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
            IExpr doc = Documentation.findDocumentation(trimmedInput);
            return printResult(doc);
        }
        if (fSeconds <= 0) {
            result = fEvaluator.eval(trimmedInput);
        } else {
            result = fEvaluator.evaluateWithTimeout(trimmedInput, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            return printResult(result);
        }
    } catch (final AbortException re) {
        return printResult(S.$Aborted);
    } catch (final FailedException re) {
        return printResult(S.$Failed);
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        stderr.println(msg);
        // stderr.println();
        stderr.flush();
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        stderr.println(buf.toString());
        stderr.flush();
        return "";
    } catch (final Exception | OutOfMemoryError | StackOverflowError e) {
        Validate.printException(buf, e);
        stderr.println(buf.toString());
        stderr.flush();
        return "";
    }
    return buf.toString();
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) ReturnException(org.matheclipse.core.eval.exception.ReturnException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

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