further cleanup

This commit is contained in:
Fredrik Johansson 2012-09-20 15:51:49 +02:00
parent 9caf29c11c
commit 942fd30c90
10 changed files with 164 additions and 58 deletions

View file

@ -138,13 +138,15 @@ fmprb_abs(fmprb_t y, const fmprb_t x)
Sets y to the absolute value of x. No attempt is made to improve the
interval represented by x if it contains zero.
void fmprb_set_si(fmprb_t x, long c)
void fmprb_set_fmpr(fmprb_t y, const fmpr_t x)
void fmprb_set_ui(fmprb_t x, ulong c)
void fmprb_set_si(fmprb_t y, long x)
void fmprb_set_fmpz(fmprb_t x, const fmpz_t c)
void fmprb_set_ui(fmprb_t y, ulong x)
Sets x exactly to the integer c.
void fmprb_set_fmpz(fmprb_t y, const fmpz_t x)
Sets y exactly to x.
void fmprb_set_fmpq(fmprb_t y, const fmpq_t x, long prec)
@ -268,6 +270,8 @@ void fmprb_add_si(fmprb_t z, const fmprb_t x, long y, long prec)
void fmprb_add_fmpz(fmprb_t z, const fmprb_t x, const fmpz_t y, long prec)
void fmprb_add_fmpr(fmprb_t z, const fmprb_t x, const fmpr_t y, long prec)
Sets $z = x + y$, rounded to prec bits. The precision can be
FMPR_PREC_EXACT provided that the result fits in memory.
@ -479,6 +483,13 @@ void fmprb_const_zeta3_bsplit(fmprb_t x, long prec)
Sets x to Apery's constant $\zeta(3)$, computed by applying binary
splitting to a hypergeometric series.
void fmprb_zeta_ui_asymp(fmprb_t z, ulong s, long prec)
Assuming $s \ge 2$, approximates $\zeta(s)$ by $1 + 2^{-s}$ along with
a correct error bound. We use the following bounds: for $s > b$,
$\zeta(s) - 1 < 2^{-b}$, and generally,
$\zeta(s) - (1 + 2^{-s}) < 2^{2-\lfloor 3 s/2 \rfloor}$.
void fmprb_zeta_ui_euler_product(fmprb_t z, ulong s, long prec)
Computes $\zeta(s)$ using the Euler product. This is fast only if s

View file

@ -128,6 +128,12 @@ fmprb_abs(fmprb_t x, const fmprb_t y)
fmpr_set(fmprb_radref(x), fmprb_radref(y));
}
static __inline__ void
fmprb_set_fmpr(fmprb_t x, const fmpr_t y)
{
fmpr_set(fmprb_midref(x), y);
fmpr_zero(fmprb_radref(x));
}
static __inline__ void
fmprb_set_si(fmprb_t x, long y)
@ -175,6 +181,7 @@ void fmprb_add(fmprb_t z, const fmprb_t x, const fmprb_t y, long prec);
void fmprb_add_ui(fmprb_t z, const fmprb_t x, ulong y, long prec);
void fmprb_add_si(fmprb_t z, const fmprb_t x, long y, long prec);
void fmprb_add_fmpz(fmprb_t z, const fmprb_t x, const fmpz_t y, long prec);
void fmprb_add_fmpr(fmprb_t z, const fmprb_t x, const fmpr_t y, long prec);
void fmprb_addmul(fmprb_t z, const fmprb_t x, const fmprb_t y, long prec);
void fmprb_addmul_ui(fmprb_t z, const fmprb_t x, ulong y, long prec);
@ -239,6 +246,7 @@ void fmprb_const_log_sqrt2pi(fmprb_t t, long prec);
void fmprb_const_euler_brent_mcmillan(fmprb_t res, long prec);
void fmprb_const_zeta3_bsplit(fmprb_t x, long prec);
void fmprb_zeta_ui_asymp(fmprb_t x, ulong s, long prec);
void fmprb_zeta_ui_bsplit(fmprb_t x, ulong s, long prec);
void fmprb_zeta_ui_euler_product(fmprb_t z, ulong s, long prec);
void fmprb_zeta_ui_bernoulli(fmprb_t x, ulong n, long prec);

View file

@ -68,3 +68,13 @@ fmprb_add_fmpz(fmprb_t z, const fmprb_t x, const fmpz_t y, long prec)
fmprb_add(z, x, t, prec);
fmprb_clear(t);
}
void
fmprb_add_fmpr(fmprb_t z, const fmprb_t x, const fmpr_t y, long prec)
{
fmprb_t t;
fmprb_init(t);
fmprb_set_fmpr(t, y);
fmprb_add(z, x, t, prec);
fmprb_clear(t);
}

View file

@ -90,6 +90,11 @@ karatsuba_error(fmpr_t bound, long s, long n, long r)
if (r < n || s < 2)
abort();
fmpr_init(t);
fmpr_init(u);
fmpr_init(A);
fmpr_init(B);
/* t = log(n)^s */
fmpr_set_ui(t, n);
fmpr_log(t, t, wp, FMPR_RND_UP);
@ -115,6 +120,11 @@ karatsuba_error(fmpr_t bound, long s, long n, long r)
/* A + B */
fmpr_add(bound, A, B, wp, FMPR_RND_UP);
fmpr_clear(t);
fmpr_clear(u);
fmpr_clear(A);
fmpr_clear(B);
}
void

View file

@ -0,0 +1,71 @@
/*=============================================================================
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("zeta_ui_asymp....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000; iter++)
{
fmprb_t r;
ulong n;
mpfr_t s;
long prec;
prec = 2 + n_randint(state, 1 << n_randint(state, 10));
fmprb_init(r);
mpfr_init2(s, prec + 100);
n = 2 + n_randint(state, 1 << n_randint(state, 10));
fmprb_zeta_ui_asymp(r, n, prec);
mpfr_zeta_ui(s, n, MPFR_RNDN);
if (!fmprb_contains_mpfr(r, s))
{
printf("FAIL: containment\n\n");
printf("n = %lu\n\n", n);
printf("r = "); fmprb_printd(r, prec / 3.33); printf("\n\n");
printf("s = "); mpfr_printf("%.275Rf\n", s); printf("\n\n");
abort();
}
fmprb_clear(r);
mpfr_clear(s);
}
flint_randclear(state);
_fmpz_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

View file

@ -41,9 +41,9 @@ fmprb_zeta_ui(fmprb_t x, ulong n, long prec)
abort();
}
/* fast detection of asymptotic case */
else if (n > 6 && n > 0.7 * prec)
else if (n > 0.7 * prec)
{
fmprb_zeta_ui_euler_product(x, n, prec);
fmprb_zeta_ui_asymp(x, n, prec);
}
else if (n == 3)
{

46
fmprb/zeta_ui_asymp.c Normal file
View file

@ -0,0 +1,46 @@
/*=============================================================================
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"
void
fmprb_zeta_ui_asymp(fmprb_t x, ulong s, long prec)
{
fmprb_set_ui(x, 1UL);
if (s != 2 && s > prec)
{
fmprb_add_error_2exp_si(x, -prec);
}
else
{
fmpr_t t;
fmpr_init(t);
fmpr_set_ui_2exp_si(t, 1, -s);
fmprb_add_fmpr(x, x, t, prec);
fmprb_add_error_2exp_si(x, 2 - (3 * s) / 2);
fmpr_clear(t);
}
}

View file

@ -173,9 +173,7 @@ fmprb_zeta_ui_bsplit(fmprb_t x, ulong s, long prec)
/* zeta(0) = -1/2 */
if (s == 0)
{
/* XXX */
fmpz_set_si(fmpr_manref(fmprb_midref(x)), -1);
fmpz_set_si(fmpr_expref(fmprb_midref(x)), -1);
fmpr_set_si_2exp_si(fmprb_midref(x), -1, -1);
fmpr_zero(fmprb_radref(x));
return;
}
@ -186,16 +184,6 @@ fmprb_zeta_ui_bsplit(fmprb_t x, ulong s, long prec)
abort();
}
/* XXX: move this to a separate method, use elsewhere */
/* for s > p, zeta(s) - 1 < 2^(-p) */
if (s != 2 && s > prec)
{
fmprb_set_ui(x, 1UL);
/* XXX: could make this even smaller when s is extremely large */
fmprb_add_error_2exp_si(x, -prec);
return;
}
n = prec / ERROR_B + 2;
wp = prec + 30;

View file

@ -68,8 +68,7 @@ fmprb_zeta_inv_ui_euler_product(fmprb_t z, ulong s, long prec)
{
fmprb_t w;
fmprb_init(w);
fmprb_set_ui(w, 1);
fmpz_set_si(fmpr_expref(fmprb_midref(w)), -s);
fmpr_set_ui_2exp_si(fmprb_midref(w), 1, -s);
fmprb_sub(z, z, w, wp);
fmprb_clear(w);
}

View file

@ -29,43 +29,6 @@
#define ERROR_A 1.5849625007211561815 /* log2(3) */
#define ERROR_B 2.5431066063272239453 /* log2(3+sqrt(8)) */
/*
Computes zeta(s) for s = start + i*step, 0 <= i < num, writing the
consecutive values to the array z. Uses Borwein's algorithm, here
extended to support fast multi-evaluation (but also works well
for a single s).
Requires start >= 2. For efficiency, the largest s should be at most about as
large as prec. Arguments approaching LONG_MAX will cause overflows.
One should therefore only use this function for s up to about prec, and
then switch to the Euler product.
References:
P. Borwein, "An Efficient Algorithm for the Riemann Zeta Function",
Constructive experimental and nonlinear analysis,
CMS Conference Proc. 27 (2000), 2934
http://www.cecm.sfu.ca/personal/pborwein/PAPERS/P155.pdf
The MPFR team (2012), "MPFR Algorithms", http://www.mpfr.org/algo.html
X. Gourdon and P. Sebah (2003),
"Numerical evaluation of the Riemann Zeta-function"
http://numbers.computation.free.fr/Constants/Miscellaneous/zetaevaluations.pdf
The algorithm for single s is basically identical to the one used in MPFR
(see the MPFR Algorithms paper for a detailed description).
In particular, we evaluate the sum backwards to avoid temporary storage of
the d_k coefficients, and use integer arithmetic throughout since it
is convenient and the terms turn out to be slightly larger than 2^prec.
The only numerical error in the main loop comes from the division by k^s,
which adds less than 1 unit of error per term.
For fast multi-evaluation, we perform repeated divisions by k^step.
Each division decreases the input error and adds at most 1 unit of rounding
error, so by induction, the error per term is always smaller than 2 units.
*/
void
fmprb_zeta_ui_vec_borwein(fmprb_struct * z, ulong start, long num,
ulong step, long prec)