Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What additional work is done by np.power?

Tags:

python

numpy

I realised that np.power(a, b) is slower than np.exp(b * np.log(a)):

import numpy as np
a, b = np.random.random((2, 100000))
%timeit np.power(a, b) # best of 3: 4.16 ms per loop
%timeit np.exp(b * np.log(a)) # best of 3: 1.74 ms per loop

The results are the same (with a few numerical errors of order 1e-16).

What additional work is done in np.power? Furthermore, how can I find an answer to these kind of questions myself?

like image 661
Jürg Merlin Spaak Avatar asked Aug 07 '17 09:08

Jürg Merlin Spaak


People also ask

What does NP power do?

The np. power() function computes exponents in Numpy. Python numpy. power() function enables us to perform the simple exponentiation like b to the power of n, and it also allows you to do this with large Numpy arrays.

How do I give power in numpy?

power() in Python. numpy. power(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is raised to the power of element from second element(all happens element-wise).


1 Answers

Under the hood both expressions call the respective C functions pow or exp and log and running a profiling on those in C++, without any numpy code, gives:

pow      : 286 ms
exp(log) :  93 ms

This is consistent with the numpy timings. It thus seems like the primary difference is that the C function pow is slower than exp(log).

Why? It seems that part of the reson is that the expressions are not equivalent for all input. For example, with negative a and integer b, power works while exp(log) fails:

>>> np.power(-2, 2)
4
>>> np.exp(2 * np.log(-2))
nan

Another example is 0 ** 0:

>>> np.power(0, 0)
1
>>> np.exp(0 * np.log(0))
nan

Hence, the exp(log) trick only works on a subset of inputs, while power works on all (valid) inputs.

In addition to this, power is guaranteed to give full precision according to the IEEE 754 standard, while exp(log) may suffer from rounding errors.

like image 155
Jonas Adler Avatar answered Oct 02 '22 20:10

Jonas Adler