Should numpy.sqrt(-1) return 1j rather than nan?

Scott Sinclair sinclaird at
Thu Oct 12 02:11:48 CDT 2006

Fernando Perez wrote:
> Please note that I see a valid reason for != when
> the scipy version uses code with extra features, is faster, has
> additional options, etc.  But as I said in a previous message, I think
> that /for the same input/, we should really try to satisfy that
> == (which is NOT the same as ' is')
> within reason.

As far as I can tell this is exactly what happens. Consider the issue 
under discussion...

 >>> import numpy as np
 >>> np.sqrt(-1)
 >>> np.sqrt(-1+0j)
 >>> a = complex(-1)
 >>> np.sqrt(a)
 >>> import scipy as sp
 >>> sp.sqrt(-1)
 >>> np.sqrt(-1+0j)
 >>> sp.sqrt(a)
 >>> np.__version__
 >>> sp.__version__


I'm sure that this hasn't changed in the development versions.

Surely the point is that when your algorithm can potentially produce a 
complex result, the logical thing to do is to use a complex data type. 
In this case Numpy and Scipy behave in a way which is intuitive. If 
complex results are surprising and unexpected then the algorithm is 
probably in error or poorly understood ;-)


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list