Why do NumPy operations with complex infinities produce funny results?

I noticed funny results with complex infinities.

In [1]: import numpy as np In [2]: np.isinf(1j * np.inf) Out[2]: True In [3]: np.isinf(1 * 1j * np.inf) Out[3]: True In [4]: np.isinf(1j * np.inf * 1) Out[4]: False 

This is due to nan . But the end result is strange.

Is this a blunder? Anything I have to do differently?

+7
source share
1 answer

This is not a NumPy bug. numpy.inf is a regular Python float, and strange results come from the usual Python multiple multiplication algorithm, which this :

 Py_complex _Py_c_prod(Py_complex a, Py_complex b) { Py_complex r; r.real = a.real*b.real - a.imag*b.imag; r.imag = a.real*b.imag + a.imag*b.real; return r; } 

When inputs have infinite real or imaginary parts, complex multiplication tends to cause subtraction inf-inf and 0*inf multiplication, resulting in nan components as a result. We see that 1j * numpy.inf has one nan component and one inf component:

 In [5]: 1j * numpy.inf Out[5]: (nan+infj) 

and multiplying the result by 1 causes two nan components:

 In [4]: 1j * numpy.inf * 1 Out[4]: (nan+nanj) 
+6
source

All Articles