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. 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.

Case 1: User expression in the loop + output

// 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());
}
[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: 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));
}
[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: 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());
}
[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: 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);
[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: 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);
[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: 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);
[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: 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);
[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

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

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