make exp_series work with an arbitrary constant term; some improvements

This commit is contained in:
Fredrik Johansson 2012-04-11 12:28:19 +02:00
parent ccc0b034ed
commit 617e4997b1
8 changed files with 339 additions and 56 deletions

6
arb.h
View file

@ -32,6 +32,8 @@
#include "flint.h"
#include "fmpz.h"
#include "fmpq.h"
#include "ufloat.h"
typedef struct
{
@ -138,7 +140,10 @@ void arb_sqrt_ui(arb_t x, ulong n);
void arb_log_ui(arb_t x, ulong n);
void arb_log(arb_t y, const arb_t x);
void arb_exp(arb_t y, const arb_t x);
void arb_add_error_2exp(arb_t x, long c);
void _arb_rad_add_ufloat(arb_t y, const ufloat_t err);
void arb_const_pi_chudnovsky(arb_t x);
void arb_const_euler_brent_mcmillan(arb_t x);
@ -165,5 +170,4 @@ void _fmpz_addmul_abs(fmpz_t, const fmpz_t, const fmpz_t);
void _fmpz_addmul_abs_ui(fmpz_t, const fmpz_t, ulong x);
#endif

82
arb/exp.c Normal file
View file

@ -0,0 +1,82 @@
/*=============================================================================
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 "arb.h"
#include "ufloat.h"
/* TODO: check for overflow (both mid and rad) */
/*
exp((a+b)*2^r) - exp(a*2^r) = exp(a*2^r) * (exp(b*2^r)-1)
*/
void
arb_exp_error(ufloat_t err, const fmpz_t mid, const fmpz_t rad, const fmpz_t exp)
{
mpfr_t a, b;
mpfr_init2(a, 32);
mpfr_init2(b, 32);
_arb_get_mpfr(a, mid, exp, MPFR_RNDU);
_arb_get_mpfr(b, rad, exp, MPFR_RNDU);
mpfr_exp(a, a, MPFR_RNDU);
mpfr_expm1(b, b, MPFR_RNDU);
mpfr_mul(a, a, b, MPFR_RNDU);
ufloat_set_mpfr(err, a);
mpfr_clear(a);
mpfr_clear(b);
}
void
arb_exp(arb_t y, const arb_t x)
{
long prec;
mpfr_t t, u;
int input_approx;
ufloat_t err;
prec = FLINT_MAX(2, arb_prec(y));
mpfr_init2(t, 2 + fmpz_bits(arb_midref(x)));
mpfr_init2(u, prec);
arb_get_mpfr(t, x, MPFR_RNDN); /* exact */
input_approx = !fmpz_is_zero(arb_radref(x));
if (input_approx)
arb_exp_error(err, arb_midref(x), arb_radref(x), arb_expref(x));
mpfr_exp(u, t, MPFR_RNDN);
arb_set_mpfr(y, u, 1);
if (input_approx)
_arb_rad_add_ufloat(y, err);
mpfr_clear(t);
mpfr_clear(u);
}

View file

@ -51,17 +51,6 @@ arb_log_error(ufloat_t err, const fmpz_t mid, const fmpz_t rad)
fmpz_clear(d);
}
static void
_arb_rad_add_ufloat(arb_t y, ufloat_t err)
{
ufloat_t w;
err->exp -= *arb_expref(y);
ufloat_set_fmpz(w, arb_radref(y));
ufloat_add(w, w, err);
ufloat_get_fmpz(arb_radref(y), w);
}
void
arb_log(arb_t y, const arb_t x)
{
@ -84,6 +73,7 @@ arb_log(arb_t y, const arb_t x)
arb_get_mpfr(t, x, MPFR_RNDN); /* exact */
input_approx = !fmpz_is_zero(arb_radref(x));
if (input_approx)
arb_log_error(err, arb_midref(x), arb_radref(x));

62
arb/rad_add_ufloat.c Normal file
View file

@ -0,0 +1,62 @@
/*=============================================================================
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 "arb.h"
void
_arb_rad_add_ufloat(arb_t y, const ufloat_t err)
{
ufloat_t t, w;
if (fmpz_is_zero(arb_radref(y)))
{
long yexp, eexp;
yexp = *arb_expref(y);
eexp = err->exp;
if (yexp >= eexp)
{
fmpz_mul_2exp(arb_midref(y), arb_midref(y), yexp - eexp);
fmpz_set_ui(arb_radref(y), err->man);
}
else
{
fmpz_tdiv_q_2exp(arb_midref(y), arb_midref(y), eexp - yexp);
fmpz_set_ui(arb_radref(y), err->man + 1);
}
fmpz_set_si(arb_expref(y), eexp);
}
else
{
t->man = err->man;
t->exp = err->exp - *arb_expref(y);
ufloat_set_fmpz(w, arb_radref(y));
ufloat_add(w, w, t);
ufloat_get_fmpz(arb_radref(y), w);
}
}

88
arb/test/t-exp.c Normal file
View file

@ -0,0 +1,88 @@
/*=============================================================================
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 "arb.h"
int main()
{
long iter;
flint_rand_t state;
printf("exp....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 100000; iter++)
{
arb_t r, s;
fmpq_t q;
mpfr_t x, y;
long wp;
arb_init(r, 1 + n_randint(state, 600));
arb_init(s, 1 + n_randint(state, 600));
wp = FLINT_MAX(arb_prec(r), arb_prec(s)) + 100;
mpfr_init2(x, wp);
mpfr_init2(y, wp);
fmpq_init(q);
do {
arb_randtest(r, state, 5);
} while (FLINT_MAX(fmpz_bits(arb_midref(r)),
fmpz_bits(arb_radref(r))) + *arb_expref(r) > 13);
arb_get_rand_fmpq(q, state, r);
fmpq_get_mpfr(x, q, MPFR_RNDN);
arb_exp(s, r);
mpfr_exp(y, x, MPFR_RNDN);
if (!arb_contains_mpfr(s, y))
{
printf("FAIL: containment\n\n");
printf("r = "); arb_debug(r); printf("\n\n");
printf("s = "); arb_debug(s); printf("\n\n");
abort();
}
arb_clear(r);
arb_clear(s);
fmpq_clear(q);
mpfr_clear(x);
mpfr_clear(y);
}
flint_randclear(state);
_fmpz_cleanup();
mpfr_free_cache();
printf("PASS\n");
return EXIT_SUCCESS;
}

View file

@ -25,44 +25,79 @@
#include "arb_poly.h"
/* z = x + y/2^shift, assumes lengths > 0, shift >= 0 */
/* z = x*2^shift + y, assumes lengths > 0, shift >= 0
TODO: handle huge shifts
*/
void
_arb_poly_add_shift(fmpz * z, fmpz_t zrad,
const fmpz * x, long xlen, const fmpz_t xrad,
const fmpz * y, long ylen, const fmpz_t yrad, long shift)
_arb_poly_add_shift(fmpz * z, fmpz_t zrad, fmpz_t zexp,
const fmpz * x, long xlen, const fmpz_t xrad, const fmpz_t xexp,
const fmpz * y, long ylen, const fmpz_t yrad, const fmpz_t yexp, long shift)
{
fmpz_t t;
long i;
fmpz_init(t);
if (xlen >= ylen)
if (fmpz_is_zero(xrad))
{
for (i = 0; i < ylen; i++)
if (xlen <= ylen)
{
fmpz_tdiv_q_2exp(t, y + i, shift);
fmpz_add(z + i, x + i, t);
for (i = 0; i < xlen; i++)
{
fmpz_mul_2exp(t, x + i, shift);
fmpz_add(z + i, y + i, t);
}
for (i = xlen; i < ylen; i++)
fmpz_set(z + i, y + i);
}
else
{
for (i = 0; i < ylen; i++)
{
fmpz_mul_2exp(t, x + i, shift);
fmpz_add(z + i, y + i, t);
}
for (i = ylen; i < xlen; i++)
fmpz_mul_2exp(z + i, x + i, shift);
}
for (i = ylen; i < xlen; i++)
fmpz_set(z + i, x + i);
fmpz_set(zexp, yexp);
fmpz_set(zrad, yrad);
}
else
{
for (i = 0; i < xlen; i++)
if (xlen >= ylen)
{
fmpz_tdiv_q_2exp(t, y + i, shift);
fmpz_add(z + i, x + i, t);
for (i = 0; i < ylen; i++)
{
fmpz_tdiv_q_2exp(t, y + i, shift);
fmpz_add(z + i, x + i, t);
}
for (i = ylen; i < xlen; i++)
fmpz_set(z + i, x + i);
}
else
{
for (i = 0; i < xlen; i++)
{
fmpz_tdiv_q_2exp(t, y + i, shift);
fmpz_add(z + i, x + i, t);
}
for (i = xlen; i < ylen; i++)
fmpz_tdiv_q_2exp(z + i, y + i, shift);
}
for (i = xlen; i < ylen; i++)
fmpz_tdiv_q_2exp(z + i, y + i, shift);
fmpz_set(zexp, xexp);
fmpz_cdiv_q_2exp(t, yrad, shift);
fmpz_add(zrad, xrad, t);
fmpz_add_ui(zrad, zrad, 1UL);
}
fmpz_cdiv_q_2exp(t, yrad, shift);
fmpz_add(zrad, xrad, t);
fmpz_add_ui(zrad, zrad, 1UL);
fmpz_clear(t);
}
@ -103,9 +138,6 @@ arb_poly_add(arb_poly_t z, const arb_poly_t x, const arb_poly_t y)
}
else
{
/* TODO: handle huge exponents */
/* TODO: be smart when one poly is exact */
/* TODO: aliasing? */
long shift;
shift = fmpz_get_si(arb_poly_expref(x)) -
@ -113,17 +145,17 @@ arb_poly_add(arb_poly_t z, const arb_poly_t x, const arb_poly_t y)
if (shift > 0)
{
fmpz_set(arb_poly_expref(z), arb_poly_expref(x));
_arb_poly_add_shift(zp, arb_poly_radref(z),
xp, xlen, arb_poly_radref(x),
yp, ylen, arb_poly_radref(y), shift);
_arb_poly_add_shift(
zp, arb_poly_radref(z), arb_poly_expref(z),
xp, xlen, arb_poly_radref(x), arb_poly_expref(x),
yp, ylen, arb_poly_radref(y), arb_poly_expref(y), shift);
}
else
{
fmpz_set(arb_poly_expref(z), arb_poly_expref(y));
_arb_poly_add_shift(zp, arb_poly_radref(z),
yp, ylen, arb_poly_radref(y),
xp, xlen, arb_poly_radref(x), -shift);
_arb_poly_add_shift(
zp, arb_poly_radref(z), arb_poly_expref(z),
yp, ylen, arb_poly_radref(y), arb_poly_expref(y),
xp, xlen, arb_poly_radref(x), arb_poly_expref(x), -shift);
}
}

View file

@ -30,15 +30,12 @@ arb_poly_exp_series(arb_poly_t z, const arb_poly_t x, long n)
{
long a[FLINT_BITS];
long i;
arb_poly_t t;
arb_poly_t t, v, c;
arb_t r;
if (z == x)
if (n == 0 || x->length == 0)
{
arb_poly_t t;
arb_poly_init(t, arb_poly_prec(x));
arb_poly_set(t, x);
arb_poly_exp_series(z, t, n);
arb_poly_clear(t);
arb_poly_zero(z);
return;
}
@ -48,23 +45,43 @@ arb_poly_exp_series(arb_poly_t z, const arb_poly_t x, long n)
arb_poly_init(t, arb_poly_prec(z));
/* first coefficient (assuming input zero, todo: general case) */
arb_poly_set_si(z, 1);
fmpz_mul_2exp(arb_poly_coeffs(z), arb_poly_coeffs(z), arb_poly_prec(z));
fmpz_set_si(arb_poly_expref(z), -arb_poly_prec(z));
fmpz_zero(arb_poly_radref(z));
/* remove constant term */
arb_poly_init(v, arb_poly_prec(x));
arb_poly_set(v, x);
fmpz_zero(arb_poly_coeffs(v));
arb_poly_init(c, arb_poly_prec(z));
/* first coefficient */
arb_init(r, arb_poly_prec(z));
fmpz_set(arb_midref(r), arb_poly_coeffs(x));
fmpz_set(arb_radref(r), arb_poly_radref(x));
fmpz_set(arb_expref(r), arb_poly_expref(x));
arb_exp(r, r);
_arb_poly_fit_length(c, 1);
fmpz_set(arb_poly_coeffs(c), arb_midref(r));
fmpz_set(arb_poly_radref(c), arb_radref(r));
fmpz_set(arb_poly_expref(c), arb_expref(r));
c->length = 1;
arb_clear(r);
arb_poly_set_si(z, 1);
/* Newton iteration */
for (i--; i >= 0; i--)
{
n = a[i];
arb_poly_log_series(t, z, n);
arb_poly_neg(t, t);
arb_poly_add(t, t, x);
arb_poly_add(t, t, v);
arb_poly_mullow(t, z, t, n);
arb_poly_add(z, z, t);
}
arb_poly_mul(z, z, c);
arb_poly_clear(t);
arb_poly_clear(v);
arb_poly_clear(c);
}

View file

@ -34,6 +34,13 @@ _arb_poly_set_coeff_same_exp(arb_poly_t z, const arb_t c)
e1 = z->exp;
e2 = c->exp;
if (z->length < 1)
{
_arb_poly_fit_length(z, 1);
fmpz_set(arb_expref(z), arb_expref(c));
z->length = 1;
}
fmpz_init(rad);
if (e1 > e2)
@ -68,6 +75,7 @@ arb_poly_log_series(arb_poly_t z, const arb_poly_t x, long n)
fmpz_set(arb_midref(c), x->coeffs);
fmpz_set(arb_radref(c), arb_poly_radref(x));
fmpz_set(arb_expref(c), arb_poly_expref(x));
arb_log(c, c);
arb_poly_inv_series(t, x, n);