.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/geometry/surfaces/plot_01_theorema_egregium.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_geometry_surfaces_plot_01_theorema_egregium.py: Gauss's Theorema Egregium: curvature you can measure from inside ====================================================================== Computes the Gaussian curvature K of a sphere, a cylinder, and a torus. The cylinder bends in space but has K = 0, because it can be unrolled flat without stretching, while no piece of a sphere can. Integrating K over the whole surface gives 4 pi for the sphere and 0 for the torus, as the Gauss-Bonnet theorem predicts. .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import integrate from mathematicskit.geometry import cylinder_surface, sphere_surface, surface_curvature, torus_surface .. GENERATED FROM PYTHON SOURCE LINES 20-22 Curvature of three surfaces ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-38 .. code-block:: Python cases = { "sphere (R = 1)": surface_curvature(sphere_surface(1.0), np.linspace(1e-3, np.pi - 1e-3, 201), np.linspace(0, 2 * np.pi, 201)), "cylinder (R = 1)": surface_curvature(cylinder_surface(1.0), np.linspace(-1, 1, 101), np.linspace(0, 2 * np.pi, 201)), "torus (R = 2, r = 0.7)": surface_curvature(torus_surface(2.0, 0.7), np.linspace(0, 2 * np.pi, 201), np.linspace(0, 2 * np.pi, 201)), } fig = plt.figure(figsize=(13, 4)) for k, (name, result) in enumerate(cases.items(), start=1): ax = fig.add_subplot(1, 3, k, projection="3d") K = result.gaussian colors = plt.cm.coolwarm((K - K.min()) / (np.ptp(K) + 1e-12)) if np.ptp(K) > 1e-9 else plt.cm.coolwarm(np.full(K.shape, 0.5)) ax.plot_surface(*result.points, facecolors=colors, rstride=4, cstride=4, linewidth=0) ax.set_title(f"{name}\nK from {K.min():+.3f} to {K.max():+.3f}", fontsize=9) ax.set_box_aspect((1, 1, 1)) ax.axis("off") .. image-sg:: /api/gallery/geometry/surfaces/images/sphx_glr_plot_01_theorema_egregium_001.png :alt: sphere (R = 1) K from +0.999 to +1.001, cylinder (R = 1) K from -0.000 to +0.000, torus (R = 2, r = 0.7) K from -1.099 to +0.529 :srcset: /api/gallery/geometry/surfaces/images/sphx_glr_plot_01_theorema_egregium_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-41 Total curvature (Gauss-Bonnet) ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-48 .. code-block:: Python for name, result in cases.items(): if name.startswith("cylinder"): continue total = integrate.trapezoid(integrate.trapezoid(result.gaussian * result.area_element, result.v, axis=1), result.u) print(f"{name:22s}: integral of K dA = {total:+.5f}") print(f"{'4 pi':22s} = {4 * np.pi:+.5f}") .. rst-class:: sphx-glr-script-out .. code-block:: none sphere (R = 1) : integral of K dA = +12.56352 torus (R = 2, r = 0.7): integral of K dA = -0.00002 4 pi = +12.56637 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.144 seconds) .. _sphx_glr_download_api_gallery_geometry_surfaces_plot_01_theorema_egregium.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_theorema_egregium.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_theorema_egregium.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_theorema_egregium.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_