Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/scipy/constants/_constants.py: 85%
147 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-12 06:31 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-12 06:31 +0000
1"""
2Collection of physical constants and conversion factors.
4Most constants are in SI units, so you can do
5print '10 mile per minute is', 10*mile/minute, 'm/s or', 10*mile/(minute*knot), 'knots'
7The list is not meant to be comprehensive, but just convenient for everyday use.
8"""
10from __future__ import annotations
12import math as _math
13from typing import TYPE_CHECKING, Any
15from ._codata import value as _cd
16import numpy as _np
18if TYPE_CHECKING:
19 import numpy.typing as npt
21"""
22BasSw 2006
23physical constants: imported from CODATA
24unit conversion: see e.g., NIST special publication 811
25Use at own risk: double-check values before calculating your Mars orbit-insertion burn.
26Some constants exist in a few variants, which are marked with suffixes.
27The ones without any suffix should be the most common ones.
28"""
30__all__ = [
31 'Avogadro', 'Boltzmann', 'Btu', 'Btu_IT', 'Btu_th', 'G',
32 'Julian_year', 'N_A', 'Planck', 'R', 'Rydberg',
33 'Stefan_Boltzmann', 'Wien', 'acre', 'alpha',
34 'angstrom', 'arcmin', 'arcminute', 'arcsec',
35 'arcsecond', 'astronomical_unit', 'atm',
36 'atmosphere', 'atomic_mass', 'atto', 'au', 'bar',
37 'barrel', 'bbl', 'blob', 'c', 'calorie',
38 'calorie_IT', 'calorie_th', 'carat', 'centi',
39 'convert_temperature', 'day', 'deci', 'degree',
40 'degree_Fahrenheit', 'deka', 'dyn', 'dyne', 'e',
41 'eV', 'electron_mass', 'electron_volt',
42 'elementary_charge', 'epsilon_0', 'erg',
43 'exa', 'exbi', 'femto', 'fermi', 'fine_structure',
44 'fluid_ounce', 'fluid_ounce_US', 'fluid_ounce_imp',
45 'foot', 'g', 'gallon', 'gallon_US', 'gallon_imp',
46 'gas_constant', 'gibi', 'giga', 'golden', 'golden_ratio',
47 'grain', 'gram', 'gravitational_constant', 'h', 'hbar',
48 'hectare', 'hecto', 'horsepower', 'hour', 'hp',
49 'inch', 'k', 'kgf', 'kibi', 'kilo', 'kilogram_force',
50 'kmh', 'knot', 'lambda2nu', 'lb', 'lbf',
51 'light_year', 'liter', 'litre', 'long_ton', 'm_e',
52 'm_n', 'm_p', 'm_u', 'mach', 'mebi', 'mega',
53 'metric_ton', 'micro', 'micron', 'mil', 'mile',
54 'milli', 'minute', 'mmHg', 'mph', 'mu_0', 'nano',
55 'nautical_mile', 'neutron_mass', 'nu2lambda',
56 'ounce', 'oz', 'parsec', 'pebi', 'peta',
57 'pi', 'pico', 'point', 'pound', 'pound_force',
58 'proton_mass', 'psi', 'pt', 'short_ton',
59 'sigma', 'slinch', 'slug', 'speed_of_light',
60 'speed_of_sound', 'stone', 'survey_foot',
61 'survey_mile', 'tebi', 'tera', 'ton_TNT',
62 'torr', 'troy_ounce', 'troy_pound', 'u',
63 'week', 'yard', 'year', 'yobi', 'yocto',
64 'yotta', 'zebi', 'zepto', 'zero_Celsius', 'zetta'
65]
68# mathematical constants
69pi = _math.pi
70golden = golden_ratio = (1 + _math.sqrt(5)) / 2
72# SI prefixes
73yotta = 1e24
74zetta = 1e21
75exa = 1e18
76peta = 1e15
77tera = 1e12
78giga = 1e9
79mega = 1e6
80kilo = 1e3
81hecto = 1e2
82deka = 1e1
83deci = 1e-1
84centi = 1e-2
85milli = 1e-3
86micro = 1e-6
87nano = 1e-9
88pico = 1e-12
89femto = 1e-15
90atto = 1e-18
91zepto = 1e-21
92yocto = 1e-24
94# binary prefixes
95kibi = 2**10
96mebi = 2**20
97gibi = 2**30
98tebi = 2**40
99pebi = 2**50
100exbi = 2**60
101zebi = 2**70
102yobi = 2**80
104# physical constants
105c = speed_of_light = _cd('speed of light in vacuum')
106mu_0 = _cd('vacuum mag. permeability')
107epsilon_0 = _cd('vacuum electric permittivity')
108h = Planck = _cd('Planck constant')
109hbar = h / (2 * pi)
110G = gravitational_constant = _cd('Newtonian constant of gravitation')
111g = _cd('standard acceleration of gravity')
112e = elementary_charge = _cd('elementary charge')
113R = gas_constant = _cd('molar gas constant')
114alpha = fine_structure = _cd('fine-structure constant')
115N_A = Avogadro = _cd('Avogadro constant')
116k = Boltzmann = _cd('Boltzmann constant')
117sigma = Stefan_Boltzmann = _cd('Stefan-Boltzmann constant')
118Wien = _cd('Wien wavelength displacement law constant')
119Rydberg = _cd('Rydberg constant')
121# mass in kg
122gram = 1e-3
123metric_ton = 1e3
124grain = 64.79891e-6
125lb = pound = 7000 * grain # avoirdupois
126blob = slinch = pound * g / 0.0254 # lbf*s**2/in (added in 1.0.0)
127slug = blob / 12 # lbf*s**2/foot (added in 1.0.0)
128oz = ounce = pound / 16
129stone = 14 * pound
130long_ton = 2240 * pound
131short_ton = 2000 * pound
133troy_ounce = 480 * grain # only for metals / gems
134troy_pound = 12 * troy_ounce
135carat = 200e-6
137m_e = electron_mass = _cd('electron mass')
138m_p = proton_mass = _cd('proton mass')
139m_n = neutron_mass = _cd('neutron mass')
140m_u = u = atomic_mass = _cd('atomic mass constant')
142# angle in rad
143degree = pi / 180
144arcmin = arcminute = degree / 60
145arcsec = arcsecond = arcmin / 60
147# time in second
148minute = 60.0
149hour = 60 * minute
150day = 24 * hour
151week = 7 * day
152year = 365 * day
153Julian_year = 365.25 * day
155# length in meter
156inch = 0.0254
157foot = 12 * inch
158yard = 3 * foot
159mile = 1760 * yard
160mil = inch / 1000
161pt = point = inch / 72 # typography
162survey_foot = 1200.0 / 3937
163survey_mile = 5280 * survey_foot
164nautical_mile = 1852.0
165fermi = 1e-15
166angstrom = 1e-10
167micron = 1e-6
168au = astronomical_unit = 149597870700.0
169light_year = Julian_year * c
170parsec = au / arcsec
172# pressure in pascal
173atm = atmosphere = _cd('standard atmosphere')
174bar = 1e5
175torr = mmHg = atm / 760
176psi = pound * g / (inch * inch)
178# area in meter**2
179hectare = 1e4
180acre = 43560 * foot**2
182# volume in meter**3
183litre = liter = 1e-3
184gallon = gallon_US = 231 * inch**3 # US
185# pint = gallon_US / 8
186fluid_ounce = fluid_ounce_US = gallon_US / 128
187bbl = barrel = 42 * gallon_US # for oil
189gallon_imp = 4.54609e-3 # UK
190fluid_ounce_imp = gallon_imp / 160
192# speed in meter per second
193kmh = 1e3 / hour
194mph = mile / hour
195mach = speed_of_sound = 340.5 # approx value at 15 degrees in 1 atm. Is this a common value?
196knot = nautical_mile / hour
198# temperature in kelvin
199zero_Celsius = 273.15
200degree_Fahrenheit = 1/1.8 # only for differences
202# energy in joule
203eV = electron_volt = elementary_charge # * 1 Volt
204calorie = calorie_th = 4.184
205calorie_IT = 4.1868
206erg = 1e-7
207Btu_th = pound * degree_Fahrenheit * calorie_th / gram
208Btu = Btu_IT = pound * degree_Fahrenheit * calorie_IT / gram
209ton_TNT = 1e9 * calorie_th
210# Wh = watt_hour
212# power in watt
213hp = horsepower = 550 * foot * pound * g
215# force in newton
216dyn = dyne = 1e-5
217lbf = pound_force = pound * g
218kgf = kilogram_force = g # * 1 kg
220# functions for conversions that are not linear
223def convert_temperature(
224 val: npt.ArrayLike,
225 old_scale: str,
226 new_scale: str,
227) -> Any:
228 """
229 Convert from a temperature scale to another one among Celsius, Kelvin,
230 Fahrenheit, and Rankine scales.
232 Parameters
233 ----------
234 val : array_like
235 Value(s) of the temperature(s) to be converted expressed in the
236 original scale.
237 old_scale : str
238 Specifies as a string the original scale from which the temperature
239 value(s) will be converted. Supported scales are Celsius ('Celsius',
240 'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'),
241 Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f'), and Rankine
242 ('Rankine', 'rankine', 'R', 'r').
243 new_scale : str
244 Specifies as a string the new scale to which the temperature
245 value(s) will be converted. Supported scales are Celsius ('Celsius',
246 'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'),
247 Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f'), and Rankine
248 ('Rankine', 'rankine', 'R', 'r').
250 Returns
251 -------
252 res : float or array of floats
253 Value(s) of the converted temperature(s) expressed in the new scale.
255 Notes
256 -----
257 .. versionadded:: 0.18.0
259 Examples
260 --------
261 >>> from scipy.constants import convert_temperature
262 >>> import numpy as np
263 >>> convert_temperature(np.array([-40, 40]), 'Celsius', 'Kelvin')
264 array([ 233.15, 313.15])
266 """
267 # Convert from `old_scale` to Kelvin
268 if old_scale.lower() in ['celsius', 'c']:
269 tempo = _np.asanyarray(val) + zero_Celsius
270 elif old_scale.lower() in ['kelvin', 'k']:
271 tempo = _np.asanyarray(val)
272 elif old_scale.lower() in ['fahrenheit', 'f']:
273 tempo = (_np.asanyarray(val) - 32) * 5 / 9 + zero_Celsius
274 elif old_scale.lower() in ['rankine', 'r']:
275 tempo = _np.asanyarray(val) * 5 / 9
276 else:
277 raise NotImplementedError("%s scale is unsupported: supported scales "
278 "are Celsius, Kelvin, Fahrenheit, and "
279 "Rankine" % old_scale)
280 # and from Kelvin to `new_scale`.
281 if new_scale.lower() in ['celsius', 'c']:
282 res = tempo - zero_Celsius
283 elif new_scale.lower() in ['kelvin', 'k']:
284 res = tempo
285 elif new_scale.lower() in ['fahrenheit', 'f']:
286 res = (tempo - zero_Celsius) * 9 / 5 + 32
287 elif new_scale.lower() in ['rankine', 'r']:
288 res = tempo * 9 / 5
289 else:
290 raise NotImplementedError("'%s' scale is unsupported: supported "
291 "scales are 'Celsius', 'Kelvin', "
292 "'Fahrenheit', and 'Rankine'" % new_scale)
294 return res
297# optics
300def lambda2nu(lambda_: npt.ArrayLike) -> Any:
301 """
302 Convert wavelength to optical frequency
304 Parameters
305 ----------
306 lambda_ : array_like
307 Wavelength(s) to be converted.
309 Returns
310 -------
311 nu : float or array of floats
312 Equivalent optical frequency.
314 Notes
315 -----
316 Computes ``nu = c / lambda`` where c = 299792458.0, i.e., the
317 (vacuum) speed of light in meters/second.
319 Examples
320 --------
321 >>> from scipy.constants import lambda2nu, speed_of_light
322 >>> import numpy as np
323 >>> lambda2nu(np.array((1, speed_of_light)))
324 array([ 2.99792458e+08, 1.00000000e+00])
326 """
327 return c / _np.asanyarray(lambda_)
330def nu2lambda(nu: npt.ArrayLike) -> Any:
331 """
332 Convert optical frequency to wavelength.
334 Parameters
335 ----------
336 nu : array_like
337 Optical frequency to be converted.
339 Returns
340 -------
341 lambda : float or array of floats
342 Equivalent wavelength(s).
344 Notes
345 -----
346 Computes ``lambda = c / nu`` where c = 299792458.0, i.e., the
347 (vacuum) speed of light in meters/second.
349 Examples
350 --------
351 >>> from scipy.constants import nu2lambda, speed_of_light
352 >>> import numpy as np
353 >>> nu2lambda(np.array((1, speed_of_light)))
354 array([ 2.99792458e+08, 1.00000000e+00])
356 """
357 return c / _np.asanyarray(nu)