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 given separately for JAVA, C# and C++. 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, C# (the code for C# is almost identical) and C++. To copy the code, double-click inside the frame.

Case 1: Function returning number of parameters provided

Java/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
FunctionPtr f = new_Function("f(...) = [npar]");

ExpressionPtr e1 = new_Expression("f(5)", f);
ExpressionPtr e2 = new_Expression("f(5,2)", f);
ExpressionPtr 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());
Code result
[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/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
FunctionPtr f = new_Function("f(...) = par(1) + par([npar])");

ExpressionPtr e1 = new_Expression("f(5)", f);
ExpressionPtr e2 = new_Expression("f(5,2)", f);
ExpressionPtr 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());
Code result
[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/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
FunctionPtr f = new_Function("f(...) = par(par(1))");

ExpressionPtr e1 = new_Expression("f(5)", f);
ExpressionPtr e2 = new_Expression("f(1,2)", f);
ExpressionPtr 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());
Code result
[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/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
FunctionPtr f = new_Function("f(...) = sum(i, 1, [npar], par(i)^2)");

ExpressionPtr e1 = new_Expression("f(5)", f);
ExpressionPtr e2 = new_Expression("f(1,2)", f);
ExpressionPtr 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());
Code result
[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/C# code
// 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());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
class SumVar : public FunctionExtensionVariadic {
public:
    double calculate(const ListPtr<double> &parameters) override {
        if (parameters == nullptr) return Double::NaN;
        if (parameters->size() == 0) return Double::NaN;
        double result = 0;
        for (double x : *parameters)
            result+=x;
        return result;
    }
    FunctionExtensionVariadicPtr clone() override {
        return std::make_shared<SumVar>();
    }
};

inline FunctionExtensionVariadicPtr new_SumVar() {
    return std::make_shared<SumVar>();
}
// Creating extended variadic function
FunctionExtensionVariadicPtr newSumVar = new_SumVar();
FunctionPtr sumVar = new_Function("sumVar", newSumVar);

// Using extended variadic function in expression
ExpressionPtr e1 = new_Expression("sumVar(1)", sumVar);
ExpressionPtr e2 = new_Expression("sumVar(1,2)", sumVar);
ExpressionPtr 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());
Code result
[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 (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser -Version 6.1.0

Nuget – .NET CLI

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

Nuget – Package Reference

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

Maven – Dependency (Java, Kotlin, Scala, Groovy, …)

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

Maven – Gradle

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

CMake – Dependency / FetchContent (C++, MSVC, LLVM/Clang, GNU/GCC, MinGW, MSYS2, WSL, Windows, Linux, Unix, MacOS)

include(FetchContent)
FetchContent_Declare(
MathParserOrgMxParser
GIT_REPOSITORY
https://github.com/mariuszgromada/MathParser.org-mXparser.git
GIT_TAG
v.6.1.0
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(
MathParserOrgMxParser)
target_link_libraries(YourExecutable
MathParserOrgMxParser)

GitHub

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

OTHER DOWNLOAD OPTIONS

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