When I perform the operation numpy.arctanh(x) for x >= 1, it returns nan, which is odd because when I perform the operation in Wolfram|alpha, it returns complex values, which is what I need for my application. Does anyone know what I can do to keep Numpy from suppressing complex values?
Asked
Active
Viewed 1,394 times
12
-
1There should be a tick mark next to Paul's answer that'll allow you to accept it. No need for the "Solved" edit. :-) – Mark Dickinson Apr 18 '15 at 08:13
-
It's great to see these 3D models behaving properly for once. Thanks guys! – user34028 Apr 18 '15 at 08:38
1 Answers
7
Add +0j
to your real inputs to make them complex numbers.
Numpy is following a variation of the maxim "Garbage in, Garbage out."
Float in, float out.
>>> import numpy as np
>>> np.sqrt(-1)
__main__:1: RuntimeWarning: invalid value encountered in sqrt
nan
Complex in, complex out.
>>> numpy.sqrt(-1+0j)
1j
>>> numpy.arctanh(24+0j)
(0.0416908044695255-1.5707963267948966j)

Paul
- 26,170
- 12
- 85
- 119
-
Hmm. That looks like the wrong branch: the imaginary part of the result should really be +1.5707... – Mark Dickinson Apr 18 '15 at 07:28
-
Take the complex conjugate with [numpy.conj()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.conj.html)? – Paul Apr 18 '15 at 07:30
-
1Nah, I'm just being extra fussy about branch cuts. Kahan gave suggestions decades ago about how they should work on an IEEE 754-supporting machine, namely that the sign of the real part (in the case of atanh) should be used to determine which side of the branch cut a point lies. It appears that NumPy doesn't follow those suggestions: `arctanh` of `complex(24, 0.0)` and of `complex(24, -0.0)` both give the same result, which is a bit disappointing. – Mark Dickinson Apr 18 '15 at 07:44
-
@Mark Dickinson Try +/- 0.000001j and it gives different branches. So in python 2.7.9 `0.0j==-0.0j` is `True` but `0.0j is -0.0j` is `False`, so it is certainly possible to code the limits properly as well... although as you point out, they appear not to be.... – Paul Apr 18 '15 at 07:47