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

Tim Hochberg tim.hochberg at ieee.org
Wed Oct 11 17:31:21 CDT 2006


Travis Oliphant wrote:
> Sven Schreiber wrote:
>
>   
>>> This is user adjustable.  You change the error mode to raise on 
>>> 'invalid' instead of pass silently which is now the default.
>>>
>>> -Travis
>>>
>>>    
>>>
>>>       
>> Could you please explain how this adjustment is done, or point to the
>> relevant documentation.
>>  
>>
>>     
>
> numpy.sqrt(-1)
>
> old = seterr(invalid='raise')
> numpy.sqrt(-1)  # should raise an error
>
> seterr(**old)  # restores error-modes for current thread
> numpy.sqrt(-1)
>
>   
With python 2.5 out now, perhaps it's time to come up with a with 
statement context manager. Something like:

    from __future__ import with_statement
    import numpy

    class errstate(object):
        def __init__(self, **kwargs):
            self.kwargs = kwargs
        def __enter__(self):
            self.oldstate = numpy.seterr(**self.kwargs)
        def __exit__(self, *exc_info):
            numpy.seterr(**self.oldstate)
           
    a = numpy.arange(10)
    a/a # ignores divide by zero
    with errstate(divide='raise'):
        a/a # raise exception on divide by zer
    # Would ignore divide by zero again if we got here.

-tim




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