在python中命名物理量 [英] naming of physical quantities in python

查看:115
本文介绍了在python中命名物理量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的模拟代码中使用的物理/数学量建立一个好的命名方案。请考虑以下示例:

I would like to establish a good naming scheme for physical/mathematical quantities used in my simulation code. Consider the following example:

from math import *

class GaussianBeamIntensity(object):
    """
    Optical intensity profile of a Gaussian laser beam.
    """

    def __init__(self, intensity_at_waist_center, waist_radius, wavelength):
        """
        Arguments:

        *intensity_at_waist_center*: The optical intensity of the beam at the
            center of its waist in W/m^2 units.

        *waist_radius*: The radius of the beam waist in meters.

        *wavelength*: The wavelength of the laser beam in meters.

        """

        self.intensity_at_waist_center = intensity_at_waist_center
        self.waist_radius = waist_radius
        self.wavelength = wavelength
        self._calculate_auxiliary_quantities()

    def _calculate_auxiliary_quantities(self):
        # Shorthand notation
        w_0, lambda_ = self.waist_radius, self.wavelength

        self.rayleigh_range = pi * w_0**2 / lambda_
        # Generally some more quantities could follow

    def __call__(self, rho, z):
        """
        Arguments:

        *rho*, *z*: Cylindrical coordinates of a spatial point.
        """
        # Shorthand notation
        I_0, w_0 = self.intensity_at_waist_center, self.waist_radius
        z_R = self.rayleigh_range

        w_z = w_0 * sqrt(1.0 + (z / z_R)**2)
        I = I_0 * (w_0 / w_z)**2 * exp(-2.0 * rho**2 / w_z**2)
        return I

您对物理属性(属性,函数参数等)为了平衡可读性简明符号之间的平衡(公式是否相对较短)?您能否改进上面的例子或者提出一个更好的方案?

What consistent naming scheme would you propose for the physical properties (properties, function arguments etc.) in order to balance between readability and concise notation (that formulae remain relatively short)? Could you please refine the example above? Or perhaps propose a better scheme?

这将是很高兴遵循 PEP8 ,记住一个愚蠢的一致性是小脑袋的大地精。在遵守传统的80个字符长度限制的情况下,坚持描述性名称是很困难的。

It would be nice to follow the guidelines of PEP8, remembering that "A Foolish Consistency is the Hobgoblin of Little Minds". It seems difficult to stick to descriptive names while obeying the traditional 80-character limit for line lengths.

提前谢谢!

推荐答案

我认为你已经找到了很好的平衡。表达式名称很重要,因此我完全同意使用 wavelenght 而不是lambda作为类属性。

I think you've already found the good balance. Expressive names are important, so I totally agree with the use of wavelenght instead of lambda as a class attribute. This way the interface remains clear and expressive.

在一个长公式中,lambda_是一个很好的选择,作为简写符号,因为这是一个被广泛接受和广泛使用的符号光学中的波长。我认为当你实现一个公式,你想做的是保持尽可能接近你写在一张纸上的方程式的形式(或他们出现在文章等)。

In a long formula, though, lambda_ is good choice as shorthand notation, because this is a commonly accepted and widely used notation for the wavelength in optics. I think when you implement a formula, what you want to do is staying as close as possible to the form of the equations you'd write on a piece of paper (or as they appear in an article etc).

总之:保持接口的表达力,公式简短。

In short: keep the interfaces expressive, the formulae short.

这篇关于在python中命名物理量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆