extend fast gamma to 1/2, 2/3, 3/4, 1/6, 5/6

This commit is contained in:
Fredrik Johansson 2013-03-01 17:06:49 +01:00
parent 5349c15a51
commit 3f5fa1b3bc
7 changed files with 210 additions and 218 deletions

View file

@ -174,27 +174,34 @@ Rising factorials
Rational arguments
--------------------------------------------------------------------------------
.. function:: void gamma_const_1_4(fmprb_t x, long prec)
.. function:: void gamma_small_frac(fmprb_t y, unsigned int p, unsigned int q, long prec)
Sets `x = \Gamma(1/4)`, evaluated using the formula
Efficiently evaluates `y = \Gamma(p/q)` where `p/q` (assumed to be reduced)
is one of `1, 1/2, 1/3, 2/3, 1/4, 3/4, 1/6, 5/6`.
The cases `\Gamma(1) = 1` and `\Gamma(1/2) = \sqrt \pi` are trivial.
We reduce all remaining cases to `\Gamma(1/3)` or `\Gamma(1/4)`
using the following relations:
.. math ::
\Gamma(1/4) = \sqrt{\frac{(2\pi)^{3/2}}{\operatorname{agm}(1, \sqrt 2)}}.
\Gamma(2/3) = \frac{2 \pi}{3^{1/2} \Gamma(1/3)}, \quad \quad
\Gamma(3/4) = \frac{2^{1/2} \pi}{\Gamma(1/4)},
The value is cached for repeated use.
\Gamma(1/6) = \frac{\Gamma(1/3)^2}{(\pi/3)^{1/2} 2^{1/3}}, \quad \quad
\Gamma(5/6) = \frac{2 \pi (\pi/3)^{1/2} 2^{1/3}}{\Gamma(1/3)^2}.
.. function:: void gamma_const_1_3(fmprb_t x, long prec)
Sets `x = \Gamma(1/3)`, evaluated using the formula
The values of `\Gamma(1/3)` and `\Gamma(1/4)` are cached for fast
repeated evaluation. We compute them rapidly to high precision using
.. math ::
\Gamma(1/3) = \left( \frac{12 \pi^4}{\sqrt{10}}
\sum_{k=0}^{\infty} \frac{(6k)!(-1)^k}{(k!)^3 (3k)! 3^k 160^{3k}} \right)^{1/6}.
\sum_{k=0}^{\infty}
\frac{(6k)!(-1)^k}{(k!)^3 (3k)! 3^k 160^{3k}} \right)^{1/6}, \quad \quad
\Gamma(1/4) = \sqrt{\frac{(2\pi)^{3/2}}{\operatorname{agm}(1, \sqrt 2)}}.
The value is cached for repeated use. An alternative formula which
could be used is
An alternative formula which could be used for `\Gamma(1/3)` is
.. math ::

View file

@ -88,9 +88,7 @@ void gamma_rising_fmpcb_ui_bsplit(fmpcb_t y, const fmpcb_t x, ulong n, long prec
void gamma_rising_fmprb_ui_multipoint(fmprb_t f, const fmprb_t c, ulong n, long prec);
void gamma_series_fmpq_hypgeom(fmprb_struct * res, const fmpq_t a, long len, long prec);
void gamma_const_1_3(fmprb_t x, long prec);
void gamma_const_1_4(fmprb_t x, long prec);
void gamma_small_frac(fmprb_t y, unsigned int p, unsigned int q, long prec);
#endif

View file

@ -1,66 +0,0 @@
/*=============================================================================
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 "gamma.h"
#include "hypgeom.h"
void
gamma_const_1_3_eval(fmprb_t s, long prec)
{
hypgeom_t series;
fmprb_t t, u;
fmprb_init(t);
fmprb_init(u);
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, "4 5 -46 108 -72");
fmpz_poly_set_str(series->Q, "4 0 0 0 512000");
prec += FLINT_CLOG2(prec);
fmprb_hypgeom_infsum(s, t, series, prec, prec);
fmprb_sqrt_ui(u, 10, prec);
fmprb_mul(t, t, u, prec);
fmprb_const_pi(u, prec);
fmprb_pow_ui(u, u, 4, prec);
fmprb_mul_ui(u, u, 12, prec);
fmprb_mul(s, s, u, prec);
fmprb_div(s, s, t, prec);
fmprb_root(s, s, 2, prec);
fmprb_root(s, s, 3, prec);
hypgeom_clear(series);
fmprb_clear(t);
fmprb_clear(u);
}
DEF_CACHED_CONSTANT(gamma_const_1_3, gamma_const_1_3_eval)

View file

@ -1,54 +0,0 @@
/*=============================================================================
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 "gamma.h"
void
gamma_const_1_4_eval(fmprb_t x, long prec)
{
fmprb_t t, u;
long wp = prec + 4 + 2 * FLINT_BIT_COUNT(prec);
fmprb_init(t);
fmprb_init(u);
fmprb_one(t);
fmprb_sqrt_ui(u, 2, wp);
fmprb_agm(x, t, u, wp);
fmprb_const_pi(t, wp);
fmprb_mul_2exp_si(t, t, 1);
fmprb_sqrt(u, t, wp);
fmprb_mul(u, u, t, wp);
fmprb_div(x, u, x, wp);
fmprb_sqrt(x, x, wp);
fmprb_clear(t);
fmprb_clear(u);
}
DEF_CACHED_CONSTANT(gamma_const_1_4, gamma_const_1_4_eval)

177
gamma/small_frac.c Normal file
View file

@ -0,0 +1,177 @@
/*=============================================================================
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 "gamma.h"
#include "hypgeom.h"
void
gamma_const_1_3_eval(fmprb_t s, long prec)
{
hypgeom_t series;
fmprb_t t, u;
fmprb_init(t);
fmprb_init(u);
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, "4 5 -46 108 -72");
fmpz_poly_set_str(series->Q, "4 0 0 0 512000");
prec += FLINT_CLOG2(prec);
fmprb_hypgeom_infsum(s, t, series, prec, prec);
fmprb_sqrt_ui(u, 10, prec);
fmprb_mul(t, t, u, prec);
fmprb_const_pi(u, prec);
fmprb_pow_ui(u, u, 4, prec);
fmprb_mul_ui(u, u, 12, prec);
fmprb_mul(s, s, u, prec);
fmprb_div(s, s, t, prec);
fmprb_root(s, s, 2, prec);
fmprb_root(s, s, 3, prec);
hypgeom_clear(series);
fmprb_clear(t);
fmprb_clear(u);
}
DEF_CACHED_CONSTANT(gamma_const_1_3, gamma_const_1_3_eval)
void
gamma_const_1_4_eval(fmprb_t x, long prec)
{
fmprb_t t, u;
long wp = prec + 4 + 2 * FLINT_BIT_COUNT(prec);
fmprb_init(t);
fmprb_init(u);
fmprb_one(t);
fmprb_sqrt_ui(u, 2, wp);
fmprb_agm(x, t, u, wp);
fmprb_const_pi(t, wp);
fmprb_mul_2exp_si(t, t, 1);
fmprb_sqrt(u, t, wp);
fmprb_mul(u, u, t, wp);
fmprb_div(x, u, x, wp);
fmprb_sqrt(x, x, wp);
fmprb_clear(t);
fmprb_clear(u);
}
DEF_CACHED_CONSTANT(gamma_const_1_4, gamma_const_1_4_eval)
void
gamma_small_frac(fmprb_t y, unsigned int p, unsigned int q, long prec)
{
long wp = prec + 4;
if (q == 1)
{
fmprb_one(y);
}
else if (q == 2) /* p = 1 */
{
fmprb_const_sqrt_pi(y, prec);
}
else if (q == 3)
{
if (p == 1)
{
gamma_const_1_3(y, prec);
}
else /* p = 2 */
{
fmprb_t t;
fmprb_init(t);
gamma_const_1_3(y, wp);
fmprb_sqrt_ui(t, 3, wp);
fmprb_mul(y, y, t, wp);
fmprb_const_pi(t, wp);
fmprb_div(y, t, y, prec);
fmprb_mul_2exp_si(y, y, 1);
fmprb_clear(t);
}
}
else if (q == 4)
{
if (p == 1)
{
gamma_const_1_4(y, prec);
}
else /* p = 3 */
{
fmprb_t t;
fmprb_init(t);
fmprb_sqrt_ui(y, 2, wp);
fmprb_const_pi(t, wp);
fmprb_mul(y, y, t, wp);
gamma_const_1_4(t, wp);
fmprb_div(y, y, t, prec);
fmprb_clear(t);
}
}
else if (q == 6)
{
fmprb_t t;
fmprb_init(t);
fmprb_const_pi(t, wp);
fmprb_div_ui(t, t, 3, wp);
fmprb_sqrt(t, t, wp);
fmprb_set_ui(y, 2);
fmprb_root(y, y, 3, wp);
fmprb_mul(t, t, y, wp);
gamma_const_1_3(y, wp);
fmprb_mul(y, y, y, prec);
if (p == 1)
{
fmprb_div(y, y, t, prec);
}
else /* p = 5 */
{
fmprb_div(y, t, y, wp);
fmprb_const_pi(t, wp);
fmprb_mul(y, y, t, prec);
fmprb_mul_2exp_si(y, y, 1);
}
fmprb_clear(t);
}
else
{
printf("small fraction not implemented!\n");
abort();
}
}

View file

@ -1,81 +0,0 @@
/*=============================================================================
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 "gamma.h"
int main()
{
long iter;
flint_rand_t state;
printf("const_1_4....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 250; iter++)
{
fmprb_t r, s;
fmpq_t q;
long accuracy, prec;
prec = 2 + n_randint(state, 1 << n_randint(state, 15));
fmprb_init(r);
fmprb_init(s);
fmpq_init(q);
fmpq_set_si(q, 1, 4);
gamma_const_1_4(r, prec);
gamma_series_fmpq_hypgeom(s, q, 1, prec);
if (!fmprb_overlaps(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);
fmprb_clear(s);
fmpq_clear(q);
}
flint_randclear(state);
_fmpz_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

View file

@ -30,11 +30,11 @@ int main()
long iter;
flint_rand_t state;
printf("const_1_3....");
printf("small_frac....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 250; iter++)
for (iter = 0; iter < 1000; iter++)
{
fmprb_t r, s;
fmpq_t q;
@ -46,8 +46,19 @@ int main()
fmprb_init(s);
fmpq_init(q);
fmpq_set_si(q, 1, 3);
gamma_const_1_3(r, prec);
switch (n_randint(state, 8))
{
case 0: fmpq_set_si(q, 1, 1); break;
case 1: fmpq_set_si(q, 1, 2); break;
case 2: fmpq_set_si(q, 1, 3); break;
case 3: fmpq_set_si(q, 2, 3); break;
case 4: fmpq_set_si(q, 1, 4); break;
case 5: fmpq_set_si(q, 3, 4); break;
case 6: fmpq_set_si(q, 1, 6); break;
default: fmpq_set_si(q, 5, 6); break;
}
gamma_small_frac(r, *fmpq_numref(q), *fmpq_denref(q), prec);
gamma_series_fmpq_hypgeom(s, q, 1, prec);
if (!fmprb_overlaps(r, s))