Physical quantities/constants/units¶
-
class
astrophysix.units.unit.
Unit
(name='', base_unit=None, coeff=1.0, dims=None, descr=None, latex=None)¶ Dimensional physical unit class
- Parameters
name (
string
) – Unit namebase_unit (
Unit
instance) – Composite unit from which this instance should be initialisedcoeff (
float
) – dimensionless value of the unit instance.dims (8-
tuple
ofint
) – dimension of the unit object expressed in the international unit system (kg
,m
,s
,K
,A
,mol
,rad
,cd
)descr (
string
or None) – Unit descriptionlatex (
string
or None) – Unit displayed name (latex format)
Examples
>>> cs_m_s = Unit(name="cs", coeff=340.0, dims=(0, 1, -1, 0, 0, 0, 0, 0), descr="sound speed unit") >>> print("sound speed = {v:g} m/h".format(v=cs_m_s.express(km/hour))) sound speed = 1224 km/h >>> >>> dens = Unit(name="Msun/kpc^3", base_unit=Msun/kpc**3, descr="Solar mass per cubic kiloparsec", latex="{u1:s}.{u2:s}^{{-3}}".format(u1=Msun.latex, u2=kpc.latex)) >>> print(dens) (6.76957356533e-29 m^-3.kg)
-
UNKNOWN_PHYSICAL_TYPE
= 'unknown'¶
-
__eq__
(other)¶ Checks Unit instance equality
- Parameters
other (
Unit
) – other unit instance to compare to- Returns
e – True if
Unit.coeff
andUnit.dimensions
are identical, otherwise False.- Return type
bool
-
appropriate_unit
(nearest_log10=1.0)¶ Try to find the better suited unit (among available equivalent units to represent this unit).
- Parameters
nearest_log10 (
float
) – log of the nearest value to round to. Default 1.0.
Example
>>> u = 2426.2 * U.ly >>> bv, bu = u.appropriate_unit() >>> print("Appropriate unit : 2426.2 ly = {v:g} {bu:s}".format(v=bv, bu=bu.name)) Appropriate unit : 2426.2 ly = 0.743876 kpc
-
property
coeff
¶ Constant value of this unit
-
classmethod
create_unit
(name='', base_unit=None, coeff=1.0, dims=None, descr=None, latex=None)¶ Add a new Unit instance to the registry
- Parameters
name (
string
) – Unit namebase_unit (
Unit
instance) – Composite unit from which this instance should be initialisedcoeff (
float
) – dimensionless value of the unit instance.dims (8-
tuple
ofint
) – dimension of the unit object expressed in the international unit system (kg
,m
,s
,K
,A
,mol
,rad
,cd
)descr (
string
or None) – Unit descriptionlatex (
string
or None) – Unit displayed name (latex format)
- Raises
ValueError – If the provided name already corresponds to a unit in the registry.
-
property
description
¶ Unit description
-
property
dimensions
¶ Unit dimension array
-
equivalent_unit_list
()¶ Get the equivalent unit list (with same physical type)
Example
>>> print(U.kg.equivalent_unit_list()) [g : (0.001 kg), t : (1000 kg), mH : (1.66e-27 kg), Msun : (1.9889e+30 kg), Mearth : (5.9722e+24 kg)]
-
express
(unit)¶ Unit conversion method. Gives the conversion factor of this
Unit
expressed into another (dimension-compatible) givenUnit
.Checks that :
the unit param. is also a
Unit
instancethe unit param. is dimension-compatible.
- Parameters
unit (
Unit
) – unit in which the conversion is made- Returns
fact – conversion factor of this unit expressed in unit
- Return type
float
Examples
Conversion of a kpc expressed in light-years :
>>> factor = kpc.express(ly) >>> print("1 kpc = {fact:f} ly".format(fact=factor)) 1 kpc = 3261.563777 ly
Conversion of \(1 M_{\odot}\) into kpc/Myr :
>>> print(Msun.express(kpc/Myr)) UnitError: Incompatible dimensions between : - Msun : (1.9889e+30 kg) (type: mass) and - (977792 m.s^-1) (type: velocity)
-
classmethod
from_name
(unit_name)¶ Get a
Unit
from its name in theastrophysix
unit registry.- Parameters
unit_name (
string
) – name of the unit to search.- Raises
AttributeError – if unit_name attribute does not correspond to any unit in the
astrophysix
unit registry.
-
identical
(other_unit)¶ Strict unit instance comparison method
- Parameters
other_unit (
Unit
) – other unit to compare to.- Returns
e – True only if other_unit is equals to self AND has identical name/description/LaTex formula. Otherwise returns False.
- Return type
bool
-
info
()¶ Print information about this unit. If any, print the name and description of this unit, then print the value of this unit and the list of equivalent unit contained in the built-in unit registry associated with their conversion factor.
Example
>>> U.kpc.info() Unit : kpc ---------- Kiloparsec Value ----- 3.0856775814671917e+19 m Equivalent units ---------------- * m : 3.24078e-20 kpc * um : 3.24078e-26 kpc * mm : 3.24078e-23 kpc * cm : 3.24078e-22 kpc * nm : 3.24078e-29 kpc * km : 3.24078e-17 kpc * Angstrom : 3.24078e-30 kpc * au : 4.84814e-09 kpc * pc : 0.001 kpc * Mpc : 1000 kpc * Gpc : 1e+06 kpc * Rsun : 2.25399e-11 kpc * ly : 0.000306601 kpc
-
is_base_unit
()¶ Checks whether the Unit is a base SI Unit (kg, m, s, K, A, mol, rad, cd).
- Returns
b – True only if unit is a base SI unit(kg, m, s, K, A, mol, rad, cd). Otherwise returns False.
- Return type
bool
-
classmethod
iterate_units
(phys_type=None)¶ Unit iterator method. Iterates over all units in the
astrophysix
unit registry.- Parameters
phys_type (
string
) – Name of the physical quantity type of the units to iterate over. Default None (all physical quantities).- Yields
u (
Unit
) – unit of the required physical quantity type, if any given.
-
property
latex
¶ Unit displayed name (LaTex format)
-
property
name
¶ Unit name
-
property
physical_type
¶ Get the unit physical type (dimensioned physical quantity).
- Returns
t – The name of the physical quantity, or
Unit.UNKNOWN_PHYSICAL_TYPE
if the physical quantity is unknown.- Return type
string