Variadic 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. To copy the code, double-click inside the frame.

Case 1: Function returning number of parameters provided

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Function f = new Function("f(...) = [npar]");

Expression e1 = new Expression("f(5)", f);
Expression e2 = new Expression("f(5,2)", f);
Expression e3 = new Expression("f(5,2,4)", f);

mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
mXparser.consolePrintln("Res 3: " + e3.getExpressionString() + " = " + e3.calculate());
[mXparser-v.5.2.1] Res 1: f(5) = 1.0
[mXparser-v.5.2.1] Res 2: f(5,2) = 2.0
[mXparser-v.5.2.1] Res 3: f(5,2,4) = 3.0

Case 2: Function returning sum of first and last parameter provided

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Function f = new Function("f(...) = par(1) + par([npar])");

Expression e1 = new Expression("f(5)", f);
Expression e2 = new Expression("f(5,2)", f);
Expression e3 = new Expression("f(5,2,4)", f);

mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
mXparser.consolePrintln("Res 3: " + e3.getExpressionString() + " = " + e3.calculate());
[mXparser-v.5.2.1] Res 1: f(5) = 10.0
[mXparser-v.5.2.1] Res 2: f(5,2) = 7.0
[mXparser-v.5.2.1] Res 3: f(5,2,4) = 9.0

Case 3: Function returning parameter at position defined by the first parameter

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Function f = new Function("f(...) = par(par(1))");

Expression e1 = new Expression("f(5)", f);
Expression e2 = new Expression("f(1,2)", f);
Expression e3 = new Expression("f(3,2,4)", f);

mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
mXparser.consolePrintln("Res 3: " + e3.getExpressionString() + " = " + e3.calculate());
[mXparser-v.5.2.1] Res 1: f(5) = NaN
[mXparser-v.5.2.1] Res 2: f(1,2) = 1.0
[mXparser-v.5.2.1] Res 3: f(3,2,4) = 4.0

Case 4: Function returning sum of all parameters squared

// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Function f = new Function("f(...) = sum(i, 1, [npar], par(i)^2)");

Expression e1 = new Expression("f(5)", f);
Expression e2 = new Expression("f(1,2)", f);
Expression e3 = new Expression("f(3,2,4)", f);

mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
mXparser.consolePrintln("Res 3: " + e3.getExpressionString() + " = " + e3.calculate());
[mXparser-v.5.2.1] Res 1: f(5) = 25.0
[mXparser-v.5.2.1] Res 2: f(1,2) = 5.0
[mXparser-v.5.2.1] Res 3: f(3,2,4) = 29.0

Case 5: Implementing your own Variadic Function Extension

// JAVA: FunctionExtensionVariadic  interface implementation
// ...
import org.mariuszgromada.math.mxparser.*;

class SumVar implements FunctionExtensionVariadic {
    public double calculate(double... parameters) {
        if (parameters == null) return Double.NaN;
        if (parameters.length == 0) return Double.NaN;
        double result = 0;
        for (double x : parameters)
            result+=x;
        return result;
    }
    public FunctionExtensionVariadic clone() {
        return new SumVar();
    }
}
// C#: FunctionExtensionVariadic  interface implementation
// ...
using org.mariuszgromada.math.mxparser;

class SumVar : FunctionExtensionVariadic {
    public double calculate(params double[] parameters) {
        if (parameters == null) return Double.NaN;
        if (parameters.Length == 0) return Double.NaN;
        double result = 0;
        foreach (double x in parameters)
            result+=x;
        return result;
    }
    public FunctionExtensionVariadic clone() {
        return new SumVar();
    }
}
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

// Creating extended variadic function
Function sumVar = new Function("sumVar", new SumVar());

// Using extended variadic function in expression
Expression e1 = new Expression("sumVar(1)", sumVar);
Expression e2 = new Expression("sumVar(1,2)", sumVar);
Expression e3 = new Expression("sumVar(1,2,3)", sumVar);

mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
mXparser.consolePrintln("Res 3: " + e3.getExpressionString() + " = " + e3.calculate());
[mXparser-v.5.2.1] Res 1: sumVar(1) = 1.0
[mXparser-v.5.2.1] Res 2: sumVar(1,2) = 3.0
[mXparser-v.5.2.1] Res 3: sumVar(1,2,3) = 6.0
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

My other creative spaces

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