1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
Decomposing and composing units
===============================
.. _decomposing:
Reducing a unit to its irreducible parts
----------------------------------------
A unit or quantity can be decomposed into its irreducible parts using
the `Unit.decompose <astropy.units.core.UnitBase.decompose>` or
`Quantity.decompose <astropy.units.quantity.Quantity.decompose>`
methods::
>>> from astropy import units as u
>>> u.Ry
Unit("Ry")
>>> u.Ry.decompose()
Unit("2.17987e-18 kg m2 / s2")
You can limit the selection of units that you want to decompose to
using the ``bases`` keyword argument::
>>> u.Ry.decompose(bases=[u.m, u.N])
Unit("2.17987e-18 m N")
This is also useful to decompose to a particular system. For example,
to decompose the Rydberg unit in terms of CGS units::
>>> u.Ry.decompose(bases=u.cgs.bases)
Unit("2.17987e-11 cm2 g / s2")
Finally, if you just want to know how a unit was defined::
>>> u.Ry.represents
Unit("13.6057 eV")
Automatically composing a unit into more complex units
------------------------------------------------------
Conversely, a unit may be recomposed back into more complex units
using the `~astropy.units.core.UnitBase.compose` method. Since there
may be multiple equally good results, a list is always returned::
>>> x = u.Ry.decompose()
>>> x.compose()
[Unit("Ry"),
Unit("2.17987e-18 J"),
Unit("2.17987e-11 erg"),
Unit("13.6057 eV")]
Some other interesting examples::
>>> (u.s ** -1).compose() # doctest: +SKIP
[Unit("Bq"), Unit("Hz"), Unit("3.7e+10 Ci")]
Composition can be combined with :ref:`unit_equivalencies`::
>>> (u.s ** -1).compose(equivalencies=u.spectral()) # doctest: +SKIP
[Unit("m"),
Unit("Hz"),
Unit("J"),
Unit("Bq"),
Unit("3.24078e-17 pc"),
Unit("1.057e-16 lyr"),
Unit("6.68459e-12 AU"),
Unit("1.4378e-09 solRad"),
Unit("0.01 k"),
Unit("100 cm"),
Unit("1e+06 micron"),
Unit("1e+07 erg"),
Unit("1e+10 Angstrom"),
Unit("3.7e+10 Ci"),
Unit("4.58743e+17 Ry"),
Unit("6.24151e+18 eV")]
Obviously a name doesn't exist for every arbitrary derived unit
imaginable. In that case, the system will do its best to reduce the
unit to the fewest possible symbols::
>>> (u.cd * u.sr * u.V * u.s).compose()
[Unit("lm Wb")]
Converting between systems
--------------------------
Built on top of this functionality is a convenience method to convert
between unit systems.
>>> u.Pa.to_system(u.cgs)
[Unit("10 P / s"), Unit("10 Ba")]
There is also a shorthand for this which only returns the first of
many possible matches::
>>> u.Pa.cgs
Unit("10 P / s")
This is equivalent to decomposing into the new system and then
composing into the most complex units possible, though
`~astropy.units.core.UnitBase.to_system` adds some extra logic to
return the results sorted in the most useful order::
>>> u.Pa.decompose(bases=u.cgs.bases)
Unit("10 g / (cm s2)")
>>> _.compose(units=u.cgs)
[Unit("10 Ba"), Unit("10 P / s")]
|