Efficient calculations in the loops

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.

Case 1: User expression in the loop + output

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x");
Expression e = new Expression("2sin(x)", x);

for (int i = 0; i <= 10; i++) {
	x.setArgumentValue(i/10.0);
	mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e.getExpressionString() + " = " + e.calculate());
}
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x");
ExpressionPtr e = new_Expression("2sin(x)", x);

for (int i = 0; i <= 10; i++) {
    x->setArgumentValue(i/10.0);
    mXparser_consolePrintln("x = " + x->getArgumentValue() + ", " + e->getExpressionString() + " = " + e->calculate());
}
Code result
[mXparser-v.5.2.1] x = 0.0, 2sin(x) = 0.0
[mXparser-v.5.2.1] x = 0.1, 2sin(x) = 0.1996668332936563
[mXparser-v.5.2.1] x = 0.2, 2sin(x) = 0.39733866159012243
[mXparser-v.5.2.1] x = 0.3, 2sin(x) = 0.5910404133226791
[mXparser-v.5.2.1] x = 0.4, 2sin(x) = 0.778836684617301
[mXparser-v.5.2.1] x = 0.5, 2sin(x) = 0.958851077208406
[mXparser-v.5.2.1] x = 0.6, 2sin(x) = 1.1292849467900707
[mXparser-v.5.2.1] x = 0.7, 2sin(x) = 1.288435374475382
[mXparser-v.5.2.1] x = 0.8, 2sin(x) = 1.4347121817990456
[mXparser-v.5.2.1] x = 0.9, 2sin(x) = 1.5666538192549668
[mXparser-v.5.2.1] x = 1.0, 2sin(x) = 1.682941969615793

Case 2: User function in the loop + output

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Function f = new Function("f(i) = 2sin(i/10)");

for (int i = 0; i <= 10; i++) {
	mXparser.consolePrintln("i = " + i + ", f(i) = " + f.calculate(i));
}
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
FunctionPtr f = new_Function("f(i) = 2sin(i/10)");

for (int i = 0; i <= 10; i++) {
    mXparser_consolePrintln("i = " + i + ", f(i) = " + f->calculate(i));
}
Code result
[mXparser-v.5.2.1] i = 0, f(i) = 0.0
[mXparser-v.5.2.1] i = 1, f(i) = 0.1996668332936563
[mXparser-v.5.2.1] i = 2, f(i) = 0.39733866159012243
[mXparser-v.5.2.1] i = 3, f(i) = 0.5910404133226791
[mXparser-v.5.2.1] i = 4, f(i) = 0.778836684617301
[mXparser-v.5.2.1] i = 5, f(i) = 0.958851077208406
[mXparser-v.5.2.1] i = 6, f(i) = 1.1292849467900707
[mXparser-v.5.2.1] i = 7, f(i) = 1.288435374475382
[mXparser-v.5.2.1] i = 8, f(i) = 1.4347121817990456
[mXparser-v.5.2.1] i = 9, f(i) = 1.5666538192549668
[mXparser-v.5.2.1] i = 10, f(i) = 1.682941969615793

Case 3: User argument (dependent) in the loop + output

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x");
Argument y = new Argument("y = 2sin(x)", x);

for (int i = 0; i <= 10; i++) {
	x.setArgumentValue(i/10.0);
	mXparser.consolePrintln("x = " + x.getArgumentValue() + ", y = " + y.getArgumentValue());
}
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x");
ArgumentPtr y = new_Argument("y = 2sin(x)", x);

for (int i = 0; i <= 10; i++) {
    x->setArgumentValue(i/10.0);
    mXparser_consolePrintln("x = " + x->getArgumentValue() + ", y = " + y->getArgumentValue());
}
Code result
[mXparser-v.5.2.1] x = 0.0, y = 0.0
[mXparser-v.5.2.1] x = 0.1, y = 0.1996668332936563
[mXparser-v.5.2.1] x = 0.2, y = 0.39733866159012243
[mXparser-v.5.2.1] x = 0.3, y = 0.5910404133226791
[mXparser-v.5.2.1] x = 0.4, y = 0.778836684617301
[mXparser-v.5.2.1] x = 0.5, y = 0.958851077208406
[mXparser-v.5.2.1] x = 0.6, y = 1.1292849467900707
[mXparser-v.5.2.1] x = 0.7, y = 1.288435374475382
[mXparser-v.5.2.1] x = 0.8, y = 1.4347121817990456
[mXparser-v.5.2.1] x = 0.9, y = 1.5666538192549668
[mXparser-v.5.2.1] x = 1.0, y = 1.682941969615793

Case 4: User expression in the loop – Low performance

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x");
Expression e = new Expression("2sin(x)", x);

long startTimeMills = System.currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
	x.setArgumentValue(xv);
	double result = e.calculate();
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);

mXparser.consolePrintln("Case A: User Expression in the loop");
mXparser.consolePrintln("-----------------");
mXparser.consolePrintln("Number of iterations = " + numOfIter);
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser.consolePrintln("Number of iterations / s = " + iterPerSec);
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x");
ExpressionPtr e = new_Expression("2sin(x)", x);

Long startTimeMills = SystemUtils::currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
    x->setArgumentValue(xv);
    double result = e->calculate();
}
Long endTimeMills = SystemUtils::currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);

mXparser_consolePrintln("Case A: User ExpressionPtr in the loop");
mXparser_consolePrintln("-----------------");
mXparser_consolePrintln("Number of iterations = " + numOfIter);
mXparser_consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser_consolePrintln("Number of iterations / s = " + iterPerSec);
Code result
[mXparser-v.5.2.1] Case A: User Expression in the loop
[mXparser-v.5.2.1] -----------------
[mXparser-v.5.2.1] Number of iterations = 10000000
[mXparser-v.5.2.1] Computing time = 45.587s
[mXparser-v.5.2.1] Number of iterations / s = 219360

Case 5: Speed up calculations by turning off the smart rounding options

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

Argument x = new Argument("x");
Expression e = new Expression("2sin(x)", x);

long startTimeMills = System.currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
	x.setArgumentValue(xv);
	double result = e.calculate();
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);

mXparser.consolePrintln("Case A: User Expression in the loop");
mXparser.consolePrintln("-----------------");
mXparser.consolePrintln("Number of iterations = " + numOfIter);
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser.consolePrintln("Number of iterations / s = " + iterPerSec);
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
mXparser::disableCanonicalRounding();
mXparser::disableUlpRounding();
mXparser::disableAlmostIntRounding();

ArgumentPtr x = new_Argument("x");
ExpressionPtr e = new_Expression("2sin(x)", x);

Long startTimeMills = SystemUtils::currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
    x->setArgumentValue(xv);
    double result = e->calculate();
}
Long endTimeMills = SystemUtils::currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);

mXparser_consolePrintln("Case A: User ExpressionPtr in the loop");
mXparser_consolePrintln("-----------------");
mXparser_consolePrintln("Number of iterations = " + numOfIter);
mXparser_consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser_consolePrintln("Number of iterations / s = " + iterPerSec);
Code result
[mXparser-v.5.2.1] Case A: User Expression in the loop
[mXparser-v.5.2.1] -----------------
[mXparser-v.5.2.1] Number of iterations = 10000000
[mXparser-v.5.2.1] Computing time = 8.703s
[mXparser-v.5.2.1] Number of iterations / s = 1149029

Case 6: User function in the loop – Performance

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

Function f = new Function("f(x) = 2sin(x)");

long startTimeMills = System.currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
	double result = f.calculate(xv);
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);

mXparser.consolePrintln("Case B: User Function in the loop");
mXparser.consolePrintln("-----------------");
mXparser.consolePrintln("Number of iterations = " + numOfIter);
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser.consolePrintln("Number of iterations / s = " + iterPerSec);
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
mXparser::disableCanonicalRounding();
mXparser::disableUlpRounding();
mXparser::disableAlmostIntRounding();

FunctionPtr f = new_Function("f(x) = 2sin(x)");

Long startTimeMills = SystemUtils::currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
    double result = f->calculate(xv);
}
Long endTimeMills = SystemUtils::currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);

mXparser_consolePrintln("Case B: User FunctionPtr in the loop");
mXparser_consolePrintln("-----------------");
mXparser_consolePrintln("Number of iterations = " + numOfIter);
mXparser_consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser_consolePrintln("Number of iterations / s = " + iterPerSec);
Code result
[mXparser-v.5.2.1] Case B: User Function in the loop
[mXparser-v.5.2.1] -----------------
[mXparser-v.5.2.1] Number of iterations = 10000000
[mXparser-v.5.2.1] Computing time = 9.651s
[mXparser-v.5.2.1] Number of iterations / s = 1036162

Case 7: User argument (dependent) in the loop – Performance

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

Argument x = new Argument("x");
Argument y = new Argument("y = 2sin(x)", x);

long startTimeMills = System.currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
	x.setArgumentValue(xv);
	double result = y.getArgumentValue();
}
long endTimeMills = System.currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);
mXparser.consolePrintln("Case C: User Argument (dependent) in the loop");

mXparser.consolePrintln("-----------------");
mXparser.consolePrintln("Number of iterations = " + numOfIter);
mXparser.consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser.consolePrintln("Number of iterations / s = " + iterPerSec);
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
mXparser::disableCanonicalRounding();
mXparser::disableUlpRounding();
mXparser::disableAlmostIntRounding();

ArgumentPtr x = new_Argument("x");
ArgumentPtr y = new_Argument("y = 2sin(x)", x);

Long startTimeMills = SystemUtils::currentTimeMillis();
int numOfIter = 10000000;
for (double xv = 0; xv <= numOfIter; xv++) {
    x->setArgumentValue(xv);
    double result = y->getArgumentValue();
}
Long endTimeMills = SystemUtils::currentTimeMillis();
double computingTimeSec = (endTimeMills - startTimeMills)/1000.0;
int iterPerSec = (int)(numOfIter / computingTimeSec);
mXparser::consolePrintln("Case C: User ArgumentPtr (dependent) in the loop");

mXparser_consolePrintln("-----------------");
mXparser_consolePrintln("Number of iterations = " + numOfIter);
mXparser_consolePrintln("Computing time = " + computingTimeSec + "s");
mXparser_consolePrintln("Number of iterations / s = " + iterPerSec);
Code result
[mXparser-v.5.2.1] Case C: User Argument (dependent) in the loop
[mXparser-v.5.2.1] -----------------
[mXparser-v.5.2.1] Number of iterations = 10000000
[mXparser-v.5.2.1] Computing time = 8.993s
[mXparser-v.5.2.1] Number of iterations / s = 1111975
Nuget – Package Manager (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser -Version 6.1.0

Nuget – .NET CLI

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

Nuget – Package Reference

<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

My other creative spaces

DONATION
Did you find the software useful?
Please consider donation 🙂
DONATE