Note
Go to the end to download the full example code.
Hausdorff and Moran: the similarity dimension#
Solves Moran’s equation sum r_i^D = 1 for several self-similar sets and compares the answers with box-counting estimates computed from the sets themselves.
import numpy as np
from mathematicskit.fractals_chaos import (
SierpinskiCarpet,
SierpinskiTriangle,
box_counting_dimension,
koch_curve,
similarity_dimension,
)
Moran’s equation against box counting#
cantor_points = np.array([[sum(int(d) * 2 * 3.0 ** -(k + 1) for k, d in enumerate(f"{i:012b}")), 0.0] for i in range(4096)])
cases = {
"Cantor set": ([1 / 3] * 2, cantor_points),
"Koch curve": ([1 / 3] * 4, koch_curve(7)),
"Sierpinski triangle": ([1 / 2] * 3, SierpinskiTriangle().generate(200000)),
"Sierpinski carpet": ([1 / 3] * 8, SierpinskiCarpet().generate(300000)),
}
for name, (ratios, points) in cases.items():
if name == "Cantor set":
points = np.column_stack([points[:, 0], np.zeros(len(points))])
sizes = np.logspace(-1, -3.3, 10)
estimate = np.polyfit(np.log(1 / sizes), np.log([len(np.unique(np.floor(points[:, 0] / s))) for s in sizes]), 1)[0]
else:
estimate = box_counting_dimension(points).dimension
print(f"{name:20s} similarity dimension {similarity_dimension(ratios):.4f}, box counting {estimate:.4f}")
Cantor set similarity dimension 0.6309, box counting 0.6353
Koch curve similarity dimension 1.2619, box counting 1.2771
Sierpinski triangle similarity dimension 1.5850, box counting 1.5538
Sierpinski carpet similarity dimension 1.8928, box counting 1.8194
Unequal ratios#
print(f"\nratios (1/2, 1/4, 1/4): D = {similarity_dimension([0.5, 0.25, 0.25]):.4f}")
print(f"ratios (0.6, 0.3): D = {similarity_dimension([0.6, 0.3]):.4f}")
ratios (1/2, 1/4, 1/4): D = 1.0000
ratios (0.6, 0.3): D = 0.8594
Total running time of the script: (0 minutes 2.537 seconds)