SciPy – 常量
Scipy代表科学Python ,在任何科学/数学计算中,我们经常需要通用常量来执行任务,一个著名的例子是计算圆的面积 = 'pi*r*r',其中 PI = 3.14...或更复杂的一个喜欢发现 力重力= G*M*m ⁄(距离) 2 其中 G = 引力常数。在所有这些情况下,如果我们有参考资料来查找这些常数并将它们轻松地合并到我们的计算中,那将非常方便。
Scipy-Constants 是 Scipy 库中的一个子模块,它为我们完成了这项工作。它包含通用数学常数、物理常数和单位的详尽列表。只需 1 行代码即可查找。
访问常量
只需在“ scipy.constants.XXXX ”格式中输入常量名称代替 XXXX 即可访问其值。下面列出了使用 scipy.constant 模块的一些最重要的常量。该列表并不详尽,但它提供了如何访问常量的好主意。
Python3
# import module
import scipy
# Just type the name of the constant in
# scipy.constant.XXXX format to access its value.
print("sciPy - pi:", scipy.constants.pi)
print("Golden ratio:", scipy.constants.golden_ratio)
print("Speed of light in vaccum:", scipy.constants.c)
print("Gravitational Constant:", scipy.constants.G)
print("Molar Gas Constant:", scipy.constants.R)
print("Boltzman Constant:", scipy.constants.k)
print("Proton mass Constant:", scipy.constants.proton_mass)
Python3
import scipy
# find method looks up in the dictorary and
# finds out all the constants containing
# 'electron' word in it and returns a list
# of constants.
res = scipy.constants.find("electron")
print(res, end='\n')
Python3
import scipy
# This returns a tuple (value, unit, uncertainty)
# associated with the physical constant
print(scipy.constants.physical_constants['alpha particle mass'])
Python3
import scipy
# Area of a circle using
# scipy.constants.pi
def Area_of_Circle(r):
return scipy.constants.pi * r * r
# Calculates the gravational for
def force_gravity(M, m, dist):
return (scipy.constants.G*M*m) / (dist**2)
print(f'Area of Circle: {Area_of_Circle(5)}')
print(f'Gravitational force: {force_gravity(10,5,1)}')
输出:
寻找常数
我们可以使用内置方法来查找与我们的用例相关的常量。常量使用字典数据结构存储,我们可以使用scipy. constants.find() API 来查找所有相关的常量 从 dict 并相应地使用它们。
下面的代码演示了如何使用 scipy。常量.find() API。下面的代码打印了所有包含“电子”字的常量,我们可以过滤掉需要的常量。
蟒蛇3
import scipy
# find method looks up in the dictorary and
# finds out all the constants containing
# 'electron' word in it and returns a list
# of constants.
res = scipy.constants.find("electron")
print(res, end='\n')
输出:
不仅是常量的大小,我们还可以使用格式访问与存储在 scipy.constants 模块中的任何 physical_constants 的大小相关的单位和不确定度
physical_constants[name] = (value, unit, uncertainty).
蟒蛇3
import scipy
# This returns a tuple (value, unit, uncertainty)
# associated with the physical constant
print(scipy.constants.physical_constants['alpha particle mass'])
输出:
(6.6446573357e-27, 'kg', 2e-36)
例子:
蟒蛇3
import scipy
# Area of a circle using
# scipy.constants.pi
def Area_of_Circle(r):
return scipy.constants.pi * r * r
# Calculates the gravational for
def force_gravity(M, m, dist):
return (scipy.constants.G*M*m) / (dist**2)
print(f'Area of Circle: {Area_of_Circle(5)}')
print(f'Gravitational force: {force_gravity(10,5,1)}')
输出:
除了上述变量外,scipy.constants 还包含更多的物理常量,下面列出了 scipy.constants 模块中所有可用的方法,并附有说明。
以下是 SciPy 模块中最常用的常量:Constants Description pi Mathematical pi value golden Mathematicalgolden ratio c Speed of light in vacuum speed_of_light Speed of light in vacuum G Standard acceleration of gravity G Newton Constant of gravitation E Elementary charge R Molar gas constant Alpha Fine-structure constant N_A Avagadro constant K Boltzmann constant Sigma Stefan-Boltzmann constant σ m_e Electron mass m_p Proton mass m_n Neutron Mass H Plank Constant Plank constant Plank constant h
以下是 SciPy 模块中可用的单位常量:
- 大量的:
Unit | Description |
---|---|
Gram | One gram in Kilogram. |
Grain | One grain in Kilogram. |
Pound | One Pound in Kilogram. |
Ounce | One Ounce in Kilogram. |
automic_mass | Atomics mass constant in Kilogram. |
- 时间:
Unit | Description |
---|---|
Minute | One minute in seconds. |
Hour | One hour in seconds. |
Day | One day in seconds. |
Year | One year in seconds. |
- 长度:
Units | Description |
---|---|
Inch | One inch in meters. |
Foot | One foot in meters. |
Yard | One yard in meters. |
Pt | One point in meters. |
Micron | One Micron in meters. |
- 压力:
Units | Description |
---|---|
Atm | The standard atmosphere in pascals. |
Atmosphere | The standard atmosphere in pascals. |
Bar | One bar in Pascals. |
Torr | One torr(mmHg) in pascals. |
- 区域:
Units | Description |
---|---|
Hectare | One hectare in square meters. |
Acre | One acre in square meters. |
- 速度:
Units | Description |
---|---|
Kmh | Kilometer per hour in meter per second. |
Mph | Miles per hour in meter per second. |
Mach | One Match in meter per second. |