From 4c0210196a1c5623a88e3d4644534e0a4d1f4d3d Mon Sep 17 00:00:00 2001 From: Fredrik Johansson Date: Mon, 22 Jul 2013 01:28:36 +0200 Subject: [PATCH] fix lgamma_series; add test code --- fmprb_poly/lgamma_series.c | 61 +++++---------- fmprb_poly/test/t-lgamma_series.c | 123 ++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 43 deletions(-) create mode 100644 fmprb_poly/test/t-lgamma_series.c diff --git a/fmprb_poly/lgamma_series.c b/fmprb_poly/lgamma_series.c index b35b7901..ce2b6360 100644 --- a/fmprb_poly/lgamma_series.c +++ b/fmprb_poly/lgamma_series.c @@ -27,16 +27,16 @@ #include "gamma.h" void -_fmprb_poly_lgamma_cpx_series(fmprb_ptr res, const fmprb_t z, long len, long prec) +_fmprb_poly_lgamma_series(fmprb_ptr res, fmprb_ptr h, long hlen, long len, long prec) { int reflect; long r, n, rflen, wp; fmprb_t zr; fmprb_ptr t, u, f; + hlen = FLINT_MIN(hlen, len); wp = prec + FLINT_BIT_COUNT(prec); - - gamma_stirling_choose_param_fmprb(&reflect, &r, &n, z, 0, 0, wp); + gamma_stirling_choose_param_fmprb(&reflect, &r, &n, h, 0, 0, wp); t = _fmprb_vec_init(len); u = _fmprb_vec_init(len); @@ -44,19 +44,26 @@ _fmprb_poly_lgamma_cpx_series(fmprb_ptr res, const fmprb_t z, long len, long pre fmprb_init(zr); /* log(gamma(z)) = log(gamma(z+r)) - log(rf(z,r)) */ - fmprb_add_ui(zr, z, r, wp); + fmprb_add_ui(zr, h, r, wp); /* u = log(gamma(z+r)) */ gamma_stirling_eval_fmprb_series(u, zr, n, len, wp); - /* t = rising factorial */ - fmprb_set(f, z); - fmprb_one(f + 1); - rflen = FLINT_MIN(len, r + 1); - _fmprb_poly_rfac_series_ui(t, f, FLINT_MIN(2, len), r, rflen, prec); - _fmprb_poly_log_series(t, t, rflen, len, wp); + /* subtract t = log(rf(z,r)) */ + if (r != 0) + { + fmprb_set(f, h); + fmprb_one(f + 1); + rflen = FLINT_MIN(len, r + 1); + _fmprb_poly_rfac_series_ui(t, f, FLINT_MIN(2, len), r, rflen, prec); + _fmprb_poly_log_series(t, t, rflen, len, wp); + _fmprb_vec_sub(u, u, t, len, prec); + } - _fmprb_vec_sub(res, u, t, len, prec); + /* compose with nonconstant part */ + fmprb_zero(t); + _fmprb_vec_set(t + 1, h + 1, hlen - 1); + _fmprb_poly_compose_series(res, u, len, t, hlen, len, prec); fmprb_clear(zr); _fmprb_vec_clear(t, len); @@ -64,38 +71,6 @@ _fmprb_poly_lgamma_cpx_series(fmprb_ptr res, const fmprb_t z, long len, long pre _fmprb_vec_clear(f, 2); } -void -fmprb_poly_lgamma_cpx_series(fmprb_poly_t res, const fmprb_t z, long num, long prec) -{ - fmprb_poly_fit_length(res, num); - _fmprb_poly_lgamma_cpx_series(res->coeffs, z, num, prec); - _fmprb_poly_set_length(res, num); - _fmprb_poly_normalise(res); -} - -void -_fmprb_poly_lgamma_series(fmprb_ptr res, fmprb_ptr h, long hlen, long len, long prec) -{ - fmprb_ptr t, u; - fmprb_t z; - - hlen = FLINT_MIN(hlen, len); - - t = _fmprb_vec_init(hlen); - u = _fmprb_vec_init(len); - fmprb_init(z); - - fmprb_set(z, h); - _fmprb_vec_set(t + 1, h + 1, hlen - 1); - - _fmprb_poly_lgamma_cpx_series(u, z, len, prec); - _fmprb_poly_compose_series(res, u, len, t, hlen, len, prec); - - fmprb_clear(z); - _fmprb_vec_clear(t, hlen); - _fmprb_vec_clear(u, len); -} - void fmprb_poly_lgamma_series(fmprb_poly_t res, const fmprb_poly_t f, long n, long prec) { diff --git a/fmprb_poly/test/t-lgamma_series.c b/fmprb_poly/test/t-lgamma_series.c new file mode 100644 index 00000000..1952b8e1 --- /dev/null +++ b/fmprb_poly/test/t-lgamma_series.c @@ -0,0 +1,123 @@ +/*============================================================================= + + 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, 2013 Fredrik Johansson + +******************************************************************************/ + +#include "fmprb_poly.h" + +int main() +{ + long iter; + flint_rand_t state; + + printf("lgamma_series...."); + fflush(stdout); + + flint_randinit(state); + + for (iter = 0; iter < 1000; iter++) + { + long m, n1, n2, qbits, rbits1, rbits2, rbits3; + fmpq_poly_t A; + fmprb_poly_t a, b, c, d; + + qbits = 2 + n_randint(state, 200); + rbits1 = 2 + n_randint(state, 400); + rbits2 = 2 + n_randint(state, 400); + rbits3 = 2 + n_randint(state, 400); + + m = 1 + n_randint(state, 30); + n1 = 1 + n_randint(state, 30); + n2 = 1 + n_randint(state, 30); + + fmpq_poly_init(A); + fmprb_poly_init(a); + fmprb_poly_init(b); + fmprb_poly_init(c); + fmprb_poly_init(d); + + fmpq_poly_randtest_not_zero(A, state, m, qbits); + fmpz_abs(A->coeffs, A->coeffs); + fmprb_poly_set_fmpq_poly(a, A, rbits1); + + fmprb_poly_lgamma_series(b, a, n1, rbits2); + fmprb_poly_lgamma_series(c, a, n2, rbits3); + + fmprb_poly_set(d, b); + fmprb_poly_truncate(d, FLINT_MIN(n1, n2)); + fmprb_poly_truncate(c, FLINT_MIN(n1, n2)); + + if (!fmprb_poly_overlaps(c, d)) + { + printf("FAIL\n\n"); + printf("n1 = %ld, n2 = %ld, bits2 = %ld, bits3 = %ld\n", n1, n2, rbits2, rbits3); + + printf("A = "); fmpq_poly_print(A); printf("\n\n"); + printf("a = "); fmprb_poly_printd(a, 15); printf("\n\n"); + printf("b = "); fmprb_poly_printd(b, 15); printf("\n\n"); + printf("c = "); fmprb_poly_printd(c, 15); printf("\n\n"); + + abort(); + } + + /* check loggamma(a) + log(a) = loggamma(a+1) */ + fmprb_poly_log_series(c, a, n1, rbits2); + fmprb_poly_add(c, b, c, rbits2); + + fmprb_poly_set(d, a); + fmprb_add_ui(d->coeffs, d->coeffs, 1, rbits2); + fmprb_poly_lgamma_series(d, d, n1, rbits2); + + if (!fmprb_poly_overlaps(c, d)) + { + printf("FAIL (functional equation)\n\n"); + + printf("A = "); fmpq_poly_print(A); printf("\n\n"); + printf("a = "); fmprb_poly_printd(a, 15); printf("\n\n"); + printf("b = "); fmprb_poly_printd(b, 15); printf("\n\n"); + printf("c = "); fmprb_poly_printd(c, 15); printf("\n\n"); + printf("d = "); fmprb_poly_printd(d, 15); printf("\n\n"); + + abort(); + } + + fmprb_poly_lgamma_series(a, a, n1, rbits2); + if (!fmprb_poly_overlaps(a, b)) + { + printf("FAIL (aliasing)\n\n"); + abort(); + } + + fmpq_poly_clear(A); + fmprb_poly_clear(a); + fmprb_poly_clear(b); + fmprb_poly_clear(c); + fmprb_poly_clear(d); + } + + flint_randclear(state); + _fmpz_cleanup(); + printf("PASS\n"); + return EXIT_SUCCESS; +} +