GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing mathparser.org — Variadic user defined functions - mXparser - Math Expressions Parser for JAVA, C#, C++, Kotlin, Android, .NET/MONO/Xamarin - Mathematical Formula Parser / Evaluator Library FOR COMMERCIAL USE, PLEASE ENSURE YOU HAVE A COMMERCIAL LICENSE.ANOTHER WAY TO SUPPORT MY WORK IS BY PURCHASING MY BOOK, CUSTOMER FIRST, VALUE NEXT.THANK YOU FOR SUPPORTING MY WORK!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 providedJava/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 providedJava/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 parameterJava/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 squaredJava/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 ExtensionJava/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> ¶meters) 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, …)
Maven – Dependency (Java, Kotlin, Scala, Groovy, …)
Maven – Gradle
CMake – Dependency / FetchContent (C++, MSVC, LLVM/Clang, GNU/GCC, MinGW, MSYS2, WSL, Windows, Linux, Unix, MacOS)
GitHub
OTHER DOWNLOAD OPTIONSDownload 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.ORGSOURCE CODESource code .zipSource code .tar.gz My other creative spacesDONATIONDonation with PayPal |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |