Expression pre-compilation

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

The tutorial consists of more than 200 live examples from 50 sections. Each of the examples can be copied and run on your own environment. In addition, mXparser provides an extensive collection of over 500 built-in math functions, expressions and symbols. Familiarize yourself with the scope and the syntax. Live testing is the best way to learn. Good luck! 🙂

Tutorial Math Collection API spec Download

Below is the code for JAVA, the code for C# is almost identical. To copy the code, double-click inside the frame.

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.

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
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.2.1] Computing time = 35.835s

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. Additionally all the smart rounding settings are tuned off for the maximum perfomance.

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
mXparser.disableUlpRounding();
mXparser.disableCanonicalRounding();
mXparser.disableAlmostIntRounding();

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.2.1] Computing time = 0.408s
Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 5.2.1

Nuget – .NET CLI

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

Nuget – Package Reference

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

Maven – Dependency

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

Maven – Gradle

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

Maven – Gradle (Kotlin)

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

GitHub

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

OTHER DOWNLOAD OPTIONS

Download latest release – v.5.2.1 Orion: .NET bin onlyDownload latest release – v.5.2.1 Orion: JAVA bin onlyDownload latest release – v.5.2.1 Orion: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

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

My other creative spaces