User defined functions

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: Fast function definition (performance of creation)

import org.mariuszgromada.math.mxparser.*;
...
Function f = new Function("f", "x^2", "x");
Expression e = new Expression("f(2)", f);
mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res 2: f(5) = " + f.calculate(5));
[mXparser-v.5.0.7] Res 1: f(2) = 4.0
[mXparser-v.5.0.7] Res 2: f(5) = 25.0

Case 2: Handy function constructor, but slower proces of function creation (performance of creation slower, but calculation the same)

import org.mariuszgromada.math.mxparser.*;
...
Function f = new Function("f(x) = x^2");
Expression e = new Expression("f(2)", f);
mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res 2: f(5) = " + f.calculate(5));
[mXparser-v.5.0.7] Res 1: f(2) = 4.0
[mXparser-v.5.0.7] Res 2: f(5) = 25.0

Case 3: Function with more parameters

import org.mariuszgromada.math.mxparser.*;
...
Function f = new Function("f(a, b, c) = a+b+c");
Expression e = new Expression("f(1,2,3)", f);
mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Res 2: f(1,2,3) = " + f.calculate(1,2,3));
[mXparser-v.5.0.7] Res 1: f(1,2,3) = 6.0
[mXparser-v.5.0.7] Res 2: f(1,2,3) = 6.0

Case 4: Function in function

import org.mariuszgromada.math.mxparser.*;
...
Function g = new Function("g(x) = 2*x");
Function f = new Function("f(x) = g(x)^2", g);

mXparser.consolePrintln("Res 1: g(1) = " + g.calculate(1));
mXparser.consolePrintln("Res 2: f(1) = " + f.calculate(1));
[mXparser-v.5.0.7] Res 1: g(1) = 2.0
[mXparser-v.5.0.7] Res 2: f(1) = 4.0

Case 5: Implementing your own Function Extension

import org.mariuszgromada.math.mxparser.*;
...
/*
 * Implementing FunctionExtension interface
 */
class Addition implements FunctionExtension {
   double x;
   double y;
   public Addition() {
      x = Double.NaN;
      y = Double.NaN;
   }
   public Addition(double x, double y) {
      this.x = x;
      this.y = y;
   }
   public int getParametersNumber() {
      return 2;
   }
   public void setParameterValue(int argumentIndex, double argumentValue) {
      if (argumentIndex == 0) x = argumentValue;
      if (argumentIndex == 1) y = argumentValue;
   }
   public String getParameterName(int parameterIndex) {
      switch (parameterIndex) {
         case 0: return "x";
         case 1: return "y";
         default: return "";
      }
   }
   public double calculate() {
      return x+y;
   }
   public FunctionExtension clone() {
      return new Addition(x, y);
   }
}
/*
* Creating extended function
*/
Function f = new Function("f", new Addition());
mXparser.consolePrintln("f.calculate(1,2) = " + f.calculate(1,2) );
/*
* Using extended function in expression
*/
Expression e = new Expression("f(2,3)", f);
mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate() );
[mXparser-v.5.0.7] f.calculate(1,2) = 3.0
[mXparser-v.5.0.7] f(2,3) = 5.0

Case 6: Getting list of missing user defined functions

Expression e = new Expression("2sin(pi)+2f(1)g(3,4)+4*x");
mXparser.consolePrintln("syntax = " + e.checkSyntax());
mXparser.consolePrintln(e.getErrorMessage());
mXparser.consolePrintln("List of missing user defined functions:");
for (String fun : e.getMissingUserDefinedFunctions())
   mXparser.consolePrintln("Function '" + fun + "' has to be defined");
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] [2sin(pi)+2f(1)g(3,4)+4*x] checking ...
[2sin(pi)+2f(1)g(3,4)+4*x] (f, 9) invalid <TOKEN>.
[2sin(pi)+2f(1)g(3,4)+4*x] (g, 14) invalid <TOKEN>.
[2sin(pi)+2f(1)g(3,4)+4*x] (x, 23) invalid <TOKEN>.
[2sin(pi)+2f(1)g(3,4)+4*x] errors were found.

[mXparser-v.5.0.7] List of missing user defined functions:
[mXparser-v.5.0.7] Function 'f' has to be defined
[mXparser-v.5.0.7] Function 'g' has to be defined
[mXparser-v.5.0.7] List of missing user defined arguments:
[mXparser-v.5.0.7] Argument 'x' has to be defined

Case 7: Possible conflict between Implied Multiplication and getting list of missing user defined functions + 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(4,5)");
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined functions:");
for (String fun : e.getMissingUserDefinedFunctions())
   mXparser.consolePrintln("Function '" + fun + "' has to be defined");
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(4,5)' is interpreted as '3*Cast*e*rAttack(4,5)'
[mXparser-v.5.0.7] List of missing user defined functions:
[mXparser-v.5.0.7] Function 'rAttack' has to be defined
[mXparser-v.5.0.7] List of missing user defined arguments:
[mXparser-v.5.0.7] Argument 'Cast' has to be defined

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

mXparser.consolePrintln("Solution #1");
Expression e = new Expression("3*CasterAttack(4,5)");
e.disableImpliedMultiplicationMode();
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined functions:");
for (String fun : e.getMissingUserDefinedFunctions())
   mXparser.consolePrintln("Function '" + fun + "' has to be defined");
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(4,5)' is interpreted as '3*CasterAttack(4,5)'
[mXparser-v.5.0.7] List of missing user defined functions:
[mXparser-v.5.0.7] Function 'CasterAttack' has to be defined
[mXparser-v.5.0.7] List of missing user defined arguments:

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

mXparser.consolePrintln("Solution #2");
mXparser.disableImpliedMultiplicationMode();
Expression e = new Expression("3*CasterAttack(4,5)");
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined functions:");
for (String fun : e.getMissingUserDefinedFunctions())
   mXparser.consolePrintln("Function '" + fun + "' has to be defined");
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(4,5)' is interpreted as '3*CasterAttack(4,5)'
[mXparser-v.5.0.7] List of missing user defined functions:
[mXparser-v.5.0.7] Function 'CasterAttack' has to be defined
[mXparser-v.5.0.7] List of missing user defined arguments:

Solution #3 – remove builtin keyword

mXparser.consolePrintln("Solution #3");
mXparser.removeBuiltinTokens("e");
Expression e = new Expression("3*CasterAttack(4,5)");
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined functions:");
for (String fun : e.getMissingUserDefinedFunctions())
   mXparser.consolePrintln("Function '" + fun + "' has to be defined");
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(4,5)' is interpreted as '3*CasterAttack(4,5)'
[mXparser-v.5.0.7] List of missing user defined functions:
[mXparser-v.5.0.7] Function 'CasterAttack' has to be defined
[mXparser-v.5.0.7] List of missing user defined arguments:

Solution #4 – modify builtin keyword

mXparser.consolePrintln("Solution #4");
mXparser.modifyBuiltinToken("e", "ee");
Expression e = new Expression("3*CasterAttack(4,5)");
mXparser.consolePrintln("String '" + e.getExpressionString() + "' is interpreted as '" + e.getCanonicalExpressionString() + "'");
mXparser.consolePrintln("List of missing user defined functions:");
for (String fun : e.getMissingUserDefinedFunctions())
   mXparser.consolePrintln("Function '" + fun + "' has to be defined");
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(4,5)' is interpreted as '3*CasterAttack(4,5)'
[mXparser-v.5.0.7] List of missing user defined functions:
[mXparser-v.5.0.7] Function 'CasterAttack' has to be defined
[mXparser-v.5.0.7] List of missing user defined arguments:
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