-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
abs() is slow for complex, add abs2() #3994
Comments
I get the opposite results (numpy 1.7.1, MacBook Pro):
|
hypot is not efficient compared to the the naive approach, it must use a different slower algorithm in order to avoid overflows and underflows, see the fallback implementation in npy_math.c, glibcs implementation is even slower as it has a rounding error of 1 ulp. that it is faster on mac os I can only explain by that their implementation does not care about the correctness of the result and just uses the naive approach. |
I'm not so sure that hypot should necessarily be slower. The naive aproach is looping over the array 3 times and calling pow twice. There is certainly similar checks for inf and nan etc in pow to what is found in hypot. If speed is desired, you probably want 'b.real*b.real' anyway, even if small integer powers are special cases in pow. |
the naive approach calls square, not pow, also the square root and addition are vectorized in numpy 1.8. |
I won't copy the assembly here (the object code is in I would not be averse to a |
just for reference the jist of the glibc implementation is:
in sysdeps/ieee754/dbl-64/e_hypot.c maybe a sort of precision state would be useful. e.g. on np.precisionstate("fast") numpy chooses faster but not always correct functions. Could also be useful for complex division. |
I checked for |
Yeah, I think the difference between your timings and mine is that OS X's implementation of |
similar results as OP on Python3.5.2 / Linux 4.10.0 / ARMv7 this issue still seems relevant. |
Hi, As I mentioned in #19321 I have currently some bandwidth and would like to contribute to Numpy. Is this issue something that needs to be done? |
Hello @dlindelof , |
Ok I'll give it a shot then, thanks. |
Note that discussion has not quite converged, but the code would be the same whether it is in numpy or scipy.special anyway. It is probably a function where clever use of vectorization could have fairly large impact. |
I would like this to get attention again. I.e. the What are the thoughts here? |
Same as in #13179, more or less. If you make a PR adding To get it into |
I'd like to take a shot at implementing this--I began work on this in June that was put on hold, I'll let you know when I have something that works in |
For the array API standard, see also data-apis/array-api#153 and maybe especially the discussion following data-apis/array-api#153 (comment) - complex values are just used infrequently enough to somewhat slip through the cracks. Though adding to #13179 may be useful too - others may be missing different functions than I was! |
Implement the absolute square function in scipy.special as suggested in numpy/numpy#3994. See also numpy/numpy#13179.
I've implemented a first version of |
A proposal has been made for adding |
See the following code (run on Ubuntu 64bits):
abs()
is slow compared to the manual formula. Is there any drawback in usingnp.sqrt(b.real**2 + b.imag**2)
(like possible overflow ?)Also, it could be useful to add an
abs2()
function which would return the value ofabs()**2
but should be really faster for complex:It is commonly used to compute the energy of a signal or to sort complex numbers by norm value, for example.
The text was updated successfully, but these errors were encountered: