Skip to content

SciPy - 常量

SciPy 中的 scipy.constants 模块提供了物理和数学常量的综合集合。这些常量在科学计算和工程应用中非常宝贵,能确保准确性和一致性。

可以从 scipy.constants 直接导入并使用常量。与标准库(如 math)相比,SciPy 的目标是为这些常量提供更高的精度(如果适用),尽管对于像圆周率 pi 这样的基本常量,由于标准化它们通常是等效的。

让我们比较 scipy.constants 和 Python 内建的 math 模块中 ‘pi’ 的值:

import scipy.constants
import math
# Accessing pi from scipy.constants
scipy_pi = scipy.constants.pi
# Accessing pi from math module
math_pi = math.pi
print(f"scipy.constants.pi: {scipy_pi:.16f}")
print(f"math.pi: {math_pi:.16f}")

上面的程序通常会产生以下输出,显示 pi 具有相同的精度:

scipy.constants.pi: 3.1415926535897931
math.pi: 3.1415926535897931

SciPy 提供了各种各样的常量。以下是一些常见的类别和示例。

这些包括基本的数学数字。

常量名称描述
pi圆周率 (π)
golden 或 golden_ratio黄金比例, (1 + sqrt(5))/2

大量精确测量的物理常量。scipy.constants.physical_constants 中的每个物理常量都是一个元组:(value, unit, uncertainty)。

physical_constants 中的键(示例)描述
'speed of light in vacuum'光速 (c)
'Planck constant'普朗克常数 (h)
'Newtonian constant of gravitation'引力常数 (G)
'elementary charge'基本电荷 (e)
'molar gas constant'摩尔气体常数 (R)
'Avogadro constant'阿伏伽德罗常数 (N_A)
'Boltzmann constant'玻尔兹曼常数 (k)
'electron mass'电子质量 (m_e)
'proton mass'质子质量 (m_p)
'neutron mass'中子质量 (m_n)

访问物理常量的示例:

import scipy.constants
# Get the data for the speed of light
c_data = scipy.constants.physical_constants['speed of light in vacuum']
value, unit, uncertainty = c_data
print(f"Speed of light: {value} {unit}")
print(f"Uncertainty: {uncertainty}")

输出:

Speed of light: 299792458.0 m s^-1
Uncertainty: 0.0

scipy.constants 还提供 SI 前缀和常用单位的转换因子。

前缀/单位名称值 (SI 因子或定义)
milli1e-3
micro1e-6
kilo1e3
gram1e-3 (以 kg 为单位的质量)
degreepi/180 (以弧度为单位的角度)
minute60.0 (以秒为单位的时间)
hour3600.0 (以秒为单位的时间)
day24 * 3600.0 (以秒为单位的时间)
inch0.0254 (以米为单位的长度)
light_year9.4607304725808e+15 (以米为单位的长度)
atm 或 atmosphere101325.0 (以帕斯卡为单位的压力)
eV 或 electron_volt基本电荷的值 (以焦耳为单位的能量)
hp 或 horsepower745.6998715822701 (以瓦特为单位的功率)

这些单位值可以用于转换。例如,将 1500 克转换为千克:

import scipy.constants
mass_grams = 1500
mass_kg = mass_grams * scipy.constants.gram # gram is 0.001 kg
print(f"{mass_grams} grams is {mass_kg} kilograms")

输出:

1500 grams is 1.5 kilograms

由于有如此多的可用常量,可能很难记住确切的键或名称。scipy.constants.find() 函数可以帮助您根据关键字搜索常量。它返回一个与搜索词匹配的 scipy.constants.physical_constants 中的键列表。

让我们查找与“alpha particle”(阿尔法粒子)相关的常量:

import scipy.constants
# Find constants containing 'alpha particle'
keys_found = scipy.constants.find(key='alpha particle')
print("Found keys related to 'alpha particle':")
for k in keys_found:
print(f"- {k}")
# Example: Accessing one of the found constants
if 'alpha particle mass' in keys_found:
alpha_mass_val, alpha_mass_unit, alpha_mass_unc = scipy.constants.physical_constants['alpha particle mass']
print(f"\nAlpha particle mass: {alpha_mass_val} {alpha_mass_unit}, uncertainty: {alpha_mass_unc}")

上面的程序将生成类似于此的输出(确切的列表可能随 SciPy 版本略有不同):

Found keys related to 'alpha particle':
- alpha particle mass
- alpha particle mass energy equivalent
- alpha particle mass energy equivalent in MeV
- alpha particle mass in u
- electron to alpha particle mass ratio
Alpha particle mass: 6.6446573357e-27 kg, uncertainty: 1.2e-35

scipy.constants.find() 是一个探索可用物理常量的强大工具。要获取完整列表,您还可以遍历 scipy.constants.physical_constants.keys()。

更多资源:请参阅 官方 SciPy 文档 (https://docs.scipy.org/doc/scipy/reference/constants.html) 获取完整列表和更多详细信息。