port digamma function

This commit is contained in:
Fredrik Johansson 2014-06-04 16:12:22 +02:00
parent 1613f6bb68
commit 66d5b62262
5 changed files with 253 additions and 12 deletions

3
acb.h
View file

@ -647,6 +647,7 @@ void acb_rising2_ui(acb_t u, acb_t v, const acb_t x, ulong n, long prec);
void acb_gamma(acb_t y, const acb_t x, long prec);
void acb_rgamma(acb_t y, const acb_t x, long prec);
void acb_lgamma(acb_t y, const acb_t x, long prec);
void acb_digamma(acb_t y, const acb_t x, long prec);
/*
TBD
@ -656,8 +657,6 @@ void acb_root_exp(acb_t r, const acb_t a, long m, long index, long prec);
void acb_root_newton(acb_t r, const acb_t a, long m, long index, long prec);
void acb_root(acb_t r, const acb_t a, long m, long index, long prec);
void acb_digamma(acb_t y, const acb_t x, long prec);
void acb_zeta(acb_t z, const acb_t s, long prec);
void acb_hurwitz_zeta(acb_t z, const acb_t s, const acb_t a, long prec);
*/

76
acb/digamma.c Normal file
View file

@ -0,0 +1,76 @@
/*=============================================================================
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 "acb.h"
void acb_gamma_stirling_choose_param(int * reflect, long * r, long * n,
const acb_t x, int use_reflect, int digamma, long prec);
void acb_gamma_stirling_eval(acb_t s, const acb_t z, long nterms, int digamma, long prec);
void
acb_digamma(acb_t y, const acb_t x, long prec)
{
int reflect;
long r, n, wp;
acb_t t, u, v;
wp = prec + FLINT_BIT_COUNT(prec);
acb_gamma_stirling_choose_param(&reflect, &r, &n, x, 1, 1, wp);
acb_init(t);
acb_init(u);
acb_init(v);
/* psi(x) = psi((1-x)+r) - h(1-x,r) - pi*cot(pi*x) */
if (reflect)
{
acb_sub_ui(t, x, 1, wp);
acb_neg(t, t);
acb_cot_pi(v, x, wp);
arb_const_pi(acb_realref(u), wp);
acb_mul_arb(v, v, acb_realref(u), wp);
acb_rising2_ui(y, u, t, r, wp);
acb_div(u, u, y, wp);
acb_add(v, v, u, wp);
acb_add_ui(t, t, r, wp);
acb_gamma_stirling_eval(u, t, n, 1, wp);
acb_sub(y, u, v, wp);
}
else
{
acb_add_ui(t, x, r, wp);
acb_gamma_stirling_eval(u, t, n, 1, wp);
acb_rising2_ui(y, t, x, r, wp);
acb_div(t, t, y, wp);
acb_sub(y, u, t, prec);
}
acb_clear(t);
acb_clear(u);
acb_clear(v);
}

100
acb/test/t-digamma.c Normal file
View file

@ -0,0 +1,100 @@
/*=============================================================================
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 "acb.h"
int main()
{
long iter;
flint_rand_t state;
printf("digamma....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 3000; iter++)
{
acb_t a, b, c;
long prec1, prec2;
prec1 = 2 + n_randint(state, 1000);
prec2 = prec1 + 30;
acb_init(a);
acb_init(b);
acb_init(c);
arb_randtest_precise(acb_realref(a), state, 1 + n_randint(state, 1000), 3);
arb_randtest_precise(acb_imagref(a), state, 1 + n_randint(state, 1000), 3);
acb_digamma(b, a, prec1);
acb_digamma(c, a, prec2);
if (!acb_overlaps(b, c))
{
printf("FAIL: overlap\n\n");
printf("a = "); acb_print(a); printf("\n\n");
printf("b = "); acb_print(b); printf("\n\n");
printf("c = "); acb_print(c); printf("\n\n");
abort();
}
acb_set(c, a);
acb_digamma(c, c, prec2);
if (!acb_overlaps(b, c))
{
printf("FAIL: aliasing\n\n");
printf("a = "); acb_print(a); printf("\n\n");
printf("b = "); acb_print(b); printf("\n\n");
printf("c = "); acb_print(c); printf("\n\n");
abort();
}
/* check digamma(z+1) = digamma(z) + 1/z */
acb_inv(c, a, prec1);
acb_add(b, b, c, prec1);
acb_add_ui(c, a, 1, prec1);
acb_digamma(c, c, prec1);
if (!acb_overlaps(b, c))
{
printf("FAIL: functional equation\n\n");
printf("a = "); acb_print(a); printf("\n\n");
printf("b = "); acb_print(b); printf("\n\n");
printf("c = "); acb_print(c); printf("\n\n");
abort();
}
acb_clear(a);
acb_clear(b);
acb_clear(c);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

76
arb/digamma.c Normal file
View file

@ -0,0 +1,76 @@
/*=============================================================================
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 "arb.h"
void arb_gamma_stirling_choose_param(int * reflect, long * r, long * n,
const arb_t x, int use_reflect, int digamma, long prec);
void arb_gamma_stirling_eval(arb_t s, const arb_t z, long nterms, int digamma, long prec);
void
arb_digamma(arb_t y, const arb_t x, long prec)
{
int reflect;
long r, n, wp;
arb_t t, u, v;
wp = prec + FLINT_BIT_COUNT(prec);
arb_gamma_stirling_choose_param(&reflect, &r, &n, x, 1, 1, wp);
arb_init(t);
arb_init(u);
arb_init(v);
/* psi(x) = psi((1-x)+r) - h(1-x,r) - pi*cot(pi*x) */
if (reflect)
{
arb_sub_ui(t, x, 1, wp);
arb_neg(t, t);
arb_cot_pi(v, x, wp);
arb_const_pi(u, wp);
arb_mul(v, v, u, wp);
arb_rising2_ui(y, u, t, r, wp);
arb_div(u, u, y, wp);
arb_add(v, v, u, wp);
arb_add_ui(t, t, r, wp);
arb_gamma_stirling_eval(u, t, n, 1, wp);
arb_sub(y, u, v, wp);
}
else
{
arb_add_ui(t, x, r, wp);
arb_gamma_stirling_eval(u, t, n, 1, wp);
arb_rising2_ui(y, t, x, r, wp);
arb_div(t, t, y, wp);
arb_sub(y, u, t, prec);
}
arb_clear(t);
arb_clear(u);
arb_clear(v);
}

View file

@ -368,16 +368,6 @@ void arb_const_glaisher(arb_t z, long prec)
fmprb_clear(t);
}
void arb_digamma(arb_t z, const arb_t x, long prec)
{
fmprb_t t;
fmprb_init(t);
arb_get_fmprb(t, x);
fmprb_digamma(t, t, prec);
arb_set_fmprb(z, t);
fmprb_clear(t);
}
void arb_zeta(arb_t z, const arb_t s, long prec)
{
fmprb_t t;