Question

EDIT: I have found that even if I try and use the simplest of expressions, i.e. 1+1, in the evaluate() method, it can't do it. It is giving me runtime errors.

I downloaded the symja_android_library.jar file, and added it to my classpath. My code is as follows:

import static org.matheclipse.core.expression.F.*;
import org.matheclipse.core.eval.EvalUtilities;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.parser.client.SyntaxError;
import org.matheclipse.parser.client.math.MathException;

public class CalculusExample {
    public static void main(String[] args) {
                try {
                        EvalUtilities util = new EvalUtilities(false);
                        IExpr result;
                        result = util.evaluate("d(x^2,x)");
                        System.out.println(result.toString());
                        result = util.evaluate("integrate(sin(x)^5,x)");
                        System.out.println(result.toString());
                } catch (SyntaxError e) {
                        System.out.println(e.getMessage());
                } catch (MathException me) {
                        System.out.println(me.getMessage());
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

Problem is,both of the ".evauate()" methods are giving me errors appearing in the console output stating "Syntax error in line: 1 - ')' expected. integrate(sin(x)^5,x), with a caret pointing to the comma. Could someone help me with the correct syntax? I have tried 1. removing the comma and the "x", 2. replacing the comma with a ")", but neither of these work. Thanks!

Was it helpful?

Solution

This is by no means a reasoned explanation, but this works and should allow you to make progress in your work

ScriptEngine fScriptEngine = fScriptManager.getEngineByExtension("m");
try {
    String evaledResult = (String) fScriptEngine.eval("D[x^2,x]");
    System.out.println(evaledResult);
} catch (Exception e) {
    e.printStackTrace();
}

Result: 2*x

The documentation for this API is pretty weak. I had to read through their JUnit tests to put this together...

The expression I used also works with EvalUtils, though it returns a slightly different result.

Result: Times[2, x]

OTHER TIPS

Try this

 public static void main(String[] args) {
                    try {

                        EvalUtilities util = new EvalUtilities(true);
                        IExpr result;
                        result = util.evaluate("d(x^2)");
                        System.out.println(result.toString());
                        result = util.evaluate("integrate[sin[x]^5,x]");
                        System.out.println(result.toString());
                } catch (SyntaxError e) {
                        System.out.println(e.getMessage());
                } catch (MathException me) {
                        System.out.println(me.getMessage());
                } catch (Exception e) {
                        e.getMessage();
                }
        }

i think the EvalUtilities have some problems but i fixed ur "Syntax error in line: 1 - ')' expected" issue.

ScriptEngine fScriptEngine = fScriptManager.getEngineByExtension("m");

Why is the code above returning null?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top