Expression pre-compilation

TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!

Tutorial Math Collection API spec Download

Below is the code for JAVA, the code for C# is almost identical.

What is pre-compilation?

One of the most difficult procedures when determining the value of an expression is verifying compliance with the grammar, verifying other elements of the syntax, and carrying out detailed tokenization. For this reason mXparser tries to execute this procedure only once, which is called pre-compiling an expression.

When is pre-compilation done?

The pre-compilation is done when the checkSyntax() method is called. If the checkSytnax() method is true, the pre-compilation is successful. From that point on, if there is no structural change to the expression, calling checkSyntax() again returns the previously remembered true value. Important – each call of the calculate() method also calls the checkSyntax() method, so the correct first call to calculate() also makes pre-compilation.

When is pre-compilation done again?

If the expression is in the status after pre-compilation, then changes to the arguments’ values and changes to the function’s parameters do not change the structure of the expression, and in these cases the calculate() method is performed efficiently.

On the other hand, if the expression string is changed, the argument name is changed, the argument definition is changed, the function name is changed, the function definition is changed, then all these objects will inform the expression dependent on them on the need of another pre-compilation.

Case 1: An example of bad practice in computing the value of an expression for a changing argument value

Example of a mistake that is new objects creation in the loop, which consumes memory and pre-compiles each of them each time.

long startTimeMills = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
	Argument x = new Argument("x");
	Expression e = new Expression("2sin(x)", x);
	x.setArgumentValue(i);
	double result = e.calculate();
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
[mXparser-v.5.0.6] Computing time = 31.99s

Case 2: An example of good practice in computing the value of an expression for a changing argument value

In this case, the pre-compilation is performed once.

long startTimeMills = System.currentTimeMillis();
Argument x = new Argument("x");
Expression e = new Expression("2sin(x)", x);
for (int i = 0; i < 100000; i++) {
	x.setArgumentValue(i);
	double result = e.calculate();
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
[mXparser-v.5.0.6] Computing time = 0.516s
Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 5.0.7

Nuget – .NET CLI

dotnet add package MathParser.org-mXparser --version 5.0.7

Nuget – Package Reference

<PackageReference Include="MathParser.org-mXparser" Version="5.0.7"/>

Maven – Dependency

<dependency>
<groupid>
org.mariuszgromada.math</groupid>
<artifactid>
MathParser.org-mXparser</artifactid>
<version>
5.0.7</version>
</dependency>

Maven – Gradle

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:5.0.7'

Maven – Gradle (Kotlin)

implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.0.7")

GitHub

git clone https://github.com/mariuszgromada/MathParser.org-mXparser

OTHER DOWNLOAD OPTIONS

Download latest release – v.5.0.7 Leonis: .NET bin onlyDownload latest release – v.5.0.7 Leonis: JAVA bin onlyDownload latest release – v.5.0.7 Leonis: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

Source code .zipSource code .tar.gz
View on GitHubMathSpace.pl