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

Bill Baxter wbaxter at gmail.com
Wed Oct 11 22:48:45 CDT 2006

```On 10/12/06, Greg Willden <gregwillden at gmail.com> wrote:
> On 10/11/06, Travis Oliphant <oliphant at ee.byu.edu> wrote:
> > Stefan van der Walt wrote:
> > >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.
>
> This is not about being a Matlab replacement.
> Numpy purports to handle complex numbers.
> Mathematically, sqrt(-1) is a complex number.
> Therefore Numpy *must* return a complex number.
> Speed should not take precedence over correctness.

Unless your goal is speed.  Then speed should take precedence over correctness.

And unless you're a fan of quaternions, in which case *which* square
root of -1 should it return?

It's interesting to note that although python has had complex numbers
pretty much from the beginning, math.sqrt(-1) returns an error.  If
you want to work with complex square roots you need to use
cmath.sqrt().  Basically you have to tell python that compex numbers
are something you care about by using the module designed for complex
math.  This scimath module is a similar deal.

But perhaps the name could be a little more obvious / short?  Right
now it seems it only deals with complex numbers, so maybe having
"complex" or "cmath" in the name would make it clearer.  Hmm there is
a numpy.math, why not a numpy.cmath.

> If Numpy doesn't return a complex number then it shouldn't pretend to
> support complex numbers.

That's certainly being overdramatic.  Lot's of folks are doing nifty
stuff with complex FFTs every day using numpy/scipy.  And I'm sure
they will continue to no matter what numpy.sqrt(-1) returns.

--bb

-------------------------------------------------------------------------
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