# Source code for sympy.physics.units

"""
Physical units and dimensions.

The base class is Unit, where all here defined units (~200) inherit from.

The find_unit function can help you find units for a given quantity:

>>> import sympy.physics.units as u
>>> u.find_unit('coul')
['coulomb', 'coulombs']
>>> u.find_unit(u.charge)
['C', 'charge', 'coulomb', 'coulombs']
>>> u.coulomb
A*s

Units are always given in terms of base units that have a name and
an abbreviation:

>>> u.A.name
'ampere'
>>> u.ampere.abbrev
'A'

The generic name for a unit (like 'length', 'mass', etc...)

>>> u.find_unit('magnet')
['magnetic_flux', 'magnetic_constant', 'magnetic_flux_density']
>>> u.find_unit(u.magnetic_flux)
['Wb', 'wb', 'weber', 'webers', 'magnetic_flux']

If, for a given session, you wish to add a unit you may do so:

>>> u.find_unit('gal')
[]
>>> u.gal = 4*u.quart
>>> u.gal/u.inch**3
231

To see a given quantity in terms of some other unit, divide by the desired
unit:

>>> mph = u.miles/u.hours
>>> (u.m/u.s/mph).n(2)
2.2

The units are defined in terms of base units, so when you divide similar
units you will obtain a pure number. This means, for example, that if you
divide a real-world mass (like grams) by the atomic mass unit (amu) you
should divide by the unit avogadro:

>>> u.grams/u.amu
602214179000000000000000
mol

For chemical calculations the unit mmu (molar mass unit) has been
defined so this conversion is handled automatically. For example, the
number of moles in 1 kg of water might be calculated as:

>>> u.kg/(18*u.mmu).n(3)
55.5*mol

If you need the number of atoms in a mol as a pure number you can use
avogadro_number but if you need it as a dimensional quantity you should use
avogadro_constant. (avogadro is a shorthand for the dimensional
quantity.)

602214179000000000000000
[docs]def find_unit(quantity): """ Return a list of matching units names. if quantity is a string -- units containing the string quantity if quantity is a unit -- units having matching base units Examples ======== >>> from sympy.physics import units as u >>> u.find_unit('charge') ['charge'] >>> u.find_unit(u.charge) ['C', 'charge', 'coulomb', 'coulombs'] >>> u.find_unit('volt') ['volt', 'volts', 'voltage'] >>> u.find_unit(u.inch**3)[:5] ['l', 'cl', 'dl', 'ml', 'liter'] """ import sympy.physics.units as u rv = [] if isinstance(quantity, str): rv = [i for i in dir(u) if quantity in i] else: units = quantity.as_coeff_Mul()[1] for i in dir(u): try: if units == eval('u.' + i).as_coeff_Mul()[1]: rv.append(str(i)) except Exception: pass return sorted(rv, key=len) # Delete this so it doesn't pollute the namespace