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

pearu at pearu at
Thu Oct 12 02:45:37 CDT 2006

PS: I am still sending this message to numpy list only because the
    proposal below affects numpy code, not scipy one.

I think Fernando points make sense, != can
cause confusion and frustration both among new numpy/scipy users and
developers (who need to find explanations for the choises made).

So, let me propose the following solution so that all parties will get
the same results without sacrifying numpy.sqrt speed on non-negative input
and scipy.sqrt backward compatibility:

Define numpy.sqrt as follows:

def sqrt(x): 
    r = nx.sqrt(x)
    if nx.nan in r:
        i = nx.where(nx.isnan(r))
        r = _tocomplex(r)
        r[i] = nx.sqrt(_tocomplex(x[i]))
    return r

and define


that takes only non-negative input, this is for those users who expect
sqrt to fail on negative input (as Numeric.sqrt and math.sqrt do).


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