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?
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With