Inspecting calculation process

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: Setting the verbose mode

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Function f = new Function("f(x) = x^2");
Expression e = new Expression("f(2)+(2+3)^2", f);

e.setDescription("Example - verbose mode");
e.setVerboseMode();

mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
[mXparser-v.5.2.1] 
[Example - verbose mode][f(2)+(2+3)^2] 
[Example - verbose mode][f(2)+(2+3)^2] Starting...
[Example - verbose mode][f(2)+(2+3)^2] Starting calculation loop.
[Example - verbose mode][f(2)+(2+3)^2] Parsing (1, 3) ---> ( 2.0 ) ... ---> f 2.0 + ( 2.0 + 3.0 ) ^ 2.0 ...  done.
[Example - verbose mode][f(2)+(2+3)^2] Parsing (0, 1) ---> f 2.0 ... 
[f(x)][x^2] 
[f(x)][x^2] Starting...
[f(x)][x^2] x = 2.0
[f(x)][x^2] Starting calculation loop.
[f(x)][x^2] Parsing (0, 2) ---> 2.0 ^ 2.0 ... ---> 4.0 ...  done.
[f(x)][x^2] Calculated value: 4.0
[f(x)][x^2] Exiting.

---> 4.0 + ( 2.0 + 3.0 ) ^ 2.0 ...  done.
[Example - verbose mode][f(2)+(2+3)^2] Parsing (2, 6) ---> ( 2.0 + 3.0 ) ... ---> 4.0 + ( 5.0 ) ^ 2.0 ...  done.
[Example - verbose mode][f(2)+(2+3)^2] Parsing (2, 4) ---> ( 5.0 ) ... ---> 4.0 + 5.0 ^ 2.0 ...  done.
[Example - verbose mode][f(2)+(2+3)^2] Parsing (0, 4) ---> 4.0 + 5.0 ^ 2.0 ... ---> 4.0 + 25.0 ...  done.
[Example - verbose mode][f(2)+(2+3)^2] Parsing (0, 2) ---> 4.0 + 25.0 ... ---> 29.0 ...  done.
[Example - verbose mode][f(2)+(2+3)^2] Calculated value: 29.0
[Example - verbose mode][f(2)+(2+3)^2] Exiting.

Res: f(2)+(2+3)^2 = 29.0

Case 2: Syntax checking

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* Function and expression definition */
Function fib = new Function("fib(n) = if( n>1, fib(n-1)+fib(n-2), if(n > 0, 1, 0) )");
Expression e = new Expression("fib(10) + 20/sin(x)", fib);
 
/* 1st trial of syntax checking. */
mXparser.consolePrintln("1st trial of syntax checking.");
e.checkSyntax();
mXparser.consolePrintln(e.getErrorMessage());
 
/* Definition modification */
Argument x = new Argument("x = 2");
e.addDefinitions(x);
 
/* 2nd trial of syntax checking. */
mXparser.consolePrintln("2nd trial of syntax checking.");
e.checkSyntax();
mXparser.consolePrintln(e.getErrorMessage());
[mXparser-v.5.2.1] 1st trial of syntax checking.
[mXparser-v.5.2.1] [fib(10) + 20/sin(x)]: Starting syntax check...
[fib(10) + 20/sin(x)]: Token 'fib', index 1: Starting syntax check of the user-defined function.
[fib(10) + 20/sin(x)]: -> [fib] = [if( n>1, fib(n-1)+fib(n-2), if(n > 0, 1, 0) )] Starting syntax check...
[fib(10) + 20/sin(x)]: -> [fib] = [if( n>1, fib(n-1)+fib(n-2), if(n > 0, 1, 0) )] No errors detected.
[fib(10) + 20/sin(x)]: Token 'x', index 10: Invalid token.
[fib(10) + 20/sin(x)]: Errors have been found.
[mXparser-v.5.2.1] 2nd trial of syntax checking.
[mXparser-v.5.2.1] [fib(10) + 20/sin(x)]: Starting syntax check...
[fib(10) + 20/sin(x)]: Token 'fib', index 1: Starting syntax check of the user-defined function.
[fib(10) + 20/sin(x)]: -> [fib] = [if( n>1, fib(n-1)+fib(n-2), if(n > 0, 1, 0) )] The syntax has already been checked - no errors detected.
[fib(10) + 20/sin(x)]: No errors detected.

Case 3: Just lexical syntax checking

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("fun(1,2)+3");

mXparser.consolePrintln("checkSyntax  = " + e.checkSyntax());
mXparser.consolePrintln(e.getErrorMessage());   
mXparser.consolePrintln("checkLexSyntax = " + e.checkLexSyntax());
[mXparser-v.5.2.1] checkSyntax  = false
[mXparser-v.5.2.1] [fun(1,2)+3]: Starting syntax check...
[fun(1,2)+3]: Token 'fun', index 1: Invalid token.
[fun(1,2)+3]: Errors have been found.
[mXparser-v.5.2.1] checkLexSyntax = true

Case 4: Getting computing time

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("sum(i, 1, 1000000, 1)");

mXparser.consolePrintln("Res : " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Computing time = " + e.getComputingTime() + " s.");
[mXparser-v.5.2.1] Res : sum(i, 1, 1000000, 1) = 1000000.0
[mXparser-v.5.2.1] Computing time = 0.476 s.

Case 5: An Attempt To Fix Expression String

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* Corrupted expression strings that can be fixed */
Expression e1 = new Expression("2++3");
Expression e2 = new Expression("2+-3");
Expression e3 = new Expression("2-+3");
Expression e4 = new Expression("2--3");
Expression e5 = new Expression("2-3+");
Expression e6 = new Expression("+2-3");
Expression e7 = new Expression("+2--3-");

mXparser.consolePrintln("An Attempt To Fix Expression String Mode is enabled by default");
mXparser.consolePrintln(e1.getExpressionString() + " -> " + e1.getCanonicalExpressionString() + ", syntax = " + e1.checkSyntax());
mXparser.consolePrintln(e2.getExpressionString() + " -> " + e2.getCanonicalExpressionString() + ", syntax = " + e2.checkSyntax());
mXparser.consolePrintln(e3.getExpressionString() + " -> " + e3.getCanonicalExpressionString() + ", syntax = " + e3.checkSyntax());
mXparser.consolePrintln(e4.getExpressionString() + " -> " + e4.getCanonicalExpressionString() + ", syntax = " + e4.checkSyntax());
mXparser.consolePrintln(e5.getExpressionString() + " -> " + e5.getCanonicalExpressionString() + ", syntax = " + e5.checkSyntax());
mXparser.consolePrintln(e6.getExpressionString() + " -> " + e6.getCanonicalExpressionString() + ", syntax = " + e6.checkSyntax());
mXparser.consolePrintln(e7.getExpressionString() + " -> " + e7.getCanonicalExpressionString() + ", syntax = " + e7.checkSyntax());

mXparser.consolePrintln("An Attempt To Fix Expression String Mode is now disabled");
e1.disableAttemptToFixExpStrMode();
e2.disableAttemptToFixExpStrMode();
e3.disableAttemptToFixExpStrMode();
e4.disableAttemptToFixExpStrMode();
e5.disableAttemptToFixExpStrMode();
e6.disableAttemptToFixExpStrMode();
e7.disableAttemptToFixExpStrMode();
mXparser.consolePrintln(e1.getExpressionString() + " -> " + e1.getCanonicalExpressionString() + ", syntax = " + e1.checkSyntax());
mXparser.consolePrintln(e2.getExpressionString() + " -> " + e2.getCanonicalExpressionString() + ", syntax = " + e2.checkSyntax());
mXparser.consolePrintln(e3.getExpressionString() + " -> " + e3.getCanonicalExpressionString() + ", syntax = " + e3.checkSyntax());
mXparser.consolePrintln(e4.getExpressionString() + " -> " + e4.getCanonicalExpressionString() + ", syntax = " + e4.checkSyntax());
mXparser.consolePrintln(e5.getExpressionString() + " -> " + e5.getCanonicalExpressionString() + ", syntax = " + e5.checkSyntax());
mXparser.consolePrintln(e6.getExpressionString() + " -> " + e6.getCanonicalExpressionString() + ", syntax = " + e6.checkSyntax());
mXparser.consolePrintln(e7.getExpressionString() + " -> " + e7.getCanonicalExpressionString() + ", syntax = " + e7.checkSyntax());

mXparser.consolePrintln("An Attempt To Fix Expression String Mode can be disabled also globally");
mXparser.disableAttemptToFixExpStrMode();
Expression e8 = new Expression("+3+-2-");
mXparser.consolePrintln(e8.getExpressionString() + " -> " + e8.getCanonicalExpressionString() + ", syntax = " + e8.checkSyntax());
[mXparser-v.5.2.1] An Attempt To Fix Expression String Mode is enabled by default
[mXparser-v.5.2.1] 2++3 -> 2+3, syntax = true
[mXparser-v.5.2.1] 2+-3 -> 2-3, syntax = true
[mXparser-v.5.2.1] 2-+3 -> 2-3, syntax = true
[mXparser-v.5.2.1] 2--3 -> 2+3, syntax = true
[mXparser-v.5.2.1] 2-3+ -> 2-3, syntax = true
[mXparser-v.5.2.1] +2-3 -> 2-3, syntax = true
[mXparser-v.5.2.1] +2--3- -> 2+3, syntax = true
[mXparser-v.5.2.1] An Attempt To Fix Expression String Mode is now disabled
[mXparser-v.5.2.1] 2++3 -> 2++3, syntax = false
[mXparser-v.5.2.1] 2+-3 -> 2+-3, syntax = false
[mXparser-v.5.2.1] 2-+3 -> 2-+3, syntax = false
[mXparser-v.5.2.1] 2--3 -> 2--3, syntax = false
[mXparser-v.5.2.1] 2-3+ -> 2-3+, syntax = false
[mXparser-v.5.2.1] +2-3 -> +2-3, syntax = true
[mXparser-v.5.2.1] +2--3- -> +2--3-, syntax = false
[mXparser-v.5.2.1] An Attempt To Fix Expression String Mode can be disabled also globally
[mXparser-v.5.2.1] +3+-2- -> +3+-2-, syntax = false
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