Open richinex opened 1 year ago
Thanks for sharing. There is indeed an overflow occurring in the calculation of tanh
for example. Here a minimal demo:
math.tanh(math.complex(1, 1)) // { re: 1.0839233273386948, im: 0.27175258531951174 }
math.tanh(math.complex(100, 100)) // { re: 1, im: -2.4171061928460553e-87 }
math.tanh(math.complex(1000, 1000)) // { re: NaN, im: -0 }
The tanh
and coth
functions for Complex numbers are currently straight from the Complex.js
library, the calculation contains cosh
which goes to Infinity
for large values:
Before solving this in mathjs, it will be good to raise this issue in the https://github.com/infusion/Complex.js project, ideally it would be solved there. @richinex can you open an issue there?
Yea sure. I have raised an issue there.
Hi, I work with impedance spectroscopy and I sometimes have to deal with large complex numbers. I have experience NaN results. I think the mathjs tanh implementation has an overflow issue as I do not experience this problem with Python. Take for instance the problem below:
So I thought I'd share my implementation below. Maybe it helps. It is a translation of numba's implementation to JavaScript https://github.com/numba/numba/blob/c04edc02deb40b84c3b21ed2a9835353181a5916/numba/targets/cmathimpl.py#L343-L367: