slong hypgeom_root_bound(const mag_t z, int r) { if (r == 0) { return 0; } else { fmpr_t t; slong v; fmpr_init(t); mag_get_fmpr(t, z); fmpr_root(t, t, r, MAG_BITS, FMPR_RND_UP); fmpr_add_ui(t, t, 1, MAG_BITS, FMPR_RND_UP); v = fmpr_get_si(t, FMPR_RND_UP); fmpr_clear(t); return v; } }
long gamma_taylor_bound_mag(long n) { fmprb_t t, u; long v; fmprb_init(t); fmprb_init(u); /* (pi-1) n */ fmprb_const_pi(t, FMPRB_RAD_PREC); fmprb_sub_ui(t, t, 1, FMPRB_RAD_PREC); fmprb_mul_ui(t, t, n, FMPRB_RAD_PREC); /* (3-5n) log(n/6) */ fmprb_set_ui(u, n); fmprb_div_ui(u, u, 6, FMPRB_RAD_PREC); fmprb_log(u, u, FMPRB_RAD_PREC); fmprb_mul_si(u, u, 3 - 5*n, FMPRB_RAD_PREC); fmprb_add(t, t, u, FMPRB_RAD_PREC); /* divide by 6 log(2) */ fmprb_log_ui(u, 2, FMPRB_RAD_PREC); fmprb_mul_ui(u, u, 6, FMPRB_RAD_PREC); fmprb_div(t, t, u, FMPRB_RAD_PREC); /* upper bound */ fmpr_add(fmprb_midref(t), fmprb_midref(t), fmprb_radref(t), FMPRB_RAD_PREC, FMPR_RND_CEIL); v = fmpr_get_si(fmprb_midref(t), FMPR_RND_CEIL); fmprb_clear(t); fmprb_clear(u); return v; }
void _fmprb_poly_rgamma_series(fmprb_ptr res, fmprb_srcptr h, long hlen, long len, long prec) { int reflect; long i, rflen, r, n, wp; fmprb_ptr t, u, v; fmprb_struct f[2]; hlen = FLINT_MIN(hlen, len); wp = prec + FLINT_BIT_COUNT(prec); t = _fmprb_vec_init(len); u = _fmprb_vec_init(len); v = _fmprb_vec_init(len); fmprb_init(f); fmprb_init(f + 1); /* use zeta values at small integers */ if (fmprb_is_int(h) && (fmpr_cmpabs_ui(fmprb_midref(h), prec / 2) < 0)) { r = fmpr_get_si(fmprb_midref(h), FMPR_RND_DOWN); gamma_lgamma_series_at_one(u, len, wp); _fmprb_vec_neg(u, u, len); _fmprb_poly_exp_series(t, u, len, len, wp); if (r == 1) { _fmprb_vec_swap(v, t, len); } else if (r <= 0) { fmprb_set(f, h); fmprb_one(f + 1); rflen = FLINT_MIN(len, 2 - r); _fmprb_poly_rising_ui_series(u, f, FLINT_MIN(2, len), 1 - r, rflen, wp); _fmprb_poly_mullow(v, t, len, u, rflen, len, wp); } else { fmprb_one(f); fmprb_one(f + 1); rflen = FLINT_MIN(len, r); _fmprb_poly_rising_ui_series(v, f, FLINT_MIN(2, len), r - 1, rflen, wp); /* TODO: use div_series? */ _fmprb_poly_inv_series(u, v, rflen, len, wp); _fmprb_poly_mullow(v, t, len, u, len, len, wp); } } else { /* otherwise use Stirling series */ gamma_stirling_choose_param_fmprb(&reflect, &r, &n, h, 1, 0, wp); /* rgamma(h) = (gamma(1-h+r) sin(pi h)) / (rf(1-h, r) * pi), h = h0 + t*/ if (reflect) { /* u = gamma(r+1-h) */ fmprb_sub_ui(f, h, r + 1, wp); fmprb_neg(f, f); gamma_stirling_eval_fmprb_series(t, f, n, len, wp); _fmprb_poly_exp_series(u, t, len, len, wp); for (i = 1; i < len; i += 2) fmprb_neg(u + i, u + i); /* v = sin(pi x) */ fmprb_const_pi(f + 1, wp); fmprb_mul(f, h, f + 1, wp); _fmprb_poly_sin_series(v, f, 2, len, wp); _fmprb_poly_mullow(t, u, len, v, len, len, wp); /* rf(1-h,r) * pi */ if (r == 0) { fmprb_const_pi(u, wp); _fmprb_vec_scalar_div(v, t, len, u, wp); } else { fmprb_sub_ui(f, h, 1, wp); fmprb_neg(f, f); fmprb_set_si(f + 1, -1); rflen = FLINT_MIN(len, r + 1); _fmprb_poly_rising_ui_series(v, f, FLINT_MIN(2, len), r, rflen, wp); fmprb_const_pi(u, wp); _fmprb_vec_scalar_mul(v, v, rflen, u, wp); /* divide by rising factorial */ /* TODO: might better to use div_series, when it has a good basecase */ _fmprb_poly_inv_series(u, v, rflen, len, wp); _fmprb_poly_mullow(v, t, len, u, len, len, wp); } } else { /* rgamma(h) = rgamma(h+r) rf(h,r) */ if (r == 0) { fmprb_add_ui(f, h, r, wp); gamma_stirling_eval_fmprb_series(t, f, n, len, wp); _fmprb_vec_neg(t, t, len); _fmprb_poly_exp_series(v, t, len, len, wp); } else { fmprb_set(f, h); fmprb_one(f + 1); rflen = FLINT_MIN(len, r + 1); _fmprb_poly_rising_ui_series(t, f, FLINT_MIN(2, len), r, rflen, wp); fmprb_add_ui(f, h, r, wp); gamma_stirling_eval_fmprb_series(v, f, n, len, wp); _fmprb_vec_neg(v, v, len); _fmprb_poly_exp_series(u, v, len, len, wp); _fmprb_poly_mullow(v, u, len, t, rflen, len, wp); } } } /* compose with nonconstant part */ fmprb_zero(t); _fmprb_vec_set(t + 1, h + 1, hlen - 1); _fmprb_poly_compose_series(res, v, len, t, hlen, len, prec); fmprb_clear(f); fmprb_clear(f + 1); _fmprb_vec_clear(t, len); _fmprb_vec_clear(u, len); _fmprb_vec_clear(v, len); }