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

Travis Oliphant oliphant at
Wed Oct 11 18:21:44 CDT 2006

Stefan van der Walt wrote:

>I agree with Fernando on this one.
>Further, if I understand correctly, changing sqrt and power to give
>the right answer by default will slow things down somewhat.  But is it
>worth sacrificing intuitive usage for speed?
For NumPy, yes. 

This is one reason that NumPy by itself is not a MATLAB replacement. 

>N.power(2,-2) == 0
>N.sqrt(-1) == nan
>just doesn't feel right.  

Only because your expectations are that NumPy *be* a MATLAB 
replacement.  The problem is that it sacrifices too much for that to be 
the case.   And we all realize that NumPy needs more stuff added to it 
to be like IDL/MATLAB such as SciPy, Matplotlib, IPython, etc.

>Why not then have
>N.power(2,-2) == 0.24
>N.sqrt(-1) == 1j
>and write a special function that does fast calculation of
>square-roots for positive values?

We've already done this.  The special functions are called


(notice that if you do numpy.sqrt(-1+0j) you get the "expected" answer 
emphasizing that numpy does no "argument" checking to determine the output).

The "intuitive" functions (which must do argument checking) are (in 
numpy.lib.scimath) but exported as

scipy.power (actually I need to check that one...)

What could be simpler?  ;-)


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