unyt.unit_object module¶
A class that represents a unit symbol.
-
class
unyt.unit_object.
Unit
[source]¶ Bases:
object
A symbolic unit, using sympy functionality. We only add “dimensions” so that sympy understands relations between different units.
-
is_positive
= True¶
-
is_commutative
= True¶
-
is_number
= False¶
-
latex_repr
¶ A LaTeX representation for the unit
Examples
>>> from unyt import g, cm >>> (g/cm**3).units.latex_repr '\\frac{\\rm{g}}{\\rm{cm}^{3}}'
-
units
¶
-
same_dimensions_as
(other_unit)[source]¶ Test if the dimensions of other_unit are the same as this unit
Examples
>>> from unyt import Msun, kg, mile >>> Msun.units.same_dimensions_as(kg.units) True >>> Msun.units.same_dimensions_as(mile.units) False
-
is_dimensionless
¶ Is this a dimensionless unit?
Returns: Return type: True for a dimensionless unit, False otherwise Examples
>>> from unyt import count, kg >>> count.units.is_dimensionless True >>> kg.units.is_dimensionless False
-
is_code_unit
¶ Is this a “code” unit?
Returns: - True if the unit consists of atom units that being with “code”.
- False otherwise
-
list_equivalencies
()[source]¶ Lists the possible equivalencies associated with this unit object
Examples
>>> from unyt import km >>> km.units.list_equivalencies() spectral: length <-> spatial_frequency <-> frequency <-> energy schwarzschild: mass <-> length compton: mass <-> length
-
has_equivalent
(equiv)[source]¶ Check to see if this unit object as an equivalent unit in equiv.
Example
>>> from unyt import km >>> km.has_equivalent('spectral') True >>> km.has_equivalent('mass_energy') False
-
get_base_equivalent
(unit_system=None)[source]¶ Create and return dimensionally-equivalent units in a specified base.
>>> from unyt import g, cm >>> (g/cm**3).get_base_equivalent('mks') kg/m**3 >>> (g/cm**3).get_base_equivalent('solar') Mearth/AU**3
-
get_cgs_equivalent
()[source]¶ Create and return dimensionally-equivalent cgs units.
Example
>>> from unyt import kg, m >>> (kg/m**3).get_cgs_equivalent() g/cm**3
-
get_mks_equivalent
()[source]¶ Create and return dimensionally-equivalent mks units.
Example
>>> from unyt import g, cm >>> (g/cm**3).get_mks_equivalent() kg/m**3
-
get_conversion_factor
(other_units, dtype=None)[source]¶ Get the conversion factor and offset (if any) from one unit to another
Parameters: - other_units (unit object) – The units we want the conversion factor for
- dtype (numpy dtype) – The dtype to return the conversion factor as
Returns: - conversion_factor (float) – old_units / new_units
- offset (float or None) – Offset between this unit and the other unit. None if there is no offset.
Examples
>>> from unyt import km, cm, degree_fahrenheit, degree_celsius >>> km.get_conversion_factor(cm) (100000.0, None) >>> degree_celsius.get_conversion_factor(degree_fahrenheit) (1.7999999999999998, -31.999999999999886)
-
latex_representation
()[source]¶ A LaTeX representation for the unit
Examples
>>> from unyt import g, cm >>> (g/cm**3).latex_representation() '\\frac{\\rm{g}}{\\rm{cm}^{3}}'
-
as_coeff_unit
()[source]¶ Factor the coefficient multiplying a unit
For units that are multiplied by a constant dimensionless coefficient, returns a tuple containing the coefficient and a new unit object for the unmultiplied unit.
Example
>>> import unyt as u >>> unit = (u.m**2/u.cm).simplify() >>> unit 100*m >>> unit.as_coeff_unit() (100.0, m)
-
simplify
()[source]¶ Return a new equivalent unit object with a simplified unit expression
>>> import unyt as u >>> unit = (u.m**2/u.cm).simplify() >>> unit 100*m
-
base_offset
¶
-
base_value
¶
-
dimensions
¶
-
expr
¶
-
is_Unit
¶
-
is_atomic
¶
-
registry
¶
-
-
unyt.unit_object.
define_unit
(symbol, value, tex_repr=None, offset=None, prefixable=False, registry=None)[source]¶ Define a new unit and add it to the specified unit registry.
Parameters: - symbol (string) – The symbol for the new unit.
- value (tuple or
unyt.array.unyt_quantity
) – The definition of the new unit in terms of some other units. For example, one would define a new “mph” unit with(1.0, "mile/hr")
or with1.0*unyt.mile/unyt.hr
- tex_repr (string, optional) – The LaTeX representation of the new unit. If one is not supplied, it will be generated automatically based on the symbol string.
- offset (float, optional) – The default offset for the unit. If not set, an offset of 0 is assumed.
- prefixable (boolean, optional) – Whether or not the new unit can use SI prefixes. Default: False
- registry (
unyt.unit_registry.UnitRegistry
or None) – The unit registry to add the unit to. If None, then defaults to the global default unit registry. If registry is set to None then the unit object will be added as an attribute to the top-levelunyt
namespace to ease working with the newly defined unit. See the example below.
Examples
>>> from unyt import day >>> two_weeks = 14.0*day >>> one_day = 1.0*day >>> define_unit("two_weeks", two_weeks) >>> from unyt import two_weeks >>> print((3*two_weeks)/one_day) 42.0 dimensionless