numpy.minimum does not seem to work for complex numbers:
np.minimum(5+3*1j,4+30*1j)
(4+30j)
I want to keep the value with the maximum magnitude. It only compares the real part. Any other function for elementwise minimum comparison? MATLAB min does work with complex numbers. Thanks
If you want to compare complex numbers by magnitude, you can use built-in abs and then compare results:
>>> np.minimum(abs(5+3*1j),abs(4+30*1j))
5.830951894845301
To get a minimal initial number instead of its magnitude, use argmin:
>>> nums = [5+3*1j, 4+30*1j]
...
>>> nums[np.abs(nums).argmin()]
(5+3j)
You can use np.where and np.abs
Suppose you have
vals1 = [4+30*1j, 7+3*1j, 4+30*1j]
vals2 = [5+3*1j, 6+3*1j, 5+3*1j]
then
>>> np.where(np.abs(vals1) > np.abs(vals2), vals2, vals1)
array([5.+3.j, 6.+3.j, 5.+3.j])
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