I am new to python and I am converting some of Matlab scripts to Python. I reached this issue when dealing with complex arrays with numpy when I need to get the reciprocal of the array elements. If the element is 0+0j then the reciprocal is: 1/(0+0j)= inf+nanj. example:
import numpy as np
array1 = np.array([[6+2j, 6], [0, 4]])
print(array1)
reciprocal_array = 1/array1
print(reciprocal_array)
However, in Matlab the result of 1/(0+0j) is only inf but here in python the imaginary NAN makes a problem for me. I need these imaginary NAN (nanj) not to be there. Are there any solution not to have them?
I found a way to get rid of nanj in the reciprocal array after I generate it but I am asking if there is more feasible solutions.
where_are_nans = np.isnan(array1 )
array1[where_are_nans] = np.inf
Your sample array:
Make 2 arrays from this:
Your divide can also be expressed with the
divideufunc:The
ufunctakes awhereparameter, telling it where to do the division. It also needs anoutarray, telling it what to use else where:This both gives the desired result, and avoids the warnings.