Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/scipy/constants/constants.py : 85%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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"""
9"""
10BasSw 2006
11physical constants: imported from CODATA
12unit conversion: see e.g., NIST special publication 811
13Use at own risk: double-check values before calculating your Mars orbit-insertion burn.
14Some constants exist in a few variants, which are marked with suffixes.
15The ones without any suffix should be the most common ones.
16"""
18import math as _math
19from .codata import value as _cd
20import numpy as _np
22# mathematical constants
23pi = _math.pi
24golden = golden_ratio = (1 + _math.sqrt(5)) / 2
26# SI prefixes
27yotta = 1e24
28zetta = 1e21
29exa = 1e18
30peta = 1e15
31tera = 1e12
32giga = 1e9
33mega = 1e6
34kilo = 1e3
35hecto = 1e2
36deka = 1e1
37deci = 1e-1
38centi = 1e-2
39milli = 1e-3
40micro = 1e-6
41nano = 1e-9
42pico = 1e-12
43femto = 1e-15
44atto = 1e-18
45zepto = 1e-21
47# binary prefixes
48kibi = 2**10
49mebi = 2**20
50gibi = 2**30
51tebi = 2**40
52pebi = 2**50
53exbi = 2**60
54zebi = 2**70
55yobi = 2**80
57# physical constants
58c = speed_of_light = _cd('speed of light in vacuum')
59mu_0 = _cd('vacuum mag. permeability')
60epsilon_0 = _cd('vacuum electric permittivity')
61h = Planck = _cd('Planck constant')
62hbar = h / (2 * pi)
63G = gravitational_constant = _cd('Newtonian constant of gravitation')
64g = _cd('standard acceleration of gravity')
65e = elementary_charge = _cd('elementary charge')
66R = gas_constant = _cd('molar gas constant')
67alpha = fine_structure = _cd('fine-structure constant')
68N_A = Avogadro = _cd('Avogadro constant')
69k = Boltzmann = _cd('Boltzmann constant')
70sigma = Stefan_Boltzmann = _cd('Stefan-Boltzmann constant')
71Wien = _cd('Wien wavelength displacement law constant')
72Rydberg = _cd('Rydberg constant')
74# mass in kg
75gram = 1e-3
76metric_ton = 1e3
77grain = 64.79891e-6
78lb = pound = 7000 * grain # avoirdupois
79blob = slinch = pound * g / 0.0254 # lbf*s**2/in (added in 1.0.0)
80slug = blob / 12 # lbf*s**2/foot (added in 1.0.0)
81oz = ounce = pound / 16
82stone = 14 * pound
83long_ton = 2240 * pound
84short_ton = 2000 * pound
86troy_ounce = 480 * grain # only for metals / gems
87troy_pound = 12 * troy_ounce
88carat = 200e-6
90m_e = electron_mass = _cd('electron mass')
91m_p = proton_mass = _cd('proton mass')
92m_n = neutron_mass = _cd('neutron mass')
93m_u = u = atomic_mass = _cd('atomic mass constant')
95# angle in rad
96degree = pi / 180
97arcmin = arcminute = degree / 60
98arcsec = arcsecond = arcmin / 60
100# time in second
101minute = 60.0
102hour = 60 * minute
103day = 24 * hour
104week = 7 * day
105year = 365 * day
106Julian_year = 365.25 * day
108# length in meter
109inch = 0.0254
110foot = 12 * inch
111yard = 3 * foot
112mile = 1760 * yard
113mil = inch / 1000
114pt = point = inch / 72 # typography
115survey_foot = 1200.0 / 3937
116survey_mile = 5280 * survey_foot
117nautical_mile = 1852.0
118fermi = 1e-15
119angstrom = 1e-10
120micron = 1e-6
121au = astronomical_unit = 149597870700.0
122light_year = Julian_year * c
123parsec = au / arcsec
125# pressure in pascal
126atm = atmosphere = _cd('standard atmosphere')
127bar = 1e5
128torr = mmHg = atm / 760
129psi = pound * g / (inch * inch)
131# area in meter**2
132hectare = 1e4
133acre = 43560 * foot**2
135# volume in meter**3
136litre = liter = 1e-3
137gallon = gallon_US = 231 * inch**3 # US
138# pint = gallon_US / 8
139fluid_ounce = fluid_ounce_US = gallon_US / 128
140bbl = barrel = 42 * gallon_US # for oil
142gallon_imp = 4.54609e-3 # UK
143fluid_ounce_imp = gallon_imp / 160
145# speed in meter per second
146kmh = 1e3 / hour
147mph = mile / hour
148mach = speed_of_sound = 340.5 # approx value at 15 degrees in 1 atm. Is this a common value?
149knot = nautical_mile / hour
151# temperature in kelvin
152zero_Celsius = 273.15
153degree_Fahrenheit = 1/1.8 # only for differences
155# energy in joule
156eV = electron_volt = elementary_charge # * 1 Volt
157calorie = calorie_th = 4.184
158calorie_IT = 4.1868
159erg = 1e-7
160Btu_th = pound * degree_Fahrenheit * calorie_th / gram
161Btu = Btu_IT = pound * degree_Fahrenheit * calorie_IT / gram
162ton_TNT = 1e9 * calorie_th
163# Wh = watt_hour
165# power in watt
166hp = horsepower = 550 * foot * pound * g
168# force in newton
169dyn = dyne = 1e-5
170lbf = pound_force = pound * g
171kgf = kilogram_force = g # * 1 kg
173# functions for conversions that are not linear
176def convert_temperature(val, old_scale, new_scale):
177 """
178 Convert from a temperature scale to another one among Celsius, Kelvin,
179 Fahrenheit, and Rankine scales.
181 Parameters
182 ----------
183 val : array_like
184 Value(s) of the temperature(s) to be converted expressed in the
185 original scale.
187 old_scale: str
188 Specifies as a string the original scale from which the temperature
189 value(s) will be converted. Supported scales are Celsius ('Celsius',
190 'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'),
191 Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f'), and Rankine
192 ('Rankine', 'rankine', 'R', 'r').
194 new_scale: str
195 Specifies as a string the new scale to which the temperature
196 value(s) will be converted. Supported scales are Celsius ('Celsius',
197 'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'),
198 Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f'), and Rankine
199 ('Rankine', 'rankine', 'R', 'r').
201 Returns
202 -------
203 res : float or array of floats
204 Value(s) of the converted temperature(s) expressed in the new scale.
206 Notes
207 -----
208 .. versionadded:: 0.18.0
210 Examples
211 --------
212 >>> from scipy.constants import convert_temperature
213 >>> convert_temperature(np.array([-40, 40]), 'Celsius', 'Kelvin')
214 array([ 233.15, 313.15])
216 """
217 # Convert from `old_scale` to Kelvin
218 if old_scale.lower() in ['celsius', 'c']:
219 tempo = _np.asanyarray(val) + zero_Celsius
220 elif old_scale.lower() in ['kelvin', 'k']:
221 tempo = _np.asanyarray(val)
222 elif old_scale.lower() in ['fahrenheit', 'f']:
223 tempo = (_np.asanyarray(val) - 32) * 5 / 9 + zero_Celsius
224 elif old_scale.lower() in ['rankine', 'r']:
225 tempo = _np.asanyarray(val) * 5 / 9
226 else:
227 raise NotImplementedError("%s scale is unsupported: supported scales "
228 "are Celsius, Kelvin, Fahrenheit, and "
229 "Rankine" % old_scale)
230 # and from Kelvin to `new_scale`.
231 if new_scale.lower() in ['celsius', 'c']:
232 res = tempo - zero_Celsius
233 elif new_scale.lower() in ['kelvin', 'k']:
234 res = tempo
235 elif new_scale.lower() in ['fahrenheit', 'f']:
236 res = (tempo - zero_Celsius) * 9 / 5 + 32
237 elif new_scale.lower() in ['rankine', 'r']:
238 res = tempo * 9 / 5
239 else:
240 raise NotImplementedError("'%s' scale is unsupported: supported "
241 "scales are 'Celsius', 'Kelvin', "
242 "'Fahrenheit', and 'Rankine'" % new_scale)
244 return res
247# optics
250def lambda2nu(lambda_):
251 """
252 Convert wavelength to optical frequency
254 Parameters
255 ----------
256 lambda_ : array_like
257 Wavelength(s) to be converted.
259 Returns
260 -------
261 nu : float or array of floats
262 Equivalent optical frequency.
264 Notes
265 -----
266 Computes ``nu = c / lambda`` where c = 299792458.0, i.e., the
267 (vacuum) speed of light in meters/second.
269 Examples
270 --------
271 >>> from scipy.constants import lambda2nu, speed_of_light
272 >>> lambda2nu(np.array((1, speed_of_light)))
273 array([ 2.99792458e+08, 1.00000000e+00])
275 """
276 return _np.asanyarray(c) / lambda_
279def nu2lambda(nu):
280 """
281 Convert optical frequency to wavelength.
283 Parameters
284 ----------
285 nu : array_like
286 Optical frequency to be converted.
288 Returns
289 -------
290 lambda : float or array of floats
291 Equivalent wavelength(s).
293 Notes
294 -----
295 Computes ``lambda = c / nu`` where c = 299792458.0, i.e., the
296 (vacuum) speed of light in meters/second.
298 Examples
299 --------
300 >>> from scipy.constants import nu2lambda, speed_of_light
301 >>> nu2lambda(np.array((1, speed_of_light)))
302 array([ 2.99792458e+08, 1.00000000e+00])
304 """
305 return c / _np.asanyarray(nu)