__init__.py 12 KB

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