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. 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.

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: 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());
[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: 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());
[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: 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());
[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: 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");
[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: 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");
[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: 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");
[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: 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");
[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: 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");
[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: 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");
[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 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

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