# Wiki

# ScMathML / Home

## ScMathML

ScMathML is a Scala library for executing Content MathML.

Why would you want this? Content MathML is a move towards a standard, open format for representing mathematics with relatively well defined semantics. If you're doing calculations, using MathML to represent formulas will make everything

- explicit - no hidden handwavy stuff
- semantically well defined - you can find out exactly what each part does
- transportable - it's machine/problem independant

ScMathML takes formulas, and evaluates them in a Context, which provides access to domain objects, constants etc.

There are simple examples below. For more, see: Variables, Summations, Objects and Expression

## Getting It

Via the ScMathML Bitbucket Repo, e.g.

$ hg clone https://bitbucket.org/mo_seph/scmathml/wiki

As a Maven dependency:

```
<dependency>
<groupId>org.xml-cml</groupId>
<artifactId>SCMathML</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
```

in the Cambridge chemistry repo:

```
<repositories>
<repository>
<id>ucc-repo</id>
<name>UCC Repository</name>
<url>http://maven.ch.cam.ac.uk/m2repo</url>
</repository>
</repositories>
```

## Getting Started

Despite being a Scala library, ScMathML works fine from Java. Here's a minimal Java example:

```
import org.xmlcml.mathml.*;
public class JavaExample
{
public static void main( String[] args )
{
MathMLParser parser = new MathMLParser();
String expression = "<math><apply><plus/><cn>2</cn><cn>2</cn></apply></math>";
MathMLDoubleElement tree = parser.buildDoubleExpressionFromString( expression );
System.out.println("Result: " + tree.eval(new Context()));
}
}
```

$ Result: 4.0

This runs the well known equation 2+2.

Now doing something slightly more complex - poking variables into the context (assuming the same wrapper around main()}

```
MathMLParser parser = new MathMLParser();
String expression = "<math><apply><times/><cn>2</cn><ci>x</ci></apply></math>";
MathMLDoubleElement tree = parser.buildDoubleExpressionFromString( expression );
Context c = new Context();
for( int i = 0; i < 5; i++ )
{
c.setVar( "x", i );
System.out.println("Result: " + tree.eval(c));
}
```

Result: 0.0 Result: 2.0 Result: 4.0 Result: 6.0 Result: 8.0

## Next Steps

Everything is made of Expressions, which use Variables to do their stuff. Objects can be used to work with JVM objects and do interesting things with them.

The tests in src/test/scala should give an idea of how things work.

Updated