0

I don't know much about computing functions in general but I would like to understand how Mathematica computes the hyperbolic tangent function for large values of x.

How do you compute the hyperbolic tangent function for high values of x?

Hyberpolic tangent function at mathworld

MJD
  • 65,394
  • 39
  • 298
  • 580
Mats Granvik
  • 7,396
  • 1
    It depends on how accurate you need the results to be. $\tanh x$ approaches 1 very rapidly as $x$ increases (for example, $\tanh 10 = 0.999999996\ldots$; $\tanh 100 = 1$ accurate to 86 decimal places), so for many purposes, it may be sufficient to use the constant value 1. (Sorry if this is obvious, but it would be helpful if you could include more context in your question about what you are looking for.) – MJD Apr 12 '17 at 13:16
  • Yes, but I am looking for something that can be integrated $\tanh(f(x)) $ where $f(x)$ is some arbitrary function like the Riemann zeta function. – Mats Granvik Apr 12 '17 at 13:21
  • 1
    Have a look at http://math.stackexchange.com/questions/57746/asymptotic-expansion-of-tanh-at-infinity – Claude Leibovici Apr 12 '17 at 13:23

1 Answers1

2

Disclaimer: I have no idea how Mathematica does it. Wolfram like to keep their algorithms quiet, or at least quieter than other mathematics software providers.


We may write $$ \tanh{x} = \frac{e^x-e^{-x}}{e^x+e^{-x}} = \frac{e^{2x}-1}{e^{2x}+1} = 1 - \frac{2e^{-2x}}{1+e^{-2x}}. $$ This then has an expansion in terms of an alternating series of decaying exponentials, which decrease very rapidly to zero: $$ 1 - \frac{2e^{-2x}}{1+e^{-2x}} = 1 + 2\sum_{k=1}^{\infty} (-1)^k(e^{-2x})^k $$ So calculate $e^{-2x}$ accurately and it's easy.

Chappers
  • 67,606
  • From the comment by the user called Myself at: https://math.stackexchange.com/questions/18445/fastest-way-to-calculate-ex-up-to-arbitrary-number-of-decimals I found this method to evaluate the exponential at large x:
    (Mathematica start)
    Clear[x, y]
    exp[x_] = Normal[Series[Exp[x], {x, 0, 2}]]
    x = N[exp[1/20000]^200000, 30]
    y = N[Exp[10], 30]
    (end)
    – Mats Granvik Apr 14 '17 at 11:24
  • Is it possible to make the last sum work also for negative numbers? – Mats Granvik Apr 18 '17 at 13:12
  • http://functions.wolfram.com/ElementaryFunctions/Tanh/06/03/ – Mats Granvik Apr 18 '17 at 13:38
  • There must be two different expansions (since the hyperbolic tangent has an essential singularity at $\infty$): $e^{-2x}$ is only smaller than $1$ for $x>0$. You have to do an expansion in powers of $e^{2x}$ for $x<0$. – Chappers Apr 18 '17 at 14:02