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

pearu at cens.ioc.ee pearu at cens.ioc.ee
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, numpy.foo(x) != scipy.foo(x) 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

  numpy.sqrtp

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

Pearu



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