物理类型#

物理类型对应于具有尺寸兼容单位的物理量。例如,物理类型 mass 对应于具有可转换为公斤的单位的物理量。物理类型表示为 PhysicalType 班级。

有关物理类型的列表,请参见 astropy.units.physical

访问物理类型#

vbl.使用 get_physical_type() 让我们获得 PhysicalType 实例从名称为物理类型、单位 Quantity 实例、可以变成数量(例如,数字)的对象,以及 PhysicalType 实例。

>>> import astropy.units as u
>>> u.get_physical_type('speed')  # from the name of a physical type
PhysicalType({'speed', 'velocity'})
>>> u.get_physical_type(u.meter)  # from a unit
PhysicalType('length')
>>> u.get_physical_type(1 * u.barn * u.Mpc)  # from a Quantity
PhysicalType('volume')
>>> u.get_physical_type(42)  # from a number
PhysicalType('dimensionless')

单元的物理类型可以通过其 physical_type 属性::

>>> u.coulomb.physical_type
PhysicalType('electrical charge')
>>> (u.meter ** 2).physical_type
PhysicalType('area')

使用物理类型#

之间的平等性比较 PhysicalType 然后一个字符串将返回 True 如果该字符串是 PhysicalType **

>>> acceleration = u.get_physical_type(u.m / u.s ** 2)
>>> acceleration == 'acceleration'
True

某些单位可能对应于多种物理类型,因为兼容单位可用于量化不同的现象:

>>> u.get_physical_type('pressure')
PhysicalType({'energy density', 'pressure', 'stress'})

我们可以循环访问一个 PhysicalType **

>>> for name in u.J.physical_type: print(name)
energy
torque
work

我们可以使用名称为 PhysicalType **

>>> 'energy' == u.J.physical_type
True
>>> 'work' in u.J.physical_type
True

量纲分析#

PhysicalType 实例支持乘法、除法和乘法。因此,它们可用于尺寸分析:

>>> length = u.get_physical_type('length')
>>> time = u.get_physical_type('time')
>>> length ** 2
PhysicalType('area')
>>> 1 / time
PhysicalType('frequency')

量纲分析可以在 PhysicalType 和一个单元或在一个 PhysicalType 和一个名为 PhysicalType **

>>> length ** 2 / u.s
PhysicalType({'diffusivity', 'kinematic viscosity'})
>>> length / 'time'
PhysicalType({'speed', 'velocity'})