TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!
The tutorial consists of more than 200 live examples from 50 sections given separately for JAVA, C# and C++. 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, C# (the code for C# is almost identical) and C++. 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/C# code
// 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");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
Long startTimeMills = SystemUtils::currentTimeMillis();
for (int i = 0; i < 100000; i++) {
ArgumentPtr x = new_Argument("x");
ExpressionPtr e = new_Expression("2sin(x)", x);
x->setArgumentValue(i);
double result = e->calculate();
}
Long endTimeMills = SystemUtils::currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
mXparser_consolePrintln("Computing time = " + computingTimeSec + "s");
Code result
[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/C# code
// 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");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
mXparser::disableUlpRounding();
mXparser::disableCanonicalRounding();
mXparser::disableAlmostIntRounding();
Long startTimeMills = SystemUtils::currentTimeMillis();
ArgumentPtr x = new_Argument("x");
ExpressionPtr e = new_Expression("2sin(x)", x);
for (int i = 0; i < 100000; i++) {
x->setArgumentValue(i);
double result = e->calculate();
}
Long endTimeMills = SystemUtils::currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
mXparser_consolePrintln("Computing time = " + computingTimeSec + "s");
Code result
[mXparser-v.5.2.1] Computing time = 0.408s
Nuget – Package Manager (C#, F#, Visual Basic, …)
Install-Package
MathParser.org-mXparser
-Version
6.1.0
dotnet add package
MathParser.org-mXparser
--version
6.1.0
<PackageReference Include=
"MathParser.org-mXparser"
Version=
"6.1.0"
/>
Maven – Dependency (Java, Kotlin, Scala, Groovy, …)
<dependency>
<groupid>org.mariuszgromada.math
</groupid>
<artifactid>MathParser.org-mXparser
</artifactid>
<version>6.1.0
</version>
</dependency>
Maven – Gradle
implementation
'org.mariuszgromada.math:MathParser.org-mXparser:6.1.0'
CMake – Dependency / FetchContent (C++, MSVC, LLVM/Clang, GNU/GCC, MinGW, MSYS2, WSL, Windows, Linux, Unix, MacOS)
include(FetchContent)
FetchContent_Declare(
MathParserOrgMxParser
GIT_REPOSITORY https://github.com/mariuszgromada/MathParser.org-mXparser.git
GIT_TAG v.6.1.0
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(MathParserOrgMxParser
)
target_link_libraries(YourExecutable MathParserOrgMxParser
)
GitHub
git clone
https://github.com/mariuszgromada/MathParser.org-mXparser
OTHER DOWNLOAD OPTIONS
Download latest release – v.6.1.0 Sagitara: .NET bin onlyDownload latest release – v.6.1.0 Sagitara: JAVA bin onlyDownload latest release – v.6.1.0 Sagitara: bin + doc
NEWS FROM MATHPARSER.ORG
SOURCE CODE
Source code .zipSource code .tar.gz
View on GitHubMathSpace.pl