2012-09-19 16:09:25 +02:00
|
|
|
/*=============================================================================
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
=============================================================================*/
|
|
|
|
/******************************************************************************
|
|
|
|
|
2013-02-20 11:58:08 +01:00
|
|
|
Copyright (C) 2012, 2013 Fredrik Johansson
|
2012-09-19 16:09:25 +02:00
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "fmprb.h"
|
2013-02-20 11:58:08 +01:00
|
|
|
#include "gamma.h"
|
2012-09-19 16:09:25 +02:00
|
|
|
|
2013-02-20 11:58:08 +01:00
|
|
|
static void
|
|
|
|
_fmprb_gamma(fmprb_t y, const fmprb_t x, long prec, int inverse)
|
2012-09-19 16:09:25 +02:00
|
|
|
{
|
2013-02-20 11:58:08 +01:00
|
|
|
int reflect;
|
|
|
|
long r, n, wp;
|
|
|
|
fmprb_t t, u, v;
|
2012-09-19 16:09:25 +02:00
|
|
|
|
2013-02-20 11:58:08 +01:00
|
|
|
wp = prec + FLINT_BIT_COUNT(prec);
|
2012-09-19 16:09:25 +02:00
|
|
|
|
2013-03-04 00:55:36 +01:00
|
|
|
gamma_stirling_choose_param_fmprb(&reflect, &r, &n, x, 1, 0, wp);
|
2012-09-19 16:09:25 +02:00
|
|
|
|
|
|
|
fmprb_init(t);
|
|
|
|
fmprb_init(u);
|
2013-02-03 18:01:26 +01:00
|
|
|
fmprb_init(v);
|
2012-09-19 16:09:25 +02:00
|
|
|
|
2013-02-20 11:58:08 +01:00
|
|
|
if (reflect)
|
2012-09-19 16:09:25 +02:00
|
|
|
{
|
2013-02-20 11:58:08 +01:00
|
|
|
/* gamma(x) = (rf(1-x, r) * pi) / (gamma(1-x+r) sin(pi x)) */
|
|
|
|
fmprb_sub_ui(t, x, 1, wp);
|
|
|
|
fmprb_neg(t, t);
|
2013-02-27 14:51:03 +01:00
|
|
|
gamma_rising_fmprb_ui_bsplit(u, t, r, wp);
|
2013-02-20 11:58:08 +01:00
|
|
|
fmprb_const_pi(v, wp);
|
|
|
|
fmprb_mul(u, u, v, wp);
|
|
|
|
fmprb_add_ui(t, t, r, wp);
|
2013-03-04 00:55:36 +01:00
|
|
|
gamma_stirling_eval_series_fmprb(v, t, n, 0, wp);
|
2013-02-20 11:58:08 +01:00
|
|
|
fmprb_exp(v, v, wp);
|
|
|
|
fmprb_sin_pi(t, x, wp);
|
|
|
|
fmprb_mul(v, v, t, wp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* gamma(x) = gamma(x+r) / rf(x,r) */
|
|
|
|
fmprb_add_ui(t, x, r, wp);
|
2013-03-04 00:55:36 +01:00
|
|
|
gamma_stirling_eval_series_fmprb(u, t, n, 0, wp);
|
2013-02-20 11:58:08 +01:00
|
|
|
fmprb_exp(u, u, prec);
|
2013-02-27 14:51:03 +01:00
|
|
|
gamma_rising_fmprb_ui_bsplit(v, x, r, wp);
|
2012-09-19 16:09:25 +02:00
|
|
|
}
|
|
|
|
|
2013-02-20 11:58:08 +01:00
|
|
|
if (inverse)
|
|
|
|
fmprb_div(y, v, u, prec);
|
|
|
|
else
|
|
|
|
fmprb_div(y, u, v, prec);
|
2012-09-19 16:09:25 +02:00
|
|
|
|
|
|
|
fmprb_clear(t);
|
|
|
|
fmprb_clear(u);
|
2013-02-03 18:01:26 +01:00
|
|
|
fmprb_clear(v);
|
2012-09-19 16:09:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-20 11:58:08 +01:00
|
|
|
fmprb_gamma(fmprb_t y, const fmprb_t x, long prec)
|
2012-09-19 16:09:25 +02:00
|
|
|
{
|
2013-02-20 11:58:08 +01:00
|
|
|
_fmprb_gamma(y, x, prec, 0);
|
2012-09-19 16:09:25 +02:00
|
|
|
}
|
2012-12-22 21:29:11 +01:00
|
|
|
|
|
|
|
void
|
2013-02-20 11:58:08 +01:00
|
|
|
fmprb_rgamma(fmprb_t y, const fmprb_t x, long prec)
|
2012-12-22 21:29:11 +01:00
|
|
|
{
|
2013-02-20 11:58:08 +01:00
|
|
|
_fmprb_gamma(y, x, prec, 1);
|
2012-12-22 21:29:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-20 11:58:08 +01:00
|
|
|
fmprb_lgamma(fmprb_t y, const fmprb_t x, long prec)
|
2012-12-22 21:29:11 +01:00
|
|
|
{
|
2013-02-20 11:58:08 +01:00
|
|
|
int reflect;
|
2012-12-22 21:29:11 +01:00
|
|
|
long r, n, wp;
|
|
|
|
fmprb_t t, u;
|
|
|
|
|
|
|
|
wp = prec + FLINT_BIT_COUNT(prec);
|
|
|
|
|
2013-03-04 00:55:36 +01:00
|
|
|
gamma_stirling_choose_param_fmprb(&reflect, &r, &n, x, 0, 0, wp);
|
2012-12-22 21:29:11 +01:00
|
|
|
|
2013-02-20 11:58:08 +01:00
|
|
|
/* log(gamma(x)) = log(gamma(x+r)) - log(rf(x,r)) */
|
2012-12-22 21:29:11 +01:00
|
|
|
fmprb_init(t);
|
|
|
|
fmprb_init(u);
|
|
|
|
|
|
|
|
fmprb_add_ui(t, x, r, wp);
|
2013-03-04 00:55:36 +01:00
|
|
|
gamma_stirling_eval_series_fmprb(u, t, n, 0, wp);
|
2013-02-27 14:51:03 +01:00
|
|
|
gamma_rising_fmprb_ui_bsplit(t, x, r, wp);
|
2013-02-20 11:58:08 +01:00
|
|
|
fmprb_log(t, t, wp);
|
|
|
|
fmprb_sub(y, u, t, prec);
|
2012-12-22 21:29:11 +01:00
|
|
|
|
|
|
|
fmprb_clear(t);
|
|
|
|
fmprb_clear(u);
|
|
|
|
}
|
2013-02-20 11:58:08 +01:00
|
|
|
|