Exponential in Python - Should I use the ** operator instead of math.pow and math.sqrt?

In my area, the square number of numbers is very common, manage them together and take the square root of the result. This is done in the Pythagorean theorem and, for example, the calculation of RMS.

In numpy, I did the following:

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

And in pure python, something like this was expected:

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

However, I used this pure python form, as I find it more compact, import-independent, and apparently equivalent:

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

I heard that some people claim that the ** operator is a kind of hack, and squaring a number by exposing it to 0.5 not readable. But I would like to ask if:

"Is there any COMPUTER reason to prefer the previous two alternatives over the third (s)?"

Thanks for reading!

+46
python math exponentiation
Sep 23 '13 at 17:40
source share
2 answers

math.sqrt is a square root C implementation and therefore differs from use with 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 really is a computational reason to prefer an implementation of the numpy or math module over the built-in. In particular, sqrt functions are probably implemented in the most efficient way, while ** works with a large number of bases and exponents and is probably not optimized for the specific case of the square root. On the other hand, the pow built-in function handles several additional cases, such as "complex numbers, unbounded integers, and modular exponentiation."

See this stack overflow question for more information on the difference between ** and math.sqrt .

In terms that are more Pythonic, I think we need to discuss the very definition of the word. From the official Python glossary , it says that a piece of code or a Pythonic idea, if it β€œfollows the most common idioms of the Python language, rather than injecting code using concepts common to other languages.” Every other language I can think of has a math module with basic square root functions. However, there are languages ​​in which there is no power operator, for example ** , for example. C ++. So ** is probably more Pythonic, but regardless of whether it is objectively dependent on usage.

+42
Sep 23 '13 at 18:02
source share

Even in basic Python, you can do the calculation in general form

 result = sum(x**2 for x in some_vector) ** 0.5 

x ** 2 , of course, is not a hack, and the calculation is the same (I checked with the cpython source code). I actually find it more readable (and readable).

Using x ** 0.5 instead to take the square root, does not perform the same calculation as math.sqrt , since the first (possibly) is calculated using logarithms, and the second (possibly) using a specific numerical instruction of the mathematical processor.

I often use x ** 0.5 just because I don't want to add math just for this. I would expect, however, that a specific instruction for a square root works better (more precisely) than a multi-step operation with logarithms.

+12
Sep 23 '13 at 18:00
source share



All Articles