1## Copyright 2014-2016 Oliver Heimlich
2##
3## This program is free software; you can redistribute it and/or modify
4## it under the terms of the GNU General Public License as published by
5## the Free Software Foundation; either version 3 of the License, or
6## (at your option) any later version.
7##
8## This program is distributed in the hope that it will be useful,
9## but WITHOUT ANY WARRANTY; without even the implied warranty of
10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11## GNU General Public License for more details.
12##
13## You should have received a copy of the GNU General Public License
14## along with this program; if not, see <http://www.gnu.org/licenses/>.
15
16## -*- texinfo -*-
17## @documentencoding UTF-8
18## @defmethod {@@infsup} tanh (@var{X})
19##
20## Compute the hyperbolic tangent.
21##
22## Accuracy: The result is a tight enclosure.
23##
24## @example
25## @group
26## tanh (infsup (1))
27##   @result{} ans ⊂ [0.76159, 0.7616]
28## @end group
29## @end example
30## @seealso{@@infsup/atanh, @@infsup/coth, @@infsup/sinh, @@infsup/cosh}
31## @end defmethod
32
33## Author: Oliver Heimlich
34## Keywords: interval
35## Created: 2014-10-07
36
37function x = tanh (x)
38
39  if (nargin ~= 1)
40    print_usage ();
41    return
42  endif
43
44  ## tanh is monotonically increasing from (-inf, -1) to (inf, 1)
45  l = mpfr_function_d ('tanh', -inf, x.inf);
46  u = mpfr_function_d ('tanh', +inf, x.sup);
47
48  emptyresult = isempty (x);
49  l(emptyresult) = inf;
50  u(emptyresult) = -inf;
51
52  l(l == 0) = -0;
53
54  x.inf = l;
55  x.sup = u;
56
57endfunction
58
59%!# from the documentation string
60%!assert (tanh (infsup (1)) == "[0x1.85EFAB514F394p-1, 0x1.85EFAB514F395p-1]");
61
62%!# correct use of signed zeros
63%!test
64%! x = tanh (infsup (0));
65%! assert (signbit (inf (x)));
66%! assert (not (signbit (sup (x))));
67
68%!shared testdata
69%! # Load compiled test data (from src/test/*.itl)
70%! testdata = load (file_in_loadpath ("test/itl.mat"));
71
72%!test
73%! # Scalar evaluation
74%! testcases = testdata.NoSignal.infsup.tanh;
75%! for testcase = [testcases]'
76%!   assert (isequaln (...
77%!     tanh (testcase.in{1}), ...
78%!     testcase.out));
79%! endfor
80
81%!test
82%! # Vector evaluation
83%! testcases = testdata.NoSignal.infsup.tanh;
84%! in1 = vertcat (vertcat (testcases.in){:, 1});
85%! out = vertcat (testcases.out);
86%! assert (isequaln (tanh (in1), out));
87
88%!test
89%! # N-dimensional array evaluation
90%! testcases = testdata.NoSignal.infsup.tanh;
91%! in1 = vertcat (vertcat (testcases.in){:, 1});
92%! out = vertcat (testcases.out);
93%! # Reshape data
94%! i = -1;
95%! do
96%!   i = i + 1;
97%!   testsize = factor (numel (in1) + i);
98%! until (numel (testsize) > 2)
99%! in1 = reshape ([in1; in1(1:i)], testsize);
100%! out = reshape ([out; out(1:i)], testsize);
101%! assert (isequaln (tanh (in1), out));
102