
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/07_cartesian_grid_class/p02_cart_arithmetic.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_07_cartesian_grid_class_p02_cart_arithmetic.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_07_cartesian_grid_class_p02_cart_arithmetic.py:


Arithmetic and NumPy Ufunc Support
==================================

:class:`~pyvisual.core.mesh3d.CartesianMesh` (and
:class:`~pyvisual.core.mesh3d.SphericalMesh`) inherit a full arithmetic suite
from :class:`~pyvisual.core.mesh3d._BaseFrameMesh`.  Standard Python operators
(``+``, ``-``, ``*``, ``/``, ``**``, etc.) and NumPy ufuncs such as
:obj:`numpy.log10` and :obj:`numpy.sqrt` operate element-wise on the active
scalar field and return a new mesh of the same type with the result as the
active scalar.  The coordinate arrays are never modified — only the data
changes.

.. GENERATED FROM PYTHON SOURCE LINES 14-19

.. code-block:: Python


    import numpy as np
    from pyvisual import Plot3d
    from pyvisual.core.mesh3d import CartesianMesh








.. GENERATED FROM PYTHON SOURCE LINES 20-27

Build a Mesh
------------

Construct a :class:`~pyvisual.core.mesh3d.CartesianMesh` over a regular
Cartesian grid.  The scalar data is the Euclidean distance
:math:`r = \sqrt{x^2 + y^2 + z^2}` from the origin, providing a smooth,
sign-definite field on which to demonstrate arithmetic operations.

.. GENERATED FROM PYTHON SOURCE LINES 27-37

.. code-block:: Python


    x = np.linspace(-5, 5, 20)
    y = np.linspace(-5, 5, 20)
    z = np.linspace(-5, 5, 20)
    X, Y, Z = np.meshgrid(x, y, z, indexing='ij')
    dist = np.sqrt(X ** 2 + Y ** 2 + Z ** 2)

    mesh = CartesianMesh(X, Y, Z, data=dist, dataid='r')
    print(f"data range : [{mesh.data.min():.2f}, {mesh.data.max():.2f}]")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    data range : [0.46, 8.66]




.. GENERATED FROM PYTHON SOURCE LINES 38-46

Scalar Arithmetic
-----------------

Standard Python arithmetic operators act element-wise on the active scalar
field and return a new :class:`~pyvisual.core.mesh3d.CartesianMesh` — the
point coordinates are untouched.  Here we subtract the field minimum to
shift the distribution to zero, then divide by the resulting maximum to
normalize to the range :math:`[0, 1]`.

.. GENERATED FROM PYTHON SOURCE LINES 46-57

.. code-block:: Python


    mesh_shifted = mesh - mesh.data.min()
    mesh_norm = mesh_shifted / mesh_shifted.data.max()
    print(f"normalised range : [{mesh_norm.data.min():.2f}, {mesh_norm.data.max():.2f}]")

    plotter = Plot3d()
    plotter.show_axes()
    plotter.add_sun()
    plotter.add_mesh(mesh_norm, cmap='plasma', clim=(0, 1), opacity=0.3, show_scalar_bar=False)
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /gallery/07_cartesian_grid_class/images/sphx_glr_p02_cart_arithmetic_001.png
        :alt: p02 cart arithmetic
        :srcset: /gallery/07_cartesian_grid_class/images/sphx_glr_p02_cart_arithmetic_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /Users/rdavidson/MHDweb/pyvisual/docs/source/gallery/07_cartesian_grid_class/images/sphx_glr_p02_cart_arithmetic_001.vtksz



.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    normalised range : [0.00, 1.00]




.. GENERATED FROM PYTHON SOURCE LINES 58-67

NumPy Ufunc: ``np.log10``
-------------------------

The :meth:`~pyvisual.core.mesh3d._BaseFrameMesh.__array_ufunc__` hook lets
any single-output NumPy ufunc act directly on the mesh.
:obj:`numpy.log10` applied to the normalized distance converts the field to
a logarithmic scale that compresses the large dynamic range near the outer
boundary and reveals structure close to the origin.  Points at or below zero
(here, the grid corner where :math:`r = 0`) are masked by the log.

.. GENERATED FROM PYTHON SOURCE LINES 67-75

.. code-block:: Python


    mesh_log = np.log10(mesh_norm + 1e-6)
    print(f"log10 range : [{mesh_log.data.min():.2f}, {mesh_log.data.max():.2f}]")

    plotter = Plot3d()
    plotter.show_axes()
    plotter.add_sun()
    plotter.add_mesh(mesh_log, cmap='rainbow', clim=(-3, 0), opacity=0.3, show_scalar_bar=False)
    plotter.show()






.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /gallery/07_cartesian_grid_class/images/sphx_glr_p02_cart_arithmetic_002.png
        :alt: p02 cart arithmetic
        :srcset: /gallery/07_cartesian_grid_class/images/sphx_glr_p02_cart_arithmetic_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /Users/rdavidson/MHDweb/pyvisual/docs/source/gallery/07_cartesian_grid_class/images/sphx_glr_p02_cart_arithmetic_002.vtksz



.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    log10 range : [-6.00, 0.00]





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.715 seconds)


.. _sphx_glr_download_gallery_07_cartesian_grid_class_p02_cart_arithmetic.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: p02_cart_arithmetic.ipynb <p02_cart_arithmetic.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: p02_cart_arithmetic.py <p02_cart_arithmetic.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: p02_cart_arithmetic.zip <p02_cart_arithmetic.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
