.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/00-pyunits/unit_aliases_example.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_examples_00-pyunits_unit_aliases_example.py: .. _ref_unit_aliases: Unit aliases ------------ PyAnsys Units supports unit aliases so you can use shorthand or alternative names for units. For example, ``deg`` for ``degree`` or ``meter`` for ``m``. This example shows you how to: - Use built-in aliases in unit and quantity creation. - Use aliases inside compound unit strings. - Register custom aliases programmatically. - Perform conversions with aliased units. .. GENERATED FROM PYTHON SOURCE LINES 40-42 Perform required imports ~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: Python from ansys.units import Quantity, Unit, UnitRegistry .. GENERATED FROM PYTHON SOURCE LINES 45-50 Built-in aliases ~~~~~~~~~~~~~~~~ Several common aliases are shipped out of the box. Use ``deg`` instead of ``degree``, ``rad`` instead of ``radian``, ``meter`` instead of ``m``, and so on. .. GENERATED FROM PYTHON SOURCE LINES 50-60 .. code-block:: Python angle = Unit("deg") print(f"Unit('deg') -> name={angle.name!r}, sf={angle.si_scaling_factor}") length = Unit("meter") print(f"Unit('meter') -> name={length.name!r}") force = Unit("newton") print(f"Unit('newton') -> name={force.name!r}") .. rst-class:: sphx-glr-script-out .. code-block:: none Unit('deg') -> name='degree', sf=0.017453292519943295 Unit('meter') -> name='m' Unit('newton') -> name='N' .. GENERATED FROM PYTHON SOURCE LINES 61-64 Aliases in quantities ~~~~~~~~~~~~~~~~~~~~~ Aliases work seamlessly when creating ``Quantity`` objects. .. GENERATED FROM PYTHON SOURCE LINES 64-71 .. code-block:: Python q_angle = Quantity(90, "deg") print(f"Quantity(90, 'deg') = {q_angle}") q_len = Quantity(5.0, "meter") print(f"Quantity(5.0, 'meter') = {q_len}") .. rst-class:: sphx-glr-script-out .. code-block:: none Quantity(90, 'deg') = 90.0 degree Quantity(5.0, 'meter') = 5.0 m .. GENERATED FROM PYTHON SOURCE LINES 72-75 Aliases in compound units ~~~~~~~~~~~~~~~~~~~~~~~~~ Aliases can be mixed with canonical names inside compound unit strings. .. GENERATED FROM PYTHON SOURCE LINES 75-82 .. code-block:: Python angular_velocity = Unit("deg sec^-1") print(f"Unit('deg sec^-1') -> name={angular_velocity.name!r}") acceleration = Unit("meter sec^-2") print(f"Unit('meter sec^-2') -> name={acceleration.name!r}") .. rst-class:: sphx-glr-script-out .. code-block:: none Unit('deg sec^-1') -> name='degree s^-1' Unit('meter sec^-2') -> name='m s^-2' .. GENERATED FROM PYTHON SOURCE LINES 83-86 Conversions with aliases ~~~~~~~~~~~~~~~~~~~~~~~~ Unit conversions work normally when aliases are used. .. GENERATED FROM PYTHON SOURCE LINES 86-95 .. code-block:: Python q_deg = Quantity(180, "deg") q_rad = q_deg.to("radian") print(f"180 deg = {q_rad.value:.6f} radian") q_m = Quantity(1, "meter") q_ft = q_m.to("ft") print(f"1 meter = {q_ft.value:.6f} ft") .. rst-class:: sphx-glr-script-out .. code-block:: none 180 deg = 3.141593 radian 1 meter = 3.280840 ft .. GENERATED FROM PYTHON SOURCE LINES 96-99 Register a custom alias ~~~~~~~~~~~~~~~~~~~~~~~ You can register your own aliases at runtime via ``UnitRegistry``. .. GENERATED FROM PYTHON SOURCE LINES 99-106 .. code-block:: Python ureg = UnitRegistry() ureg.register_alias("angular_deg", "degree") custom = Unit("angular_deg") print(f"Unit('angular_deg') -> name={custom.name!r}") .. rst-class:: sphx-glr-script-out .. code-block:: none Unit('angular_deg') -> name='degree' .. GENERATED FROM PYTHON SOURCE LINES 107-110 Alias equality ~~~~~~~~~~~~~~ A unit created from an alias is fully equal to the canonical unit. .. GENERATED FROM PYTHON SOURCE LINES 110-115 .. code-block:: Python assert Unit("deg") == Unit("degree") assert Unit("rad") == Unit("radian") assert Unit("meter") == Unit("m") print("All alias-based units equal their canonical counterparts.") .. rst-class:: sphx-glr-script-out .. code-block:: none All alias-based units equal their canonical counterparts. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.021 seconds) .. _sphx_glr_download_examples_00-pyunits_unit_aliases_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: unit_aliases_example.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: unit_aliases_example.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: unit_aliases_example.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_