[Numpy-discussion] Infinity as nan as real part

Robert Kern robert.kern@gmail....
Wed Jun 25 15:30:39 CDT 2008


On Wed, Jun 25, 2008 at 15:16, Stéfan van der Walt <stefan@sun.ac.za> wrote:
> 2008/6/25 Robert Kern <robert.kern@gmail.com>:
>> In [1]: from numpy import *
>>
>> In [3]: 1j == (0+1j)
>> Out[3]: True
>>
>> In [4]: 0*inf
>> Out[4]: nan
>
> Fair enough.
>
> How about
>
> z = np.complex(0, np.inf)
> z**2 == (nannanj)
>
> Shouldn't that be -inf?

Well, complex(-inf, nan).

It's a bit difficult with standard type conversion rules, though. You
have to special-case integer powers (probably worthwhile, but it is
another special case). In this case, this is Python's problem, not
numpy's. numpy.complex is just __builtin__.complex. With numpy arrays,
we get the expected result.

In [9]: from numpy import *

In [10]: za = array([complex(0,inf)])

In [11]: za**2
Out[11]: array([-Inf NaNj])

In [12]: za*za
Out[12]: array([-Inf NaNj])

Unfortunately, not so with numpy scalars:

In [17]: z = complex(0, inf)

In [18]: z128 = complex128(z)

In [19]: z128**2
Out[19]: (nan+nanj)

In [20]: z64 = complex64(z)

In [21]: z64**2
Out[21]: (nan+nanj)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco


More information about the Numpy-discussion mailing list