# Thomae's function

Point plot of the function on (0,1)

Thomae's function, named after Carl Johannes Thomae, has many names: the popcorn function, the raindrop function, the countable cloud function, the modified Dirichlet function, the ruler function,[1] the Riemann function, or the Stars over Babylon (John Horton Conway's name).[2] This real-valued function f(x) of the real variable x is defined as:

${\displaystyle f(x)={\begin{cases}1&{\text{if }}x=0\\{\frac {1}{q}}&{\text{if }}x={\tfrac {p}{q}}\quad (x{\text{ is rational), with }}p{\text{ and }}q{\text{ relatively prime integers and }}q>0\\0&{\text{if }}x{\text{ is irrational.}}\end{cases}}}$

Since every nonzero rational number has a unique representation with relatively prime p and q in the above form, the function is well-defined.

It is a modification of the Dirichlet function, which is 1 at rational numbers and 0 elsewhere.

## Properties

The popcorn function has a complicated set of discontinuities: f is continuous at all irrational numbers and discontinuous at all rational numbers.

The popcorn function also has a strict local maximum at each rational number.

### Informal proof of discontinuities

Clearly, f is discontinuous at all rational numbers: since the irrationals are dense in the reals, for any rational x, no matter what ε we select, there is an irrational a even nearer to our x where f(a) = 0 (while f(x) is positive). In other words, f can never "get close" and "stay close" to any positive number because its domain is dense with zeros.

To show continuity at the irrationals, assume without loss of generality that our ε is rational (for any irrational ε′, we can choose a smaller rational ε″ and the proof is transitive). Since ε is rational, it can be expressed in lowest terms as a/b. We want to show that f(x) is continuous when x is irrational.

Note that f takes a maximum value of 1 at each whole integer, so we may limit our examination to the space between ${\displaystyle \scriptstyle \lfloor x\rfloor }$ and ${\displaystyle \scriptstyle \lceil x\rceil }$. Since ε has a finite denominator of b, the only values for which f may return a value greater than ε are those with a reduced denominator no larger than b. There exist only a finite number of values between two integers with denominator no larger than b, so these can be exhaustively listed. Setting δ to be smaller than the nearest distance from x to one of these values guarantees every value within δ of x has f(x) < ε.

## Integrability

The Lebesgue criterion for integrability states that a bounded function is Riemann integrable if and only if the set of all discontinuities has measure zero.[3] Since the set of all discontinuities is the rational numbers, and the rational numbers are countable, the set has measure zero. As well, the function is bounded on the interval [0, 1], so by the Lebesgue criterion, the function is Riemann integrable on [0, 1]. Its integral is equal to 0 over [0,1].

## Follow-up

A natural follow-up question one might ask is if there is a function which is continuous on the rational numbers and discontinuous on the irrational numbers. This turns out to be impossible; the set of discontinuities of any function must be an Fσ set. If such a function existed, then the irrationals would be an Fσ set. The irrationals would then be the countable union of closed sets ${\displaystyle \cup _{i=0}^{\infty }C_{i}}$, but since the irrationals do not contain an interval, nor can any of the ${\displaystyle C_{i}}$. Therefore, each of the ${\displaystyle C_{i}}$ would be nowhere dense, and the irrationals would be a meager set. It would follow that the real numbers, being a union of the irrationals and the rationals (which is evidently meager), would also be a meager set. This would contradict the Baire category theorem: because the reals form a complete metric space, they form a Baire space, which cannot be meager in itself.

A variant of the popcorn function can be used to show that any Fσ subset of the real numbers can be the set of discontinuities of a function. If ${\displaystyle \scriptstyle A\;=\;\bigcup _{n=1}^{\infty }F_{n}}$ is a countable union of closed sets ${\displaystyle \scriptstyle F_{n}}$, define

${\displaystyle f_{A}(x)={\begin{cases}{\frac {1}{n}}&{\text{if }}x{\text{ is rational and }}n{\text{ is minimal so that }}x\in F_{n}\\-{\frac {1}{n}}&{\text{if }}x{\text{ is irrational and }}n{\text{ is minimal so that }}x\in F_{n}\\0&{\text{if }}x\notin A\end{cases}}}$

Then a similar argument as for the popcorn function shows that ${\displaystyle \scriptstyle f_{A}}$ has A as its set of discontinuities.

For a general construction on arbitrary metric space, see this article Kim, Sung Soo. "A Characterization of the Set of Points of Continuity of a Real Function." American Mathematical Monthly 106.3 (1999): 258-259.

## Related probability distributions

Empirical probability distributions related to Thomae's function appear in DNA sequencing.[4] The human genome is diploid, having two strands per chromosome. When sequenced, small pieces ("reads") are generated: for each spot on the genome, an integer number of reads overlap with it. Their ratio is a rational number, and typically distributed similar to Thomae's function.

If pairs of positive integers ${\displaystyle m,n}$ are sampled from a distribution ${\displaystyle f(n,m)}$ and used to generate ratios ${\displaystyle q=n/(n+m)}$, this gives rise to a distribution ${\displaystyle g(q)}$ on the rational numbers. If the integers are independent the distribution can be viewed as a convolution over the rational numbers, ${\displaystyle g(a/(a+b))=\sum _{t=1}^{\infty }f(ta)f(tb)}$. Closed form solutions exist for power-law distributions with a cut-off. If ${\displaystyle f(k)=k^{-\alpha }e^{-\beta k}/\mathrm {Li} _{\alpha }(e^{-\beta })}$ (where ${\displaystyle \mathrm {Li} _{\alpha }}$ is the polylogarithm function) then ${\displaystyle g(a/(a+b))=(ab)^{-\alpha }\mathrm {Li} _{2\alpha }(e^{-(a+b)\beta })/\mathrm {Li} _{\alpha }^{2}(e^{-\beta })}$. In the case of uniform distributions on the set ${\displaystyle \{1,2,\ldots ,L\}}$ ${\displaystyle g(a/(a+b))=(1/L^{2})\lfloor L/\max(a,b)\rfloor }$, which is very similar to Thomae's function. Both their graphs have fractal dimension 3/2.[4]

## The ruler function

For integers, the exponent of the highest power of 2 dividing ${\displaystyle n}$ gives 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, ... (sequence A007814 in the OEIS). If 1 is added, or if the 0's are removed, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, ... (sequence A001511 in the OEIS). The values resemble tick-marks on a 1/16th graduated ruler, hence the name. These values correspond to the restriction of the Thomae function to those rational numbers whose denominators are powers of 2.