pynbody.units.Unit#
- class pynbody.units.Unit(s)[source]#
Bases:
UnitBaseClass factory for units.
Methods
copy()Shortcut for
copy.copy(unit)using python's copy moduledimensional_project(basis_units)Work out how to express the dimensions of this unit relative to the specified list of basis units.
Returns the dimensionality of the Unit object.
dimensionless_constant(**substitutions)If this unit is dimensionless, return its scalar quantity.
in_units(*a, **kw)Alias for ratio
irrep()Return a unit equivalent to this one in terms of the currently defined :class:`IrreducibleUnit`s
Returns True if the unit is dimensionless, False otherwise.
latex()Returns a LaTeX representation of this unit.
ratio(other, **substitutions)Get the conversion ratio between this Unit and another specified unit.
simplify()Return a simplified version of the unit.
- __init__(s)[source]#
Given a string s, creates an appropriate Unit object.
The string format is
[<scale>] [<unit_name>][**<rational_power>] [[<unit_name>] ... ]Examples:
1.e30 kg,kpc**2,26.2 m s**-1,15 m**1/2 s**-1/2.Note that the actual class returned depends on the string provided. Generally you should not need to worry about this, as the class returned will be a subclass of
UnitBaseand will behave as expected.
- copy()#
Shortcut for
copy.copy(unit)using python’s copy module
- dimensional_project(basis_units)#
Work out how to express the dimensions of this unit relative to the specified list of basis units.
This is used by the framework when making inferences about sensible units to use in various situations.
For example, you can represent a length as an energy divided by a force:
>>> Unit("23 kpc").dimensional_project(["J", "N"]) array([1, -1], dtype=object)
However it’s not possible to represent a length by energy alone:
>>> Unit("23 kpc").dimensional_project(["J"]) UnitsException: Basis units do not span dimensions of specified unit
This function also doesn’t know what to do if the result is ambiguous:
>>> Unit("23 kpc").dimensional_project(["J", "N", "kpc"]) UnitsException: Basis units are not linearly independent
- dimensionality_as_string()#
Returns the dimensionality of the Unit object.
Example
> pynbody.units.Unit(“3e8 m s**-1 yr”).dimensionality_as_string() ‘m^1’
- abstract dimensionless_constant(**substitutions)#
If this unit is dimensionless, return its scalar quantity.
Direct use of this function is not recommended. It is generally better to use the ratio function instead.
Provide keyword arguments to set values for named IrreducibleUnits – see the
ratio()function for more information.
- in_units(*a, **kw)#
Alias for ratio
- abstract irrep()#
Return a unit equivalent to this one in terms of the currently defined :class:`IrreducibleUnit`s
Two units are equivalent if they have the same
irrep.
- is_dimensionless()#
Returns True if the unit is dimensionless, False otherwise.
- abstract latex()#
Returns a LaTeX representation of this unit.
Prefactors are converted into exponent notation. Named units by default are represented by the string ‘mathrm{unit_name}’, although this can be overriden in the pynbody configuration files or by setting unit_name._latex.
- ratio(other, **substitutions)#
Get the conversion ratio between this Unit and another specified unit.
- Parameters:
other (Unit or str) – The other unit to convert to.
substitutions (dict) – Substitutions to make for units in the conversion. For example, if the unit is a comoving distance, you can specify the scale factor a. More generally, you can specify a valuable for any IrreducibleUnit.
Notes
Usage examples:
>>> Unit("1 Mpc a").ratio("kpc", a=0.25) 250.0 >>> Unit("1 Mpc").ratio("Msol") UnitsException: not convertible >>> Unit("1 Mpc").ratio("Msol", kg=25.0, m=50.0) 3.1028701506345152e-08
- simplify()#
Return a simplified version of the unit.