.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/ifs/plot_01_sierpinski_triangle_and_carpet.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_ifs_plot_01_sierpinski_triangle_and_carpet.py: Sierpiński's triangle and carpet ====================================== Sierpiński's triangle (1915) removes the middle quarter of a filled triangle and repeats on the three corner triangles; his carpet (1916) removes the middle ninth of a square and repeats on the eight remaining squares. Both limits have zero area and a dimension strictly between 1 and 2. Here both are drawn with the chaos game. .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.fractals_chaos import SierpinskiCarpet, SierpinskiTriangle from mathematicskit.fractals_chaos.visualizers.plots import plot_ifs_points .. GENERATED FROM PYTHON SOURCE LINES 20-22 The two sets ------------ .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(10, 5)) plot_ifs_points(SierpinskiTriangle().generate(60000, seed=0), ax=axes[0], color="steelblue") axes[0].set_title("Sierpiński triangle") plot_ifs_points(SierpinskiCarpet().generate(80000, seed=0), ax=axes[1], color="firebrick") axes[1].set_title("Sierpiński carpet") fig.tight_layout() .. image-sg:: /api/gallery/fractals_chaos/ifs/images/sphx_glr_plot_01_sierpinski_triangle_and_carpet_001.png :alt: Sierpiński triangle, Sierpiński carpet :srcset: /api/gallery/fractals_chaos/ifs/images/sphx_glr_plot_01_sierpinski_triangle_and_carpet_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 31-38 Area vanishes, dimension is fractional -------------------------------------- After :math:`n` removals the triangle keeps :math:`(3/4)^n` of its area and the carpet :math:`(8/9)^n`; both tend to zero. Three copies at scale 1/2 and eight copies at scale 1/3 give dimensions :math:`\log 3/\log 2` and :math:`\log 8/\log 3`. .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: Python for n in (1, 5, 20, 50): print(f"n = {n:2d}: triangle area {(3 / 4) ** n:.2e}, carpet area {(8 / 9) ** n:.2e}") print(f"dimension of triangle: {np.log(3) / np.log(2):.4f}, carpet: {np.log(8) / np.log(3):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none n = 1: triangle area 7.50e-01, carpet area 8.89e-01 n = 5: triangle area 2.37e-01, carpet area 5.55e-01 n = 20: triangle area 3.17e-03, carpet area 9.48e-02 n = 50: triangle area 5.66e-07, carpet area 2.77e-03 dimension of triangle: 1.5850, carpet: 1.8928 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.145 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_ifs_plot_01_sierpinski_triangle_and_carpet.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_sierpinski_triangle_and_carpet.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_sierpinski_triangle_and_carpet.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_sierpinski_triangle_and_carpet.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_