Matheclipse Scripting Engine

<<Back

MathEclipse 0.0.6 is a JSR 223 compatible symbolic math scripting engine.

Contents


Download

You can download version 0.0.6 from Sourceforge.net:

Discussion Forum:

Examples

To create a MathEclipse ScriptEngine we start with the following snippet:

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 Sin[x]*Cos[x]:


		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 c:\temp\test.m script file contains this code:

  $m={$x, $y, {13, 7, 8}}; $m.$m

we can now define values for the variables $x and $y (which contain the first and second row of the matrix $m) and multiply the matrix $m by itself:

		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 console

You can run the following command from the console.bat file under windows:

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 console

You 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