add const_e, const_log2, const_catalan

This commit is contained in:
Fredrik Johansson 2013-03-04 12:55:54 +01:00
parent 03cf9d4e16
commit 397778e6c1
8 changed files with 428 additions and 20 deletions

View file

@ -620,33 +620,45 @@ Factorials and other integer functions
Constants Constants
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
.. function:: void fmprb_const_pi_chudnovsky(fmprb_t x, long prec)
Sets *x* to `\pi`, computed using the Chudnovsky algorithm.
Letting `A = 13591409`, `B = 545140134`, `C = 640320`,
we have `\pi \approx 1 / s_N` where
.. math ::
s_N = 12 \sum_{k=0}^N \frac{(-1)^k (6k)! (A+Bk)}
{(3k)! (k!)^3 C^{3k+3/2}}
The implementation computes an approximation for the
algebraic number `1/s_N` using binary splitting, bounding
the rounding error automatically.
The hypergeometric term ratio is asymptotically
`R = C^3 / (2^6 \times 3^3) \approx 1.5 \times 10^{14}`, and in fact we have
`|\pi - 1/s_N| < 1/R^N` (with a more detailed calculation, the truncation
error could be bounded closer to `1/R^{N+1}`).
.. function:: void fmprb_const_pi(fmprb_t x, long prec) .. function:: void fmprb_const_pi(fmprb_t x, long prec)
Sets *x* to `\pi`. The value is cached for repeated use. Sets *x* to `\pi`. The value is cached for repeated use.
Uses the generic hypergeometric series code to evaluate the Chudnovsky series
.. math ::
\frac{1}{\pi} = 12 \sum^\infty_{k=0} \frac{(-1)^k (6k)! (13591409 + 545140134k)}{(3k)!(k!)^3 640320^{3k + 3/2}}
.. function:: void fmprb_const_sqrt_pi(fmprb_t x, long prec) .. function:: void fmprb_const_sqrt_pi(fmprb_t x, long prec)
Sets *x* to `\sqrt{\pi}`. The value is cached for repeated use. Sets *x* to `\sqrt{\pi}`. The value is cached for repeated use.
.. function:: void fmprb_const_log2(fmprb_t s, long prec)
Sets *x* to `\log 2`. The value is cached for repeated use.
Uses the generic hypergeometric series code to evaluate the representation
.. math ::
\log 2 = \frac{3}{4} \sum_{k=0}^{\infty} \frac{(-1)^k (k!)^2}{2^k (2k+1)!}
.. function:: void fmprb_const_e(fmprb_t s, long prec)
Sets *x* to Euler's number `e = \sum_{n=0}^{\infty} 1/n!`, evaluated
using the generic hypergeometric series code.
The value is cached for repeated use.
.. function:: void fmprb_const_catalan(fmprb_t s, long prec)
Sets *x* to Catalan's constant `C = \sum_{n=0}^{\infty} (-1)^n / (2n+1)^2`.
The value is cached for repeated use. Uses the generic hypergeometric
series code to evaluate the representation
.. math ::
C = \sum_{k=0}^{\infty} \frac{(-1)^k 4^{4 k+1}
\left(40 k^2+56 k+19\right) [(k+1)!]^2 [(2k+2)!]^3}{(k+1)^3 (2 k+1) [(4k+4)!]^2}
.. function:: void fmprb_const_log_sqrt2pi(fmprb_t x, long prec) .. function:: void fmprb_const_log_sqrt2pi(fmprb_t x, long prec)
Sets *x* to `\log \sqrt{2 \pi}`. The value is cached for repeated use. Sets *x* to `\log \sqrt{2 \pi}`. The value is cached for repeated use.

View file

@ -304,9 +304,11 @@ void fmprb_fib_ui(fmprb_t f, ulong n, long prec);
void fmprb_const_pi_chudnovsky(fmprb_t x, long prec); void fmprb_const_pi_chudnovsky(fmprb_t x, long prec);
void fmprb_const_pi(fmprb_t x, long prec); void fmprb_const_pi(fmprb_t x, long prec);
void fmprb_const_sqrt_pi(fmprb_t t, long prec); void fmprb_const_sqrt_pi(fmprb_t t, long prec);
void fmprb_const_log_sqrt2pi(fmprb_t t, long prec); void fmprb_const_log_sqrt2pi(fmprb_t t, long prec);
void fmprb_const_log2(fmprb_t s, long prec);
void fmprb_const_catalan(fmprb_t s, long prec);
void fmprb_const_e(fmprb_t s, long prec);
void fmprb_agm(fmprb_t z, const fmprb_t x, const fmprb_t y, long prec); void fmprb_agm(fmprb_t z, const fmprb_t x, const fmprb_t y, long prec);

53
fmprb/const_catalan.c Normal file
View file

@ -0,0 +1,53 @@
/*=============================================================================
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"
#include "hypgeom.h"
void
fmprb_const_catalan_eval(fmprb_t s, long prec)
{
hypgeom_t series;
fmprb_t t;
fmprb_init(t);
hypgeom_init(series);
fmpz_poly_set_str(series->A, "3 19 56 40");
fmpz_poly_set_str(series->B, "1 1");
fmpz_poly_set_str(series->P, "5 0 0 0 32 -64");
fmpz_poly_set_str(series->Q, "5 9 96 352 512 256");
prec += FLINT_CLOG2(prec);
fmprb_hypgeom_infsum(s, t, series, prec, prec);
fmprb_mul_ui(t, t, 18, prec);
fmprb_div(s, s, t, prec);
hypgeom_clear(series);
fmprb_clear(t);
}
DEF_CACHED_CONSTANT(fmprb_const_catalan, fmprb_const_catalan_eval)

52
fmprb/const_e.c Normal file
View file

@ -0,0 +1,52 @@
/*=============================================================================
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"
#include "hypgeom.h"
void
fmprb_const_e_eval(fmprb_t s, long prec)
{
hypgeom_t series;
fmprb_t t;
fmprb_init(t);
hypgeom_init(series);
fmpz_poly_set_str(series->A, "1 1");
fmpz_poly_set_str(series->B, "1 1");
fmpz_poly_set_str(series->P, "1 1");
fmpz_poly_set_str(series->Q, "2 0 1");
prec += FLINT_CLOG2(prec);
fmprb_hypgeom_infsum(s, t, series, prec, prec);
fmprb_div(s, s, t, prec);
hypgeom_clear(series);
fmprb_clear(t);
}
DEF_CACHED_CONSTANT(fmprb_const_e, fmprb_const_e_eval)

54
fmprb/const_log2.c Normal file
View file

@ -0,0 +1,54 @@
/*=============================================================================
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"
#include "hypgeom.h"
void
fmprb_const_log2_eval(fmprb_t s, long prec)
{
hypgeom_t series;
fmprb_t t;
fmprb_init(t);
hypgeom_init(series);
fmpz_poly_set_str(series->A, "1 1");
fmpz_poly_set_str(series->B, "1 1");
fmpz_poly_set_str(series->P, "2 0 -1");
fmpz_poly_set_str(series->Q, "2 4 8");
prec += FLINT_CLOG2(prec);
fmprb_hypgeom_infsum(s, t, series, prec, prec);
fmprb_mul_ui(s, s, 3, prec);
fmprb_mul_2exp_si(t, t, 2);
fmprb_div(s, s, t, prec);
hypgeom_clear(series);
fmprb_clear(t);
}
DEF_CACHED_CONSTANT(fmprb_const_log2, fmprb_const_log2_eval)

View file

@ -0,0 +1,78 @@
/*=============================================================================
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) 2012 Fredrik Johansson
******************************************************************************/
#include "fmprb.h"
int main()
{
long iter;
flint_rand_t state;
printf("const_catalan....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 250; iter++)
{
fmprb_t r;
mpfr_t s;
long accuracy, prec;
prec = 2 + n_randint(state, 1 << n_randint(state, 16));
fmprb_init(r);
mpfr_init2(s, prec + 1000);
fmprb_const_catalan(r, prec);
mpfr_const_catalan(s, MPFR_RNDN);
if (!fmprb_contains_mpfr(r, s))
{
printf("FAIL: containment\n\n");
printf("prec = %ld\n", prec);
printf("r = "); fmprb_printd(r, prec / 3.33); printf("\n\n");
abort();
}
accuracy = fmprb_rel_accuracy_bits(r);
if (accuracy < prec - 4)
{
printf("FAIL: poor accuracy\n\n");
printf("prec = %ld\n", prec);
printf("r = "); fmprb_printd(r, prec / 3.33); printf("\n\n");
abort();
}
fmprb_clear(r);
mpfr_clear(s);
}
flint_randclear(state);
_fmpz_cleanup();
mpfr_free_cache();
printf("PASS\n");
return EXIT_SUCCESS;
}

79
fmprb/test/t-const_e.c Normal file
View file

@ -0,0 +1,79 @@
/*=============================================================================
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) 2012 Fredrik Johansson
******************************************************************************/
#include "fmprb.h"
int main()
{
long iter;
flint_rand_t state;
printf("const_e....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 250; iter++)
{
fmprb_t r;
mpfr_t s;
long accuracy, prec;
prec = 2 + n_randint(state, 1 << n_randint(state, 16));
fmprb_init(r);
mpfr_init2(s, prec + 1000);
fmprb_const_e(r, prec);
mpfr_set_ui(s, 1, MPFR_RNDN);
mpfr_exp(s, s, MPFR_RNDN);
if (!fmprb_contains_mpfr(r, s))
{
printf("FAIL: containment\n\n");
printf("prec = %ld\n", prec);
printf("r = "); fmprb_printd(r, prec / 3.33); printf("\n\n");
abort();
}
accuracy = fmprb_rel_accuracy_bits(r);
if (accuracy < prec - 4)
{
printf("FAIL: poor accuracy\n\n");
printf("prec = %ld\n", prec);
printf("r = "); fmprb_printd(r, prec / 3.33); printf("\n\n");
abort();
}
fmprb_clear(r);
mpfr_clear(s);
}
flint_randclear(state);
_fmpz_cleanup();
mpfr_free_cache();
printf("PASS\n");
return EXIT_SUCCESS;
}

78
fmprb/test/t-const_log2.c Normal file
View file

@ -0,0 +1,78 @@
/*=============================================================================
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) 2012 Fredrik Johansson
******************************************************************************/
#include "fmprb.h"
int main()
{
long iter;
flint_rand_t state;
printf("const_log2....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 250; iter++)
{
fmprb_t r;
mpfr_t s;
long accuracy, prec;
prec = 2 + n_randint(state, 1 << n_randint(state, 16));
fmprb_init(r);
mpfr_init2(s, prec + 1000);
fmprb_const_log2(r, prec);
mpfr_const_log2(s, MPFR_RNDN);
if (!fmprb_contains_mpfr(r, s))
{
printf("FAIL: containment\n\n");
printf("prec = %ld\n", prec);
printf("r = "); fmprb_printd(r, prec / 3.33); printf("\n\n");
abort();
}
accuracy = fmprb_rel_accuracy_bits(r);
if (accuracy < prec - 4)
{
printf("FAIL: poor accuracy\n\n");
printf("prec = %ld\n", prec);
printf("r = "); fmprb_printd(r, prec / 3.33); printf("\n\n");
abort();
}
fmprb_clear(r);
mpfr_clear(s);
}
flint_randclear(state);
_fmpz_cleanup();
mpfr_free_cache();
printf("PASS\n");
return EXIT_SUCCESS;
}