Skip to content

Instantly share code, notes, and snippets.

@Gro-Tsen
Created June 11, 2025 14:46
Show Gist options
  • Save Gro-Tsen/5d0f21a076f72dd3970a2f0485caa63a to your computer and use it in GitHub Desktop.
Save Gro-Tsen/5d0f21a076f72dd3970a2f0485caa63a to your computer and use it in GitHub Desktop.
Plot the staircase function (cumulative graph) of a fat Cantor set.
# Compute the cumulative measure function of a "fat" Cantor set (aka
# Smith-Volterra-Cantor set) with parameter alpha (meaning that
# 2^(k-1) intervals of length alpha^k are removed at the k-th stage of
# the construction.
alpha = 1/4
# Total measure of the fat Cantor set:
totmeas = N((1-3*alpha)/(1-2*alpha))
def staircase(x):
x = N(x)
if x <= 0: return 0
if x >= 1: return 1
# ycumul and yscale are the lower bound and length of the y-axis
# interval on which we've determined the value to lie.
yscale = N(1)
ycumul = N(0)
# lbnd and rbnd are the lower and upper bounds of the x-axis
# interval on which we've located the input point x.
lbnd = N(0)
rbnd = N(1)
# alphapow = alpha^(i+1) where i is how many steps in the
# constuction we've performed (we go up to i=30).
alphapow = N(alpha)
for i in range(30):
# mlbnd and mrbnd are the lower and upper bounds of the x-axis
# interval removed in the middle ofthe interval from lbnd to
# rbnd.
mlbnd = (lbnd+rbnd-alphapow)/2
mrbnd = (lbnd+rbnd+alphapow)/2
if x<mlbnd:
# We are left of the removed interval
rbnd = mlbnd
yscale /= 2
elif x>mrbnd:
# We are right of the removed interval
lbnd = mrbnd
yscale /= 2
ycumul += yscale
else:
# We are in the removed interval
return ycumul + yscale/2
alphapow *= alpha
return ycumul + yscale*(x-lbnd)/(rbnd-lbnd)
# For alpha=1/3 (standard Cantor set), of course, totmeas=0 so if you
# wish to plot this case, remove *totmeas below.
g = plot(lambda x: staircase(x)*totmeas, (0,1), plot_points=960)
g.save(filename="plot.png", dpi=300, aspect_ratio=1)
@Gro-Tsen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment