Some physical quantities are directly related to other unitful quantities by a constant, but otherwise do not have the same units. To facilitate conversions between these quantities, `yt`

implements a system of unit equivalencies (inspired by the AstroPy implementation). The possible unit equivalencies are:

`"thermal"`

: conversions between temperature and energy ($E = k_BT$)`"spectral"`

: conversions between wavelength, frequency, and energy for photons ($E = h\nu = hc/\lambda, c = \lambda\nu$)`"mass_energy"`

: conversions between mass and energy ($E = mc^2$)`"lorentz"`

: conversions between velocity and Lorentz factor ($\gamma = 1/\sqrt{1-(v/c)^2}$)`"schwarzschild"`

: conversions between mass and Schwarzschild radius ($R_S = 2GM/c^2$)`"compton"`

: conversions between mass and Compton wavelength ($\lambda = h/mc$)

The following unit equivalencies only apply under conditions applicable for an ideal gas with a constant mean molecular weight $\mu$ and ratio of specific heats $\gamma$:

`"number_density"`

: conversions between density and number density ($n = \rho/\mu{m_p}$)`"sound_speed"`

: conversions between temperature and sound speed for an ideal gas ($c_s^2 = \gamma{k_BT}/\mu{m_p}$)

A `YTArray`

or `YTQuantity`

can be converted to an equivalent using `to_equivalent`

, where the unit and the equivalence name are provided as arguments:

In [1]:

```
import yt
from yt import YTQuantity
import numpy as np
ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')
dd = ds.all_data()
print (dd["temperature"].to_equivalent("erg", "thermal"))
print (dd["temperature"].to_equivalent("eV", "thermal"))
# Rest energy of the proton
from yt.units import mp
E_p = mp.to_equivalent("GeV", "mass_energy")
print (E_p)
```

In [2]:

```
from yt.units import clight
v = 0.1*clight
g = v.to_equivalent("dimensionless", "lorentz")
print (g)
print (g.to_equivalent("c", "lorentz"))
```

`"number_density"`

equivalence can take a custom mean molecular weight (default is $\mu = 0.6$):

In [3]:

```
print (dd["density"].max())
print (dd["density"].to_equivalent("cm**-3", "number_density").max())
print (dd["density"].to_equivalent("cm**-3", "number_density", mu=0.75).max())
```

`"sound_speed"`

equivalence optionally takes the ratio of specific heats $\gamma$ and the mean molecular weight $\mu$ (defaults are $\gamma$ = 5/3, $\mu = 0.6$):

In [4]:

```
print (dd["temperature"].to_equivalent("km/s", "sound_speed").mean())
print (dd["temperature"].to_equivalent("km/s", "sound_speed", gamma=4./3., mu=0.5).mean())
```

In [5]:

```
Q1 = YTQuantity(1.0,"C")
Q2 = YTQuantity(1.0,"esu")
print ("Q1 dims =", Q1.units.dimensions)
print ("Q2 dims =", Q2.units.dimensions)
print ("Q1 base units =", Q1.in_mks())
print ("Q2 base units =", Q2.in_cgs())
```

To convert from a cgs unit to an SI unit, use the "SI" equivalency:

In [6]:

```
from yt.units import qp # the elementary charge in esu
qp_SI = qp.to_equivalent("C","SI") # convert to Coulombs
print (qp)
print (qp_SI)
```

To convert from an SI unit to a cgs unit, use the "CGS" equivalency:

In [7]:

```
B = YTQuantity(1.0,"T") # magnetic field in Tesla
print (B, B.to_equivalent("gauss","CGS")) # convert to Gauss
```

In [8]:

```
I = YTQuantity(1.0,"A")
I_cgs = I.to_equivalent("statA","CGS")
R = YTQuantity(1.0,"ohm")
R_cgs = R.to_equivalent("statohm","CGS")
P = I**2*R
P_cgs = I_cgs**2*R_cgs
```

In [9]:

```
print (P_cgs.units.dimensions == P.units.dimensions)
print (P.in_units("W"), P_cgs.in_units("W"))
```

If a certain equivalence does not exist for a particular unit, then an error will be thrown:

In [10]:

```
from yt.utilities.exceptions import YTInvalidUnitEquivalence
try:
x = v.to_equivalent("angstrom", "spectral")
except YTInvalidUnitEquivalence as e:
print (e)
```

You can check if a `YTArray`

has a given equivalence with `has_equivalent`

:

In [11]:

```
print (mp.has_equivalent("compton"))
print (mp.has_equivalent("thermal"))
```

`YTArray`

or `YTQuantity`

, use the `list_equivalencies`

method:

In [12]:

```
E_p.list_equivalencies()
```

(6)_Unit_Equivalencies.ipynb; 6)_Unit_Equivalencies_evaluated.ipynb; 6)_Unit_Equivalencies.py)