|
|
Matheclipse Scripting Engine
MathEclipse 0.0.6 is a JSR 223 compatible symbolic math scripting engine.
DownloadYou can download version 0.0.6 from Sourceforge.net: Discussion Forum: ExamplesTo create a MathEclipse
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;
public class Test {
public static void main(String[] args) {
ScriptEngineManager scriptManager = new ScriptEngineManager();
String stringResult = null;
ScriptEngine meEngine = scriptManager.getEngineByExtension("m");
The following snippet evaluates the derivative of
try {
stringResult = (String) meEngine.eval("D[Sin[x]*Cos[x],x]");
System.out.println(stringResult);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
The following snippet expands an expression and after that factorizes it again:
try {
stringResult = (String) meEngine.eval("Expand[(x+5)^3]");
System.out.println(stringResult);
stringResult = (String) meEngine.eval("Factor["+stringResult+"]");
System.out.println(stringResult);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
This snippet shows the assigning of values to variables:
try {
meEngine.put("$x", new Boolean(true));
meEngine.put("$y", new Boolean(true));
stringResult = (String) meEngine.eval("$x && $y");
System.out.println(stringResult);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
Assuming that the
$m={$x, $y, {13, 7, 8}}; $m.$m
we can now define values for the variables
try {
ArrayList<Object> row = new ArrayList<Object>();
row.add("List"); // head of the expression
row.add(Integer.valueOf(1));
row.add(Integer.valueOf(2));
row.add(Integer.valueOf(3));
int[] intArr = { 3, 4, 11 };
meEngine.put("$x", row);
meEngine.put("$y", intArr);
// the test.m file contains this script for matrix multiplication:
// $m={$x, $y, {13, 7, 8}}; $m.$m
ScriptContext context = meEngine.getContext();
context.setAttribute(MathScriptEngine.RETURN_OBJECT, Boolean.TRUE, ScriptContext.ENGINE_SCOPE);
Object objectResult = meEngine.eval(new FileReader("C:\\temp\\test.m"));
// print the result for matrix multiplication: {{1,2,3}, {3, 4, 11}, {13, 7, 8}}.{{1,2,3}, {3, 4, 11}, {13, 7, 8}}
System.out.println(objectResult.toString());
if (objectResult instanceof IExpr) {
// decompose the matrix into rows
IExpr expr = (IExpr) objectResult;
// gives the head "List", because matrices are list of row-lists
System.out.println(expr.getHeader());
if (expr instanceof List) {
// use java.util.List to print the rows
List<IExpr> list = (List<IExpr>) expr;
for (IExpr subExpr : list) {
System.out.println(subExpr);
}
IExpr subExpr;
// there's a difference between foreach and for loop
// because the head is stored at index 0:
for (int i = 0; i < list.size(); i++) {
subExpr = list.get(i);
System.out.println(subExpr);
}
}
if (expr instanceof IAST) {
// use org.matheclipse.core.interfaces.IAST to print the rows
IAST list = (IAST) expr;
for (IExpr subExpr : list) {
System.out.println(subExpr);
}
IExpr subExpr;
// there's a difference between foreach and for loop
// because the head is stored at index 0:
for (int i = 0; i < list.size(); i++) {
subExpr = list.get(i);
System.out.println(subExpr);
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
How to start the symbolic evaluation consoleYou can run the following command from the java -classpath commons-discovery.jar;commons-math.jar;google-collect-snapshot.jar;xercesImpl.jar;matheclipse-script-0.0.6.jar org.matheclipse.core.eval.Console How to start the numeric (no symbolic calculation; double and complex mode) calculator consoleYou can run the calculator console with the following command: java -classpath commons-discovery.jar;commons-math.jar;google-collect-snapshot.jar;xercesImpl.jar;matheclipse-script-0.0.6.jar org.matheclipse.parser.util.Console |
|
|
All contents copyright of the author. ©2007. JAMWiki Version 0.6.0 |
||