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.

Case 1: Dealing with free arguments

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

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

import 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.0.7] Res 1: n*( sin(x+y)-cos(y/z) ) = 5.365883939231586
[mXparser-v.5.0.7] Res 2: n*( sin(x+y)-cos(y/z) ) = 0.0
[mXparser-v.5.0.7] Res 3: x = 5.0

Case 2: Defining dependent arguments

$$y=x^2$$

import 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.0.7] Res 1: y = 1.0
[mXparser-v.5.0.7] Res 2: y = 4.0
[mXparser-v.5.0.7] Res 3: y = 9.0
[mXparser-v.5.0.7] Res 4: x*y = 27.0

Case 3: Implementing your own Argument Extension

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

Argument pim = new Argument("pim", new PiMultArgExt());
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.0.7] Res: 1st call: pim = 3.141592653589793
[mXparser-v.5.0.7] Res: 2nd call: pim = 6.283185307179586
[mXparser-v.5.0.7] Res: 3rd call: pim = 9.42477796076938

Case 4: Getting list of missing user defined arguments

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.0.7] syntax = false
[mXparser-v.5.0.7] [2x+3y+4*a] checking ...
[2x+3y+4*a] (y, 6) invalid <TOKEN>.
[2x+3y+4*a] (a, 10) invalid <TOKEN>.
[2x+3y+4*a] errors were found.

[mXparser-v.5.0.7] List of missing user defined arguments:
[mXparser-v.5.0.7] Argument 'y' has to be defined
[mXparser-v.5.0.7] 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.

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.0.7] String '3*CasterAttack' is interpreted as '3*Cast*e*rAttack'
[mXparser-v.5.0.7] List of missing user defined arguments:
[mXparser-v.5.0.7] Argument 'Cast' has to be defined
[mXparser-v.5.0.7] Argument 'rAttack' has to be defined

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

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.0.7] Solution #1
[mXparser-v.5.0.7] String '3*CasterAttack' is interpreted as '3*CasterAttack'
[mXparser-v.5.0.7] List of missing user defined arguments:
[mXparser-v.5.0.7] Argument 'CasterAttack' has to be defined

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

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.0.7] Solution #2
[mXparser-v.5.0.7] String '3*CasterAttack' is interpreted as '3*CasterAttack'
[mXparser-v.5.0.7] List of missing user defined arguments:
[mXparser-v.5.0.7] Argument 'CasterAttack' has to be defined

Solution #3 – remove builtin keyword

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.0.7] Solution #3
[mXparser-v.5.0.7] String '3*CasterAttack' is interpreted as '3*CasterAttack'
[mXparser-v.5.0.7] List of missing user defined arguments:
[mXparser-v.5.0.7] Argument 'CasterAttack' has to be defined

Solution #4 – modify builtin keyword

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.0.7] Solution #4
[mXparser-v.5.0.7] String '3*CasterAttack' is interpreted as '3*CasterAttack'
[mXparser-v.5.0.7] List of missing user defined arguments:
[mXparser-v.5.0.7] Argument 'CasterAttack' has to be defined
Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 5.2.0

Nuget – .NET CLI

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

Nuget – Package Reference

<PackageReference Include="MathParser.org-mXparser" Version="5.2.0"/>

Maven – Dependency

<dependency>
<groupid>
org.mariuszgromada.math</groupid>
<artifactid>
MathParser.org-mXparser</artifactid>
<version>
5.2.0</version>
</dependency>

Maven – Gradle

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:5.2.0'

Maven – Gradle (Kotlin)

implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.2.0")

GitHub

git clone https://github.com/mariuszgromada/MathParser.org-mXparser

OTHER DOWNLOAD OPTIONS

Download latest release – v.5.2.0 Orion: .NET bin onlyDownload latest release – v.5.2.0 Orion: JAVA bin onlyDownload latest release – v.5.2.0 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