Implied multiplication

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: Numbers and parenthesis

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2(3+4)3");
mXparser.consolePrintln(e.getExpressionString() + " = " + e.getCanonicalExpressionString());
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2(3+4)3");
mXparser::consolePrintln(e->getExpressionString() + " = " + e->getCanonicalExpressionString());
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] 2(3+4)3 = 2*(3+4)*3
[mXparser-v.5.2.1] Res: 2(3+4)3 = 42.0

Case 2: Numbers and constants / arguments

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2pi+3e");
mXparser.consolePrintln(e.getExpressionString() + " = " + e.getCanonicalExpressionString());
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2pi+3e");
mXparser::consolePrintln(e->getExpressionString() + " = " + e->getCanonicalExpressionString());
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] 2pi+3e = 2*pi+3*e
[mXparser-v.5.2.1] Res: 2pi+3e = 14.438030792556722

Case 3: Numbers and constants / arguments and parenthesis

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2pi(3+4)2e");
mXparser.consolePrintln(e.getExpressionString() + " = " + e.getCanonicalExpressionString());
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2pi(3+4)2e");
mXparser::consolePrintln(e->getExpressionString() + " = " + e->getCanonicalExpressionString());
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] 2pi(3+4)2e = 2*pi*(3+4)*2*e
[mXparser-v.5.2.1] Res: 2pi(3+4)2e = 239.11255823485985

Case 4: Numbers and constants / arguments and parenthesis and functions

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2pi(3+4)2sin(3)e");
mXparser.consolePrintln(e.getExpressionString() + " = " + e.getCanonicalExpressionString());
mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2pi(3+4)2sin(3)e");
mXparser::consolePrintln(e->getExpressionString() + " = " + e->getCanonicalExpressionString());
mXparser::consolePrintln("Res: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] 2pi(3+4)2sin(3)e = 2*pi*(3+4)*2*sin(3)*e
[mXparser-v.5.2.1] Res: 2pi(3+4)2sin(3)e = 33.74356614531889

Case 5: Implied multiplication and possible ambiguity

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2pi2");
e.checkSyntax();
mXparser.consolePrintln(e.getExpressionString() + " = " + e.getCanonicalExpressionString());
mXparser.consolePrintln(e.getErrorMessage());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2pi2");
e->checkSyntax();
mXparser::consolePrintln(e->getExpressionString() + " = " + e->getCanonicalExpressionString());
mXparser::consolePrintln(e->getErrorMessage());
Code result
[mXparser-v.5.2.1] 2pi2 = 2*pi2
[mXparser-v.5.2.1] [2pi2]: Starting syntax check...
[2pi2]: Token 'pi2', index 3: Invalid token.
[2pi2]: Errors have been found.

Case 6: Implied multiplication and list of tokens

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2pi(3+4)2e");
e.consolePrintCopyOfInitialTokens();
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2pi(3+4)2e");
e->consolePrintCopyOfInitialTokens();
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 |           2 |             |           1 |           0 |           0 |         2.0 |             |
[mXparser-v.5.2.1] |           1 |           * |             |           3 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |           2 |          pi |             |           1 |           9 |           0 |         NaN |             |
[mXparser-v.5.2.1] |           3 |           * |             |           3 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |           4 |           ( |           ( |           1 |          20 |           1 |         NaN |             |
[mXparser-v.5.2.1] |           5 |           3 |       _num_ |           1 |           0 |           1 |         3.0 |             |
[mXparser-v.5.2.1] |           6 |           + |           + |           1 |           1 |           1 |         NaN |             |
[mXparser-v.5.2.1] |           7 |           4 |       _num_ |           1 |           0 |           1 |         4.0 |             |
[mXparser-v.5.2.1] |           8 |           ) |           ) |           2 |          20 |           1 |         NaN |             |
[mXparser-v.5.2.1] |           9 |           * |             |           3 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |          10 |           2 |             |           1 |           0 |           0 |         2.0 |             |
[mXparser-v.5.2.1] |          11 |           * |             |           3 |           1 |           0 |         NaN |             |
[mXparser-v.5.2.1] |          12 |           e |             |           2 |           9 |           0 |         NaN |             |
[mXparser-v.5.2.1]  ---------------------------------------------------------------------------------------------------------------

Case 7: Enable / disable implied multiplication

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("2pi+3e");
e.disableImpliedMultiplicationMode();
e.checkSyntax();
mXparser.consolePrintln("Disable: \n" + e.getErrorMessage());
e.enableImpliedMultiplicationMode();
e.checkSyntax();
mXparser.consolePrintln("Enable: \n" + e.getErrorMessage());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("2pi+3e");
e->disableImpliedMultiplicationMode();
e->checkSyntax();
mXparser::consolePrintln("Disable: \n" + e->getErrorMessage());
e->enableImpliedMultiplicationMode();
e->checkSyntax();
mXparser::consolePrintln("Enable: \n" + e->getErrorMessage());
Code result
[mXparser-v.5.2.1] Disable: 
[2pi+3e]: Starting syntax check...
[2pi+3e]: Token '2pi', index 1: Invalid token. Possibly missing multiplication operator - try implied multiplication mode.
[2pi+3e]: Token '3e', index 3: Invalid token. Possibly missing multiplication operator - try implied multiplication mode.
[2pi+3e]: Errors have been found.
[mXparser-v.5.2.1] Enable: 
[2pi+3e]: Starting syntax check...
[2pi+3e]: No errors detected.
Nuget – Package Manager (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser-Version 6.1.1

Nuget – .NET CLI

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

Nuget – Package Reference

<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

My other creative spaces

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