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

A. M. Archibald peridot.faceted at gmail.com
Wed Oct 11 20:24:01 EDT 2006


On 11/10/06, pearu at cens.ioc.ee <pearu at cens.ioc.ee> wrote:

> """
> 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".
>
> 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?
> Could sqrt(-1) made to return 1j again? If not, shouldn't
> numpy.sqrt(-1) raise a ValueError instead of returning silently nan?

What is the desired behaviour of sqrt?

Should sqrt always return a complex array, regardless of the type of
its input? This will be extremely surprising to many users, whose
memory usage suddenly doubles and for whom many functions no longer
work the way they're accustomed to.

Should it return a complex array only when any entry in its input is
negative? This will be even *more* surprising when a negative (perhaps
even -0) value appears in their matrix (for example, does a+min(a)
yield -0s in the minimal values?) and suddenly it's complex.

A ValueError is also surprising, and it forces the user to sanitize
her array before taking the square root, instead of whenever
convenient.

If you want MATLAB behaviour, use only complex arrays.

If the problem is backward incompatibility, there's a reason 1.0
hasn't been released yet...

A. M. Archibald

-------------------------------------------------------------------------
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
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the NumPy-Discussion mailing list