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: Printing expression tokens
Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("1+2^3-sin(10+cos(20-1))+30");
mXparser.consolePrintTokens(e.getCopyOfInitialTokens());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("1+2^3-sin(10+cos(20-1))+30");
mXparser::consolePrintTokens(e->getCopyOfInitialTokens());
Code result
[mXparser-v.5.2.1] --------------------
[mXparser-v.5.2.1] | Expression tokens: |
[mXparser-v.5.2.1] ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.2.1] | TokenIdx | Token | KeyW | TokenId | TokenTypeId | TokenLevel | TokenValue | LooksLike |
[mXparser-v.5.2.1] ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.2.1] | 0 | 1 | _num_ | 1 | 0 | 0 | 1.0 | |
[mXparser-v.5.2.1] | 1 | + | + | 1 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 2 | 2 | _num_ | 1 | 0 | 0 | 2.0 | |
[mXparser-v.5.2.1] | 3 | ^ | ^ | 5 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 4 | 3 | _num_ | 1 | 0 | 0 | 3.0 | |
[mXparser-v.5.2.1] | 5 | - | - | 2 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 6 | sin | sin | 1 | 4 | 1 | NaN | |
[mXparser-v.5.2.1] | 7 | ( | ( | 1 | 20 | 2 | NaN | |
[mXparser-v.5.2.1] | 8 | 10 | _num_ | 1 | 0 | 2 | 10.0 | |
[mXparser-v.5.2.1] | 9 | + | + | 1 | 1 | 2 | NaN | |
[mXparser-v.5.2.1] | 10 | cos | cos | 2 | 4 | 3 | NaN | |
[mXparser-v.5.2.1] | 11 | ( | ( | 1 | 20 | 4 | NaN | |
[mXparser-v.5.2.1] | 12 | 20 | _num_ | 1 | 0 | 4 | 20.0 | |
[mXparser-v.5.2.1] | 13 | - | - | 2 | 1 | 4 | NaN | |
[mXparser-v.5.2.1] | 14 | 1 | _num_ | 1 | 0 | 4 | 1.0 | |
[mXparser-v.5.2.1] | 15 | ) | ) | 2 | 20 | 4 | NaN | |
[mXparser-v.5.2.1] | 16 | ) | ) | 2 | 20 | 2 | NaN | |
[mXparser-v.5.2.1] | 17 | + | + | 1 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 18 | 30 | _num_ | 1 | 0 | 0 | 30.0 | |
[mXparser-v.5.2.1] ---------------------------------------------------------------------------------------------------------------
Case 2: Using tokens to print expression in a fancy way
Java/C# code
// JAVA
// ...
import org.mariuszgromada.math.mxparser.*;
import org.mariuszgromada.math.mxparser.parsertokens.*;
import java.util.List;
/* Expression definition */
Expression e = new Expression("1+2^3-sin(10+cos(20-1))+30");
/* Getting list of tokens */
List<Token> tokens = e.getCopyOfInitialTokens();
/* Evaluating max level */
int mxlev = 0;
for (Token t : tokens)
if (t.tokenLevel > mxlev) mxlev = t.tokenLevel;
/* Printing tokens */
mXparser.consolePrintln(e.getExpressionString());
mXparser.consolePrintln("--------------------------");
for (int level = 0; level <= mxlev; level++) {
for (Token t : tokens) {
String s;
if (t.tokenLevel == level)
s = t.tokenStr;
else {
s = "";
for (int i = 0; i < t.tokenStr.length(); i++)
s = s + " ";
}
mXparser.consolePrint(s);
}
mXparser.consolePrintln();
}
// C#
// ...
using org.mariuszgromada.math.mxparser;
using org.mariuszgromada.math.mxparser.parsertokens;
using System;
using System.Collections.Generic;
/* Expression definition */
Expression e = new Expression("1+2^3-sin(10+cos(20-1))+30");
/* Getting list of tokens */
List<Token> tokens = e.getCopyOfInitialTokens();
/* Evaluating max level */
int mxlev = 0;
foreach (Token t in tokens)
if (t.tokenLevel > mxlev) mxlev = t.tokenLevel;
/* Printing tokens */
mXparser.consolePrintln(e.getExpressionString());
mXparser.consolePrintln("--------------------------");
for (int level = 0; level <= mxlev; level++) {
foreach (Token t in tokens) {
String s;
if (t.tokenLevel == level)
s = t.tokenStr;
else {
s = "";
for (int i = 0; i < t.tokenStr.Length; i++)
s = s + " ";
}
mXparser.consolePrint(s);
}
mXparser.consolePrintln();
}
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
/* Expression definition */
ExpressionPtr e = new_Expression("1+2^3-sin(10+cos(20-1))+30");
/* Getting list of tokens */
ListPtr<TokenPtr> tokens = e->getCopyOfInitialTokens();
/* Evaluating max level */
int mxlev = 0;
for (const TokenPtr &t : *tokens)
if (t->tokenLevel > mxlev) mxlev = t->tokenLevel;
/* Printing tokens */
mXparser::consolePrintln(e->getExpressionString());
mXparser::consolePrintln("--------------------------");
for (int level = 0; level <= mxlev; level++) {
for (const TokenPtr &t : *tokens) {
StringPtr s;
if (t->tokenLevel == level)
s = t->tokenStr;
else {
s = S("");
for (int i = 0; i < t->tokenStr->length(); i++)
s = s + " ";
}
mXparser::consolePrint(s);
}
mXparser::consolePrintln();
}
Code result
[mXparser-v.5.2.1] 41+2^3-sin(10+cos(20-1))+30
[mXparser-v.5.2.1] --------------------------
[mXparser-v.5.2.1] 1+2^3- +30
[mXparser-v.5.2.1] sin
[mXparser-v.5.2.1] (10+ )
[mXparser-v.5.2.1] cos
[mXparser-v.5.2.1] (20-1)
Case 3: Playing with invalid tokens
Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("pi+var1-fun1(var1)+fun2(var2)-1pi");
mXparser.consolePrintTokens(e.getCopyOfInitialTokens());
Please check “Looks Like” field
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("pi+var1-fun1(var1)+fun2(var2)-1pi");
mXparser::consolePrintTokens(e->getCopyOfInitialTokens());
Code result
[mXparser-v.5.2.1] --------------------
[mXparser-v.5.2.1] | Expression tokens: |
[mXparser-v.5.2.1] ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.2.1] | TokenIdx | Token | KeyW | TokenId | TokenTypeId | TokenLevel | TokenValue | LooksLike |
[mXparser-v.5.2.1] ---------------------------------------------------------------------------------------------------------------
[mXparser-v.5.2.1] | 0 | pi | pi | 1 | 9 | 0 | NaN | |
[mXparser-v.5.2.1] | 1 | + | + | 1 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 2 | var1 | | -1 | -1 | 0 | NaN | argument |
[mXparser-v.5.2.1] | 3 | - | - | 2 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 4 | fun1 | | -1 | -1 | 0 | NaN | function |
[mXparser-v.5.2.1] | 5 | ( | ( | 1 | 20 | 1 | NaN | |
[mXparser-v.5.2.1] | 6 | var1 | | -1 | -1 | 1 | NaN | argument |
[mXparser-v.5.2.1] | 7 | ) | ) | 2 | 20 | 1 | NaN | |
[mXparser-v.5.2.1] | 8 | + | + | 1 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 9 | fun2 | | -1 | -1 | 0 | NaN | function |
[mXparser-v.5.2.1] | 10 | ( | ( | 1 | 20 | 1 | NaN | |
[mXparser-v.5.2.1] | 11 | var2 | | -1 | -1 | 1 | NaN | argument |
[mXparser-v.5.2.1] | 12 | ) | ) | 2 | 20 | 1 | NaN | |
[mXparser-v.5.2.1] | 13 | - | - | 2 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 14 | 1 | | 1 | 0 | 0 | 1.0 | |
[mXparser-v.5.2.1] | 15 | * | | 3 | 1 | 0 | NaN | |
[mXparser-v.5.2.1] | 16 | pi | | 1 | 9 | 0 | NaN | |
[mXparser-v.5.2.1] ---------------------------------------------------------------------------------------------------------------
Nuget – Package Manager
Install-Package
MathParser.org-mXparser
-Version
6.0.0
dotnet add package
MathParser.org-mXparser
--version
6.0.0
<PackageReference Include=
"MathParser.org-mXparser"
Version=
"6.0.0"
/>
Maven – Dependency
<dependency>
<groupid>org.mariuszgromada.math
</groupid>
<artifactid>MathParser.org-mXparser
</artifactid>
<version>6.0.0
</version>
</dependency>
Maven – Gradle
implementation
'org.mariuszgromada.math:MathParser.org-mXparser:6.0.0'
CMake – Dependency / FetchContent
include(FetchContent)
FetchContent_Declare(MathParserOrgMxParser
GIT_REPOSITORY https://github.com/mariuszgromada/MathParser.org-mXparser.git
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.0.0 Picon: .NET bin onlyDownload latest release – v.6.0.0 Picon: JAVA bin onlyDownload latest release – v.6.0.0 Picon: bin + doc
NEWS FROM MATHPARSER.ORG
SOURCE CODE
Source code .zipSource code .tar.gz
View on GitHubMathSpace.pl