# Expression evaluation

## MathType is provided with an evaluation engine for MathML. This engine computes a formula in order to obtain its decimal value. This feature should not be confused with Wiris CAS which is able to perform more complex operations.

For example, if x=8

The engine will be able to evaluate only well-formed formulas according to an internal built-in grammar.

As seen in the previous example, it is possible to predefine the value of some variables before the evaluation is done.

The decimal evaluation engine is related to MathType and content MathML in the sense that the evaluation consists on converting first from presentation MathML to content MathML. Then the resulting formula tree is traversed to perform the operations.

## Using MathType to edit formulas suitable to be evaluated

The evaluation engine receives MathML. You can optionally use MathType 7 to edit formulas to be sent to the evaluation engine. You will get the advantage of a real-time syntax checker and a specialized toolbar. Elsewhere we describe how to embed MathType in a web page.

To enable the syntax checking and the evaluation toolbar, when initializing MathType you should add some parameters

editor = com.wiris.jsEditor.JsEditor.newInstance(
{'toolbar':'evaluate', 'checkSyntax': 'true'});


or any time after the editor is created

editor.setParams({'toolbar':'evaluate', 'checkSyntax': 'true'})


Formally, the syntax checker looks for a well-formed expression. Even if you won't be using content MathML though (and quite likely you won't be), the process will essentially let you know if something simply doesn't make sense -- like one plus times abc:

## Calling the evaluation engine

The evaluation engine can be called using a Web service or doing a direct Java or .NET call.

### Calling the evaluation engine with a Web service

A call to the Web service with both GET and POST methods are possible. In general, it is preferable the POST method in order to avoid size limitations. The call has the form:

www.wiris.net/demo/editor/evaluate?mml=mathml&var_name1=value1&...&var_nameN=valueN


The encoding will be always the URL encoding with the underlying UTF-8 for non-ASCII characters.

Encode the MathML as the argument to the mml parameter. If you declare variables, they should have the form var_<name>=<value>.

For example,

 www.wiris.net/demo/editor/evaluate?mml=$<mn>1</mn><mo>-</mo><mn>2</mn>$


And if we want to assign x = 8.5,

 www.wiris.net/demo/editor/evaluate?mml=$<mi>x</mi><mo>-</mo><mn>2</mn>$&var_x=8.5


### Caution

Note the variable "x", when it appears as a parameter in the URL, is prepended by "var_" and the decimal separator is always ".".

If there is any problem in the evaluation (for example, a syntax error or a division by zero), the Web service will fail with a 5xx error code.

### Calling the evaluation engine with a direct Java or .NET call

To use this method, you will need a license to download and run the MathType server components. Available for Java and .NET.

The source code in Java:

    PublicServicesInterface ps = PublicServices.getInstance();
Properties p = new Properties();
p.setProperty("x","8.5");
String content =
ps.evaluate("$<mi>x</mi><mo>+</mo><mn>2</mn>$", p, null);


you will need to add the WIRISeditor.jar in the classpath.

The source code in c#:

    PublicServicesInterface ps = PublicServices.getInstance();
Dictionary<string,string> d = new Dictionary<string,string> ();
d.set("x","8.5");
string content =
ps.evaluate("$<mi>x</mi><mo>+</mo><mn>2</mn>$", d, null);


You will need to include a reference to the WIRISeditor.dll.