FOR COMMERCIAL USE, PLEASE ENSURE YOU HAVE A COMMERCIAL LICENSE.
ANOTHER WAY TO SUPPORT MY WORK IS BY PURCHASING MY BOOK, CUSTOMER FIRST, VALUE NEXT.
THANK YOU FOR SUPPORTING MY WORK!
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: Setting the verbose mode
Java/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
FunctionPtr f = new_Function("f(x) = x^2");
ExpressionPtr e = new_Expression("f(2)+(2+3)^2", f);
e->setDescription("Example - verbose mode");
e->setVerboseMode();
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[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/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
/* FunctionPtr and expression definition */
FunctionPtr fib = new_Function("fib(n) = if( n>1, fib(n-1)+fib(n-2), if(n > 0, 1, 0) )");
ExpressionPtr 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 */
ArgumentPtr 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());
Code result
[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/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("fun(1,2)+3");
mXparser_consolePrintln("checkSyntax = " + e->checkSyntax());
mXparser_consolePrintln(e->getErrorMessage());
mXparser_consolePrintln("checkLexSyntax = " + e->checkLexSyntax());
Code result
[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/C# code
// 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.");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("sum(i, 1, 1000000, 1)");
mXparser_consolePrintln("Res : " + e->getExpressionString() + " = " + e->calculate());
mXparser_consolePrintln("Computing time = " + e->getComputingTime() + " s.");
Code result
[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/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
/* Corrupted expression strings that can be fixed */
ExpressionPtr e1 = new_Expression("2++3");
ExpressionPtr e2 = new_Expression("2+-3");
ExpressionPtr e3 = new_Expression("2-+3");
ExpressionPtr e4 = new_Expression("2--3");
ExpressionPtr e5 = new_Expression("2-3+");
ExpressionPtr e6 = new_Expression("+2-3");
ExpressionPtr 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();
ExpressionPtr e8 = new_Expression("+3+-2-");
mXparser::consolePrintln(e8->getExpressionString() + " -> " + e8->getCanonicalExpressionString() + ", syntax = " + e8->checkSyntax());
Code result
[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 (C#, F#, Visual Basic, …)
Install-Package MathParser.org-mXparser-Version 6.1.1
dotnet add package MathParser.org-mXparser--version 6.1.1
<PackageReference Include="MathParser.org-mXparser"Version="6.1.1"/>
Maven – Dependency (Java, Kotlin, Scala, Groovy, …)
<dependency>
<groupid>org.mariuszgromada.math</groupid>
<artifactid>MathParser.org-mXparser</artifactid>
<version>6.1.1</version>
</dependency>
Maven – Gradle
implementation 'org.mariuszgromada.math:MathParser.org-mXparser:6.1.1'
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.1
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(MathParserOrgMxParser)
mxparser_link(YourExecutable)
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

