Eclipse Wiki Weblog

All | General | Java | Eclipse | Groovy | Grails | GWT | Google | MathEclipse | Bliki
20080504 Sunday May 04, 2008

MathEclipse 0.0.6 - JSR 223 compatible symbolic math scripting engine

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

Contents
  1. Download
  2. Java Examples
  3. How to start the symbolic evaluation console
  4. How to start the numeric calculator console (no symbolic calculation; double and complex mode)

Download

You can download version 0.0.6 from Sourceforge.net:

Project Homepage:

Discussion Forum:

Java Examples

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

import java.io.FileReader;
import java.util.ArrayList;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Test {

	public static void main(String[] args) {
		ScriptEngineManager scriptManager = new ScriptEngineManager();

		String stringResult = null;
		ScriptEngine meEngine = scriptManager.getEngineByExtension("m");

This 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 how to assign 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());
		} 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 calculator console (no symbolic calculation; double and complex mode)

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

Posted by axelclk ( May 04 2008, 10:04:15 AM CEST ) Permalink Comments [2]

Calendar

Links

Search

del.icio.us Tag Cloud

RSS Feeds

Navigation

Referers