📜  SciPy – 常量

📅  最后修改于: 2022-05-13 01:55:13.739000             🧑  作者: Mango

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 的大小相关的单位和不确定度 

蟒蛇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 模块中最常用的常量:

ConstantsDescription
piMathematical pi value
goldenMathematicalgolden ratio
cSpeed of light in vacuum
speed_of_lightSpeed of light in vacuum
GStandard acceleration of gravity
GNewton Constant of gravitation
EElementary charge
RMolar gas constant
AlphaFine-structure constant
N_AAvagadro constant
KBoltzmann constant
SigmaStefan-Boltzmann constant σ
m_eElectron mass
m_pProton mass
m_nNeutron Mass
HPlank Constant
Plank constantPlank constant h

以下是 SciPy 模块中可用的单位常量:

  • 大量的:
UnitDescription
GramOne gram in Kilogram.
GrainOne grain in Kilogram.
PoundOne Pound in Kilogram.
OunceOne Ounce in Kilogram.
automic_massAtomics mass constant in Kilogram.
  • 时间:
UnitDescription
MinuteOne minute in seconds.
HourOne hour in seconds.
DayOne day in seconds.
YearOne year in seconds.
  • 长度:
UnitsDescription
InchOne inch in meters.
FootOne foot in meters.
YardOne yard in meters.
PtOne point in meters.
MicronOne Micron in meters.
  • 压力:
UnitsDescription
AtmThe standard atmosphere in pascals.
AtmosphereThe standard atmosphere in pascals.
BarOne bar in Pascals.
TorrOne torr(mmHg) in pascals.
  • 区域:
UnitsDescription
HectareOne hectare in square meters.
AcreOne acre in square meters.
  • 速度:
UnitsDescription
KmhKilometer per hour in meter per second.
MphMiles per hour in meter per second.
MachOne Match in meter per second.