Variadic user defined functions

TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!

Tutorial Math Collection API spec Download

Below is the code for JAVA, the code for C# is almost identical.

Case 1: Function returning number of parameters provided

import 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.0.0] Res 1: f(5) = 1.0
[mXparser-v.5.0.0] Res 2: f(5,2) = 2.0
[mXparser-v.5.0.0] Res 3: f(5,2,4) = 3.0

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

import 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.0.0] Res 1: f(5) = 10.0
[mXparser-v.5.0.0] Res 2: f(5,2) = 7.0
[mXparser-v.5.0.0] Res 3: f(5,2,4) = 9.0

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

import 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.0.0] Res 1: f(5) = NaN
[mXparser-v.5.0.0] Res 2: f(1,2) = 1.0
[mXparser-v.5.0.0] Res 3: f(3,2,4) = 4.0

Case 4: Function returning sum of all parameters squared

import 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.0.0] Res 1: f(5) = 25.0
[mXparser-v.5.0.0] Res 2: f(1,2) = 5.0
[mXparser-v.5.0.0] Res 3: f(3,2,4) = 29.0

Case 5: Implementing your own Variadic Function Extension

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 FunExtVar clone() {
        return new FunExtVar();
    }
}
import org.mariuszgromada.math.mxparser.*;
...
Function sumVar = new Function("sumVar", new SumVar());
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.0.0] Res 1: sumVar(1) = 1.0
[mXparser-v.5.0.0] Res 2: sumVar(1,2) = 3.0
[mXparser-v.5.0.0] Res 3: sumVar(1,2,3) = 6.0
Nuget – Package Manager

Install-Package MathParser.org-mXparser -Version 5.0.7

Nuget – .NET CLI

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

Nuget – Package Reference

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

Maven – Dependency

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

Maven – Gradle

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

Maven – Gradle (Kotlin)

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

GitHub

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

OTHER DOWNLOAD OPTIONS

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