[pypy-dev] numpy fails trigonometry with complex numbers, what to do?
Mike Müller
mmueller at python-academy.de
Wed Sep 5 11:54:58 CEST 2012
Am 05.09.12 03:30, schrieb Matti Picus:
> I am trying to complete complex numbers in numpypy.
> Progress is good, I picked up from previous work on the numpypy-complex2 branch.
> Complex numbers come with extensive tests, it seems all the corner cases are
> covered.
> In porting the tests to numpypy, I came across a problem: numpy returns
> different results than cmath.
> Some of the differences are due to the fact that numpy does not raise a
> ValueError for dividing by 0 or other silly input values,
> but other differences are inexplicable (note the sign of the imaginary part):
>>>> numpy.arccos(complex(0.,-0.))
> (1.5707963267948966-0j)
>>>> cmath.acos(complex(0.,-0.))
> (1.5707963267948966+0j)
>>>>
>
> or this one:
>>>> cmath.acos(complex(float('inf'),2.3))
> -infj
>>>> numpy.arccos(complex(float('inf'),2.3))
> (0.78539816339744828-inf*j)
>
> Should I ignore the inconsistencies, or fix the 700 out of 2300 test instance
> failures?
> What should pypy's numpypy do - be consistent with numpy or with cmath?
> cmath is easier and probably faster (no need to mangle results or input args),
> so I would prefer cmath to trying to understand the logic behind numpy.
> Matti
>
In NumPy you can change how numerical exception are handled:
http://docs.scipy.org/doc/numpy/reference/routines.err.html
http://docs.scipy.org/doc/numpy/user/misc.html#how-numpy-handles-numerical-exceptions
>>> import numpy
>>> numpy.__version__
'1.6.2'
>>> numpy.arccos(complex(float('inf'),2.3))
-c:1: RuntimeWarning: invalid value encountered in arccos
(nan-inf*j)
# Warning only once.
>>> numpy.arccos(complex(float('inf'),2.3))
(nan-inf*j)
>>> old_settings = numpy.seterr(all='raise')
>>> old_settings
Out[8]: {'divide': 'warn', 'invalid': 'warn', 'over': 'warn', 'under': 'ignore'}
>>> numpy.arccos(complex(float('inf'),2.3))
---------------------------------------------------------------------------
FloatingPointError Traceback (most recent call last)
<ipython-input-11-92051afcce38> in <module>()
----> 1 numpy.arccos(complex(float('inf'),2.3))
>>> old_settings = numpy.seterr(all='ignore')
>>> numpy.arccos(complex(float('inf'),2.3))
(nan-inf*j)
HTH,
Mike
More information about the pypy-dev
mailing list