Beispiel #1
0
int main()
{
    slong i, bound;
    double a, b;
    fmpq_t q;
    fmpr_t t;

    flint_printf("bound_2exp_si....");
    fflush(stdout);

    fmpq_init(q);
    fmpr_init(t);

    for (i = 0; i < 1000; i++)
    {
        arith_bernoulli_number(q, i);
        bound = bernoulli_bound_2exp_si(i);

        fmpr_set_round_fmpz(t, fmpq_numref(q), 32, FMPR_RND_UP);
        fmpr_div_fmpz(t, t, fmpq_denref(q), 32, FMPR_RND_UP);

        if (fmpr_cmpabs_2exp_si(t, bound) > 0)
        {
            flint_printf("FAIL: %wd\n", i);
            fmpr_print(t); flint_printf("\n\n");
            flint_printf("%wd\n", bound); flint_printf("\n\n");
            abort();
        }
    }

    fmpq_clear(q);
    fmpr_clear(t);

    for (i = 100; i < 4000000; i += 1)
    {
        i += (i & 1);
        a = bernoulli_bound_2exp_si(i);
        b = log2bern_approx(i);

        if (a < b || a > 1.01 * b)
        {
            flint_printf("FAIL: %wd\n", i);
            flint_printf("%wd: %f %f %f\n", i, a, b, (float) a / b);
            abort();
        }
    }

    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
Beispiel #2
0
void
gamma_stirling_eval_fmprb(fmprb_t s, const fmprb_t z, long nterms, int digamma, long prec)
{
    fmprb_t b, t, logz, zinv, zinv2;
    fmpr_t err;

    long k, term_prec;
    double z_mag, term_mag;

    fmprb_init(b);
    fmprb_init(t);
    fmprb_init(logz);
    fmprb_init(zinv);
    fmprb_init(zinv2);

    fmprb_log(logz, z, prec);
    fmprb_ui_div(zinv, 1UL, z, prec);

    nterms = FLINT_MAX(nterms, 1);

    fmprb_zero(s);

    if (nterms > 1)
    {
        fmprb_mul(zinv2, zinv, zinv, prec);

        z_mag = fmpr_get_d(fmprb_midref(logz), FMPR_RND_UP) * 1.44269504088896;

        for (k = nterms - 1; k >= 1; k--)
        {
            term_mag = bernoulli_bound_2exp_si(2 * k);
            term_mag -= (2 * k - 1) * z_mag;
            term_prec = prec + term_mag;
            term_prec = FLINT_MIN(term_prec, prec);
            term_prec = FLINT_MAX(term_prec, 10);

            if (prec > 2000)
            {
                fmprb_set_round(t, zinv2, term_prec);
                fmprb_mul(s, s, t, term_prec);
            }
            else
                fmprb_mul(s, s, zinv2, term_prec);

            gamma_stirling_coeff(b, k, digamma, term_prec);
            fmprb_add(s, s, b, term_prec);
        }

        if (digamma)
            fmprb_mul(s, s, zinv2, prec);
        else
            fmprb_mul(s, s, zinv, prec);
    }

    /* remainder bound */
    fmpr_init(err);
    gamma_stirling_bound_fmprb(err, z, digamma ? 1 : 0, 1, nterms);
    fmprb_add_error_fmpr(s, err);
    fmpr_clear(err);

    if (digamma)
    {
        fmprb_neg(s, s);
        fmprb_mul_2exp_si(zinv, zinv, -1);
        fmprb_sub(s, s, zinv, prec);
        fmprb_add(s, s, logz, prec);
    }
    else
    {
        /* (z-0.5)*log(z) - z + log(2*pi)/2 */
        fmprb_one(t);
        fmprb_mul_2exp_si(t, t, -1);
        fmprb_sub(t, z, t, prec);
        fmprb_mul(t, logz, t, prec);
        fmprb_add(s, s, t, prec);
        fmprb_sub(s, s, z, prec);
        fmprb_const_log_sqrt2pi(t, prec);
        fmprb_add(s, s, t, prec);
    }

    fmprb_clear(t);
    fmprb_clear(b);
    fmprb_clear(zinv);
    fmprb_clear(zinv2);
    fmprb_clear(logz);
}
Beispiel #3
0
void
acb_gamma_stirling_eval(acb_t s, const acb_t z, long nterms, int digamma, long prec)
{
    acb_t t, logz, zinv, zinv2;
    arb_t b;
    mag_t err;

    long k, term_prec;
    double z_mag, term_mag;

    acb_init(t);
    acb_init(logz);
    acb_init(zinv);
    acb_init(zinv2);
    arb_init(b);

    acb_log(logz, z, prec);
    acb_inv(zinv, z, prec);

    nterms = FLINT_MAX(nterms, 1);

    acb_zero(s);
    if (nterms > 1)
    {
        acb_mul(zinv2, zinv, zinv, prec);

        z_mag = arf_get_d(arb_midref(acb_realref(logz)), ARF_RND_UP) * 1.44269504088896;

        for (k = nterms - 1; k >= 1; k--)
        {
            term_mag = bernoulli_bound_2exp_si(2 * k);
            term_mag -= (2 * k - 1) * z_mag;
            term_prec = prec + term_mag;
            term_prec = FLINT_MIN(term_prec, prec);
            term_prec = FLINT_MAX(term_prec, 10);

            arb_gamma_stirling_coeff(b, k, digamma, term_prec);

            if (prec > 2000)
            {
                acb_set_round(t, zinv2, term_prec);
                acb_mul(s, s, t, term_prec);
            }
            else
                acb_mul(s, s, zinv2, term_prec);

            arb_add(acb_realref(s), acb_realref(s), b, term_prec);
        }

        if (digamma)
            acb_mul(s, s, zinv2, prec);
        else
            acb_mul(s, s, zinv, prec);
    }

    /* remainder bound */
    mag_init(err);
    acb_gamma_stirling_bound(err, z, digamma ? 1 : 0, 1, nterms);
    mag_add(arb_radref(acb_realref(s)), arb_radref(acb_realref(s)), err);
    mag_add(arb_radref(acb_imagref(s)), arb_radref(acb_imagref(s)), err);
    mag_clear(err);

    if (digamma)
    {
        acb_neg(s, s);
        acb_mul_2exp_si(zinv, zinv, -1);
        acb_sub(s, s, zinv, prec);
        acb_add(s, s, logz, prec);
    }
    else
    {
        /* (z-0.5)*log(z) - z + log(2*pi)/2 */
        arb_one(b);
        arb_mul_2exp_si(b, b, -1);
        arb_set(acb_imagref(t), acb_imagref(z));
        arb_sub(acb_realref(t), acb_realref(z), b, prec);
        acb_mul(t, logz, t, prec);
        acb_add(s, s, t, prec);
        acb_sub(s, s, z, prec);
        arb_const_log_sqrt2pi(b, prec);
        arb_add(acb_realref(s), acb_realref(s), b, prec);
    }

    acb_clear(t);
    acb_clear(logz);
    acb_clear(zinv);
    acb_clear(zinv2);
    arb_clear(b);
}