1 /*
2 Copyright (C) 2013 Fredrik Johansson
3
4 This file is part of Arb.
5
6 Arb is free software: you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version. See <http://www.gnu.org/licenses/>.
10 */
11
12 #include "arb.h"
13
14 void
arb_tanh(arb_t y,const arb_t x,slong prec)15 arb_tanh(arb_t y, const arb_t x, slong prec)
16 {
17 arb_t t, u;
18 int sign = arf_sgn(arb_midref(x)) < 0;
19
20 arb_init(t);
21 arb_init(u);
22
23 arb_mul_2exp_si(t, x, 1);
24
25 if (!sign)
26 arb_neg(t, t);
27
28 if (arf_cmpabs_2exp_si(arb_midref(x), 1) > 0)
29 {
30 /* tanh(x) = 1 - 2 exp(-2x) / (1 + exp(-2x)) */
31 arb_exp(t, t, prec + 4);
32 arb_add_ui(u, t, 1, prec + 4);
33 arb_div(y, t, u, prec + 4);
34 arb_mul_2exp_si(y, y, 1);
35 arb_sub_ui(y, y, 1, prec);
36 }
37 else
38 {
39 /* tanh(x) = (exp(2x) - 1) / (exp(2x) + 1) */
40 arb_expm1(t, t, prec + 4);
41 arb_add_ui(y, t, 2, prec + 4);
42 arb_div(y, t, y, prec);
43 }
44
45 if (!sign)
46 arb_neg(y, y);
47
48 arb_clear(t);
49 arb_clear(u);
50 }
51
52