# mXparser - v.2.4.0 - Average, Variance, Standard deviation, New iterated operators

Dear All,

I am happy to announce that new version of mXparser has just been released. Update delivers below functionalities

##### - Mean value: mean(a1, a2, ..., an)
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

##### - Bias-corrected sample variance: var(a1, a2, ..., an)
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

##### - Bias-corrected sample standard deviation: std(a1, a2, ..., an)
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


### New iterated operators

##### - Minimum from sample function values: mini(i, from, to, f(i), <by>)
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

##### - Maximum from sample function values: maxi(i, from, to, f(i), <by>)
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

##### - Average from sample function values: avg(i, from, to, f(i), <by>)
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

##### - Bias-corrected variance from sample function values: vari(i, from, to, f(i), <by>)
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

##### - Bias-corrected standard deviation from sample function values: stdi(i, from, to, f(i), <by>)
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


# Stirling numbers of the second kind and sampling with replacement

### Sampling with replacement

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:

### Number of ways with k-unique outputs

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.

### 1st step - partial permutation (k-permutation of n aka variation without repetition)

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).



### 2nd step - Stirling numbers of the second kind

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



### mXparser - Math Parser - definition

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,

# Indirect recursion using mXparser

Hello,

Today I will present how flexible mXparser really is. As an example we will approximate  and  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:




Above formals can be equivalently written as




Please notice that knowing the solution for smaller value  it is possible to get solution for the original one .  This simply means that mentioned trigonometric identities are in fact example of recursion - here indirect recursion as  function is using , and  is using . The complete recursion definition requires base case definition (stop condition).

For  near to  function  can be well approximated exactly by , while in case of  constant  is pretty good approximation. This gives good stop condition.

For small  let us define two recursive functions:




Once again please notice that  (and  separately) is using both  and .

We expect that smaller parameter  is giving better approximations - below you will find functions graphs separately for  and .

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' */