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: Fibonacci numbers using user defined recursive function Java/C# code// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... Function fib = new Function("fib(n) = if( n>1, fib(n-1)+fib(n-2), if(n>0, 1, 0) )"); Expression e = new Expression("fib(10)", fib); mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate()); mXparser.consolePrintln("Res 2: fib(11) = " + fib.calculate(11));C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ...Code result
[mXparser-v.5.2.1] Res 1: fib(10) = 55.0 [mXparser-v.5.2.1] Res 2: fib(11) = 89.0Case 2: Number of recursive parameters is not limited β binomial coefficient definition using user defined recursive function Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... Function Cnk = new Function("Cnk(n,k) = if( k>0, if( k<n, Cnk(n-1,k-1)+Cnk(n-1,k), 1), 1)"); Expression e = new Expression("Cnk(10,3) - C(10,3)", Cnk); mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate()); mXparser.consolePrintln("Res 2: Cnk(10,3) = " + Cnk.calculate(10,3));C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... FunctionPtr Cnk = new_Function("Cnk(n,k) = if( k>0, if( k<n, Cnk(n-1,k-1)+Cnk(n-1,k), 1), 1)"); ExpressionPtr e = new_Expression("Cnk(10,3) - C(10,3)", Cnk); mXparser::consolePrintln("Res 1: " + e->getExpressionString() + " = " + e->calculate()); mXparser_consolePrintln("Res 2: Cnk(10,3) = " + Cnk->calculate(10,3));Code result
[mXparser-v.5.2.1] Res 1: Cnk(10,3) - C(10,3) = 0.0 [mXparser-v.5.2.1] Res 2: Cnk(10,3) = 120.0Case 3: Mixing function parameters β part causing recursive calls, other part as βtypicalβ parameter. Below example is presenting definition of Chebyshev polynomial using recursive function Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... Function T = new Function("T(n,x) = if(n>1, 2*x*T(n-1,x)-T(n-2,x), if(n>0, x, 1) )"); Argument k = new Argument("k = 5"); Argument x = new Argument("x = 2"); Expression e = new Expression("T(k,x) - ( (x + sqrt(x^2-1))^k + (x - sqrt(x^2-1))^k)/2", T, k, x); mXparser.consolePrintln("Res : " + e.getExpressionString() + " = " + e.calculate());C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... FunctionPtr T = new_Function("T(n,x) = if(n>1, 2*x*T(n-1,x)-T(n-2,x), if(n>0, x, 1) )"); ArgumentPtr k = new_Argument("k = 5"); ArgumentPtr x = new_Argument("x = 2"); ExpressionPtr e = new_Expression("T(k,x) - ( (x + sqrt(x^2-1))^k + (x - sqrt(x^2-1))^k)/2", T, k, x); mXparser::consolePrintln("Res : " + e->getExpressionString() + " = " + e->calculate());Code result
[mXparser-v.5.2.1] Res : T(k,x) - ( (x + sqrt(x^2-1))^k + (x - sqrt(x^2-1))^k)/2 = 0.0Case 4: Indirect recursion β approximating sin(x) and cos(x) Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*; // C#: using org.mariuszgromada.math.mxparser; // ... Constant a = new Constant("a = 0.0001"); Function s = new Function("s(x) = if( abs(x) < a, x, 2*s(x/2)*c(x/2) )", a); Function c = new Function("c(x) = if( abs(x) < a, 1, c(x/2)^2-s(x/2)^2 )", a); /* * Functions s and c must point to each other, * i.e. references should be added only after * they have been created */ s.addDefinitions(c); c.addDefinitions(s); Expression e1 = new Expression("sin(5)-s(5)", s); Expression e2 = new Expression("cos(5)-c(5)", c); mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate()); mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());C++ code
#include "org/mariuszgromada/math/mxparser.hpp" // ... ConstantPtr a = new_Constant("a = 0.0001"); FunctionPtr s = new_Function("s(x) = if( abs(x) < a, x, 2*s(x/2)*c(x/2) )", a); FunctionPtr c = new_Function("c(x) = if( abs(x) < a, 1, c(x/2)^2-s(x/2)^2 )", a); /* * Functions s and c must point to each other, * i.e. references should be added only after * they have been created */ s->addDefinitions(c); c->addDefinitions(s); ExpressionPtr e1 = new_Expression("sin(5)-s(5)", s); ExpressionPtr e2 = new_Expression("cos(5)-c(5)", c); mXparser::consolePrintln("Res 1: " + e1->getExpressionString() + " = " + e1->calculate()); mXparser::consolePrintln("Res 2: " + e2->getExpressionString() + " = " + e2->calculate());Code result
[mXparser-v.5.2.1] Res 1: sin(5)-s(5) = 1.829204866182E-4 [mXparser-v.5.2.1] Res 2: cos(5)-c(5) = -5.410012369295E-5Nuget β 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