Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.minimum for complex numbers

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

like image 628
torpedo Avatar asked Oct 22 '25 10:10

torpedo


2 Answers

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)
like image 59
Oleh Rybalchenko Avatar answered Oct 24 '25 23:10

Oleh Rybalchenko


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])
like image 37
rafaelc Avatar answered Oct 25 '25 00:10

rafaelc