Mathos Parser is now an expression compiler

[This article was firstly published here]

The requirement of performing several thousands of calculations (for example, during integration) led to the optimization with pre-scanned expressions. But the aim to make Mathos Parser even faster still remained. Now, I would like to introduce you to a new project of Mathos Project - Mathos Expression Compiler, based on the math tokenizer and parser logic of Mathos Parser.

This API allows you to parse an expression and "store" it as CIL code. Then, using ILAsm, you can generate a new assembly that will contain your expression entirely parsed. The only operation that occurs at runtime is the insertion of values into the variables that you declared and the operation on that variable value. To make it even faster, most of the calculations are already done before the runtime, for example, x+3+5 would be stored as x+8.

In this scenario, the time consideration can be important. Even if it is faster to execute CIL instructions, the compilation process can take some time. This has not been tested yet. The process is illustrated below:

a1

This is a very simplified way of looking at it but the point is to always test Mathos Parser together with Mathos Expression Compiler and make an estimate of how much time would be saved in different scenarios. It all depends on the context. But note, that

A performance hit is incurred only the first time the method is called. All subsequent calls to the method execute at the full speed of the native code because verification and compilation to native code don't need to be performed again. //p. 12. Jeffrey Richter. CLR via C# 3rd Edition

So, if you plan to execute the function trillion times, it might be a good a idea to keep in mind the Expression Compiler as a better alternative, because at that point, the compilation time will be negligible.

Now, back to the features. It was mentioned in the beginning that it is based on the Mathos Parser. Note, in terms of features, Mathos Expression Compiler has at this point only a subset of those of Mathos Parser. At this point, you cannot have functions as sincos, and your own custom functions, but this will be changed in the nearest future.

There are also some changes to the way operators work. To declare an operator properly, you should use the variables OperatorListOperatorAction, and OperatorActionIL. Below, a simple example of addition operator:

Comparison operators are a bit more tricky. An example of greater than operator is shown below:

For those who understand IL, the @ sign might seem a bit strange. It is simply a way to tell the stack count to the if statement (in the compilation process).

When you actually generate an expression, this is how your IL code for that expression might look:

Will return (only IL for the expression):

When you have the IL code, you should name the output executable(or dll) as MathosILParser, otherwise, you might get an error. In future, an option will be added to customize the name of the output file, but at this point, please use RegEx (or replace option) to change the MathosILParser.exe to something else (in the IL code).

Your function can later by called as shown below. Note, I am not using reflection at all.

I hope this short guide was helpful, even if it might be a bit messy. If you have performed any testing related to speed or if you want to show some examples, please feel free to comment or contact me.

Advantages of the new GetTokens feature - Mathos Parser

Read the entire article here.

mp2 mp_separatly

Mathos Parser 1.0.7.1 is available

Please read more here!

Early version of Mathos Core Library Interpreter

Mathos Core Library Interpreter, or Iron Ruby Based Mathos Core Library Interpreter, is a project with one of the aims:

/../ to allow non-programmers to easily access the computation power of Mathos Core Library.

For those who would like to try out this app, please download the alpha version here.

At this point, there are only short commands for integration, function and expression evalution, saving and loading data object. The remaining features can be included into the interpreter in the same way as it would be done using other .NET targeting languages, for example C#. If you have some experience with Ruby, why not try out some calculations through this interface instead? API Documentation for the Core Library can be found here.

There is a video available that describes some basics of the interpreter. Please feel to write articles with examples of how Mathos Core Library modules can be used and send us a link. Or, why not record a video? If you are a developer, why not join us at this early stage when many of decisions are being made?

Links & Downlods:

Benchmark Utility - Testing Algorithms

Sometimes, there are many solutions to one problem. In terms of algorithms, there can exist several algorithms that will be able to come to the same result. At this stage, the execution time can be used as a comparison.

In Mathos Test project, there is a class called BenchmarkUtil with a single function Benchmark. It was developed by Masoud Shokri. This algorithm is very useful when the execution speed is to be calculated and compared to another algorithm. An example of the way it can be used is shown below:

Relative execution time can also be compared, as shown below:

The iter specifies the number of executions of a specific Action. This allows an average time to be calculated. The first parameter takes an action, which can be a method or a function (make sure to use the lambda expression as shown in the examples above).