-
-
Notifications
You must be signed in to change notification settings - Fork 87
Java usage
Here is an annotated Java usage Example:
package org.matheclipse.core.examples;
import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.ISymbol;
import org.matheclipse.parser.client.SyntaxError;
import org.matheclipse.parser.client.math.MathException;
public class Example {
public static void main(String[] args) {
try {
ExprEvaluator util = new ExprEvaluator(false, 100);
The method toJavaForm
converts an expression to the internal Java form.
Note: single character identifiers are case sensitive (the D
function identifier must be written as upper case character)
String javaForm = util.toJavaForm("D(sin(x)*cos(x),x)");
The following line will print: D(Times(Sin(x),Cos(x)),x)
System.out.println("Out[1]: " + javaForm.toString());
Use the Java form to create an expression with F.* static methods:
ISymbol x = F.Dummy("x");
IAST function = F.D(F.Times(F.Sin(x), F.Cos(x)), x);
IExpr result = util.eval(function);
The following line will print: Cos(x)^2-Sin(x)^2
System.out.println("Out[2]: " + result.toString());
result = util.eval("diff(sin(x)*cos(x),x)");
The following line will print the same result: Cos(x)^2-Sin(x)^2
, because diff
is an alias for the D
function.
System.out.println("Out[3]: " + result.toString());
Evaluate the last result (%
contains the last answer)
result = util.eval("%+cos(x)^2");
The following line will print: 2*Cos(x)^2-Sin(x)^2
System.out.println("Out[4]: " + result.toString());
Evaluate an Integrate()
expression.
result = util.eval("integrate(sin(x)^5,x)");
The following line will print: 2/3*Cos(x)^3-1/5*Cos(x)^5-Cos(x)
System.out.println("Out[5]: " + result.toString());
Set the value of a variable a
to 10.
result = util.eval("a=10");
Print 10
.
System.out.println("Out[6]: " + result.toString());
Do a calculation with variable a
:
result = util.eval("a*3+b");
Print: 30+b
System.out.println("Out[7]: " + result.toString());
Do a calculation in "numeric mode" with the N()
function.
Note: single character identifiers are case sensistive (the N
function identifier must be written as upper case
character)
result = util.eval("N(sinh(5))");
Print: 74.20321057778875
System.out.println("Out[8]: " + result.toString());
Define a function with a recursive factorial function definition.
Note: fac(0)
is the stop condition for the recursion.
result = util.eval("fac(x_IntegerQ):=x*fac(x-1);fac(0)=1");
// now calculate factorial of 10:
result = util.eval("fac(10)");
Print: 3628800
.
System.out.println("Out[9]: " + result.toString());
} catch (SyntaxError e) {
You can catch Symja parser errors with the SyntaxError
exception here:
System.out.println(e.getMessage());
} catch (MathException me) {
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());
}
}
}