From a1cada8ca9af2129dee54e09e100484d6225936d Mon Sep 17 00:00:00 2001 From: Fredrik Johansson Date: Tue, 26 Mar 2013 13:39:19 +0100 Subject: [PATCH] add fmprb_tanh, fmprb_coth --- doc/source/fmprb.rst | 15 ++++++ fmprb.h | 2 + fmprb/coth.c | 56 ++++++++++++++++++++++ fmprb/tanh.c | 56 ++++++++++++++++++++++ fmprb/test/t-coth.c | 112 +++++++++++++++++++++++++++++++++++++++++++ fmprb/test/t-tanh.c | 112 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 353 insertions(+) create mode 100644 fmprb/coth.c create mode 100644 fmprb/tanh.c create mode 100644 fmprb/test/t-coth.c create mode 100644 fmprb/test/t-tanh.c diff --git a/doc/source/fmprb.rst b/doc/source/fmprb.rst index c904b93a..2ced83d1 100644 --- a/doc/source/fmprb.rst +++ b/doc/source/fmprb.rst @@ -580,6 +580,9 @@ Trigonometric functions (otherwise the minimal polynomial becomes impractically large, possibly exhausting the available memory). +Inverse trigonometric functions +------------------------------------------------------------------------------- + .. function:: void fmprb_atan(fmprb_t z, const fmprb_t x, long prec) Sets `z = \tan^{-1} x`. Letting `d = \max(0, |m| - r)`, @@ -607,6 +610,18 @@ Hyperbolic functions evaluates `(e^{2x}\pm1) / (2e^x)` via :func:`fmprb_expm1` to avoid loss of accuracy. Otherwise evaluates `(e^x \pm e^{-x}) / 2`. +.. function:: void fmprb_tanh(fmprb_t y, const fmprb_t x, long prec) + + Sets `y = \tanh x = (\sinh x) / (\cosh x)`, evaluated + via :func:`expm1` as `\tanh x = (e^{2x} - 1) / (e^{2x} + 1)` if + the midpoint of `x` is negative and as + `\tanh x = (1 - e^{-2x}) / (1 + e^{-2x})` otherwise. + +.. function:: void fmprb_coth(fmprb_t y, const fmprb_t x, long prec) + + Sets `y = \coth x = (\cosh x) / (\sinh x)`, evaluated using + the same strategy as :func:`fmprb_tanh`. + Factorials and other integer functions ------------------------------------------------------------------------------- diff --git a/fmprb.h b/fmprb.h index 628cb2fb..091ab049 100644 --- a/fmprb.h +++ b/fmprb.h @@ -294,6 +294,8 @@ void _fmprb_cos_pi_fmpq_algebraic(fmprb_t c, ulong p, ulong q, long prec); void fmprb_sinh(fmprb_t z, const fmprb_t x, long prec); void fmprb_cosh(fmprb_t z, const fmprb_t x, long prec); void fmprb_sinh_cosh(fmprb_t s, fmprb_t c, const fmprb_t x, long prec); +void fmprb_tanh(fmprb_t y, const fmprb_t x, long prec); +void fmprb_coth(fmprb_t y, const fmprb_t x, long prec); void fmprb_atan(fmprb_t z, const fmprb_t x, long prec); void fmprb_atan2(fmprb_t z, const fmprb_t b, const fmprb_t a, long prec); diff --git a/fmprb/coth.c b/fmprb/coth.c new file mode 100644 index 00000000..2793d941 --- /dev/null +++ b/fmprb/coth.c @@ -0,0 +1,56 @@ +/*============================================================================= + + This file is part of ARB. + + ARB is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + ARB is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ARB; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +=============================================================================*/ +/****************************************************************************** + + Copyright (C) 2013 Fredrik Johansson + +******************************************************************************/ + +#include "fmprb.h" + +void +fmprb_coth(fmprb_t y, const fmprb_t x, long prec) +{ + fmprb_t t, u; + + fmprb_init(t); + fmprb_init(u); + + fmprb_mul_2exp_si(t, x, 1); + + if (fmpr_sgn(fmprb_midref(x)) >= 0) + { + fmprb_neg(t, t); + fmprb_expm1(t, t, prec + 4); + fmprb_add_ui(y, t, 2, prec + 4); + fmprb_div(y, y, t, prec); + fmprb_neg(y, y); + } + else + { + fmprb_expm1(t, t, prec + 4); + fmprb_add_ui(y, t, 2, prec + 4); + fmprb_div(y, y, t, prec); + } + + fmprb_clear(t); + fmprb_clear(u); +} + diff --git a/fmprb/tanh.c b/fmprb/tanh.c new file mode 100644 index 00000000..a1f074a3 --- /dev/null +++ b/fmprb/tanh.c @@ -0,0 +1,56 @@ +/*============================================================================= + + This file is part of ARB. + + ARB is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + ARB is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ARB; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +=============================================================================*/ +/****************************************************************************** + + Copyright (C) 2013 Fredrik Johansson + +******************************************************************************/ + +#include "fmprb.h" + +void +fmprb_tanh(fmprb_t y, const fmprb_t x, long prec) +{ + fmprb_t t, u; + + fmprb_init(t); + fmprb_init(u); + + fmprb_mul_2exp_si(t, x, 1); + + if (fmpr_sgn(fmprb_midref(x)) >= 0) + { + fmprb_neg(t, t); + fmprb_expm1(t, t, prec + 4); + fmprb_add_ui(y, t, 2, prec + 4); + fmprb_div(y, t, y, prec); + fmprb_neg(y, y); + } + else + { + fmprb_expm1(t, t, prec + 4); + fmprb_add_ui(y, t, 2, prec + 4); + fmprb_div(y, t, y, prec); + } + + fmprb_clear(t); + fmprb_clear(u); +} + diff --git a/fmprb/test/t-coth.c b/fmprb/test/t-coth.c new file mode 100644 index 00000000..b0ef3add --- /dev/null +++ b/fmprb/test/t-coth.c @@ -0,0 +1,112 @@ +/*============================================================================= + + This file is part of ARB. + + ARB is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + ARB is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ARB; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +=============================================================================*/ +/****************************************************************************** + + Copyright (C) 2013 Fredrik Johansson + +******************************************************************************/ + +#include "fmprb.h" + +int main() +{ + long iter; + flint_rand_t state; + + printf("coth...."); + fflush(stdout); + + flint_randinit(state); + + for (iter = 0; iter < 10000; iter++) + { + fmprb_t x, y, a, b, c, d; + long prec1, prec2; + + prec1 = 2 + n_randint(state, 1000); + prec2 = prec1 + 30; + + fmprb_init(x); + fmprb_init(y); + fmprb_init(a); + fmprb_init(b); + fmprb_init(c); + fmprb_init(d); + + fmprb_randtest_precise(x, state, 1 + n_randint(state, 1000), 100); + fmprb_randtest_precise(y, state, 1 + n_randint(state, 1000), 100); + + fmprb_coth(a, x, prec1); + fmprb_coth(b, x, prec2); + + /* check consistency */ + if (!fmprb_overlaps(a, b)) + { + printf("FAIL: overlap\n\n"); + printf("x = "); fmprb_print(x); printf("\n\n"); + printf("a = "); fmprb_print(a); printf("\n\n"); + printf("b = "); fmprb_print(b); printf("\n\n"); + abort(); + } + + /* check coth(x+y) = (1 + coth(x) coth(y)) / (coth(x) + coth(y)) */ + fmprb_add(b, x, y, prec1); + fmprb_coth(b, b, prec1); + + fmprb_coth(c, y, prec1); + fmprb_add(d, a, c, prec1); + fmprb_mul(c, a, c, prec1); + fmprb_add_ui(c, c, 1, prec1); + fmprb_div(d, c, d, prec1); + + if (!fmprb_overlaps(b, d)) + { + printf("FAIL: functional equation\n\n"); + printf("x = "); fmprb_print(x); printf("\n\n"); + printf("y = "); fmprb_print(y); printf("\n\n"); + printf("b = "); fmprb_print(b); printf("\n\n"); + printf("d = "); fmprb_print(d); printf("\n\n"); + abort(); + } + + fmprb_coth(x, x, prec1); + + if (!fmprb_overlaps(a, x)) + { + printf("FAIL: aliasing\n\n"); + printf("a = "); fmprb_print(a); printf("\n\n"); + printf("x = "); fmprb_print(x); printf("\n\n"); + abort(); + } + + fmprb_clear(x); + fmprb_clear(y); + fmprb_clear(a); + fmprb_clear(b); + fmprb_clear(c); + fmprb_clear(d); + } + + flint_randclear(state); + _fmpz_cleanup(); + printf("PASS\n"); + return EXIT_SUCCESS; +} + diff --git a/fmprb/test/t-tanh.c b/fmprb/test/t-tanh.c new file mode 100644 index 00000000..b016ad5c --- /dev/null +++ b/fmprb/test/t-tanh.c @@ -0,0 +1,112 @@ +/*============================================================================= + + This file is part of ARB. + + ARB is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + ARB is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ARB; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +=============================================================================*/ +/****************************************************************************** + + Copyright (C) 2013 Fredrik Johansson + +******************************************************************************/ + +#include "fmprb.h" + +int main() +{ + long iter; + flint_rand_t state; + + printf("tanh...."); + fflush(stdout); + + flint_randinit(state); + + for (iter = 0; iter < 10000; iter++) + { + fmprb_t x, y, a, b, c, d; + long prec1, prec2; + + prec1 = 2 + n_randint(state, 1000); + prec2 = prec1 + 30; + + fmprb_init(x); + fmprb_init(y); + fmprb_init(a); + fmprb_init(b); + fmprb_init(c); + fmprb_init(d); + + fmprb_randtest_precise(x, state, 1 + n_randint(state, 1000), 100); + fmprb_randtest_precise(y, state, 1 + n_randint(state, 1000), 100); + + fmprb_tanh(a, x, prec1); + fmprb_tanh(b, x, prec2); + + /* check consistency */ + if (!fmprb_overlaps(a, b)) + { + printf("FAIL: overlap\n\n"); + printf("x = "); fmprb_print(x); printf("\n\n"); + printf("a = "); fmprb_print(a); printf("\n\n"); + printf("b = "); fmprb_print(b); printf("\n\n"); + abort(); + } + + /* check tanh(x+y) = (tanh(x) + tanh(y)) / (1 + tanh(x) tanh(y)) */ + fmprb_add(b, x, y, prec1); + fmprb_tanh(b, b, prec1); + + fmprb_tanh(c, y, prec1); + fmprb_add(d, a, c, prec1); + fmprb_mul(c, a, c, prec1); + fmprb_add_ui(c, c, 1, prec1); + fmprb_div(d, d, c, prec1); + + if (!fmprb_overlaps(b, d)) + { + printf("FAIL: functional equation\n\n"); + printf("x = "); fmprb_print(x); printf("\n\n"); + printf("y = "); fmprb_print(y); printf("\n\n"); + printf("b = "); fmprb_print(b); printf("\n\n"); + printf("d = "); fmprb_print(d); printf("\n\n"); + abort(); + } + + fmprb_tanh(x, x, prec1); + + if (!fmprb_overlaps(a, x)) + { + printf("FAIL: aliasing\n\n"); + printf("a = "); fmprb_print(a); printf("\n\n"); + printf("x = "); fmprb_print(x); printf("\n\n"); + abort(); + } + + fmprb_clear(x); + fmprb_clear(y); + fmprb_clear(a); + fmprb_clear(b); + fmprb_clear(c); + fmprb_clear(d); + } + + flint_randclear(state); + _fmpz_cleanup(); + printf("PASS\n"); + return EXIT_SUCCESS; +} +