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. You may also be interested in the following tutorial sections:// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using 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));C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... FunctionPtr f = new_Function("f", "x^2", "x"); ExpressionPtr e = new_Expression("f(2)", f); mXparser::consolePrintln("Res 1: " + e->getExpressionString() + " = " + e->calculate()); mXparser_consolePrintln("Res 2: f(5) = " + f->calculate(5));Code result
[mXparser-v.5.2.1] Res 1: f(2) = 4.0 [mXparser-v.5.2.1] Res 2: f(5) = 25.0Case 2: Handy function constructor, but slower proces of function creation (performance of creation slower, but calculation the same) Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using 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));C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... FunctionPtr f = new_Function("f(x) = x^2"); ExpressionPtr e = new_Expression("f(2)", f); mXparser::consolePrintln("Res 1: " + e->getExpressionString() + " = " + e->calculate()); mXparser_consolePrintln("Res 2: f(5) = " + f->calculate(5));Code result
[mXparser-v.5.2.1] Res 1: f(2) = 4.0 [mXparser-v.5.2.1] Res 2: f(5) = 25.0Case 3: Function with more parameters Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using 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));C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... FunctionPtr f = new_Function("f(a, b, c) = a+b+c"); ExpressionPtr 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));Code result
[mXparser-v.5.2.1] Res 1: f(1,2,3) = 6.0 [mXparser-v.5.2.1] Res 2: f(1,2,3) = 6.0Case 4: Function in function Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using 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));C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... FunctionPtr g = new_Function("g(x) = 2*x"); FunctionPtr 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));Code result
[mXparser-v.5.2.1] Res 1: g(1) = 2.0 [mXparser-v.5.2.1] Res 2: f(1) = 4.0Case 5: Implementing your own Function Extension Java/C# code
// JAVA: FunctionExtension interface implementation // ... import org.mariuszgromada.math.mxparser.*; 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 parameterIndex, double parameterValue) { if (parameterIndex== 0) x = parameterValue; if (parameterIndex== 1) y = parameterValue; } 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); } }
// C#: FunctionExtension interface implementation // ... using System; using org.mariuszgromada.math.mxparser; class Addition : 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 parameterIndex, double parameterValue) { if (parameterIndex== 0) x = parameterValue; if (parameterIndex== 1) y = parameterValue; } 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); } }
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... // 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() );C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... class Addition : public FunctionExtension { private: double x; double y; public: Addition() { x = Double::NaN; y = Double::NaN; } Addition(double x, double y) { this->x = x; this->y = y; } int getParametersNumber() override { return 2; } void setParameterValue(int parameterIndex, double parameterValue) override { if (parameterIndex == 0) x = parameterValue; if (parameterIndex == 1) y = parameterValue; } StringPtr getParameterName(int parameterIndex) override { if (parameterIndex == 0) return S("x"); if (parameterIndex == 1) return S("y"); return S(""); } double calculate() override { return x+y; } FunctionExtensionPtr clone() override { return std::make_shared<Addition>(x, y); } }; inline FunctionExtensionPtr new_Addition() { return std::make_shared<Addition>(); } inline FunctionExtensionPtr new_Addition(double x, double y) { return std::make_shared<Addition>(x, y); }
// Creating extended function FunctionExtensionPtr addition = new_Addition(); FunctionPtr f = new_Function("f", addition); mXparser_consolePrintln("f->calculate(1,2) = " + f->calculate(1,2) ); // Using extended function in expression ExpressionPtr e = new_Expression("f(2,3)", f); mXparser::consolePrintln(e->getExpressionString() + " = " + e->calculate() );Code result
[mXparser-v.5.2.1] f.calculate(1,2) = 3.0 [mXparser-v.5.2.1] f(2,3) = 5.0Case 6: Getting list of missing user defined functions Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... 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");C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... ExpressionPtr 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 (const StringPtr &fun : *e->getMissingUserDefinedFunctions()) mXparser::consolePrintln("Function '" + fun + "' has to be defined"); mXparser::consolePrintln("List of missing user defined arguments:"); for (const StringPtr &arg : *e->getMissingUserDefinedArguments()) mXparser::consolePrintln("Argument '" + arg + "' has to be defined");Code result
[mXparser-v.5.2.1] syntax = false [mXparser-v.5.2.1] [2sin(pi)+2f(1)g(3,4)+4*x]: Starting syntax check... [2sin(pi)+2f(1)g(3,4)+4*x]: Token 'f', index 10: Invalid token. [2sin(pi)+2f(1)g(3,4)+4*x]: Token 'g', index 15: Invalid token. [2sin(pi)+2f(1)g(3,4)+4*x]: Token 'x', index 24: Invalid token. [2sin(pi)+2f(1)g(3,4)+4*x]: Errors have been found. [mXparser-v.5.2.1] List of missing user defined functions: [mXparser-v.5.2.1] Function 'f' has to be defined [mXparser-v.5.2.1] Function 'g' has to be defined [mXparser-v.5.2.1] List of missing user defined arguments: [mXparser-v.5.2.1] Argument 'x' has to be definedCase 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.
Java/C# code// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... 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");C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... ExpressionPtr 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 (const StringPtr &fun : *e->getMissingUserDefinedFunctions()) mXparser::consolePrintln("Function '" + fun + "' has to be defined"); mXparser::consolePrintln("List of missing user defined arguments:"); for (const StringPtr &arg : *e->getMissingUserDefinedArguments()) mXparser::consolePrintln("Argument '" + arg + "' has to be defined");Code result
[mXparser-v.5.2.1] String '3*CasterAttack(4,5)' is interpreted as '3*Cast*e*rAttack(4,5)' [mXparser-v.5.2.1] List of missing user defined functions: [mXparser-v.5.2.1] Function 'rAttack' has to be defined [mXparser-v.5.2.1] List of missing user defined arguments: [mXparser-v.5.2.1] Argument 'Cast' has to be definedSolution #1 β turn off the Implied Multiplication Mode (locally) Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... 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");C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... mXparser::consolePrintln("Solution #1"); ExpressionPtr 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 (const StringPtr &fun : *e->getMissingUserDefinedFunctions()) mXparser::consolePrintln("Function '" + fun + "' has to be defined"); mXparser::consolePrintln("List of missing user defined arguments:"); for (const StringPtr &arg : *e->getMissingUserDefinedArguments()) mXparser::consolePrintln("Argument '" + arg + "' has to be defined");Code result
[mXparser-v.5.2.1] Solution #1 [mXparser-v.5.2.1] String '3*CasterAttack(4,5)' is interpreted as '3*CasterAttack(4,5)' [mXparser-v.5.2.1] List of missing user defined functions: [mXparser-v.5.2.1] Function 'CasterAttack' has to be defined [mXparser-v.5.2.1] List of missing user defined arguments:Solution #2 β turn off the Implied Multiplication Mode (globally) Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... 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");C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... mXparser::consolePrintln("Solution #2"); mXparser::disableImpliedMultiplicationMode(); ExpressionPtr 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 (const StringPtr &fun : *e->getMissingUserDefinedFunctions()) mXparser::consolePrintln("Function '" + fun + "' has to be defined"); mXparser::consolePrintln("List of missing user defined arguments:"); for (const StringPtr &arg : *e->getMissingUserDefinedArguments()) mXparser::consolePrintln("Argument '" + arg + "' has to be defined");Code result
[mXparser-v.5.2.1] Solution #2 [mXparser-v.5.2.1] String '3*CasterAttack(4,5)' is interpreted as '3*CasterAttack(4,5)' [mXparser-v.5.2.1] List of missing user defined functions: [mXparser-v.5.2.1] Function 'CasterAttack' has to be defined [mXparser-v.5.2.1] List of missing user defined arguments:Solution #3 β remove builtin keyword Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... 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");C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... mXparser::consolePrintln("Solution #3"); mXparser::removeBuiltinTokens("e"); ExpressionPtr 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 (const StringPtr &fun : *e->getMissingUserDefinedFunctions()) mXparser::consolePrintln("Function '" + fun + "' has to be defined"); mXparser::consolePrintln("List of missing user defined arguments:"); for (const StringPtr &arg : *e->getMissingUserDefinedArguments()) mXparser::consolePrintln("Argument '" + arg + "' has to be defined");Code result
[mXparser-v.5.2.1] Solution #3 [mXparser-v.5.2.1] String '3*CasterAttack(4,5)' is interpreted as '3*CasterAttack(4,5)' [mXparser-v.5.2.1] List of missing user defined functions: [mXparser-v.5.2.1] Function 'CasterAttack' has to be defined [mXparser-v.5.2.1] List of missing user defined arguments:Solution #4 β modify builtin keyword Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... 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");C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... mXparser::consolePrintln("Solution #4"); mXparser::modifyBuiltinToken("e", "ee"); ExpressionPtr 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 (const StringPtr &fun : *e->getMissingUserDefinedFunctions()) mXparser::consolePrintln("Function '" + fun + "' has to be defined"); mXparser::consolePrintln("List of missing user defined arguments:"); for (const StringPtr &arg : *e->getMissingUserDefinedArguments()) mXparser::consolePrintln("Argument '" + arg + "' has to be defined");Code result
[mXparser-v.5.2.1] Solution #4 [mXparser-v.5.2.1] String '3*CasterAttack(4,5)' is interpreted as '3*CasterAttack(4,5)' [mXparser-v.5.2.1] List of missing user defined functions: [mXparser-v.5.2.1] Function 'CasterAttack' has to be defined [mXparser-v.5.2.1] List of missing user defined arguments:Nuget β Package Manager (C#, F#, Visual Basic, β¦)
Install-Package
MathParser.org-mXparser
-Version
6.1.0
dotnet add package
MathParser.org-mXparser
--version
6.1.0
<PackageReference Include=
"MathParser.org-mXparser"
Version=
"6.1.0"
/>
<dependency>
<groupid>org.mariuszgromada.math
</groupid>
<artifactid>MathParser.org-mXparser
</artifactid>
<version>6.1.0
</version>
</dependency>
implementation
'org.mariuszgromada.math:MathParser.org-mXparser:6.1.0'
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
)
git clone
https://github.com/mariuszgromada/MathParser.org-mXparser
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 CODESource code .zipSource code .tar.gz
View on GitHubMathSpace.pl
Did you find the software useful?
Please consider donation π
DONATERetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4