Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.arctanh(x) for x >= 1 returns NaN but I want complex

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?

like image 506
user34028 Avatar asked Apr 18 '15 07:04

user34028


1 Answers

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)
like image 185
Paul Avatar answered Sep 30 '22 21:09

Paul