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

Travis Oliphant oliphant at
Wed Oct 11 15:41:25 CDT 2006

pearu at wrote:

>I have recieved the following note from a user:
>In SciPy 0.3.x the ufuncs were overloaded by more "intelligent" versions.
>A very attractive feature was that sqrt(-1) would yield 1j as in Matlab.
>Then you can program formulas directly (e.g., roots of a 2nd order
>polynomial) and the right answer is always achieved. In the Matlab-Python
>battle in mathematics education, this feature is important.
>Now in SciPy 0.5.x sqrt(-1) yields nan. A lot of code we have, especially
>for introductory numerics and physics courses, is now broken.
>This has already made my colleagues at the University skeptical to
>Python as "this lack of backward compatibility would never happen in Matlab".
This was a consequence of moving scipy_base into NumPy but not exposing 
the scimath library in NumPy.   It would be a very easy thing to put 
from numpy.lib.scimath import *
into the scipy name-space.

I'm supportive of that as a backward-compatibility measure.

>Another problem related to Numeric and numpy is that in these courses we
>use ScientificPython several places, which applies Numeric and will
>continue to do so. You then easily get a mix of numpy and Numeric
>in scripts, which may cause problems and at least extra overhead.
>Just converting to numpy in your own scripts isn't enough if you call
>up libraries using and returning Numeric.
>I wonder, what are the reasons that numpy.sqrt(-1) returns nan?
Because that is backwards compatible.  You have to construct a 
function-wrapper in order to handle the negative case correctly.  The 
function wrapper is going to be slower.  Thus, it is placed in a 
separate library.

>Could sqrt(-1) made to return 1j again? 
Not in NumPy.  But, in scipy it could.

>If not, shouldn't
>numpy.sqrt(-1) raise a ValueError instead of returning silently nan?
This is user adjustable.  You change the error mode to raise on 
'invalid' instead of pass silently which is now the default.


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