test_quantities.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. import warnings
  2. from sympy.core.add import Add
  3. from sympy.core.function import (Function, diff)
  4. from sympy.core.numbers import (Number, Rational)
  5. from sympy.core.singleton import S
  6. from sympy.core.symbol import (Symbol, symbols)
  7. from sympy.functions.elementary.complexes import Abs
  8. from sympy.functions.elementary.exponential import (exp, log)
  9. from sympy.functions.elementary.miscellaneous import sqrt
  10. from sympy.functions.elementary.trigonometric import sin
  11. from sympy.integrals.integrals import integrate
  12. from sympy.physics.units import (amount_of_substance, area, convert_to, find_unit,
  13. volume, kilometer, joule, molar_gas_constant,
  14. vacuum_permittivity, elementary_charge, volt,
  15. ohm)
  16. from sympy.physics.units.definitions import (amu, au, centimeter, coulomb,
  17. day, foot, grams, hour, inch, kg, km, m, meter, millimeter,
  18. minute, quart, s, second, speed_of_light, bit,
  19. byte, kibibyte, mebibyte, gibibyte, tebibyte, pebibyte, exbibyte,
  20. kilogram, gravitational_constant, electron_rest_mass)
  21. from sympy.physics.units.definitions.dimension_definitions import (
  22. Dimension, charge, length, time, temperature, pressure,
  23. energy, mass
  24. )
  25. from sympy.physics.units.prefixes import PREFIXES, kilo
  26. from sympy.physics.units.quantities import PhysicalConstant, Quantity
  27. from sympy.physics.units.systems import SI
  28. from sympy.testing.pytest import raises
  29. k = PREFIXES["k"]
  30. def test_str_repr():
  31. assert str(kg) == "kilogram"
  32. def test_eq():
  33. # simple test
  34. assert 10*m == 10*m
  35. assert 10*m != 10*s
  36. def test_convert_to():
  37. q = Quantity("q1")
  38. q.set_global_relative_scale_factor(S(5000), meter)
  39. assert q.convert_to(m) == 5000*m
  40. assert speed_of_light.convert_to(m / s) == 299792458 * m / s
  41. # TODO: eventually support this kind of conversion:
  42. # assert (2*speed_of_light).convert_to(m / s) == 2 * 299792458 * m / s
  43. assert day.convert_to(s) == 86400*s
  44. # Wrong dimension to convert:
  45. assert q.convert_to(s) == q
  46. assert speed_of_light.convert_to(m) == speed_of_light
  47. expr = joule*second
  48. conv = convert_to(expr, joule)
  49. assert conv == joule*second
  50. def test_Quantity_definition():
  51. q = Quantity("s10", abbrev="sabbr")
  52. q.set_global_relative_scale_factor(10, second)
  53. u = Quantity("u", abbrev="dam")
  54. u.set_global_relative_scale_factor(10, meter)
  55. km = Quantity("km")
  56. km.set_global_relative_scale_factor(kilo, meter)
  57. v = Quantity("u")
  58. v.set_global_relative_scale_factor(5*kilo, meter)
  59. assert q.scale_factor == 10
  60. assert q.dimension == time
  61. assert q.abbrev == Symbol("sabbr")
  62. assert u.dimension == length
  63. assert u.scale_factor == 10
  64. assert u.abbrev == Symbol("dam")
  65. assert km.scale_factor == 1000
  66. assert km.func(*km.args) == km
  67. assert km.func(*km.args).args == km.args
  68. assert v.dimension == length
  69. assert v.scale_factor == 5000
  70. def test_abbrev():
  71. u = Quantity("u")
  72. u.set_global_relative_scale_factor(S.One, meter)
  73. assert u.name == Symbol("u")
  74. assert u.abbrev == Symbol("u")
  75. u = Quantity("u", abbrev="om")
  76. u.set_global_relative_scale_factor(S(2), meter)
  77. assert u.name == Symbol("u")
  78. assert u.abbrev == Symbol("om")
  79. assert u.scale_factor == 2
  80. assert isinstance(u.scale_factor, Number)
  81. u = Quantity("u", abbrev="ikm")
  82. u.set_global_relative_scale_factor(3*kilo, meter)
  83. assert u.abbrev == Symbol("ikm")
  84. assert u.scale_factor == 3000
  85. def test_print():
  86. u = Quantity("unitname", abbrev="dam")
  87. assert repr(u) == "unitname"
  88. assert str(u) == "unitname"
  89. def test_Quantity_eq():
  90. u = Quantity("u", abbrev="dam")
  91. v = Quantity("v1")
  92. assert u != v
  93. v = Quantity("v2", abbrev="ds")
  94. assert u != v
  95. v = Quantity("v3", abbrev="dm")
  96. assert u != v
  97. def test_add_sub():
  98. u = Quantity("u")
  99. v = Quantity("v")
  100. w = Quantity("w")
  101. u.set_global_relative_scale_factor(S(10), meter)
  102. v.set_global_relative_scale_factor(S(5), meter)
  103. w.set_global_relative_scale_factor(S(2), second)
  104. assert isinstance(u + v, Add)
  105. assert (u + v.convert_to(u)) == (1 + S.Half)*u
  106. # TODO: eventually add this:
  107. # assert (u + v).convert_to(u) == (1 + S.Half)*u
  108. assert isinstance(u - v, Add)
  109. assert (u - v.convert_to(u)) == S.Half*u
  110. # TODO: eventually add this:
  111. # assert (u - v).convert_to(u) == S.Half*u
  112. def test_quantity_abs():
  113. v_w1 = Quantity('v_w1')
  114. v_w2 = Quantity('v_w2')
  115. v_w3 = Quantity('v_w3')
  116. v_w1.set_global_relative_scale_factor(1, meter/second)
  117. v_w2.set_global_relative_scale_factor(1, meter/second)
  118. v_w3.set_global_relative_scale_factor(1, meter/second)
  119. expr = v_w3 - Abs(v_w1 - v_w2)
  120. assert SI.get_dimensional_expr(v_w1) == (length/time).name
  121. Dq = Dimension(SI.get_dimensional_expr(expr))
  122. assert SI.get_dimension_system().get_dimensional_dependencies(Dq) == {
  123. length: 1,
  124. time: -1,
  125. }
  126. assert meter == sqrt(meter**2)
  127. def test_check_unit_consistency():
  128. u = Quantity("u")
  129. v = Quantity("v")
  130. w = Quantity("w")
  131. u.set_global_relative_scale_factor(S(10), meter)
  132. v.set_global_relative_scale_factor(S(5), meter)
  133. w.set_global_relative_scale_factor(S(2), second)
  134. def check_unit_consistency(expr):
  135. SI._collect_factor_and_dimension(expr)
  136. raises(ValueError, lambda: check_unit_consistency(u + w))
  137. raises(ValueError, lambda: check_unit_consistency(u - w))
  138. raises(ValueError, lambda: check_unit_consistency(u + 1))
  139. raises(ValueError, lambda: check_unit_consistency(u - 1))
  140. raises(ValueError, lambda: check_unit_consistency(1 - exp(u / w)))
  141. def test_mul_div():
  142. u = Quantity("u")
  143. v = Quantity("v")
  144. t = Quantity("t")
  145. ut = Quantity("ut")
  146. v2 = Quantity("v")
  147. u.set_global_relative_scale_factor(S(10), meter)
  148. v.set_global_relative_scale_factor(S(5), meter)
  149. t.set_global_relative_scale_factor(S(2), second)
  150. ut.set_global_relative_scale_factor(S(20), meter*second)
  151. v2.set_global_relative_scale_factor(S(5), meter/second)
  152. assert 1 / u == u**(-1)
  153. assert u / 1 == u
  154. v1 = u / t
  155. v2 = v
  156. # Pow only supports structural equality:
  157. assert v1 != v2
  158. assert v1 == v2.convert_to(v1)
  159. # TODO: decide whether to allow such expression in the future
  160. # (requires somehow manipulating the core).
  161. # assert u / Quantity('l2', dimension=length, scale_factor=2) == 5
  162. assert u * 1 == u
  163. ut1 = u * t
  164. ut2 = ut
  165. # Mul only supports structural equality:
  166. assert ut1 != ut2
  167. assert ut1 == ut2.convert_to(ut1)
  168. # Mul only supports structural equality:
  169. lp1 = Quantity("lp1")
  170. lp1.set_global_relative_scale_factor(S(2), 1/meter)
  171. assert u * lp1 != 20
  172. assert u**0 == 1
  173. assert u**1 == u
  174. # TODO: Pow only support structural equality:
  175. u2 = Quantity("u2")
  176. u3 = Quantity("u3")
  177. u2.set_global_relative_scale_factor(S(100), meter**2)
  178. u3.set_global_relative_scale_factor(Rational(1, 10), 1/meter)
  179. assert u ** 2 != u2
  180. assert u ** -1 != u3
  181. assert u ** 2 == u2.convert_to(u)
  182. assert u ** -1 == u3.convert_to(u)
  183. def test_units():
  184. assert convert_to((5*m/s * day) / km, 1) == 432
  185. assert convert_to(foot / meter, meter) == Rational(3048, 10000)
  186. # amu is a pure mass so mass/mass gives a number, not an amount (mol)
  187. # TODO: need better simplification routine:
  188. assert str(convert_to(grams/amu, grams).n(2)) == '6.0e+23'
  189. # Light from the sun needs about 8.3 minutes to reach earth
  190. t = (1*au / speed_of_light) / minute
  191. # TODO: need a better way to simplify expressions containing units:
  192. t = convert_to(convert_to(t, meter / minute), meter)
  193. assert t.simplify() == Rational(49865956897, 5995849160)
  194. # TODO: fix this, it should give `m` without `Abs`
  195. assert sqrt(m**2) == m
  196. assert (sqrt(m))**2 == m
  197. t = Symbol('t')
  198. assert integrate(t*m/s, (t, 1*s, 5*s)) == 12*m*s
  199. assert (t * m/s).integrate((t, 1*s, 5*s)) == 12*m*s
  200. def test_issue_quart():
  201. assert convert_to(4 * quart / inch ** 3, meter) == 231
  202. assert convert_to(4 * quart / inch ** 3, millimeter) == 231
  203. def test_electron_rest_mass():
  204. assert convert_to(electron_rest_mass, kilogram) == 9.1093837015e-31*kilogram
  205. assert convert_to(electron_rest_mass, grams) == 9.1093837015e-28*grams
  206. def test_issue_5565():
  207. assert (m < s).is_Relational
  208. def test_find_unit():
  209. assert find_unit('coulomb') == ['coulomb', 'coulombs', 'coulomb_constant']
  210. assert find_unit(coulomb) == ['C', 'coulomb', 'coulombs', 'planck_charge', 'elementary_charge']
  211. assert find_unit(charge) == ['C', 'coulomb', 'coulombs', 'planck_charge', 'elementary_charge']
  212. assert find_unit(inch) == [
  213. 'm', 'au', 'cm', 'dm', 'ft', 'km', 'ly', 'mi', 'mm', 'nm', 'pm', 'um', 'yd',
  214. 'nmi', 'feet', 'foot', 'inch', 'mile', 'yard', 'meter', 'miles', 'yards',
  215. 'inches', 'meters', 'micron', 'microns', 'angstrom', 'angstroms', 'decimeter',
  216. 'kilometer', 'lightyear', 'nanometer', 'picometer', 'centimeter', 'decimeters',
  217. 'kilometers', 'lightyears', 'micrometer', 'millimeter', 'nanometers', 'picometers',
  218. 'centimeters', 'micrometers', 'millimeters', 'nautical_mile', 'planck_length',
  219. 'nautical_miles', 'astronomical_unit', 'astronomical_units']
  220. assert find_unit(inch**-1) == ['D', 'dioptre', 'optical_power']
  221. assert find_unit(length**-1) == ['D', 'dioptre', 'optical_power']
  222. assert find_unit(inch ** 2) == ['ha', 'hectare', 'planck_area']
  223. assert find_unit(inch ** 3) == [
  224. 'L', 'l', 'cL', 'cl', 'dL', 'dl', 'mL', 'ml', 'liter', 'quart', 'liters', 'quarts',
  225. 'deciliter', 'centiliter', 'deciliters', 'milliliter',
  226. 'centiliters', 'milliliters', 'planck_volume']
  227. assert find_unit('voltage') == ['V', 'v', 'volt', 'volts', 'planck_voltage']
  228. assert find_unit(grams) == ['g', 't', 'Da', 'kg', 'me', 'mg', 'ug', 'amu', 'mmu', 'amus',
  229. 'gram', 'mmus', 'grams', 'pound', 'tonne', 'dalton', 'pounds',
  230. 'kilogram', 'kilograms', 'microgram', 'milligram', 'metric_ton',
  231. 'micrograms', 'milligrams', 'planck_mass', 'milli_mass_unit', 'atomic_mass_unit',
  232. 'electron_rest_mass', 'atomic_mass_constant']
  233. def test_Quantity_derivative():
  234. x = symbols("x")
  235. assert diff(x*meter, x) == meter
  236. assert diff(x**3*meter**2, x) == 3*x**2*meter**2
  237. assert diff(meter, meter) == 1
  238. assert diff(meter**2, meter) == 2*meter
  239. def test_quantity_postprocessing():
  240. q1 = Quantity('q1')
  241. q2 = Quantity('q2')
  242. SI.set_quantity_dimension(q1, length*pressure**2*temperature/time)
  243. SI.set_quantity_dimension(q2, energy*pressure*temperature/(length**2*time))
  244. assert q1 + q2
  245. q = q1 + q2
  246. Dq = Dimension(SI.get_dimensional_expr(q))
  247. assert SI.get_dimension_system().get_dimensional_dependencies(Dq) == {
  248. length: -1,
  249. mass: 2,
  250. temperature: 1,
  251. time: -5,
  252. }
  253. def test_factor_and_dimension():
  254. assert (3000, Dimension(1)) == SI._collect_factor_and_dimension(3000)
  255. assert (1001, length) == SI._collect_factor_and_dimension(meter + km)
  256. assert (2, length/time) == SI._collect_factor_and_dimension(
  257. meter/second + 36*km/(10*hour))
  258. x, y = symbols('x y')
  259. assert (x + y/100, length) == SI._collect_factor_and_dimension(
  260. x*m + y*centimeter)
  261. cH = Quantity('cH')
  262. SI.set_quantity_dimension(cH, amount_of_substance/volume)
  263. pH = -log(cH)
  264. assert (1, volume/amount_of_substance) == SI._collect_factor_and_dimension(
  265. exp(pH))
  266. v_w1 = Quantity('v_w1')
  267. v_w2 = Quantity('v_w2')
  268. v_w1.set_global_relative_scale_factor(Rational(3, 2), meter/second)
  269. v_w2.set_global_relative_scale_factor(2, meter/second)
  270. expr = Abs(v_w1/2 - v_w2)
  271. assert (Rational(5, 4), length/time) == \
  272. SI._collect_factor_and_dimension(expr)
  273. expr = Rational(5, 2)*second/meter*v_w1 - 3000
  274. assert (-(2996 + Rational(1, 4)), Dimension(1)) == \
  275. SI._collect_factor_and_dimension(expr)
  276. expr = v_w1**(v_w2/v_w1)
  277. assert ((Rational(3, 2))**Rational(4, 3), (length/time)**Rational(4, 3)) == \
  278. SI._collect_factor_and_dimension(expr)
  279. def test_dimensional_expr_of_derivative():
  280. l = Quantity('l')
  281. t = Quantity('t')
  282. t1 = Quantity('t1')
  283. l.set_global_relative_scale_factor(36, km)
  284. t.set_global_relative_scale_factor(1, hour)
  285. t1.set_global_relative_scale_factor(1, second)
  286. x = Symbol('x')
  287. y = Symbol('y')
  288. f = Function('f')
  289. dfdx = f(x, y).diff(x, y)
  290. dl_dt = dfdx.subs({f(x, y): l, x: t, y: t1})
  291. assert SI.get_dimensional_expr(dl_dt) ==\
  292. SI.get_dimensional_expr(l / t / t1) ==\
  293. Symbol("length")/Symbol("time")**2
  294. assert SI._collect_factor_and_dimension(dl_dt) ==\
  295. SI._collect_factor_and_dimension(l / t / t1) ==\
  296. (10, length/time**2)
  297. def test_get_dimensional_expr_with_function():
  298. v_w1 = Quantity('v_w1')
  299. v_w2 = Quantity('v_w2')
  300. v_w1.set_global_relative_scale_factor(1, meter/second)
  301. v_w2.set_global_relative_scale_factor(1, meter/second)
  302. assert SI.get_dimensional_expr(sin(v_w1)) == \
  303. sin(SI.get_dimensional_expr(v_w1))
  304. assert SI.get_dimensional_expr(sin(v_w1/v_w2)) == 1
  305. def test_binary_information():
  306. assert convert_to(kibibyte, byte) == 1024*byte
  307. assert convert_to(mebibyte, byte) == 1024**2*byte
  308. assert convert_to(gibibyte, byte) == 1024**3*byte
  309. assert convert_to(tebibyte, byte) == 1024**4*byte
  310. assert convert_to(pebibyte, byte) == 1024**5*byte
  311. assert convert_to(exbibyte, byte) == 1024**6*byte
  312. assert kibibyte.convert_to(bit) == 8*1024*bit
  313. assert byte.convert_to(bit) == 8*bit
  314. a = 10*kibibyte*hour
  315. assert convert_to(a, byte) == 10240*byte*hour
  316. assert convert_to(a, minute) == 600*kibibyte*minute
  317. assert convert_to(a, [byte, minute]) == 614400*byte*minute
  318. def test_conversion_with_2_nonstandard_dimensions():
  319. good_grade = Quantity("good_grade")
  320. kilo_good_grade = Quantity("kilo_good_grade")
  321. centi_good_grade = Quantity("centi_good_grade")
  322. kilo_good_grade.set_global_relative_scale_factor(1000, good_grade)
  323. centi_good_grade.set_global_relative_scale_factor(S.One/10**5, kilo_good_grade)
  324. charity_points = Quantity("charity_points")
  325. milli_charity_points = Quantity("milli_charity_points")
  326. missions = Quantity("missions")
  327. milli_charity_points.set_global_relative_scale_factor(S.One/1000, charity_points)
  328. missions.set_global_relative_scale_factor(251, charity_points)
  329. assert convert_to(
  330. kilo_good_grade*milli_charity_points*millimeter,
  331. [centi_good_grade, missions, centimeter]
  332. ) == S.One * 10**5 / (251*1000) / 10 * centi_good_grade*missions*centimeter
  333. def test_eval_subs():
  334. energy, mass, force = symbols('energy mass force')
  335. expr1 = energy/mass
  336. units = {energy: kilogram*meter**2/second**2, mass: kilogram}
  337. assert expr1.subs(units) == meter**2/second**2
  338. expr2 = force/mass
  339. units = {force:gravitational_constant*kilogram**2/meter**2, mass:kilogram}
  340. assert expr2.subs(units) == gravitational_constant*kilogram/meter**2
  341. def test_issue_14932():
  342. assert (log(inch) - log(2)).simplify() == log(inch/2)
  343. assert (log(inch) - log(foot)).simplify() == -log(12)
  344. p = symbols('p', positive=True)
  345. assert (log(inch) - log(p)).simplify() == log(inch/p)
  346. def test_issue_14547():
  347. # the root issue is that an argument with dimensions should
  348. # not raise an error when the `arg - 1` calculation is
  349. # performed in the assumptions system
  350. from sympy.physics.units import foot, inch
  351. from sympy.core.relational import Eq
  352. assert log(foot).is_zero is None
  353. assert log(foot).is_positive is None
  354. assert log(foot).is_nonnegative is None
  355. assert log(foot).is_negative is None
  356. assert log(foot).is_algebraic is None
  357. assert log(foot).is_rational is None
  358. # doesn't raise error
  359. assert Eq(log(foot), log(inch)) is not None # might be False or unevaluated
  360. x = Symbol('x')
  361. e = foot + x
  362. assert e.is_Add and set(e.args) == {foot, x}
  363. e = foot + 1
  364. assert e.is_Add and set(e.args) == {foot, 1}
  365. def test_issue_22164():
  366. warnings.simplefilter("error")
  367. dm = Quantity("dm")
  368. SI.set_quantity_dimension(dm, length)
  369. SI.set_quantity_scale_factor(dm, 1)
  370. bad_exp = Quantity("bad_exp")
  371. SI.set_quantity_dimension(bad_exp, length)
  372. SI.set_quantity_scale_factor(bad_exp, 1)
  373. expr = dm ** bad_exp
  374. # deprecation warning is not expected here
  375. SI._collect_factor_and_dimension(expr)
  376. def test_issue_22819():
  377. from sympy.physics.units import tonne, gram, Da
  378. from sympy.physics.units.systems.si import dimsys_SI
  379. assert tonne.convert_to(gram) == 1000000*gram
  380. assert dimsys_SI.get_dimensional_dependencies(area) == {length: 2}
  381. assert Da.scale_factor == 1.66053906660000e-24
  382. def test_issue_20288():
  383. from sympy.core.numbers import E
  384. from sympy.physics.units import energy
  385. u = Quantity('u')
  386. v = Quantity('v')
  387. SI.set_quantity_dimension(u, energy)
  388. SI.set_quantity_dimension(v, energy)
  389. u.set_global_relative_scale_factor(1, joule)
  390. v.set_global_relative_scale_factor(1, joule)
  391. expr = 1 + exp(u**2/v**2)
  392. assert SI._collect_factor_and_dimension(expr) == (1 + E, Dimension(1))
  393. def test_issue_24062():
  394. from sympy.core.numbers import E
  395. from sympy.physics.units import impedance, capacitance, time, ohm, farad, second
  396. R = Quantity('R')
  397. C = Quantity('C')
  398. T = Quantity('T')
  399. SI.set_quantity_dimension(R, impedance)
  400. SI.set_quantity_dimension(C, capacitance)
  401. SI.set_quantity_dimension(T, time)
  402. R.set_global_relative_scale_factor(1, ohm)
  403. C.set_global_relative_scale_factor(1, farad)
  404. T.set_global_relative_scale_factor(1, second)
  405. expr = T / (R * C)
  406. dim = SI._collect_factor_and_dimension(expr)[1]
  407. assert SI.get_dimension_system().is_dimensionless(dim)
  408. exp_expr = 1 + exp(expr)
  409. assert SI._collect_factor_and_dimension(exp_expr) == (1 + E, Dimension(1))
  410. def test_issue_24211():
  411. from sympy.physics.units import time, velocity, acceleration, second, meter
  412. V1 = Quantity('V1')
  413. SI.set_quantity_dimension(V1, velocity)
  414. SI.set_quantity_scale_factor(V1, 1 * meter / second)
  415. A1 = Quantity('A1')
  416. SI.set_quantity_dimension(A1, acceleration)
  417. SI.set_quantity_scale_factor(A1, 1 * meter / second**2)
  418. T1 = Quantity('T1')
  419. SI.set_quantity_dimension(T1, time)
  420. SI.set_quantity_scale_factor(T1, 1 * second)
  421. expr = A1*T1 + V1
  422. # should not throw ValueError here
  423. SI._collect_factor_and_dimension(expr)
  424. def test_prefixed_property():
  425. assert not meter.is_prefixed
  426. assert not joule.is_prefixed
  427. assert not day.is_prefixed
  428. assert not second.is_prefixed
  429. assert not volt.is_prefixed
  430. assert not ohm.is_prefixed
  431. assert centimeter.is_prefixed
  432. assert kilometer.is_prefixed
  433. assert kilogram.is_prefixed
  434. assert pebibyte.is_prefixed
  435. def test_physics_constant():
  436. from sympy.physics.units import definitions
  437. for name in dir(definitions):
  438. quantity = getattr(definitions, name)
  439. if not isinstance(quantity, Quantity):
  440. continue
  441. if name.endswith('_constant'):
  442. assert isinstance(quantity, PhysicalConstant), f"{quantity} must be PhysicalConstant, but is {type(quantity)}"
  443. assert quantity.is_physical_constant, f"{name} is not marked as physics constant when it should be"
  444. for const in [gravitational_constant, molar_gas_constant, vacuum_permittivity, speed_of_light, elementary_charge]:
  445. assert isinstance(const, PhysicalConstant), f"{const} must be PhysicalConstant, but is {type(const)}"
  446. assert const.is_physical_constant, f"{const} is not marked as physics constant when it should be"
  447. assert not meter.is_physical_constant
  448. assert not joule.is_physical_constant