User defined arguments

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.

You may also be interested in the following tutorial sections:

Case 1: Dealing with free arguments

$$x,y, z,\ldots\in\mathbb{R}$$

$$\sin(x+y)-\cos(\frac{y}{z})$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x = 1");
Argument y = new Argument("y = 2*pi");
Argument z = new Argument("z", 3);
Argument n = new Argument("n", 4);
          
Expression e = new Expression("n*( sin(x+y)-cos(y/z) )", x, y, z, n);
mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());
  
x.setArgumentValue(0);
z.setArgumentValue(4);
mXparser.consolePrintln("Res 2: " + e.getExpressionString() + " = " + e.calculate());
  
x.setArgumentValue(5);
mXparser.consolePrintln("Res 3: " + x.getArgumentName() + " = " + x.getArgumentValue());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 1");
ArgumentPtr y = new_Argument("y = 2*pi");
ArgumentPtr z = new_Argument("z", 3);
ArgumentPtr n = new_Argument("n", 4);

ExpressionPtr e = new_Expression("n*( sin(x+y)-cos(y/z) )", x, y, z, n);
mXparser::consolePrintln("Res 1: " + e->getExpressionString() + " = " + e->calculate());

x->setArgumentValue(0);
z->setArgumentValue(4);
mXparser::consolePrintln("Res 2: " + e->getExpressionString() + " = " + e->calculate());

x->setArgumentValue(5);
mXparser::consolePrintln("Res 3: " + x->getArgumentName() + " = " + x->getArgumentValue());
Code result
[mXparser-v.5.2.1] Res 1: n*( sin(x+y)-cos(y/z) ) = 5.365883939231586
[mXparser-v.5.2.1] Res 2: n*( sin(x+y)-cos(y/z) ) = 0.0
[mXparser-v.5.2.1] Res 3: x = 5.0

Case 2: Defining dependent arguments

$$y=x^2$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x");     
Argument y = new Argument("y = x^2", x);
          
x.setArgumentValue(1);
mXparser.consolePrintln("Res 1: " + y.getArgumentName() + " = " + y.getArgumentValue());
          
x.setArgumentValue(2);
mXparser.consolePrintln("Res 2: " + y.getArgumentName() + " = " + y.getArgumentValue());
          
x.setArgumentValue(3);
mXparser.consolePrintln("Res 3: " + y.getArgumentName() + " = " + y.getArgumentValue());
          
Expression e = new Expression("x*y", x, y);
mXparser.consolePrintln("Res 4: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x");
ArgumentPtr y = new_Argument("y = x^2", x);

x->setArgumentValue(1);
mXparser::consolePrintln("Res 1: " + y->getArgumentName() + " = " + y->getArgumentValue());

x->setArgumentValue(2);
mXparser::consolePrintln("Res 2: " + y->getArgumentName() + " = " + y->getArgumentValue());

x->setArgumentValue(3);
mXparser::consolePrintln("Res 3: " + y->getArgumentName() + " = " + y->getArgumentValue());

ExpressionPtr e = new_Expression("x*y", x, y);
mXparser::consolePrintln("Res 4: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res 1: y = 1.0
[mXparser-v.5.2.1] Res 2: y = 4.0
[mXparser-v.5.2.1] Res 3: y = 9.0
[mXparser-v.5.2.1] Res 4: x*y = 27.0

Case 3: Implementing your own Argument Extension

Java/C# code
// JAVA: ArgumentExtension interface implementation
// ...
import org.mariuszgromada.math.mxparser.*;
import org.mariuszgromada.math.mxparser.mathcollection.*;

class PiMultArgExt implements ArgumentExtension {
   private int multiple = 0;
   public double getArgumentValue() {
      multiple++;
      return  MathConstants.PI * multiple;
   }
   public ArgumentExtension clone() {
      return new PiMultArgExt();
   }
}
// C#: ArgumentExtension interface implementation
// ...
using org.mariuszgromada.math.mxparser;
using org.mariuszgromada.math.mxparser.mathcollection;

class PiMultArgExt : ArgumentExtension {
   private int multiple = 0;
   public double getArgumentValue() {
      multiple++;
      return  MathConstants.PI * multiple;
   }
   public ArgumentExtension clone() {
      return new PiMultArgExt();
   }
}
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

// Creating extended argument
Argument pim = new Argument("pim", new PiMultArgExt());

// Using extended argument in expression
Expression e = new Expression("pim", pim);

mXparser.consolePrintln("Res: 1st call: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res: 2nd call: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res: 3rd call: " + e.getExpressionString() + " = " + e.calculate());
C++ code
// C++: ArgumentExtension interface implementation
// ...
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
class PiMultArgExt : public ArgumentExtension {
private:
    int multiple = 0;

public:
    double getArgumentValue() override {
        multiple++;
        return  Math::PI * multiple;
    }
    ArgumentExtensionPtr clone() override {
        return std::make_shared<PiMultArgExt>();
    }
};

inline ArgumentExtensionPtr new_PiMultArgExt() {
    return std::make_shared<PiMultArgExt>();
}
// Creating extended argument
ArgumentExtensionPtr piMultArgExt = new_PiMultArgExt();
ArgumentPtr pim = new_Argument("pim", piMultArgExt);

// Using extended argument in expression
ExpressionPtr e = new_Expression("pim", pim);

mXparser::consolePrintln("Res: 1st call: " + e->getExpressionString() + " = " + e->calculate());
mXparser::consolePrintln("Res: 2nd call: " + e->getExpressionString() + " = " + e->calculate());
mXparser::consolePrintln("Res: 3rd call: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res: 1st call: pim = 3.141592653589793
[mXparser-v.5.2.1] Res: 2nd call: pim = 6.283185307179586
[mXparser-v.5.2.1] Res: 3rd call: pim = 9.42477796076938

Case 4: Getting list of missing user defined arguments

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x = 2");
Expression e = new Expression("2x+3y+4*a", x);
mXparser.consolePrintln("syntax = " + e.checkSyntax());
mXparser.consolePrintln(e.getErrorMessage());
mXparser.consolePrintln("List of missing user defined arguments:");
for (String arg : e.getMissingUserDefinedArguments())
   mXparser.consolePrintln("Argument '" + arg + "' has to be defined");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 2");
ExpressionPtr e = new_Expression("2x+3y+4*a", x);
mXparser::consolePrintln("syntax = " + e->checkSyntax());
mXparser::consolePrintln(e->getErrorMessage());
mXparser::consolePrintln("List of missing user defined arguments:");
for (const StringPtr &arg : *e->getMissingUserDefinedArguments())
    mXparser::consolePrintln("Argument '" + arg + "' has to be defined");
Code result
[mXparser-v.5.2.1] syntax = false
[mXparser-v.5.2.1] [2x+3y+4*a]: Starting syntax check...
[2x+3y+4*a]: Token 'y', index 7: Invalid token.
[2x+3y+4*a]: Token 'a', index 11: Invalid token.
[2x+3y+4*a]: Errors have been found.
[mXparser-v.5.2.1] List of missing user defined arguments:
[mXparser-v.5.2.1] Argument 'y' has to be defined
[mXparser-v.5.2.1] Argument 'a' has to be defined

Case 5: Possible conflict between Implied Multiplication and getting list of missing user defined arguments + recommended solutions

mXparser has many built-in constants. This list can also be expanded by user-defined arguments. As a result, a list of keywords recognized by mXparser is created. By default, the parser operates in implied multiplication mode. In this case, in many situations, these known constants / arguments will be treated as “hidden” multiplication. This is best illustrated by an example where “e” is a builtin famous Euler’s number.

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Expression e = new Expression("3*CasterAttack");
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined arguments:");
for (String arg : e.getMissingUserDefinedArguments())
   mXparser.consolePrintln("Argument '" + arg + "' has to be defined");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ExpressionPtr e = new_Expression("3*CasterAttack");
mXparser::consolePrintln("String '" + e->getExpressionString() + "' is interpreted as '" + e->getCanonicalExpressionString() + "'");
mXparser::consolePrintln("List of missing user defined arguments:");
for (const StringPtr &arg : *e->getMissingUserDefinedArguments())
    mXparser::consolePrintln("Argument '" + arg + "' has to be defined");
Code result
[mXparser-v.5.2.1] String '3*CasterAttack' is interpreted as '3*Cast*e*rAttack'
[mXparser-v.5.2.1] List of missing user defined arguments:
[mXparser-v.5.2.1] Argument 'Cast' has to be defined
[mXparser-v.5.2.1] Argument 'rAttack' has to be defined

Solution #1 – turn off the Implied Multiplication Mode (locally)

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
mXparser.consolePrintln("Solution #1");
Expression e = new Expression("3*CasterAttack");
e.disableImpliedMultiplicationMode();
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined arguments:");
for (String arg : e.getMissingUserDefinedArguments())
   mXparser.consolePrintln("Argument '" + arg + "' has to be defined");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
mXparser::consolePrintln("Solution #1");
ExpressionPtr e = new_Expression("3*CasterAttack");
e->disableImpliedMultiplicationMode();
mXparser::consolePrintln("String '" + e->getExpressionString() + "' is interpreted as '" + e->getCanonicalExpressionString() + "'");
mXparser::consolePrintln("List of missing user defined arguments:");
for (const StringPtr &arg : *e->getMissingUserDefinedArguments())
    mXparser::consolePrintln("Argument '" + arg + "' has to be defined");
Code result
[mXparser-v.5.2.1] Solution #1
[mXparser-v.5.2.1] String '3*CasterAttack' is interpreted as '3*CasterAttack'
[mXparser-v.5.2.1] List of missing user defined arguments:
[mXparser-v.5.2.1] Argument 'CasterAttack' has to be defined

Solution #2 – turn off the Implied Multiplication Mode (globally)

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
mXparser.consolePrintln("Solution #2");
mXparser.disableImpliedMultiplicationMode();
Expression e = new Expression("3*CasterAttack");
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined arguments:");
for (String arg : e.getMissingUserDefinedArguments())
   mXparser.consolePrintln("Argument '" + arg + "' has to be defined");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
mXparser::consolePrintln("Solution #2");
mXparser::disableImpliedMultiplicationMode();
ExpressionPtr e = new_Expression("3*CasterAttack");
mXparser::consolePrintln("String '" + e->getExpressionString() + "' is interpreted as '" + e->getCanonicalExpressionString() + "'");
mXparser::consolePrintln("List of missing user defined arguments:");
for (const StringPtr &arg : *e->getMissingUserDefinedArguments())
    mXparser::consolePrintln("Argument '" + arg + "' has to be defined");
Code result
[mXparser-v.5.2.1] Solution #2
[mXparser-v.5.2.1] String '3*CasterAttack' is interpreted as '3*CasterAttack'
[mXparser-v.5.2.1] List of missing user defined arguments:
[mXparser-v.5.2.1] Argument 'CasterAttack' has to be defined

Solution #3 – remove builtin keyword

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
mXparser.consolePrintln("Solution #3");
mXparser.removeBuiltinTokens("e");
Expression e = new Expression("3*CasterAttack");
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined arguments:");
for (String arg : e.getMissingUserDefinedArguments())
   mXparser.consolePrintln("Argument '" + arg + "' has to be defined");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
mXparser::consolePrintln("Solution #3");
mXparser::removeBuiltinTokens("e");
ExpressionPtr e = new_Expression("3*CasterAttack");
mXparser::consolePrintln("String '" + e->getExpressionString() + "' is interpreted as '" + e->getCanonicalExpressionString() + "'");
mXparser::consolePrintln("List of missing user defined arguments:");
for (const StringPtr &arg : *e->getMissingUserDefinedArguments())
    mXparser::consolePrintln("Argument '" + arg + "' has to be defined");
Code result
[mXparser-v.5.2.1] Solution #3
[mXparser-v.5.2.1] String '3*CasterAttack' is interpreted as '3*CasterAttack'
[mXparser-v.5.2.1] List of missing user defined arguments:
[mXparser-v.5.2.1] Argument 'CasterAttack' has to be defined

Solution #4 – modify builtin keyword

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
mXparser.consolePrintln("Solution #4");
mXparser.modifyBuiltinToken("e", "ee");
Expression e = new Expression("3*CasterAttack");
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined arguments:");
for (String arg : e.getMissingUserDefinedArguments())
   mXparser.consolePrintln("Argument '" + arg + "' has to be defined");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
mXparser::consolePrintln("Solution #4");
mXparser::modifyBuiltinToken("e", "ee");
ExpressionPtr e = new_Expression("3*CasterAttack");
mXparser::consolePrintln("String '" + e->getExpressionString() + "' is interpreted as '" + e->getCanonicalExpressionString() + "'");
mXparser::consolePrintln("List of missing user defined arguments:");
for (const StringPtr &arg : *e->getMissingUserDefinedArguments())
    mXparser::consolePrintln("Argument '" + arg + "' has to be defined");
Code result
[mXparser-v.5.2.1] Solution #4
[mXparser-v.5.2.1] String '3*CasterAttack' is interpreted as '3*CasterAttack'
[mXparser-v.5.2.1] List of missing user defined arguments:
[mXparser-v.5.2.1] Argument 'CasterAttack' has to be defined
Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 6.0.0

Nuget – .NET CLI

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

Nuget – Package Reference

<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

My other creative spaces

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