The mXparser package is now available on the nuget.org site.
MathParser.org-mXparser on nuget.org
PM> Install-Package MathParser.org-mXparser
Best regards,
Mariusz Gromada
The mXparser package is now available on the nuget.org site.
PM> Install-Package MathParser.org-mXparser
Best regards,
Mariusz Gromada
Dear All,
I am happy to announce that new version of mXparser has just been released. Update delivers below functionalities
import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("mean(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)");
mXparser.consolePrintln("Res: " + e.calculate());
[mXparser-v.2.4.0] Res: 5.5
import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("var(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)");
mXparser.consolePrintln("Res: " + e.calculate());
[mXparser-v.2.4.0] Res: 9.166666666666666
import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("std(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)");
mXparser.consolePrintln("Res: " + e.calculate());
[mXparser-v.2.4.0] Res: 3.0276503540974917
import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("mini(x, -pi, pi, sin(x), 2*pi/1000)");
mXparser.consolePrintln("Res: " + e.calculate());
[mXparser-v.2.4.0] Res: -1.0
import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("maxi(x, -pi, pi, sin(x), 2*pi/1000)");
mXparser.consolePrintln("Res: " + e.calculate());
[mXparser-v.2.4.0] Res: 1.0
import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("avg(x, -pi, pi, sin(x), 2*pi/1000)");
mXparser.consolePrintln("Res: " + e.calculate());
[mXparser-v.2.4.0] Res: 4.8615748597837905E-17
import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("vari(x, -pi, pi, sin(x), 2*pi/1000)");
mXparser.consolePrintln("Res: " + e.calculate());
[mXparser-v.2.4.0] Res: 0.4999999999999962
import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("stdi(x, -pi, pi, sin(x), 2*pi/1000)");
mXparser.consolePrintln("Res: " + e.calculate());
[mXparser-v.2.4.0] Res: 0.7071067811865449
Mariusz Gromada
Some time ago I considered the problem of sampling with replacement exactly n-elements from n-element set. As a result of such an operation output set may contains duplicates – let us here assume that we received exactly k unique elements (of course 1 ≤ k ≤ n). Then the question came:
What is the number of ways to obtain exactly k unique elements sampling with replacement n-elements from n-element set?
Above question relates to bootstrap estimation, as the answer gives distribution of unique elements (its number) in bootstrap samples.
Let B(n,k) be the function representing the number of such ways.
Pretty standard – the number of ways to choose k different elements of n paying attention to the order – this is a variation without repetition V(n,k).
$$V(n,k)=\frac{n!}{(n-k)!}$$
Let us think form the other perspective, forgetting for a moment about just sampled k unique elements and the need of additional n-k (though it’s true). Instead let’s imagine that we have n items, including k unique. The trick is now to understand that having k different elements in a set of n elements is generating division of original set into k non-empty disjunctive subsets. How many ways to divide n-element set into k-subsets? This is the Stirling number of the second kind marked S2 (n, k). Finally
$$B(n,k)=V(n,k)\times S_2(n,k)$$
import org.mariuszgromada.math.mxparser.*;
...
Function V = new Function("V(n,k) = n! / (n-k)!");
Function B = new Function("B(n,k) = V(n,k) * Stirl2(n,k)", V);
int n = 5;
for (int k = 0; k <= n; k++)
mXparser.consolePrintln("B(" + n + "," + k + ") = " + B.calculate(n,k) );
Result
[mXparser-v.2.3.1] B(5,0) = 0.0 [mXparser-v.2.3.1] B(5,1) = 5.0 [mXparser-v.2.3.1] B(5,2) = 300.0 [mXparser-v.2.3.1] B(5,3) = 1500.0 [mXparser-v.2.3.1] B(5,4) = 1200.0 [mXparser-v.2.3.1] B(5,5) = 120.0
Best regards,
Mariusz Gromada
Hello,
Today I will present how flexible mXparser really is. As an example we will approximate $$\sin(x)$$ and $$\cos(x)$$ using indirect recursion steps, which means two functions depending on each other. Let us start with a bit of theory starting with basic trigonometric identities:
$$\sin(2x)=2\sin(x)\cos(x)$$
$$\cos(2x)=\cos^2(x)-\sin^2(x)$$
Above formals can be equivalently written as
$$\sin(x)=2\sin\big(\frac{x}{2}\big)\cos\big(\frac{x}{2}\big)$$
$$\cos(x)=\cos^2\big(\frac{x}{2}\big)-\sin^2\big(\frac{x}{2}\big)$$
Please notice that knowing the solution for smaller value $$\frac{x}{2}$$ it is possible to get solution for the original one $$x$$. This simply means that mentioned trigonometric identities are in fact example of recursion – here indirect recursion as $$\sin(x)$$ function is using $$\cos(x)$$, and $$\cos(x)$$ is using $$\sin(x)$$. The complete recursion definition requires base case definition (stop condition).
For $$x$$ near to $$0$$ function $$\sin(x)$$ can be well approximated exactly by $$x$$, while in case of $$\cos(x)$$ constant $$1$$ is pretty good approximation. This gives good stop condition.
For small $$a>0$$ let us define two recursive functions:
$$\text{s}(x)=\begin{cases}x&\text{dla}\quad |x|<a\\2\text{s}\big(\frac{x}{2}\big)\text{c}\big(\frac{x}{2}\big)&\text{dla}\quad |x|\geq a\end{cases}$$
$$\text{c}(x)=\begin{cases}1&\text{dla}\quad |x|<a\\\text{c}^2\big(\frac{x}{2}\big)-\text{s}^2\big(\frac{x}{2}\big)&\text{dla}\quad |x|\geq a\end{cases}$$
Once again please notice that $$\text{s}$$ (and $$\text{c}$$ separately) is using both $$\text{s}\big(\frac{x}{2}\big)$$ and $$\text{c}\big(\frac{x}{2}\big)$$.
We expect that smaller parameter $$a$$ is giving better approximations – below you will find functions graphs separately for $$a = 0.5$$ and $$a=0.01$$.
import org.mariuszgromada.math.mxparser.*;
...
/* Recursive functions definition */
Constant a = new Constant("a", 0.1);
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);
/* Pointing that 's' is using 'c', and 'c' is using 's' */
s.addDefinitions(c);
c.addDefinitions(s);
Best regards,
Mariusz Gromada
Enjoy 🙂
Mariusz Gromada
Since mXparser-v.2.2.0 library is being always tested also on the Android platform. I can confirm that all regression tests were passed without any problems. Recommended library to use directly in the Android project is mXparser built with JDK 1.7.
In terms of System.out.println() Android behavior is different than JVM causing that all data passed to Console on JVM is printed in Log.cat by Android Dalvik. If you will use mXparser.consolePrintln()/Print() methods instead of System.out. equivalents you will also get access to the console output string containing printed data. Please refer to the API specification of the mXparser class.
Mariusz Gromada
No changes to API, but library tested and compiled for:
Please follow below link to download mXparser v.2.1.1-1
Enjoy 🙂
Mariusz Gromada
Fundamental Theorem of Calculus is a kind of a link between two most important calculus concepts: derivative and integral.
For continuous real-valued function $$f:[a,b]\to\mathbb{R}$$ defined on closed interval $$[a,b]$$ let $$F:[a,b]\to\mathbb{R}$$ be the function given by
$$F(x)=\int_a^x f(t)\text{d}t$$
The $$F$$ is uniformly continuous on $$[a, b]$$, differentiable on the open interval $$(a, b)$$, and
$$F'(x)=f(x)$$
import org.mariuszgromada.math.mxparser.*;
...
/* Function */
Function f = new Function("f(x) = sin(x)");
/* Antiderivative */
Function F = new Function("F(x) = int(f(t), t, 0, x)", f);
/* function = derivative ( antiderivative ) */
Argument x = new Argument("x = pi");
Expression e = new Expression("f(x) - der(F(x), x)", x, f, F);
mXparser.consolePrintln("Res : " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Computing time = " + e.getComputingTime() + " s.");
Res : f(x) - der(F(x), x) = 6.237833817291525E-8 Computing time = 0.411 s.
Best regards,
Mariusz Gromada
By continuing to use the site, you agree to the use of cookies. more information
The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.