Coverage for /usr/lib/python3/dist-packages/sympy/physics/units/__init__.py: 42%
36 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1# isort:skip_file
2"""
3Dimensional analysis and unit systems.
5This module defines dimension/unit systems and physical quantities. It is
6based on a group-theoretical construction where dimensions are represented as
7vectors (coefficients being the exponents), and units are defined as a dimension
8to which we added a scale.
10Quantities are built from a factor and a unit, and are the basic objects that
11one will use when doing computations.
13All objects except systems and prefixes can be used in SymPy expressions.
14Note that as part of a CAS, various objects do not combine automatically
15under operations.
17Details about the implementation can be found in the documentation, and we
18will not repeat all the explanations we gave there concerning our approach.
19Ideas about future developments can be found on the `Github wiki
20<https://github.com/sympy/sympy/wiki/Unit-systems>`_, and you should consult
21this page if you are willing to help.
23Useful functions:
25- ``find_unit``: easily lookup pre-defined units.
26- ``convert_to(expr, newunit)``: converts an expression into the same
27 expression expressed in another unit.
29"""
31from .dimensions import Dimension, DimensionSystem
32from .unitsystem import UnitSystem
33from .util import convert_to
34from .quantities import Quantity
36from .definitions.dimension_definitions import (
37 amount_of_substance, acceleration, action, area,
38 capacitance, charge, conductance, current, energy,
39 force, frequency, impedance, inductance, length,
40 luminous_intensity, magnetic_density,
41 magnetic_flux, mass, momentum, power, pressure, temperature, time,
42 velocity, voltage, volume
43)
45Unit = Quantity
47speed = velocity
48luminosity = luminous_intensity
49magnetic_flux_density = magnetic_density
50amount = amount_of_substance
52from .prefixes import (
53 # 10-power based:
54 yotta,
55 zetta,
56 exa,
57 peta,
58 tera,
59 giga,
60 mega,
61 kilo,
62 hecto,
63 deca,
64 deci,
65 centi,
66 milli,
67 micro,
68 nano,
69 pico,
70 femto,
71 atto,
72 zepto,
73 yocto,
74 # 2-power based:
75 kibi,
76 mebi,
77 gibi,
78 tebi,
79 pebi,
80 exbi,
81)
83from .definitions import (
84 percent, percents,
85 permille,
86 rad, radian, radians,
87 deg, degree, degrees,
88 sr, steradian, steradians,
89 mil, angular_mil, angular_mils,
90 m, meter, meters,
91 kg, kilogram, kilograms,
92 s, second, seconds,
93 A, ampere, amperes,
94 K, kelvin, kelvins,
95 mol, mole, moles,
96 cd, candela, candelas,
97 g, gram, grams,
98 mg, milligram, milligrams,
99 ug, microgram, micrograms,
100 t, tonne, metric_ton,
101 newton, newtons, N,
102 joule, joules, J,
103 watt, watts, W,
104 pascal, pascals, Pa, pa,
105 hertz, hz, Hz,
106 coulomb, coulombs, C,
107 volt, volts, v, V,
108 ohm, ohms,
109 siemens, S, mho, mhos,
110 farad, farads, F,
111 henry, henrys, H,
112 tesla, teslas, T,
113 weber, webers, Wb, wb,
114 optical_power, dioptre, D,
115 lux, lx,
116 katal, kat,
117 gray, Gy,
118 becquerel, Bq,
119 km, kilometer, kilometers,
120 dm, decimeter, decimeters,
121 cm, centimeter, centimeters,
122 mm, millimeter, millimeters,
123 um, micrometer, micrometers, micron, microns,
124 nm, nanometer, nanometers,
125 pm, picometer, picometers,
126 ft, foot, feet,
127 inch, inches,
128 yd, yard, yards,
129 mi, mile, miles,
130 nmi, nautical_mile, nautical_miles,
131 angstrom, angstroms,
132 ha, hectare,
133 l, L, liter, liters,
134 dl, dL, deciliter, deciliters,
135 cl, cL, centiliter, centiliters,
136 ml, mL, milliliter, milliliters,
137 ms, millisecond, milliseconds,
138 us, microsecond, microseconds,
139 ns, nanosecond, nanoseconds,
140 ps, picosecond, picoseconds,
141 minute, minutes,
142 h, hour, hours,
143 day, days,
144 anomalistic_year, anomalistic_years,
145 sidereal_year, sidereal_years,
146 tropical_year, tropical_years,
147 common_year, common_years,
148 julian_year, julian_years,
149 draconic_year, draconic_years,
150 gaussian_year, gaussian_years,
151 full_moon_cycle, full_moon_cycles,
152 year, years,
153 G, gravitational_constant,
154 c, speed_of_light,
155 elementary_charge,
156 hbar,
157 planck,
158 eV, electronvolt, electronvolts,
159 avogadro_number,
160 avogadro, avogadro_constant,
161 boltzmann, boltzmann_constant,
162 stefan, stefan_boltzmann_constant,
163 R, molar_gas_constant,
164 faraday_constant,
165 josephson_constant,
166 von_klitzing_constant,
167 Da, dalton, amu, amus, atomic_mass_unit, atomic_mass_constant,
168 me, electron_rest_mass,
169 gee, gees, acceleration_due_to_gravity,
170 u0, magnetic_constant, vacuum_permeability,
171 e0, electric_constant, vacuum_permittivity,
172 Z0, vacuum_impedance,
173 coulomb_constant, electric_force_constant,
174 atmosphere, atmospheres, atm,
175 kPa,
176 bar, bars,
177 pound, pounds,
178 psi,
179 dHg0,
180 mmHg, torr,
181 mmu, mmus, milli_mass_unit,
182 quart, quarts,
183 ly, lightyear, lightyears,
184 au, astronomical_unit, astronomical_units,
185 planck_mass,
186 planck_time,
187 planck_temperature,
188 planck_length,
189 planck_charge,
190 planck_area,
191 planck_volume,
192 planck_momentum,
193 planck_energy,
194 planck_force,
195 planck_power,
196 planck_density,
197 planck_energy_density,
198 planck_intensity,
199 planck_angular_frequency,
200 planck_pressure,
201 planck_current,
202 planck_voltage,
203 planck_impedance,
204 planck_acceleration,
205 bit, bits,
206 byte,
207 kibibyte, kibibytes,
208 mebibyte, mebibytes,
209 gibibyte, gibibytes,
210 tebibyte, tebibytes,
211 pebibyte, pebibytes,
212 exbibyte, exbibytes,
213)
215from .systems import (
216 mks, mksa, si
217)
220def find_unit(quantity, unit_system="SI"):
221 """
222 Return a list of matching units or dimension names.
224 - If ``quantity`` is a string -- units/dimensions containing the string
225 `quantity`.
226 - If ``quantity`` is a unit or dimension -- units having matching base
227 units or dimensions.
229 Examples
230 ========
232 >>> from sympy.physics import units as u
233 >>> u.find_unit('charge')
234 ['C', 'coulomb', 'coulombs', 'planck_charge', 'elementary_charge']
235 >>> u.find_unit(u.charge)
236 ['C', 'coulomb', 'coulombs', 'planck_charge', 'elementary_charge']
237 >>> u.find_unit("ampere")
238 ['ampere', 'amperes']
239 >>> u.find_unit('angstrom')
240 ['angstrom', 'angstroms']
241 >>> u.find_unit('volt')
242 ['volt', 'volts', 'electronvolt', 'electronvolts', 'planck_voltage']
243 >>> u.find_unit(u.inch**3)[:9]
244 ['L', 'l', 'cL', 'cl', 'dL', 'dl', 'mL', 'ml', 'liter']
245 """
246 unit_system = UnitSystem.get_unit_system(unit_system)
248 import sympy.physics.units as u
249 rv = []
250 if isinstance(quantity, str):
251 rv = [i for i in dir(u) if quantity in i and isinstance(getattr(u, i), Quantity)]
252 dim = getattr(u, quantity)
253 if isinstance(dim, Dimension):
254 rv.extend(find_unit(dim))
255 else:
256 for i in sorted(dir(u)):
257 other = getattr(u, i)
258 if not isinstance(other, Quantity):
259 continue
260 if isinstance(quantity, Quantity):
261 if quantity.dimension == other.dimension:
262 rv.append(str(i))
263 elif isinstance(quantity, Dimension):
264 if other.dimension == quantity:
265 rv.append(str(i))
266 elif other.dimension == Dimension(unit_system.get_dimensional_expr(quantity)):
267 rv.append(str(i))
268 return sorted(set(rv), key=lambda x: (len(x), x))
270# NOTE: the old units module had additional variables:
271# 'density', 'illuminance', 'resistance'.
272# They were not dimensions, but units (old Unit class).
274__all__ = [
275 'Dimension', 'DimensionSystem',
276 'UnitSystem',
277 'convert_to',
278 'Quantity',
280 'amount_of_substance', 'acceleration', 'action', 'area',
281 'capacitance', 'charge', 'conductance', 'current', 'energy',
282 'force', 'frequency', 'impedance', 'inductance', 'length',
283 'luminous_intensity', 'magnetic_density',
284 'magnetic_flux', 'mass', 'momentum', 'power', 'pressure', 'temperature', 'time',
285 'velocity', 'voltage', 'volume',
287 'Unit',
289 'speed',
290 'luminosity',
291 'magnetic_flux_density',
292 'amount',
294 'yotta',
295 'zetta',
296 'exa',
297 'peta',
298 'tera',
299 'giga',
300 'mega',
301 'kilo',
302 'hecto',
303 'deca',
304 'deci',
305 'centi',
306 'milli',
307 'micro',
308 'nano',
309 'pico',
310 'femto',
311 'atto',
312 'zepto',
313 'yocto',
315 'kibi',
316 'mebi',
317 'gibi',
318 'tebi',
319 'pebi',
320 'exbi',
322 'percent', 'percents',
323 'permille',
324 'rad', 'radian', 'radians',
325 'deg', 'degree', 'degrees',
326 'sr', 'steradian', 'steradians',
327 'mil', 'angular_mil', 'angular_mils',
328 'm', 'meter', 'meters',
329 'kg', 'kilogram', 'kilograms',
330 's', 'second', 'seconds',
331 'A', 'ampere', 'amperes',
332 'K', 'kelvin', 'kelvins',
333 'mol', 'mole', 'moles',
334 'cd', 'candela', 'candelas',
335 'g', 'gram', 'grams',
336 'mg', 'milligram', 'milligrams',
337 'ug', 'microgram', 'micrograms',
338 't', 'tonne', 'metric_ton',
339 'newton', 'newtons', 'N',
340 'joule', 'joules', 'J',
341 'watt', 'watts', 'W',
342 'pascal', 'pascals', 'Pa', 'pa',
343 'hertz', 'hz', 'Hz',
344 'coulomb', 'coulombs', 'C',
345 'volt', 'volts', 'v', 'V',
346 'ohm', 'ohms',
347 'siemens', 'S', 'mho', 'mhos',
348 'farad', 'farads', 'F',
349 'henry', 'henrys', 'H',
350 'tesla', 'teslas', 'T',
351 'weber', 'webers', 'Wb', 'wb',
352 'optical_power', 'dioptre', 'D',
353 'lux', 'lx',
354 'katal', 'kat',
355 'gray', 'Gy',
356 'becquerel', 'Bq',
357 'km', 'kilometer', 'kilometers',
358 'dm', 'decimeter', 'decimeters',
359 'cm', 'centimeter', 'centimeters',
360 'mm', 'millimeter', 'millimeters',
361 'um', 'micrometer', 'micrometers', 'micron', 'microns',
362 'nm', 'nanometer', 'nanometers',
363 'pm', 'picometer', 'picometers',
364 'ft', 'foot', 'feet',
365 'inch', 'inches',
366 'yd', 'yard', 'yards',
367 'mi', 'mile', 'miles',
368 'nmi', 'nautical_mile', 'nautical_miles',
369 'angstrom', 'angstroms',
370 'ha', 'hectare',
371 'l', 'L', 'liter', 'liters',
372 'dl', 'dL', 'deciliter', 'deciliters',
373 'cl', 'cL', 'centiliter', 'centiliters',
374 'ml', 'mL', 'milliliter', 'milliliters',
375 'ms', 'millisecond', 'milliseconds',
376 'us', 'microsecond', 'microseconds',
377 'ns', 'nanosecond', 'nanoseconds',
378 'ps', 'picosecond', 'picoseconds',
379 'minute', 'minutes',
380 'h', 'hour', 'hours',
381 'day', 'days',
382 'anomalistic_year', 'anomalistic_years',
383 'sidereal_year', 'sidereal_years',
384 'tropical_year', 'tropical_years',
385 'common_year', 'common_years',
386 'julian_year', 'julian_years',
387 'draconic_year', 'draconic_years',
388 'gaussian_year', 'gaussian_years',
389 'full_moon_cycle', 'full_moon_cycles',
390 'year', 'years',
391 'G', 'gravitational_constant',
392 'c', 'speed_of_light',
393 'elementary_charge',
394 'hbar',
395 'planck',
396 'eV', 'electronvolt', 'electronvolts',
397 'avogadro_number',
398 'avogadro', 'avogadro_constant',
399 'boltzmann', 'boltzmann_constant',
400 'stefan', 'stefan_boltzmann_constant',
401 'R', 'molar_gas_constant',
402 'faraday_constant',
403 'josephson_constant',
404 'von_klitzing_constant',
405 'Da', 'dalton', 'amu', 'amus', 'atomic_mass_unit', 'atomic_mass_constant',
406 'me', 'electron_rest_mass',
407 'gee', 'gees', 'acceleration_due_to_gravity',
408 'u0', 'magnetic_constant', 'vacuum_permeability',
409 'e0', 'electric_constant', 'vacuum_permittivity',
410 'Z0', 'vacuum_impedance',
411 'coulomb_constant', 'electric_force_constant',
412 'atmosphere', 'atmospheres', 'atm',
413 'kPa',
414 'bar', 'bars',
415 'pound', 'pounds',
416 'psi',
417 'dHg0',
418 'mmHg', 'torr',
419 'mmu', 'mmus', 'milli_mass_unit',
420 'quart', 'quarts',
421 'ly', 'lightyear', 'lightyears',
422 'au', 'astronomical_unit', 'astronomical_units',
423 'planck_mass',
424 'planck_time',
425 'planck_temperature',
426 'planck_length',
427 'planck_charge',
428 'planck_area',
429 'planck_volume',
430 'planck_momentum',
431 'planck_energy',
432 'planck_force',
433 'planck_power',
434 'planck_density',
435 'planck_energy_density',
436 'planck_intensity',
437 'planck_angular_frequency',
438 'planck_pressure',
439 'planck_current',
440 'planck_voltage',
441 'planck_impedance',
442 'planck_acceleration',
443 'bit', 'bits',
444 'byte',
445 'kibibyte', 'kibibytes',
446 'mebibyte', 'mebibytes',
447 'gibibyte', 'gibibytes',
448 'tebibyte', 'tebibytes',
449 'pebibyte', 'pebibytes',
450 'exbibyte', 'exbibytes',
452 'mks', 'mksa', 'si',
453]