-1

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?

Arbuja
  • 59
  • 4
  • 18

1 Answers1

3

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.

flinty
  • 25,147
  • 2
  • 20
  • 86
  • On the last edit I fixed ]] too, make sure you pick up the latest change if you use this. – flinty Jul 09 '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]$. – Arbuja Jul 09 '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. – Arbuja Jul 10 '20 at 16:44
  • Total[1/Range[1, k]] is always a number too. – flinty Jul 10 '20 at 16:46
  • So if $S_2=\left{\frac{1}{c}:c\in\mathbb{Z}\right}\cap[0,1]$ and we split $[0,1]$ into $r$ equal sub-intervals, how do we make a list where for each sub-interval, we split them into $m_2$ sub-intervals (don't ask why we're doing this) and count the number of $m_2^{'}$ sub-intervals, out of $m_2$ sub-intervals, that cover $S_2$. (There should be $r$ elements in this list, and $m_2$ should be very large.) – Arbuja Jul 10 '20 at 16:55
  • Consider Interval and functions like RegionWithin and RegionDisjoint. Generate subintervals like this: subd[interval_, pieces_] := Interval @@@Partition[Subdivide[interval[[1, 1]], interval[[1, 2]], pieces], 2, 1]

    I can then call pieces = subd[#, 4] & /@ subd[Interval[{0, 1}], 6] to chop {0,1} into 6 pieces, and chop those pieces into 4 pieces.

    Let S be Interval[{0.4,.0.7}] I count pieces on S with: Count[Flatten[Map[RegionWithin[Interval[{0.4, 0.7}], #] &, pieces, {2}]], True] or count how many are disjoint by changing RegionWithin to RegionDisjoint.

    – flinty Jul 10 '20 at 17:11
  • Since your $S_2$ is just a bunch of points, also consider using RegionMember on the intervals - this will simplify things a lot. – flinty Jul 10 '20 at 17:17
  • I tried your code: subd[interval_, pieces_] := Interval @@@ Partition[Subdivide[interval[[1, 1]], interval[[1, 2]], pieces], 2, 1]; pieces = subd[#, 10000] & /@ subd[Interval[{0, 1}], 100]; Subscript[S, 1, 1][c_] := Select[Union@ Flatten[Table[ 1/Subscript[u, 1] + Sqrt[2]/2, {Subscript[u, 1], 1, c}]], Between[#, {0, 1}] &]; Count[Flatten[ Map[RegionMember[Subscript[S, 1, 1][10000], #] &, pieces, {2}]], True] but I get........ – Arbuja Jul 10 '20 at 18:49
  • But I keep getting: RegionMember::regp: A correctly specified region expected at position 1 of RegionMember[{1/10000+1/Sqrt[2],1/9999+1/Sqrt[2],1/9998+1/Sqrt[2],1/9997+1/Sqrt[2],1/9996+1/Sqrt[2],1/9995+1/Sqrt[2],1/9994+1/Sqrt[2],1/9993+1/Sqrt[2],1/9992+1/Sqrt[2],1/9991+1/Sqrt[2],1/9990+1/Sqrt[2],1/9989+1/Sqrt[2],1/9988+1/Sqrt[2],1/9987+1/Sqrt[2],1/9986+1/Sqrt[2],1/9985+1/Sqrt[2],1/9984+1/Sqrt[2],1/9983+1/Sqrt[2],1/9982+1/Sqrt[2],1/9981+1/Sqrt[2],<<12>>..... – Arbuja Jul 10 '20 at 18:51