简体   繁体   English

操作:保存变量然后操作 vs 单行

[英]Operations: Saving in Variables Then Operating vs Single Liners

I am writing a program in Python (using the numpy package).我正在用 Python 编写程序(使用 numpy 包)。 I am writing a program that contains a very long function that involves many terms:我正在编写一个程序,其中包含一个很长的函数,其中涉及许多术语:

result = a + b + c + d +...

...whatever. ...任何。 These terms a, b, c, d, etc...themselves are matrices that involve many operations, for example in Python code:这些术语 a、b、c、d 等……它们本身就是涉及许多操作的矩阵,例如在 Python 代码中:

a = np.identity(3, dtype = np.double)/3.0
b = np.kron(vec1, vec2).reshape(3,3) # Also with np.double precision.

Just taking two variables, I have been wondering if doing:只取两个变量,我一直想知道是否这样做:

a = np.identity(3, dtype = np.double)/3.0
b = np.kron(vec1, vec2).reshape(3,3) # Also with np.double precision.
c = a + b

is the same as doing:与执行相同:

c = np.identity(3, dtype = np.double)/3.0 + np.kron(vec1, vec2).reshape(3,3)

This may sound silly, but I require a very high numerical stability, ie, introducing numerical errors, as subtle as they are, might ruing the program or yield a weird result.这听起来可能很愚蠢,但我需要非常高的数值稳定性,即引入数值错误,尽管它们很微妙,但可能会破坏程序或产生奇怪的结果。 Of course, this question can be extended to other programming languages.当然,这个问题可以扩展到其他编程语言。

Which is suggested?建议使用哪个? Does it matter?有关系吗? Any suggested references?有什么推荐的参考吗?

Under "normal" circumstances, both approaches are equivalent.在“正常”情况下,两种方法是等效的。

In other words, whether you use a value through an explicit expression (eg, np.identity(3, dtype = np.double)/3.0 ) or through a variable-name that has been initialized with that expression (here, a ), the outcome would "normally" be the same.换句话说,无论您是通过显式表达式(例如np.identity(3, dtype = np.double)/3.0 )还是通过已使用该表达式初始化的变量名(此处为a )使用值,结果“通常”是一样的。

There are some not-so-normal circumstances, where they may produce different results.有一些不太正常的情况,它们可能会产生不同的结果。 As far as I can see all these have to do with situations in which there are side-effects such that the outcome depends upon the order in which things happen.据我所知,所有这些都与存在副作用的情况有关,因此结果取决于事情发生的顺序。 For example:例如:

Consider a scenario where the initialization of the variable-name b involves a side-effect that affects the initialization of the variable-name a .考虑一个场景,其中变量名b的初始化涉及影响变量名a初始化的副作用。 And let's say your code depends on that side-effect.假设您的代码取决于该副作用。 In this scenario, in the case of the fist approach (where you first initialize the variable-names and then use only those variables), your code would have to initialize b first, and a later -- the order of the initialization of the variable-names matters.在这种情况下,在拳头的方法(在这里你首先初始化变量,名称,然后只使用这些变量)的情况下,你的代码必须初始化b第一,和a后-变量的初始化顺序-名字很重要。 In the second approach (where you would have explicit expressions rather than variable-names, participating in a larger expression), to achieve the same effect, you will have to pay attention to the order in which Python interpreter evaluates sub-expressions within an expression.在第二种方法中(您将使用显式表达式而不是变量名,参与更大的表达式),要达到相同的效果,您必须注意 Python 解释器在表达式中计算子表达式的顺序. If you don't, then the order of evaluation of sub-expressions may not produce the side-effect that your code needs, and you might end up getting a different result.如果不这样做,则子表达式的计算顺序可能不会产生您的代码所需的副作用,并且您最终可能会得到不同的结果。

As for other programming languages the answer is a big yes , the two approaches can yield different results, in languages (such as Java), where the variable-names have associated data-types, which can cause some silent numerical conversions (such as truncations) to happen during variable-assignment.至于其他编程语言,答案是肯定的,这两种方法可以产生不同的结果,在语言(例如 Java)中,变量名具有关联的数据类型,这可能会导致一些无声的数字转换(例如截断) 在变量赋值期间发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM