# Using coding to calculate my average for functions defined on countable sets?

Suppose we have a function $$f:A\to\mathbb{R}$$ where $$A\subseteq[a,b]$$, $$a,b\in\mathbb{R}$$ and $$S\subseteq A$$.

Now suppose $$f$$ is

$$f(x)=\begin{cases} x^2+1 & x\in S_1\\ x & x\in S_2\\ \end{cases}$$

Where $$S_1$$ and $$S_2$$ are pairwise disjoint subsets of $$A=S_1\cup S_2$$ where $$S_1=\left\{\frac{1}{c}+\frac{\sqrt{2}}{2}:c\in\mathbb{Z}\right\}\cap[0,1]$$, $$S_2=\left\{\frac{1}{2^{(d_1)}}+\frac{1}{2^{(d_2)}}:d_1,d_2\in\mathbb{Z}\right\}\cap[0,1]$$ and $$[a,b]=[0,1]$$ where $$a=0$$ and $$b=1$$.

Suppose we divide $$[0,1]$$ into $$r$$ sub-intervals $$\left\{[x_{i-1},x_i]\right\}_{i=1}^{r}$$, where $$a=x_0\le x_1 \le ...\le x_i \le ...\le x_{r-1}\le x_r=b$$; and, if $$f$$ is defined in that sub-interval we take a point from that sub-interval; (however, if $$f$$ is not defined, we set $$f=0$$) and multiply it by measures $$\mu(S_1\cap[x_{i-1},x_i],A)$$, for $$f(S_1)$$, and $$\mu(S_2\cap[x_{i-1},x_i],A)$$, for $$f(S_2)$$,

We define $$\mu(S_1,A)$$ and $$\mu(S_2,A)$$ as the following:

In each sub-interval $$[x_{i-1},x_i]$$, we cover $$S_1\cap [x_{i-1},x_i]$$ by $$m_1$$ sub-intervals, of each of the sub-intervals in $$\left(I_{i}\right)_{i=1}^{r}=\left\{[x_{i-1},x_i]\right\}_{i=1}^{r}$$. We define this as $$\left(I_{i_1,k}\right)_{k=1}^{m_1}$$, where $$1 \le m_1 \le \max\left\{|S_1|,1\right\}$$. Then cover $$S_2\cap[x_{i-1},x_i]$$ by $$m_2$$ sub-intervals of each of the sub-intervals in $$\left(I_{i}\right)_{i=1}^{r}=\left\{[x_{i-1},x_i]\right\}_{i=1}^{r}$$, which we define as $$\left(I_{i_2,k}\right)_{k=1}^{m_2}$$, where $$1 \le m_2 \le \max\left\{|S_2|,A\right\}$$. Last, cover $$A=S_1\cup S_2$$ by $$n$$ sub-intervals of $$J=[a,b]$$, which we define as $$\left(J_{k}\right)_{k=1}^{n}$$ where $$1 \le n \le \max\left\{|A|,1\right\}$$. All these sub-intervals have the same length which we define as $$c\in\mathbb{R}^{+}$$.

We wish to minimize (or find the infimum) of the total sum of the lengths of the $$m_1$$ sub-intervals covering $$S_1\cap[x_{i-1},x_i]$$, where the infimum depends on $$c$$. This means we must set $$c$$ to specific values before taking the infimum. This would give us $$m_1$$ divided by $$n$$. The same is applied with $$m_2$$ sub-intervals covering $$S_2\cap [x_{i-1},x_i]$$ and the $$n$$ sub-intervals covering $$A\cap [x_{i-1},x_i]$$. This would give us $$m_2$$ divided by $$n$$.

Next we want want to find the infimum of $$m_1$$ divided by $$n$$ over $$c$$, (both $$m_1$$ and $$n$$ depends on $$c$$). We want the same with the infimum of $$m_2$$ divided by $$n$$ over $$c$$. This gives us $$\mu(S_1\cap[x_{i-1},x_i],A)$$ and $$\mu(S_2\cap[x_{i-1},x_i],A)$$.

To get our average one must take

$$\sum_{i=1}^{r}(x_i^2+1)\mu(S_1\cap[x_{i-1},x_i],A)+\sum_{i=1}^{r}(x_i)\mu(S_2\cap[x_{i-1},x_i],A)$$

Questions

1. How would we do this on Mathematica?
2. How would we generalize this to any piece-wise function defined on a countable set?

## My Attempt

Subscript[S, 1][c_Integer, r_] :=
Select[Union@
Flatten[Table[
1/2^(Subscript[d, 1]) + 1/2^(Subscript[d, 2]), {Subscript[d, 1],
1, c}, {Subscript[d, 2], Subscript[d, 1], c}]],
Between[#,
r] &]


This lists all elements in $$S_1$$ as c$$\to\infty$$ and groups them into $$r$$ sub-intervals of $$[0,1]$$

Subscript[S, 2][c_Integer, q_] :=
Select[Union@
Flatten[Table[
1/Subscript[u, 1] + Sqrt[2]/2, {Subscript[u, 1], 1, c}]],
Between[#,
q] &]


This lists all elements of Set $$S_2$$ as and groups them into $$r$$ sub-intervals of $$[0,1]$$

Subscript[f, 1][x_] :=
x^2


This is the function $$f=f_1$$ on $$x\in S_1$$

Subscript[f, 1, 1][c_Integer, r_] :=
Subscript[f,
1] /@ (Subscript[S, 1][c, #] & /@ Partition[Subdivide[r], 2, 1])


Groups $$S_1$$ into $$r$$ sub-intervals

Subscript[f, 1, 1, 1][c_Integer, r_] :=
Subscript[f, 1, 1][c, r] /. {} -> {0}


Converts null sets to zero

Subscript[f, 1, 1, 1][30, 100];

Subscript[f, 2][
x_] := x


This is the function $$f=f_2$$ on $$x\in S_2$$

Subscript[f, 2, 2][c_Integer, r_] :=
Subscript[f,
2] /@ (Subscript[S, 2][c, #] & /@
Partition[Subdivide[r], 2,
1]) (* Groups Subscript[S, 2] into r sub-intervals *)
Subscript[f, 2, 2, 2][c_Integer, r_] :=
Subscript[f, 2, 2][c, r] /. {} -> {0}


Converts null sets to zero

In[205]:=
Subscript[I, 1][r_] :=
Block[{m = 10^16},
With[{k = Ceiling@Log2@N@m + 2},
Total[#] + 1 - Last[#] &@
Unitize@BinCounts[
Total[Tuples[2^Range[-k, -1], 2], {2}], {Min[#1] &, Max[#1] &,
1/m}]]] /@ Partition[Subdivide[r], 2, 1]


This is supposed to give $$m_1$$ from where the total sum of the length of all $$m_1$$ sub-intervals, of each of the $$r$$ sub-intervals, covering the intersection of $$S_1$$ and the $$r$$ sub-intervals are as small as possible.

In[206]:= Subscript[I, 1][10]

During evaluation of In[206]:= BinCounts::bins: The bin specification {Min[#1]&,Max[#1]&,1/10000000000000000} is not a list of 2 or 3 real values.

Out[206]= If[29155653097335068495 === $SessionID, Out[206], Message[ MessageName[Syntax, "noinfoker"]]; Missing["NotAvailable"]; Null]  Unfortunately, there is something wrong with this function. Subscript[I, 2][r_] := Block[{m = 10^16}, With[{k = Ceiling@N@m + 2}, Total[#] + 1 - Last[#] &@ Unitize@BinCounts[ Total[Tuples[Range[-k, -1], 1], {2}], {Min[#1] &, Max[#1] &, 1/m}]]] /@ Partition[Subdivide[r], 2, 1]  This is supposed to give $$m_2$$ from where the total sum of the length of all $$m_2$$ sub-intervals, of each of the r sub-intervals, covering the intersection of $$S_2$$ and the $$r$$ sub-intervals are as small as possible Subscript[I, 2][10] J[r_] := Sum[ Subscript[I, 1][i] + Subscript[I, 2][i], {i, 1, r}]  This is supposed to give $$n$$ from where the total sum of the length of all $$n$$ sub-intervals, of each of the $$r$$ sub-intervals, covering the intersection of $$A$$ and the $$r$$ sub-intervals are as small as possible Subscript[Avg, 1][r_] := Sum[Subscript[f, 1, 1, 1][30, i][[i, 1]]* Subscript[I, 1][r][[i]]/J[r], {i, 1, r}] (* Subscript[I, 1][r][[i]]/A[r] is mu(Subscript[S, 1],A) as m\ \[Rule] Infinity, the whole sum is supposed to give the average over \ Subscript[f, 1] *) Subscript[Avg, 2][r_] := Sum[Subscript[f, 2, 2, 2][30, i][[i, 1]]* Subscript[I, 2][r][[i]]/J[r], {i, 1, r}] (* Subscript[I, 1][r][[i]]/A[r] is mu(Subscript[S, 2],A) as m\ \[Rule]Infinity, the whole sum is supposed to give the average over \ Subscript[f, 2] *) Subscript[Avg, 1][10000] + Subscript[Avg, 1][10000] (* This is the combined total sum *) During evaluation of In[217]:= General::nomem: The current computation was aborted because there was insufficient memory available to complete the computation. During evaluation of In[217]:= Throw::sysexc: Uncaught SystemException returned to top level. Can be caught with Catch[\[Ellipsis], _SystemException]. Out[218]= SystemException["MemoryAllocationFailure"] During evaluation of In[217]:= BinCounts::bins: The bin specification {Min[#1]&,Max[#1]&,1/10000000000000000} is not a list of 2 or 3 real values. During evaluation of In[217]:= BinCounts::bins: The bin specification {Min[#1]&,Max[#1]&,1/10000000000000000} is not a list of 2 or 3 real values. During evaluation of In[217]:= General::nomem: The current computation was aborted because there was insufficient memory available to complete the computation. During evaluation of In[217]:= Throw::sysexc: Uncaught SystemException returned to top level. Can be caught with Catch[\[Ellipsis], _SystemException]. Out[222]= SystemException["MemoryAllocationFailure"]  Unfortunately, as you see here I am not getting any results. How do we fix the errors in my code? Is my attempt correct? ## 1 Answer I can't understand what your code does but I think this might fix your BinCounts problem: 1. Don't use the letter I - it's reserved. I replaced it with ii 2. Use a Module because m isn't accessible in that inner With scope. 3. Move the & just outside the BinCounts and before /@ Subscript[ii, 1][r_] := Module[{m = 10^16}, With[{k = Ceiling@Log2@N@m + 2}, Total[#] + 1 - Last[#] &@ Unitize[BinCounts[ Total[Tuples[2^Range[-k, -1], 2], {2}], {Min[#1], Max[#1], 1/m}]] & /@ Partition[Subdivide[r], 2, 1]]] Subscript[ii,1][10] (* returns: {1282, 52, 51, 3, 1, 52, 2, 2, 1, 1} *)  You should consider removing as many Subscript's from your code as this can quickly become hell, and introduce subtle bugs. It's also harder to copy-paste readable code on this site if it has subscripts, Greek, or special formatting for sums, integrals, overbars etc. • On the last edit I fixed ]] too, make sure you pick up the latest change if you use this. – flinty Jul 9 '20 at 23:02 • How do we fix this code Subscript[ii, 2][r_] := Module[{m = 10^16}, With[{k = Ceiling@N@m + 1}, Total[#] + 1 - Last[#] &@ Unitize[BinCounts[Total[1/k], {2}], {Min[#1], Max[#1], 1/m}]] & /@Partition[Subdivide[r], 2, 1]]. Here I'm splitting$[x_{i-1},x_{i}]$, a sub-interval of$[a,b]=[0,1]$, into$m_2$sub-intervals and counting the number of${m_2}^{\prime}$sub-intervals, out of$m$sub-intervals, that cover$S_2\cap[x_{i-1},x_i]\$. – RajanArak Jul 9 '20 at 23:56
• You're using BinCounts on a number, not a list BinCounts[Total[1/k], {2}] – flinty Jul 10 '20 at 16:42
• @flinity I tried Subscript[ii, 2][r_] := Module[{m = 10^16}, With[{k = Ceiling@N@m + 1}, Total[#] + 1 - Last[#] &@ Unitize[BinCounts[Total[1/Range[1,k]], {2}], {Min[#1], Max[#1], 1/m}]] & /@Partition[Subdivide[r], 2, 1]] and I still don't get a proper answer. – RajanArak Jul 10 '20 at 16:44
• Total[1/Range[1, k]] is always a number too. – flinty Jul 10 '20 at 16:46