Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exponentiation in Python - should I prefer ** operator instead of math.pow and math.sqrt? [duplicate]

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.

In numpy, I have done the following:

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

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. 

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 

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)?"

Thanks for reading!

like image 502
heltonbiker Avatar asked Sep 23 '13 17:09

heltonbiker


People also ask

Which is the correct operator for exponentiation in Python?

The operator that can be used to perform the exponent arithmetic in Python is ** . Given two real number operands, one on each side of the operator, it performs the exponential calculation ( 2**5 translates to 2*2*2*2*2 ).

Is NP sqrt faster than math sqrt?

For scalar math, Python functions are faster than numpy functions. It turns out that the sqrt() function from the standard Python math module is about seven times faster than the corresponding sqrt() function from numpy.

Does math POW work in Python?

Java and Python both come with a library of math functions that allow users to perform a variety of mathematical operations. One useful function is pow() which can be found in the java. lang. Math package in Java and the math module in Python.


1 Answers

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".

See this Stack Overflow question for more information on the difference between ** and math.sqrt.

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.

like image 195
Shashank Avatar answered Sep 17 '22 12:09

Shashank