some new features
This commit is contained in:
368
.venv/lib/python3.12/site-packages/scipy/constants/_constants.py
Normal file
368
.venv/lib/python3.12/site-packages/scipy/constants/_constants.py
Normal file
@ -0,0 +1,368 @@
|
||||
"""
|
||||
Collection of physical constants and conversion factors.
|
||||
|
||||
Most constants are in SI units, so you can do
|
||||
print '10 mile per minute is', 10*mile/minute, 'm/s or', 10*mile/(minute*knot), 'knots'
|
||||
|
||||
The list is not meant to be comprehensive, but just convenient for everyday use.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math as _math
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from ._codata import value as _cd
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import numpy.typing as npt
|
||||
|
||||
from scipy._lib._array_api import array_namespace, _asarray
|
||||
|
||||
|
||||
"""
|
||||
BasSw 2006
|
||||
physical constants: imported from CODATA
|
||||
unit conversion: see e.g., NIST special publication 811
|
||||
Use at own risk: double-check values before calculating your Mars orbit-insertion burn.
|
||||
Some constants exist in a few variants, which are marked with suffixes.
|
||||
The ones without any suffix should be the most common ones.
|
||||
"""
|
||||
|
||||
__all__ = [
|
||||
'Avogadro', 'Boltzmann', 'Btu', 'Btu_IT', 'Btu_th', 'G',
|
||||
'Julian_year', 'N_A', 'Planck', 'R', 'Rydberg',
|
||||
'Stefan_Boltzmann', 'Wien', 'acre', 'alpha',
|
||||
'angstrom', 'arcmin', 'arcminute', 'arcsec',
|
||||
'arcsecond', 'astronomical_unit', 'atm',
|
||||
'atmosphere', 'atomic_mass', 'atto', 'au', 'bar',
|
||||
'barrel', 'bbl', 'blob', 'c', 'calorie',
|
||||
'calorie_IT', 'calorie_th', 'carat', 'centi',
|
||||
'convert_temperature', 'day', 'deci', 'degree',
|
||||
'degree_Fahrenheit', 'deka', 'dyn', 'dyne', 'e',
|
||||
'eV', 'electron_mass', 'electron_volt',
|
||||
'elementary_charge', 'epsilon_0', 'erg',
|
||||
'exa', 'exbi', 'femto', 'fermi', 'fine_structure',
|
||||
'fluid_ounce', 'fluid_ounce_US', 'fluid_ounce_imp',
|
||||
'foot', 'g', 'gallon', 'gallon_US', 'gallon_imp',
|
||||
'gas_constant', 'gibi', 'giga', 'golden', 'golden_ratio',
|
||||
'grain', 'gram', 'gravitational_constant', 'h', 'hbar',
|
||||
'hectare', 'hecto', 'horsepower', 'hour', 'hp',
|
||||
'inch', 'k', 'kgf', 'kibi', 'kilo', 'kilogram_force',
|
||||
'kmh', 'knot', 'lambda2nu', 'lb', 'lbf',
|
||||
'light_year', 'liter', 'litre', 'long_ton', 'm_e',
|
||||
'm_n', 'm_p', 'm_u', 'mach', 'mebi', 'mega',
|
||||
'metric_ton', 'micro', 'micron', 'mil', 'mile',
|
||||
'milli', 'minute', 'mmHg', 'mph', 'mu_0', 'nano',
|
||||
'nautical_mile', 'neutron_mass', 'nu2lambda',
|
||||
'ounce', 'oz', 'parsec', 'pebi', 'peta',
|
||||
'pi', 'pico', 'point', 'pound', 'pound_force',
|
||||
'proton_mass', 'psi', 'pt', 'quecto', 'quetta', 'ronna', 'ronto',
|
||||
'short_ton', 'sigma', 'slinch', 'slug', 'speed_of_light',
|
||||
'speed_of_sound', 'stone', 'survey_foot',
|
||||
'survey_mile', 'tebi', 'tera', 'ton_TNT',
|
||||
'torr', 'troy_ounce', 'troy_pound', 'u',
|
||||
'week', 'yard', 'year', 'yobi', 'yocto',
|
||||
'yotta', 'zebi', 'zepto', 'zero_Celsius', 'zetta'
|
||||
]
|
||||
|
||||
|
||||
# mathematical constants
|
||||
pi = _math.pi
|
||||
golden = golden_ratio = (1 + _math.sqrt(5)) / 2
|
||||
|
||||
# SI prefixes
|
||||
quetta = 1e30
|
||||
ronna = 1e27
|
||||
yotta = 1e24
|
||||
zetta = 1e21
|
||||
exa = 1e18
|
||||
peta = 1e15
|
||||
tera = 1e12
|
||||
giga = 1e9
|
||||
mega = 1e6
|
||||
kilo = 1e3
|
||||
hecto = 1e2
|
||||
deka = 1e1
|
||||
deci = 1e-1
|
||||
centi = 1e-2
|
||||
milli = 1e-3
|
||||
micro = 1e-6
|
||||
nano = 1e-9
|
||||
pico = 1e-12
|
||||
femto = 1e-15
|
||||
atto = 1e-18
|
||||
zepto = 1e-21
|
||||
yocto = 1e-24
|
||||
ronto = 1e-27
|
||||
quecto = 1e-30
|
||||
|
||||
# binary prefixes
|
||||
kibi = 2**10
|
||||
mebi = 2**20
|
||||
gibi = 2**30
|
||||
tebi = 2**40
|
||||
pebi = 2**50
|
||||
exbi = 2**60
|
||||
zebi = 2**70
|
||||
yobi = 2**80
|
||||
|
||||
# physical constants
|
||||
c = speed_of_light = _cd('speed of light in vacuum')
|
||||
mu_0 = _cd('vacuum mag. permeability')
|
||||
epsilon_0 = _cd('vacuum electric permittivity')
|
||||
h = Planck = _cd('Planck constant')
|
||||
hbar = h / (2 * pi)
|
||||
G = gravitational_constant = _cd('Newtonian constant of gravitation')
|
||||
g = _cd('standard acceleration of gravity')
|
||||
e = elementary_charge = _cd('elementary charge')
|
||||
R = gas_constant = _cd('molar gas constant')
|
||||
alpha = fine_structure = _cd('fine-structure constant')
|
||||
N_A = Avogadro = _cd('Avogadro constant')
|
||||
k = Boltzmann = _cd('Boltzmann constant')
|
||||
sigma = Stefan_Boltzmann = _cd('Stefan-Boltzmann constant')
|
||||
Wien = _cd('Wien wavelength displacement law constant')
|
||||
Rydberg = _cd('Rydberg constant')
|
||||
|
||||
# mass in kg
|
||||
gram = 1e-3
|
||||
metric_ton = 1e3
|
||||
grain = 64.79891e-6
|
||||
lb = pound = 7000 * grain # avoirdupois
|
||||
blob = slinch = pound * g / 0.0254 # lbf*s**2/in (added in 1.0.0)
|
||||
slug = blob / 12 # lbf*s**2/foot (added in 1.0.0)
|
||||
oz = ounce = pound / 16
|
||||
stone = 14 * pound
|
||||
long_ton = 2240 * pound
|
||||
short_ton = 2000 * pound
|
||||
|
||||
troy_ounce = 480 * grain # only for metals / gems
|
||||
troy_pound = 12 * troy_ounce
|
||||
carat = 200e-6
|
||||
|
||||
m_e = electron_mass = _cd('electron mass')
|
||||
m_p = proton_mass = _cd('proton mass')
|
||||
m_n = neutron_mass = _cd('neutron mass')
|
||||
m_u = u = atomic_mass = _cd('atomic mass constant')
|
||||
|
||||
# angle in rad
|
||||
degree = pi / 180
|
||||
arcmin = arcminute = degree / 60
|
||||
arcsec = arcsecond = arcmin / 60
|
||||
|
||||
# time in second
|
||||
minute = 60.0
|
||||
hour = 60 * minute
|
||||
day = 24 * hour
|
||||
week = 7 * day
|
||||
year = 365 * day
|
||||
Julian_year = 365.25 * day
|
||||
|
||||
# length in meter
|
||||
inch = 0.0254
|
||||
foot = 12 * inch
|
||||
yard = 3 * foot
|
||||
mile = 1760 * yard
|
||||
mil = inch / 1000
|
||||
pt = point = inch / 72 # typography
|
||||
survey_foot = 1200.0 / 3937
|
||||
survey_mile = 5280 * survey_foot
|
||||
nautical_mile = 1852.0
|
||||
fermi = 1e-15
|
||||
angstrom = 1e-10
|
||||
micron = 1e-6
|
||||
au = astronomical_unit = 149597870700.0
|
||||
light_year = Julian_year * c
|
||||
parsec = au / arcsec
|
||||
|
||||
# pressure in pascal
|
||||
atm = atmosphere = _cd('standard atmosphere')
|
||||
bar = 1e5
|
||||
torr = mmHg = atm / 760
|
||||
psi = pound * g / (inch * inch)
|
||||
|
||||
# area in meter**2
|
||||
hectare = 1e4
|
||||
acre = 43560 * foot**2
|
||||
|
||||
# volume in meter**3
|
||||
litre = liter = 1e-3
|
||||
gallon = gallon_US = 231 * inch**3 # US
|
||||
# pint = gallon_US / 8
|
||||
fluid_ounce = fluid_ounce_US = gallon_US / 128
|
||||
bbl = barrel = 42 * gallon_US # for oil
|
||||
|
||||
gallon_imp = 4.54609e-3 # UK
|
||||
fluid_ounce_imp = gallon_imp / 160
|
||||
|
||||
# speed in meter per second
|
||||
kmh = 1e3 / hour
|
||||
mph = mile / hour
|
||||
# approx value of mach at 15 degrees in 1 atm. Is this a common value?
|
||||
mach = speed_of_sound = 340.5
|
||||
knot = nautical_mile / hour
|
||||
|
||||
# temperature in kelvin
|
||||
zero_Celsius = 273.15
|
||||
degree_Fahrenheit = 1/1.8 # only for differences
|
||||
|
||||
# energy in joule
|
||||
eV = electron_volt = elementary_charge # * 1 Volt
|
||||
calorie = calorie_th = 4.184
|
||||
calorie_IT = 4.1868
|
||||
erg = 1e-7
|
||||
Btu_th = pound * degree_Fahrenheit * calorie_th / gram
|
||||
Btu = Btu_IT = pound * degree_Fahrenheit * calorie_IT / gram
|
||||
ton_TNT = 1e9 * calorie_th
|
||||
# Wh = watt_hour
|
||||
|
||||
# power in watt
|
||||
hp = horsepower = 550 * foot * pound * g
|
||||
|
||||
# force in newton
|
||||
dyn = dyne = 1e-5
|
||||
lbf = pound_force = pound * g
|
||||
kgf = kilogram_force = g # * 1 kg
|
||||
|
||||
# functions for conversions that are not linear
|
||||
|
||||
|
||||
def convert_temperature(
|
||||
val: npt.ArrayLike,
|
||||
old_scale: str,
|
||||
new_scale: str,
|
||||
) -> Any:
|
||||
"""
|
||||
Convert from a temperature scale to another one among Celsius, Kelvin,
|
||||
Fahrenheit, and Rankine scales.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
val : array_like
|
||||
Value(s) of the temperature(s) to be converted expressed in the
|
||||
original scale.
|
||||
old_scale : str
|
||||
Specifies as a string the original scale from which the temperature
|
||||
value(s) will be converted. Supported scales are Celsius ('Celsius',
|
||||
'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'),
|
||||
Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f'), and Rankine
|
||||
('Rankine', 'rankine', 'R', 'r').
|
||||
new_scale : str
|
||||
Specifies as a string the new scale to which the temperature
|
||||
value(s) will be converted. Supported scales are Celsius ('Celsius',
|
||||
'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'),
|
||||
Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f'), and Rankine
|
||||
('Rankine', 'rankine', 'R', 'r').
|
||||
|
||||
Returns
|
||||
-------
|
||||
res : float or array of floats
|
||||
Value(s) of the converted temperature(s) expressed in the new scale.
|
||||
|
||||
Notes
|
||||
-----
|
||||
.. versionadded:: 0.18.0
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from scipy.constants import convert_temperature
|
||||
>>> import numpy as np
|
||||
>>> convert_temperature(np.array([-40, 40]), 'Celsius', 'Kelvin')
|
||||
array([ 233.15, 313.15])
|
||||
|
||||
"""
|
||||
xp = array_namespace(val)
|
||||
_val = _asarray(val, xp=xp, subok=True)
|
||||
# Convert from `old_scale` to Kelvin
|
||||
if old_scale.lower() in ['celsius', 'c']:
|
||||
tempo = _val + zero_Celsius
|
||||
elif old_scale.lower() in ['kelvin', 'k']:
|
||||
tempo = _val
|
||||
elif old_scale.lower() in ['fahrenheit', 'f']:
|
||||
tempo = (_val - 32) * 5 / 9 + zero_Celsius
|
||||
elif old_scale.lower() in ['rankine', 'r']:
|
||||
tempo = _val * 5 / 9
|
||||
else:
|
||||
raise NotImplementedError(f"{old_scale=} is unsupported: supported scales "
|
||||
"are Celsius, Kelvin, Fahrenheit, and "
|
||||
"Rankine")
|
||||
# and from Kelvin to `new_scale`.
|
||||
if new_scale.lower() in ['celsius', 'c']:
|
||||
res = tempo - zero_Celsius
|
||||
elif new_scale.lower() in ['kelvin', 'k']:
|
||||
res = tempo
|
||||
elif new_scale.lower() in ['fahrenheit', 'f']:
|
||||
res = (tempo - zero_Celsius) * 9 / 5 + 32
|
||||
elif new_scale.lower() in ['rankine', 'r']:
|
||||
res = tempo * 9 / 5
|
||||
else:
|
||||
raise NotImplementedError(f"{new_scale=} is unsupported: supported "
|
||||
"scales are 'Celsius', 'Kelvin', "
|
||||
"'Fahrenheit', and 'Rankine'")
|
||||
|
||||
return res
|
||||
|
||||
|
||||
# optics
|
||||
|
||||
|
||||
def lambda2nu(lambda_: npt.ArrayLike) -> Any:
|
||||
"""
|
||||
Convert wavelength to optical frequency
|
||||
|
||||
Parameters
|
||||
----------
|
||||
lambda_ : array_like
|
||||
Wavelength(s) to be converted.
|
||||
|
||||
Returns
|
||||
-------
|
||||
nu : float or array of floats
|
||||
Equivalent optical frequency.
|
||||
|
||||
Notes
|
||||
-----
|
||||
Computes ``nu = c / lambda`` where c = 299792458.0, i.e., the
|
||||
(vacuum) speed of light in meters/second.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from scipy.constants import lambda2nu, speed_of_light
|
||||
>>> import numpy as np
|
||||
>>> lambda2nu(np.array((1, speed_of_light)))
|
||||
array([ 2.99792458e+08, 1.00000000e+00])
|
||||
|
||||
"""
|
||||
xp = array_namespace(lambda_)
|
||||
return c / _asarray(lambda_, xp=xp, subok=True)
|
||||
|
||||
|
||||
def nu2lambda(nu: npt.ArrayLike) -> Any:
|
||||
"""
|
||||
Convert optical frequency to wavelength.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
nu : array_like
|
||||
Optical frequency to be converted.
|
||||
|
||||
Returns
|
||||
-------
|
||||
lambda : float or array of floats
|
||||
Equivalent wavelength(s).
|
||||
|
||||
Notes
|
||||
-----
|
||||
Computes ``lambda = c / nu`` where c = 299792458.0, i.e., the
|
||||
(vacuum) speed of light in meters/second.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from scipy.constants import nu2lambda, speed_of_light
|
||||
>>> import numpy as np
|
||||
>>> nu2lambda(np.array((1, speed_of_light)))
|
||||
array([ 2.99792458e+08, 1.00000000e+00])
|
||||
|
||||
"""
|
||||
xp = array_namespace(nu)
|
||||
return c / _asarray(nu, xp=xp, subok=True)
|
||||
Reference in New Issue
Block a user