# How to plot Lebesgue singular function in Tikz?

I would like to know if there is a way of plotting a Lebesgue singular function using LaTeX (Tikz).

Lebesgue singular function $L_{a}\colon[0,1]\to[0,1]$ is defined as follow.

Imagine fipping an unfair coin with probability $0.5\neq a\in(0, 1)$ of heads and probability $1-a$ of tails. Let the binary expansion of $t\in [0, 1]\colon t =\sum_{k=1}^{\infty}\frac{\omega_{k}}{2^{k}}$ be determined by flipping the coin infinitely many times. In particular $\omega_{k}=0$ if the $k$-th toss is heads and $\omega_{k}=1$ if it is tails. Then $$L_{a}(x)\colon=\text{Prob}(t\leq x)$$

I don't know hot to do it manually with Tikz, because I'm at the very beginning at using it. Here is an example how to plot Cantor function. Maybe can be done something similar?

Here's a solution using the sagetex package, which gives you access to a computer algebra system and Python programming.

\documentclass{article}
\usepackage{sagetex}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{sagesilent}
def LSF(binexp):
a = .6666
L = [0]
U = [1]
M = [1]
for i in range(1,9):
M += [(1-a)*L[len(L)-1]+a*U[len(U)-1]]
if str(binexp)[i] == "1":
L += [M[len(M)-1]]
U += [U[len(U)-1]]
else:
U += [M[len(M)-1]]
L += [L[len(L)-1]]

return U[len(U)-1]

def BTD(mystr):
sum = 0
for i in range(1,9):
sum += int(mystr[i])*(.5)^i
return sum

xcoordsb = ['.{0:08b}'.format(i) for i in range(0,2^9)]
xcoords = [BTD(xcoordsb[i]) for i in range(0,2^9)]
ycoords = [LSF(xcoordsb[j]) for j in range(0,2^9)]
plotpoints = sorted([[xcoords[i],ycoords[i]] for i in range(0,2^9)], key=lambda k: [k[1], k[0]])

output = r""
output += r"\begin{tikzpicture}[scale=.7]"
output += r"\begin{axis}[xmin=0,xmax=1,ymin= 0,ymax=1,"
output += r"title={Lebesgue singular function, $a=.6666$}]"
output += r"\addplot[thin, blue, unbounded coords=jump] coordinates {"
for i in range(0,len(plotpoints)-1):
output += r"(%s,%s) "%(plotpoints[i][0],plotpoints[i][1])
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}


The solution is shown running in Cocalc for when a=.6666. Changing the value of a and then compiling will let you get other values.

My programming skills aren't strong, there's almost certainly an easier and more elegant ways to do this. I'm relying on an algorithm from Daniel Bernstein's thesis which I've linked to. The diagram on page 18 matches the output from Sage. The algorithm used is on page 19. Sage is not part of the LaTeX distribution. The quickest way to get started using it is with a free Cocalc account. You can install Sage on your computer so you aren't relying on an internet account. Search this site for sagetex to get more information.

Compute the function value at 2-adic fractions. You can do this by the following:

f(1/2) = a
do induction on n:
for 0 ≤ k < 2^{n-1}:
f(k/2^n) = f(k/2^{n-1})*a
for 2^{n-1} ≤ k < 2^n:
f(k/2^n) = f((k-2^{n-1})/2^{n-1})*(1-a)+a


So you got the following tex code. The \pgfkeys here acts as data array. The \pgfpath part plot the data point.

\documentclass[border=9,tikz]{standalone}

\begin{document}

\let\PMS\pgfmathsetmacro
\let\PMT\pgfmathtruncatemacro
\def\pgfkeysgloballet#1#2{\global\expandafter\let\csname pgfk@#1\endcsname#2}
\pgfkeys{/handlers/.let/.code=\pgfkeysgloballet{\pgfkeyscurrentpath}{#1}}
\tikz[x=10cm,y=10cm]{
\draw[yellow](0,0)rectangle(1,1);
\def\a{.3}
\PMS\b{1-\a}
\def\n{9}
\pgfkeys{/Leb/0::0/.let=\a}
\foreach\dep in{1,...,\n}{
\message{^^J\dep:}
\PMT\depmo{\dep-1}
\PMT\twotodep{2^\dep}
\PMT\twotodmo{2^(\dep-1)}
\foreach\ind in{0,...,\numexpr\twotodmo-1}{
\pgfkeys{/Leb/\depmo::\ind/.get=\parentvalue}
\PMS\childvalue{\parentvalue*\a}
\pgfkeys{/Leb/\dep::\ind/.let=\childvalue}
}
\foreach\ind in{\twotodmo,...,\numexpr\twotodep-1}{
\PMT\indmod{\ind-\twotodmo}
\pgfkeys{/Leb/\depmo::\indmod/.get=\parentvalue}
\PMS\childvalue{\parentvalue*\b+\a}
\pgfkeys{/Leb/\dep::\ind/.let=\childvalue}
}
}
\pgfpathmoveto{\pgfpointorigin}
\PMT\twoton{2^\n}
\foreach\ind in{0,...,\numexpr\twoton-1}{
\pgfkeys{/Leb/\n::\ind/.get=\yvalue}
\pgfpathlineto{\pgfpointxy{(\ind+.5)/\twoton}{\yvalue}}
}
\pgfpathlineto{\pgfpointxy{1}{1}}
\pgfusepath{stroke}
}

\end{document}