Python中的幂运算-我应该更喜欢**运算符,而不是math.pow和math.sqrt吗? [英] Exponentiation in Python - should I prefer ** operator instead of math.pow and math.sqrt?

查看:210
本文介绍了Python中的幂运算-我应该更喜欢**运算符,而不是math.pow和math.sqrt吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的领域中,对一些数字求平方,将它们运算在一起并取结果的平方根是很常见的.例如,可以用勾股定理和RMS计算来完成.

In my field it's very common to square some numbers, operate them together, and take the square root of the result. This is done in pythagorean theorem, and the RMS calculation, for example.

在numpy中,我已执行以下操作:

In numpy, I have done the following:

result = numpy.sqrt(numpy.sum(numpy.pow(some_vector, 2)))

在纯python中,应该会出现这样的情况:

And in pure python something like this would be expected:

result = math.sqrt(math.pow(A, 2) + math.pow(B,2)) # example with two dimensions.

但是,我一直使用这种纯python形式,因为我发现它更加紧凑,独立于导入并且看似等效:

However, I have been using this pure python form, since I find it much more compact, import-independent, and seemingly equivalent:

result = (A**2 + B**2)**0.5   # two dimensions
result = (A**2 + B**2 + C**2 + D**2)**0.5

我听说有人认为**运算符有点可笑,而通过0.5取幂来对数字求平方并不那么容易理解.但是我想问的是:

I have heard some people argue that the ** operator is sort of a hack, and that squaring a number by exponentiating it by 0.5 is not so readable. But what I'd like to ask is if:

有没有计算上的理由要比前三个更喜欢前两个?"

"Is there any COMPUTATIONAL reason to prefer the former two alternatives over the third one(s)?"

感谢阅读!

推荐答案

math.sqrt是平方根的C实现,因此不同于使用实现Python内置pow函数的**运算符.因此,与使用**运算符相比,使用math.sqrt实际上给出了不同的答案,并且确实有计算上的原因,而不是内置的,更倾向于使用numpymath模块实现.特别是,sqrt函数可能以最有效的方式实现,而**在大量的基数和指数上运行,并且对于平方根的特定情况可能未进行优化.另一方面,内置的pow函数处理一些额外的情况,例如复数,无穷整数幂和模幂".

math.sqrt is the C implementation of square root and is therefore different from using the ** operator which implements Python's built-in pow function. Thus, using math.sqrt actually gives a different answer than using the ** operator and there is indeed a computational reason to prefer numpy or math module implementation over the built-in. Specifically the sqrt functions are probably implemented in the most efficient way possible whereas ** operates over a large number of bases and exponents and is probably unoptimized for the specific case of square root. On the other hand, the built-in pow function handles a few extra cases like "complex numbers, unbounded integer powers, and modular exponentiation".

有关差异的更多信息,请参见此堆栈溢出问题在**math.sqrt之间.

就哪个更"Pythonic"而言,我认为我们需要讨论该词的确切定义.从官方Python词汇表中,它指出一段代码或想法如果它紧密遵循Python语言最常见的习惯用法,而不是使用其他语言的通用概念来实现代码",则为Pythonic.在我能想到的每种其他语言中,都有一些具有基本平方根函数的数学模块.但是,有些语言缺少像**这样的幂运算符,例如C ++.因此**可能更像Python,但客观上是否更好取决于使用情况.

In terms of which is more "Pythonic", I think we need to discuss the very definition of that word. From the official Python glossary, it states that a piece of code or idea is Pythonic if it "closely follows the most common idioms of the Python language, rather than implementing code using concepts common to other languages." In every single other language I can think of, there is some math module with basic square root functions. However there are languages that lack a power operator like ** e.g. C++. So ** is probably more Pythonic, but whether or not it's objectively better depends on the use case.

这篇关于Python中的幂运算-我应该更喜欢**运算符,而不是math.pow和math.sqrt吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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