Search in sources :

Example 16 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 : MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Example 17 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 MathException("MathUtils:getFunctionVal - cannot compute function values");
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Example 18 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.evaluate(formula);
        // print: I*Sqrt(2)
        System.out.println(result.toString());
        // numerical evaluations
        result = util.evaluate(F.N(formula));
        // I*1.4142135623730951
        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 19 with MathException

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

the class CalculusExample method main.

public static void main(String[] args) {
    try {
        // don't distinguish between lower- and uppercase identifiers
        Config.PARSER_USE_LOWERCASE_SYMBOLS = true;
        ExprEvaluator util = new ExprEvaluator(false, 100);
        // Show an expression in the Java form:
        // Note: single character identifiers are case sensistive
        // (the "D()" function input must be written as upper case
        // character)
        String javaForm = util.toJavaForm("D(sin(x)*cos(x),x)");
        // prints: D(Times(Sin(x),Cos(x)),x)
        System.out.println(javaForm.toString());
        // Use the Java form to create an expression with F.* static
        // methods:
        IAST function = D(Times(Sin(x), Cos(x)), x);
        IExpr result = util.evaluate(function);
        // print: Cos(x)^2-Sin(x)^2
        System.out.println(result.toString());
        // Note "diff" is an alias for the "D" function
        result = util.evaluate("diff(sin(x)*cos(x),x)");
        // print: Cos(x)^2-Sin(x)^2
        System.out.println(result.toString());
        // evaluate the last result ($ans contains "last answer")
        result = util.evaluate("$ans+cos(x)^2");
        // print: 2*Cos(x)^2-Sin(x)^2
        System.out.println(result.toString());
        // evaluate an Integrate[] expression
        result = util.evaluate("integrate(sin(x)^5,x)");
        // print: 2/3*Cos(x)^3-1/5*Cos(x)^5-Cos(x)
        System.out.println(result.toString());
        // set the value of a variable "a" to 10
        // Note: in server mode the variable name must have a preceding '$'
        // character
        result = util.evaluate("a=10");
        // print: 10
        System.out.println(result.toString());
        // do a calculation with variable "a"
        result = util.evaluate("a*3+b");
        // print: 30+b
        System.out.println(result.toString());
        // Do a calculation in "numeric mode" with the N() function
        // Note: single character identifiers are case sensistive
        // (the "N()" function input must be written as upper case
        // character)
        result = util.evaluate("N(sinh(5))");
        // print: 74.20321057778875
        System.out.println(result.toString());
        // define a function with a recursive factorial function definition.
        // Note: fac(0) is the stop condition.
        result = util.evaluate("fac(x_IntegerQ):=x*fac(x-1);fac(0)=1");
        // now calculate factorial of 10:
        result = util.evaluate("fac(10)");
        // print: 3628800
        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 (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) SyntaxError(org.matheclipse.parser.client.SyntaxError) 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 20 with MathException

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

the class EvalDoubleTestCase method testMissingFunction009.

public void testMissingFunction009() {
    try {
        DoubleEvaluator engine = new DoubleEvaluator();
        double d = engine.evaluate("aTest[1.0]");
        assertEquals(Double.toString(d), "");
    } catch (MathException e) {
        assertEquals("EvalDouble#evaluateFunction(FunctionNode) not possible for: aTest(1.0)", e.getMessage());
    }
}
Also used : DoubleEvaluator(org.matheclipse.parser.client.eval.DoubleEvaluator) 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