[5/6] let's remove multiple inheritance (RelMeths,ArithMeths: no more)
[sympy.git] / sympy / physics / units.py
blobb9d145e9573514372fef1d7fbea99f0f1ca6187c
1 """
2 Physical units and dimensions.
4 The base class is Unit, where all here defined units (~200) inherit from.
5 """
7 from sympy import Rational, pi
8 from sympy.core.basic import Basic, Atom
10 class Unit(Atom):
11 """
12 Base class for all physical units.
14 Create own units like:
15 m = Unit("meter", "m")
16 """
17 is_positive = True # make (m**2)**Rational(1,2) --> m
18 is_commutative = True
20 __slots__ = ["name", "abbrev"]
22 def __new__(cls, name, abbrev, **assumptions):
23 obj = Basic.__new__(cls, **assumptions)
24 assert isinstance(name, str),`type(name)`
25 assert isinstance(abbrev, str),`type(abbrev)`
26 obj.name = name
27 obj.abbrev = abbrev
28 return obj
30 def __eq__(self, other):
31 return isinstance(other, Unit) and self.name == other.name
33 def _hashable_content(self):
34 return (self.name,self.abbrev)
36 # Delete this so it doesn't pollute the namespace
37 del Atom
39 def defunit(value, *names):
40 u = value
41 g = globals()
42 for name in names:
43 g[name] = u
46 # Dimensionless
48 percent = percents = Rational(1,100)
49 permille = permille = Rational(1,1000)
51 ten = Rational(10)
53 yotta = ten**24
54 zetta = ten**21
55 exa = ten**18
56 peta = ten**15
57 tera = ten**12
58 giga = ten**9
59 mega = ten**6
60 kilo = ten**3
61 deca = ten**1
62 deci = ten**-1
63 centi = ten**-2
64 milli = ten**-3
65 micro = ten**-6
66 nano = ten**-9
67 pico = ten**-12
68 femto = ten**-15
69 atto = ten**-18
70 zepto = ten**-21
71 yocto = ten**-24
73 rad = radian = radians = 1
74 deg = degree = degrees = pi/180
77 # Base units
79 defunit(Unit('meter', 'm'), 'm', 'meter', 'meters')
80 defunit(Unit('kilogram', 'kg'), 'kg', 'kilogram', 'kilograms')
81 defunit(Unit('second', 's'), 's', 'second', 'seconds')
82 defunit(Unit('ampere', 'A'), 'A', 'ampere', 'amperes')
83 defunit(Unit('kelvin', 'K'), 'K', 'kelvin', 'kelvins')
84 defunit(Unit('mole', 'mol'), 'mol', 'mole', 'moles')
85 defunit(Unit('candela', 'cd'), 'cd', 'candela', 'candelas')
88 # Derived units
90 defunit(1/s, 'Hz', 'hz', 'hertz')
91 defunit(m*kg/s**2, 'N', 'newton', 'newtons')
92 defunit(N*m, 'J', 'joule', 'joules')
93 defunit(J/s, 'W', 'watt', 'watts')
94 defunit(N/m**2, 'Pa', 'pa', 'pascal', 'pascals')
95 defunit(s*A, 'C', 'coulomb', 'coulombs')
96 defunit(W/A, 'v', 'V', 'volt', 'volts')
97 defunit(V/A, 'ohm', 'ohms')
98 defunit(A/V, 'S', 'siemens', 'mho', 'mhos')
99 defunit(C/V, 'F', 'farad', 'farads')
100 defunit(J/A, 'Wb', 'wb', 'weber', 'webers')
101 defunit(V*s/m**2, 'T', 'tesla', 'teslas')
102 defunit(V*s/A, 'H', 'henry', 'henrys')
105 # Common length units
107 defunit(kilo*m, 'km', 'kilometer', 'kilometers')
108 defunit(deci*m, 'dm', 'decimeter', 'decimeters')
109 defunit(centi*m, 'cm', 'centimeter', 'centimeters')
110 defunit(milli*m, 'mm', 'millimeter', 'millimeters')
111 defunit(micro*m, 'um', 'micrometer', 'micrometers', 'micron', 'microns')
112 defunit(nano*m, 'nm', 'nanometer', 'nanometers')
113 defunit(pico*m, 'pm', 'picometer', 'picometers')
115 defunit(Rational('0.3048')*m, 'ft', 'foot', 'feet')
116 defunit(Rational('25.4')*mm, 'inch', 'inches')
117 defunit(3*ft, 'yd', 'yard', 'yards')
118 defunit(5280*ft, 'mi', 'mile', 'miles')
121 # Common volume and area units
123 defunit(m**3 / 1000, 'l', 'liter', 'liters')
124 defunit(deci*l, 'dl', 'deciliter', 'deciliters')
125 defunit(centi*l, 'cl', 'centiliter', 'centiliters')
126 defunit(milli*l, 'ml', 'milliliter', 'milliliters')
129 # Common time units
131 defunit(milli*s, 'ms', 'millisecond', 'milliseconds')
132 defunit(micro*s, 'us', 'microsecond', 'microseconds')
133 defunit(nano*s, 'ns', 'nanosecond', 'nanoseconds')
134 defunit(pico*s, 'ps', 'picosecond', 'picoseconds')
136 defunit(60*s, 'minute', 'minutes')
137 defunit(60*minute, 'h', 'hour', 'hours')
138 defunit(24*hour, 'day', 'days')
140 defunit(Rational('31558149.540')*s, 'sidereal_year', 'sidereal_years')
141 defunit(Rational('365.24219')*day, 'tropical_year', 'tropical_years')
142 defunit(Rational('365')*day, 'common_year', 'common_years')
143 defunit(Rational('365.25')*day, 'julian_year', 'julian_years')
145 year = years = tropical_year
148 # Common mass units
150 defunit(kilogram / kilo, 'g', 'gram', 'grams')
151 defunit(milli * g, 'mg', 'milligram', 'milligrams')
152 defunit(micro * g, 'ug', 'microgram', 'micrograms')
156 #----------------------------------------------------------------------------
157 # Physical constants
160 c = speed_of_light = 299792458 * m/s
161 G = gravitational_constant = Rational('6.67428') * ten**-11 * m**3 / kg / s**2
162 u0 = magnetic_constant = 4*pi * ten**-7 * N/A**2
163 e0 = electric_constant = 1/(u0 * c**2)
164 Z0 = vacuum_impedance = u0 * c
166 planck = Rational('6.2606896') * ten**-34 * J*s
167 hbar = planck / (2*pi)
169 avogadro = (Rational('6.02214179') * 10**23) / mol
170 boltzmann = Rational('1.3806505') * ten**-23 * J / K
172 gee = gees = Rational('9.80665') * m/s**2
173 atmosphere = atmospheres = atm = 101325 * pascal
176 # Other convenient units and magnitudes
178 defunit(c*julian_year, 'ly', 'lightyear', 'lightyears')
179 defunit(149597870691*m, 'au', 'astronomical_unit', 'astronomical_units')
181 # Delete this so it doesn't pollute the namespace
182 del Rational, pi