.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/curves/plot_05_similarity_dimension.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_fractals_chaos_curves_plot_05_similarity_dimension.py: 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. .. GENERATED FROM PYTHON SOURCE LINES 11-21 .. code-block:: Python import numpy as np from mathematicskit.fractals_chaos import ( SierpinskiCarpet, SierpinskiTriangle, box_counting_dimension, koch_curve, similarity_dimension, ) .. GENERATED FROM PYTHON SOURCE LINES 22-24 Moran's equation against box counting ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-41 .. code-block:: Python 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}") .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 42-44 Unequal ratios ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: Python 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}") .. rst-class:: sphx-glr-script-out .. code-block:: none ratios (1/2, 1/4, 1/4): D = 1.0000 ratios (0.6, 0.3): D = 0.8594 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.537 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_curves_plot_05_similarity_dimension.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_similarity_dimension.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_similarity_dimension.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_similarity_dimension.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_