add fmprb_tanh, fmprb_coth

This commit is contained in:
Fredrik Johansson 2013-03-26 13:39:19 +01:00
parent f1d6faaf07
commit a1cada8ca9
6 changed files with 353 additions and 0 deletions

View file

@ -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
-------------------------------------------------------------------------------

View file

@ -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);

56
fmprb/coth.c Normal file
View file

@ -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);
}

56
fmprb/tanh.c Normal file
View file

@ -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);
}

112
fmprb/test/t-coth.c Normal file
View file

@ -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;
}

112
fmprb/test/t-tanh.c Normal file
View file

@ -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;
}