unexpected behavior for numpy.real_if_close()
See original GitHub issueI am working my way through the scipy tutorial, and I encountered a behavior of the function numpy.real_if_close that I don’t quite understand From the documentation of real_if_close, real, and imag the parameters of the three functions are described the same way, therefore I am a little lost in their different behavior with respect to the same object.
Reproducing code example:
b = np.array([1+1j, 2+1j, 3+1j, 4+5e-15j])
print(b)
>>>[1.+1.e+00j 2.+1.e+00j 3.+1.e+00j 4.+5.e-15j]
print(np.real(b))
>>>[1. 2. 3. 4.]
print(b.real)
>>>[1. 2. 3. 4.]
print(np.imag(b))
>>>[0. 0. 0. 5.]
print(b.imag)
>>>[0. 0. 0. 5.]
print(np.finfo(float).eps)
>>>2.220446049250313e-16
print(b) # <-- to make sure I didn't change b
>>>[1.+1.e+00j 2.+1.e+00j 3.+1.e+00j 4.+5.e-15j]
print(np.real_if_close(b, tol=1000)) # <-- does not perform the approx 4+5e-14j ~= 4
>>>[1.+1.e+00j 2.+1.e+00j 3.+1.e+00j 4.+5.e-15j]
print(b.real_if_close(tol=1000)) # <-- raises an AttributeError
>>>AttributeError: 'numpy.ndarray' object has no attribute 'real_if_close'
print(np.real_if_close([2.1 + 4e-14j], tol=1000)) # <-- example from the tutorial
>>>[2.1]
Numpy/Python version information:
1.17.4 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Unexpected behavior of numpy.real_if_close compared to ...
Function real_if_close will only convert the array to real if all the elements have imaginary part close to zero. Your b.imag is actually:...
Read more >Unexpected behavior for **= when __numpy_ufunc__ is ... - GitHub
I've been working on getting Pint ready for the new __numpy_ufunc__ functionality, but I've run into a snag. I've done my best to...
Read more >numpy.real_if_close — NumPy v1.24 Manual
Parameters: aarray_like. Input array. tolfloat. Tolerance in machine epsilons for the complex part of the elements in the array.
Read more >Guide to NumPy
a general class so that function behavior is the same. All ufuncs perform element- by-element operations over an array or a set of...
Read more >[Solved]-Unexpected numpy.unique behavior-numpy
TL;DR. Solve the issue by changing the default fill value for masked arrays: import numpy as np x = np.array([64, 0, 1, 2,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Sure! I’ll try to update the documentation, also with my pitfall example, so that there might be less confusion for the time being.
The function only casts if all of the numbers are almost real. I am wondering if we should deprecate it, at least in documentation. I seem to remember others being confused about it before, and I feel it is useful, but not sure it is useful enough (and tricky from a type safety perspective, so more a user/convenience than a library function). I.e. it would be nicer to write something:
Writing such a function yourself should be fairly easy (with whatever “close” semantics you want, another issue since it only supports absolute, not relative semantics).