Ejemplo n.º 1
0
static __inline__ void
fmpcb_sub_mid(fmpcb_t z, const fmpcb_t x, const fmpcb_t y, long prec)
{
    fmpr_sub(fmprb_midref(fmpcb_realref(z)),
        fmprb_midref(fmpcb_realref(x)),
        fmprb_midref(fmpcb_realref(y)), prec, FMPR_RND_DOWN);
    fmpr_sub(fmprb_midref(fmpcb_imagref(z)),
        fmprb_midref(fmpcb_imagref(x)),
        fmprb_midref(fmpcb_imagref(y)), prec, FMPR_RND_DOWN);
}
Ejemplo n.º 2
0
Archivo: sqrt.c Proyecto: isuruf/arb
void
fmprb_sqrt(fmprb_t z, const fmprb_t x, slong prec)
{
    slong r;

    if (fmprb_contains_negative(x))
    {
        fmpr_nan(fmprb_midref(z));
        fmpr_pos_inf(fmprb_radref(z));
        return;
    }

    if (fmprb_is_exact(x))
    {
        r = fmpr_sqrt(fmprb_midref(z), fmprb_midref(x), prec, FMPR_RND_DOWN);
        fmpr_set_error_result(fmprb_radref(z), fmprb_midref(z), r);
    }
    else
    {
        fmpr_t err;
        fmpr_init(err);
        fmpr_sub(err, fmprb_midref(x), fmprb_radref(x), FMPRB_RAD_PREC, FMPR_RND_DOWN);
        fmpr_rsqrt(err, err, FMPRB_RAD_PREC, FMPR_RND_UP);
        fmpr_mul(err, fmprb_radref(x), err, FMPRB_RAD_PREC, FMPR_RND_UP);
        fmpr_mul_2exp_si(err, err, -1);

        r = fmpr_sqrt(fmprb_midref(z), fmprb_midref(x), prec, FMPR_RND_DOWN);
        fmpr_add_error_result(fmprb_radref(z), err, fmprb_midref(z), r,
            FMPRB_RAD_PREC, FMPR_RND_UP);

        fmpr_clear(err);
    }

    fmprb_adjust(z);
}
Ejemplo n.º 3
0
void
gamma_taylor_bound_remainder(fmpr_t err, const fmpr_t z, long n)
{
    fmpr_t t, u;

    gamma_taylor_bound_extend_cache(n + 1);

    fmpr_init(t);
    fmpr_init(u);

    /* denominator: 1 - r(n+1) * z, rounded down */
    fmpr_mul(t, gamma_taylor_bound_ratio_cache + n + 1,
        z, FMPRB_RAD_PREC, FMPR_RND_UP);
    fmpr_one(u);
    fmpr_sub(u, u, t, FMPRB_RAD_PREC, FMPR_RND_DOWN);

    if (fmpr_sgn(u) <= 0)
    {
        fmpr_pos_inf(err);
    }
    else
    {
        fmpr_pow_sloppy_ui(t, z, n, FMPRB_RAD_PREC, FMPR_RND_UP);
        fmpr_mul_2exp_si(t, t, gamma_taylor_bound_mag_cache[n + 1]);
        fmpr_div(err, t, u, FMPRB_RAD_PREC, FMPR_RND_UP);
    }

    fmpr_clear(t);
    fmpr_clear(u);
}
Ejemplo n.º 4
0
Archivo: sub.c Proyecto: argriffing/arb
slong
fmpr_sub_ui(fmpr_t z, const fmpr_t x, ulong y, slong prec, fmpr_rnd_t rnd)
{
    fmpr_t t; slong r;
    fmpr_init(t);
    fmpr_set_ui(t, y);
    r = fmpr_sub(z, x, t, prec, rnd);
    fmpr_clear(t);
    return r;
}
Ejemplo n.º 5
0
slong
fmpr_submul(fmpr_t z, const fmpr_t x, const fmpr_t y, slong prec, fmpr_rnd_t rnd)
{
    fmpr_t t;
    slong r;
    fmpr_init(t);
    fmpr_mul(t, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN);
    r = fmpr_sub(z, z, t, prec, rnd);
    fmpr_clear(t);
    return r;
}
Ejemplo n.º 6
0
static __inline__ void
fmpcb_mul_mid(fmpcb_t z, const fmpcb_t x, const fmpcb_t y, long prec)
{
#define a fmprb_midref(fmpcb_realref(x))
#define b fmprb_midref(fmpcb_imagref(x))
#define c fmprb_midref(fmpcb_realref(y))
#define d fmprb_midref(fmpcb_imagref(y))
#define e fmprb_midref(fmpcb_realref(z))
#define f fmprb_midref(fmpcb_imagref(z))

    fmpr_t t, u, v;

    fmpr_init(t);
    fmpr_init(u);
    fmpr_init(v);

    fmpr_add(t, a, b, prec, FMPR_RND_DOWN);
    fmpr_add(u, c, d, prec, FMPR_RND_DOWN);
    fmpr_mul(v, t, u, prec, FMPR_RND_DOWN);

    fmpr_mul(t, a, c, prec, FMPR_RND_DOWN);
    fmpr_mul(u, b, d, prec, FMPR_RND_DOWN);

    fmpr_sub(e, t, u, prec, FMPR_RND_DOWN);
    fmpr_sub(f, v, t, prec, FMPR_RND_DOWN);
    fmpr_sub(f, f, u, prec, FMPR_RND_DOWN);

    fmpr_clear(t);
    fmpr_clear(u);
    fmpr_clear(v);

#undef a
#undef b
#undef c
#undef d
#undef e
#undef f
}
Ejemplo n.º 7
0
Archivo: sub.c Proyecto: isuruf/arb
void
fmprb_sub(fmprb_t z, const fmprb_t x, const fmprb_t y, slong prec)
{
    slong r;

    fmpr_add(fmprb_radref(z), fmprb_radref(x), fmprb_radref(y), FMPRB_RAD_PREC, FMPR_RND_UP);

    r = fmpr_sub(fmprb_midref(z), fmprb_midref(x), fmprb_midref(y), prec, FMPR_RND_DOWN);

    fmpr_add_error_result(fmprb_radref(z), fmprb_radref(z), fmprb_midref(z),
        r, FMPRB_RAD_PREC, FMPR_RND_UP);

    fmprb_adjust(z);
}
Ejemplo n.º 8
0
int
fmpr_cmpabs(const fmpr_t x, const fmpr_t y)
{
    int res, xsign, ysign;
    fmpr_t t;

    if (fmpr_equal(x, y))
        return 0;

    if (fmpr_is_special(x) || fmpr_is_special(y))
    {
        if (fmpr_is_nan(x) || fmpr_is_nan(y))
            return 0;
        if (fmpr_is_zero(x)) return -1;
        if (fmpr_is_zero(y)) return 1;
        if (fmpr_is_inf(x)) return fmpr_is_inf(y) ? 0 : 1;
        if (fmpr_is_inf(y)) return -1;
        return -1;
    }

    /* Reduces to integer comparison if bottom exponents are the same */
    if (fmpz_equal(fmpr_expref(x), fmpr_expref(y)))
    {
        res = fmpz_cmpabs(fmpr_manref(x), fmpr_manref(y));
        if (res != 0)
            res = (res < 0) ? -1 : 1;
    }
    else
    {
        /* TODO: compare position of top exponents to avoid subtraction */
        xsign = fmpr_sgn(x);
        ysign = fmpr_sgn(y);

        fmpr_init(t);
        if (xsign == ysign)
            fmpr_sub(t, x, y, 2, FMPR_RND_DOWN);
        else
            fmpr_add(t, x, y, 2, FMPR_RND_DOWN);
        res = fmpr_sgn(t) * xsign;
        fmpr_clear(t);
    }

    return res;
}
Ejemplo n.º 9
0
Archivo: t-sub.c Proyecto: isuruf/arb
int
arf_sub_naive(arf_t z, const arf_t x, const arf_t y, slong prec, arf_rnd_t rnd)
{
    fmpr_t a, b;
    slong r;

    fmpr_init(a);
    fmpr_init(b);

    arf_get_fmpr(a, x);
    arf_get_fmpr(b, y);

    r = fmpr_sub(a, a, b, prec, rnd);

    arf_set_fmpr(z, a);

    fmpr_clear(a);
    fmpr_clear(b);

    return (r == FMPR_RESULT_EXACT) ? 0 : 1;
}
Ejemplo n.º 10
0
void
fmprb_get_interval_fmpz_2exp(fmpz_t a, fmpz_t b, fmpz_t exp, const fmprb_t x)
{

    if (fmprb_is_exact(x))
    {
        fmpr_get_fmpz_2exp(a, exp, fmprb_midref(x));
        fmpz_set(b, a);
    }
    else
    {
        fmpr_t t;
        fmpz_t exp2;
        slong s;

        fmpr_init(t);
        fmpz_init(exp2);

        fmpr_sub(t, fmprb_midref(x), fmprb_radref(x), FMPR_PREC_EXACT, FMPR_RND_DOWN);
        fmpr_get_fmpz_2exp(a, exp, t);

        fmpr_add(t, fmprb_midref(x), fmprb_radref(x), FMPR_PREC_EXACT, FMPR_RND_DOWN);
        fmpr_get_fmpz_2exp(b, exp2, t);

        s = _fmpz_sub_small(exp, exp2);

        if (s <= 0)
        {
            fmpz_mul_2exp(b, b, -s);
        }
        else
        {
            fmpz_mul_2exp(a, a, s);
            fmpz_set(exp, exp2);
        }

        fmpr_clear(t);
        fmpz_clear(exp2);
    }
}
Ejemplo n.º 11
0
Archivo: cmp.c Proyecto: bluescarni/arb
int
fmpr_cmp(const fmpr_t x, const fmpr_t y)
{
    int res, xsign, ysign;
    fmpr_t t;

    if (fmpr_equal(x, y))
        return 0;

    if (fmpr_is_special(x) || fmpr_is_special(y))
    {
        if (fmpr_is_nan(x) || fmpr_is_nan(y))
            return 0;
        if (fmpr_is_zero(y)) return fmpr_sgn(x);
        if (fmpr_is_zero(x)) return -fmpr_sgn(y);
        if (fmpr_is_pos_inf(x)) return 1;
        if (fmpr_is_neg_inf(y)) return 1;
        return -1;
    }

    xsign = fmpr_sgn(x);
    ysign = fmpr_sgn(y);

    if (xsign != ysign)
        return (xsign < 0) ? -1 : 1;

    /* Reduces to integer comparison if bottom exponents are the same */
    if (fmpz_equal(fmpr_expref(x), fmpr_expref(y)))
        return fmpz_cmp(fmpr_manref(x), fmpr_manref(y)) < 0 ? -1 : 1;

    /* TODO: compare position of top exponents to avoid subtraction */

    fmpr_init(t);
    fmpr_sub(t, x, y, 2, FMPR_RND_DOWN);
    res = fmpr_sgn(t);
    fmpr_clear(t);

    return res;
}
Ejemplo n.º 12
0
Archivo: t-sub.c Proyecto: isuruf/arb
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    /* test exact subtraction: (x - y) - z == (x - z) - y */
    for (iter = 0; iter < 100000; iter++)
    {
        slong bits, res1, res2, res3, res4;
        fmpr_t x, y, z, t, u;

        bits = 2 + n_randint(state, 200);

        fmpr_init(x);
        fmpr_init(y);
        fmpr_init(z);
        fmpr_init(t);
        fmpr_init(u);

        fmpr_randtest_special(x, state, bits, 10);
        fmpr_randtest_special(y, state, bits, 10);
        fmpr_randtest_special(z, state, bits, 10);
        fmpr_randtest_special(t, state, bits, 10);
        fmpr_randtest_special(u, state, bits, 10);

        res1 = fmpr_sub(t, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN);
        res2 = fmpr_sub(t, t, z, FMPR_PREC_EXACT, FMPR_RND_DOWN);
        res3 = fmpr_sub(u, x, z, FMPR_PREC_EXACT, FMPR_RND_DOWN);
        res4 = fmpr_sub(u, u, y, FMPR_PREC_EXACT, FMPR_RND_DOWN);

        if (!fmpr_equal(t, u) ||
            res1 != FMPR_RESULT_EXACT || res2 != FMPR_RESULT_EXACT ||
            res3 != FMPR_RESULT_EXACT || res4 != FMPR_RESULT_EXACT)
        {
            flint_printf("FAIL\n\n");
            flint_printf("bits = %wd\n", bits);
            flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
            flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
            flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
            flint_printf("t = "); fmpr_print(t); flint_printf("\n\n");
            flint_printf("u = "); fmpr_print(u); flint_printf("\n\n");
            abort();
        }

        fmpr_clear(x);
        fmpr_clear(y);
        fmpr_clear(z);
        fmpr_clear(t);
        fmpr_clear(u);
    }

    /* compare rounding with mpfr */
    for (iter = 0; iter < 100000; iter++)
    {
        slong bits, res;
        int mpfr_res;
        fmpr_t x, y, z, w;
        mpfr_t X, Y, Z;

        bits = 2 + n_randint(state, 200);

        fmpr_init(x);
        fmpr_init(y);
        fmpr_init(z);
        fmpr_init(w);

        mpfr_init2(X, bits + 100);
        mpfr_init2(Y, bits + 100);
        mpfr_init2(Z, bits);

        fmpr_randtest_special(x, state, bits + n_randint(state, 100), 10);
        fmpr_randtest_special(y, state, bits + n_randint(state, 100), 10);
        fmpr_randtest_special(z, state, bits, 10);

        fmpr_get_mpfr(X, x, MPFR_RNDN);
        fmpr_get_mpfr(Y, y, MPFR_RNDN);

        switch (n_randint(state, 4))
        {
            case 0:
                mpfr_res = mpfr_sub(Z, X, Y, MPFR_RNDZ);
                res = fmpr_sub(z, x, y, bits, FMPR_RND_DOWN);
                break;
            case 1:
                mpfr_res = mpfr_sub(Z, X, Y, MPFR_RNDA);
                res = fmpr_sub(z, x, y, bits, FMPR_RND_UP);
                break;
            case 2:
                mpfr_res = mpfr_sub(Z, X, Y, MPFR_RNDD);
                res = fmpr_sub(z, x, y, bits, FMPR_RND_FLOOR);
                break;
            default:
                mpfr_res = mpfr_sub(Z, X, Y, MPFR_RNDU);
                res = fmpr_sub(z, x, y, bits, FMPR_RND_CEIL);
                break;
        }

        fmpr_set_mpfr(w, Z);

        if (!fmpr_equal(z, w) || (res == FMPR_RESULT_EXACT) != (mpfr_res == 0)
            || !fmpr_check_ulp(z, res, bits))
        {
            flint_printf("FAIL\n\n");
            flint_printf("bits = %wd\n", bits);
            flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
            flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
            flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
            flint_printf("w = "); fmpr_print(w); flint_printf("\n\n");
            flint_printf("returned: %wd, %d\n", res, mpfr_res);
            abort();
        }

        /* check error bound */
        if (!fmpr_is_nan(z) && !fmpr_is_inf(z))
        {
            fmpr_t z_exact, error_bound, true_error;

            fmpr_init(z_exact);
            fmpr_init(error_bound);
            fmpr_init(true_error);

            fmpr_set_error_result(error_bound, z, res);
            fmpr_sub(z_exact, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN);

            fmpr_sub(true_error, z, z_exact, FMPR_PREC_EXACT, FMPR_RND_DOWN);
            fmpr_abs(true_error, true_error);

            if (fmpr_is_zero(error_bound) != fmpr_is_zero(true_error) ||
                fmpr_cmp(true_error, error_bound) > 0)
            {
                flint_printf("FAIL: error bound\n\n");
                flint_printf("bits = %wd\n", bits);
                flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
                flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
                flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
                flint_printf("z_exact = "); fmpr_print(z_exact); flint_printf("\n\n");
                flint_printf("true_error = "); fmpr_print(true_error); flint_printf("\n\n");
                flint_printf("error_bound = "); fmpr_print(error_bound); flint_printf("\n\n");
                abort();
            }

            fmpr_clear(z_exact);
            fmpr_clear(error_bound);
            fmpr_clear(true_error);
        }

        fmpr_clear(x);
        fmpr_clear(y);
        fmpr_clear(z);
        fmpr_clear(w);

        mpfr_clear(X);
        mpfr_clear(Y);
        mpfr_clear(Z);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
Ejemplo n.º 13
0
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    /* test exact addition: (x + y) + z == x + (y + z) */
    for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++)
    {
        slong bits, res1, res2, res3, res4;
        fmpr_t x, y, z, t, u;

        bits = 2 + n_randint(state, 200);

        fmpr_init(x);
        fmpr_init(y);
        fmpr_init(z);
        fmpr_init(t);
        fmpr_init(u);

        fmpr_randtest_special(x, state, bits, 10);
        fmpr_randtest_special(y, state, bits, 10);
        fmpr_randtest_special(z, state, bits, 10);
        fmpr_randtest_special(t, state, bits, 10);
        fmpr_randtest_special(u, state, bits, 10);

        res1 = fmpr_add(t, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN);
        res2 = fmpr_add(t, t, z, FMPR_PREC_EXACT, FMPR_RND_DOWN);
        res3 = fmpr_add(u, y, z, FMPR_PREC_EXACT, FMPR_RND_DOWN);
        res4 = fmpr_add(u, x, u, FMPR_PREC_EXACT, FMPR_RND_DOWN);

        if (!fmpr_equal(t, u) ||
            res1 != FMPR_RESULT_EXACT || res2 != FMPR_RESULT_EXACT ||
            res3 != FMPR_RESULT_EXACT || res4 != FMPR_RESULT_EXACT)
        {
            flint_printf("FAIL\n\n");
            flint_printf("bits = %wd\n", bits);
            flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
            flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
            flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
            flint_printf("t = "); fmpr_print(t); flint_printf("\n\n");
            flint_printf("u = "); fmpr_print(u); flint_printf("\n\n");
            abort();
        }

        fmpr_clear(x);
        fmpr_clear(y);
        fmpr_clear(z);
        fmpr_clear(t);
        fmpr_clear(u);
    }

    /* compare with add_naive */
    for (iter = 0; iter < 1000000 * arb_test_multiplier(); iter++)
    {
        slong prec, ret1, ret2;
        fmpr_t x, y, z, w;
        fmpr_rnd_t rnd;

        fmpr_init(x);
        fmpr_init(y);
        fmpr_init(z);
        fmpr_init(w);

        fmpr_randtest_special(x, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200));
        fmpr_randtest_special(y, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200));
        fmpr_randtest_special(z, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200));

        prec = 2 + n_randint(state, 1000);
        if (n_randint(state, 10) == 0 &&
                fmpz_bits(fmpr_expref(x)) < 10 &&
                fmpz_bits(fmpr_expref(y)) < 10)
            prec = FMPR_PREC_EXACT;

        switch (n_randint(state, 4))
        {
            case 0: rnd = FMPR_RND_DOWN; break;
            case 1: rnd = FMPR_RND_UP; break;
            case 2: rnd = FMPR_RND_FLOOR; break;
            default: rnd = FMPR_RND_CEIL; break;
        }

        ret1 = fmpr_add(z, x, y, prec, rnd);
        ret2 = fmpr_add_naive(w, x, y, prec, rnd);

        if (!fmpr_equal(z, w) || ret1 != ret2 ||
            !fmpr_check_ulp(z, ret1, prec) || !fmpr_check_ulp(w, ret2, prec))
        {
            flint_printf("FAIL\n\n");
            flint_printf("iter %wd\n", iter);
            flint_printf("prec = %wd\n", prec);
            flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
            flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
            flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
            flint_printf("w = "); fmpr_print(w); flint_printf("\n\n");
            flint_printf("ret1 = %wd, ret2 = %wd\n", ret1, ret2);
            abort();
        }

        fmpr_clear(x);
        fmpr_clear(y);
        fmpr_clear(z);
        fmpr_clear(w);
    }

    /* compare rounding with mpfr */
    for (iter = 0; iter < 1000000 * arb_test_multiplier(); iter++)
    {
        slong bits, res;
        int mpfr_res;
        fmpr_t x, y, z, w;
        mpfr_t X, Y, Z;

        bits = 2 + n_randint(state, 500);

        fmpr_init(x);
        fmpr_init(y);
        fmpr_init(z);
        fmpr_init(w);

        mpfr_init2(X, bits + 500);
        mpfr_init2(Y, bits + 500);
        mpfr_init2(Z, bits);

        fmpr_randtest_special(x, state, bits + n_randint(state, 500), 10);
        fmpr_randtest_special(y, state, bits + n_randint(state, 500), 10);
        fmpr_randtest_special(z, state, bits, 10);

        fmpr_get_mpfr(X, x, MPFR_RNDN);
        fmpr_get_mpfr(Y, y, MPFR_RNDN);

        switch (n_randint(state, 4))
        {
            case 0:
                mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDZ);
                res = fmpr_add(z, x, y, bits, FMPR_RND_DOWN);
                break;
            case 1:
                mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDA);
                res = fmpr_add(z, x, y, bits, FMPR_RND_UP);
                break;
            case 2:
                mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDD);
                res = fmpr_add(z, x, y, bits, FMPR_RND_FLOOR);
                break;
            default:
                mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDU);
                res = fmpr_add(z, x, y, bits, FMPR_RND_CEIL);
                break;
        }

        fmpr_set_mpfr(w, Z);

        if (!fmpr_equal(z, w) || (res == FMPR_RESULT_EXACT) != (mpfr_res == 0)
            || !fmpr_check_ulp(z, res, bits))
        {
            flint_printf("FAIL\n\n");
            flint_printf("iter %wd\n", iter);
            flint_printf("bits = %wd\n", bits);
            flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
            flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
            flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
            flint_printf("w = "); fmpr_print(w); flint_printf("\n\n");
            flint_printf("returned: %wd, %d\n", res, mpfr_res);
            abort();
        }

        /* check error bound */
        if (!fmpr_is_nan(z) && !fmpr_is_inf(z))
        {
            fmpr_t z_exact, error_bound, true_error;

            fmpr_init(z_exact);
            fmpr_init(error_bound);
            fmpr_init(true_error);

            fmpr_set_error_result(error_bound, z, res);
            fmpr_add(z_exact, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN);

            fmpr_sub(true_error, z, z_exact, FMPR_PREC_EXACT, FMPR_RND_DOWN);
            fmpr_abs(true_error, true_error);

            if (fmpr_is_zero(error_bound) != fmpr_is_zero(true_error) ||
                fmpr_cmp(true_error, error_bound) > 0)
            {
                flint_printf("FAIL: error bound\n\n");
                flint_printf("bits = %wd\n", bits);
                flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
                flint_printf("y = "); fmpr_print(y); flint_printf("\n\n");
                flint_printf("z = "); fmpr_print(z); flint_printf("\n\n");
                flint_printf("z_exact = "); fmpr_print(z_exact); flint_printf("\n\n");
                flint_printf("true_error = "); fmpr_print(true_error); flint_printf("\n\n");
                flint_printf("error_bound = "); fmpr_print(error_bound); flint_printf("\n\n");
                abort();
            }

            fmpr_clear(z_exact);
            fmpr_clear(error_bound);
            fmpr_clear(true_error);
        }

        fmpr_clear(x);
        fmpr_clear(y);
        fmpr_clear(z);
        fmpr_clear(w);

        mpfr_clear(X);
        mpfr_clear(Y);
        mpfr_clear(Z);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
Ejemplo n.º 14
0
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    for (iter = 0; iter < 1000000 * arb_test_multiplier(); iter++)
    {
        slong i, len, prec, bits, expbits, res1, res2;
        fmpr_t s1, s2, s3, err, err_bound;
        fmpr_struct terms[20];
        fmpr_rnd_t rnd;

        len = n_randint(state, 20);
        bits = 2 + n_randint(state, 1000);
        prec = 2 + n_randint(state, 1000);
        expbits = n_randint(state, 14);

        fmpr_init(s1);
        fmpr_init(s2);
        fmpr_init(s3);
        fmpr_init(err);
        fmpr_init(err_bound);
        for (i = 0; i < len; i++)
        {
            fmpr_init(terms + i);
            fmpr_randtest_special(terms + i, state, bits, expbits);
        }

        switch (n_randint(state, 4))
        {
            case 0: rnd = FMPR_RND_DOWN; break;
            case 1: rnd = FMPR_RND_UP; break;
            case 2: rnd = FMPR_RND_FLOOR; break;
            default: rnd = FMPR_RND_CEIL; break;
        }

        res1 = fmpr_sum(s1, terms, len, prec, rnd);

        fmpr_zero(s2);
        for (i = 0; i < len; i++)
            fmpr_add(s2, s2, terms + i, FMPR_PREC_EXACT, FMPR_RND_DOWN);
        res2 = fmpr_set_round(s3, s2, prec, rnd);

        if (!fmpr_equal(s1, s3) || res1 != res2 ||
            !fmpr_check_ulp(s1, res1, prec) || !fmpr_check_ulp(s3, res2, prec))
        {
            flint_printf("FAIL (%wd)\n\n", iter);
            flint_printf("prec = %wd\n\n", prec);
            for (i = 0; i < len; i++)
            {
                flint_printf("terms[%wd] = ", i); fmpr_print(terms + i); flint_printf("\n\n");
            }
            flint_printf("s1 = "); fmpr_print(s1); flint_printf("\n\n");
            flint_printf("s2 = "); fmpr_print(s2); flint_printf("\n\n");
            flint_printf("s3 = "); fmpr_print(s3); flint_printf("\n\n");
            flint_printf("res1 = %wd, res2 = %wd\n\n", res1, res2);
            abort();
        }

        fmpr_sub(err, s1, s2, FMPR_PREC_EXACT, FMPR_RND_DOWN);
        fmpr_abs(err, err);
        fmpr_set_error_result(err_bound, s1, res1);

        if (fmpr_cmp(err, err_bound) > 0)
        {
            flint_printf("FAIL (error bound)!\n");
            flint_printf("prec = %wd\n\n", prec);
            for (i = 0; i < len; i++)
            {
                flint_printf("terms[%wd] = ", i); fmpr_print(terms + i); flint_printf("\n\n");
            }
            flint_printf("s1 = "); fmpr_print(s1); flint_printf("\n\n");
            flint_printf("s2 = "); fmpr_print(s2); flint_printf("\n\n");
            flint_printf("s3 = "); fmpr_print(s3); flint_printf("\n\n");
            flint_printf("error: "); fmpr_print(err); flint_printf("\n\n");
            flint_printf("error bound: "); fmpr_print(err_bound); flint_printf("\n\n");
            flint_printf("res1 = %wd, res2 = %wd\n\n", res1, res2);
            abort();
        }

        fmpr_clear(s1);
        fmpr_clear(s2);
        fmpr_clear(s3);
        fmpr_clear(err);
        fmpr_clear(err_bound);
        for (i = 0; i < len; i++)
            fmpr_clear(terms + i);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
Ejemplo n.º 15
0
int main(int argc, char *argv[])
{
    fmprb_ptr blocks;
    fmprb_calc_func_t function;
    int * info;
    long digits, low_prec, high_prec, i, num, found_roots, found_unknown;
    long maxdepth, maxeval, maxfound;
    int refine;
    double a, b;
    fmprb_t t;
    fmpr_t fa, fb, C;

    if (argc < 4)
    {
        printf("real_roots function a b [-refine d] [-verbose] "
            "[-maxdepth n] [-maxeval n] [-maxfound n] [-prec n]\n");
        printf("available functions:\n");
        printf("  0  Z(x), Riemann-Siegel Z-function\n");
        printf("  1  sin(x)\n");
        printf("  2  sin(x^2)\n");
        printf("  3  sin(1/x)\n");
        return 1;
    }

    switch (atoi(argv[1]))
    {
        case 0:
            function = z_function;
            break;
        case 1:
            function = sin_x;
            break;
        case 2:
            function = sin_x2;
            break;
        case 3:
            function = sin_1x;
            break;
        default:
            printf("require a function 0-3\n");
            return 1;
    }

    a = atof(argv[2]);
    b = atof(argv[3]);

    if (a >= b)
    {
        printf("require a < b!\n");
        return 1;
    }

    refine = 0;
    digits = 0;
    maxdepth = 30;
    maxeval = 100000;
    maxfound = 100000;
    low_prec = 30;

    for (i = 4; i < argc; i++)
    {
        if (!strcmp(argv[i], "-refine"))
        {
            refine = 1;
            digits = atol(argv[i+1]);
        }
        else if (!strcmp(argv[i], "-verbose"))
        {
            fmprb_calc_verbose = 1;
        }
        else if (!strcmp(argv[i], "-maxdepth"))
        {
            maxdepth = atol(argv[i+1]);
        }
        else if (!strcmp(argv[i], "-maxeval"))
        {
            maxeval = atol(argv[i+1]);
        }
        else if (!strcmp(argv[i], "-maxfound"))
        {
            maxfound = atol(argv[i+1]);
        }
        else if (!strcmp(argv[i], "-prec"))
        {
            low_prec = atol(argv[i+1]);
        }
    }

    high_prec = digits * 3.32192809488736 + 10;
    found_roots = 0;
    found_unknown = 0;

    fmprb_init(t);
    fmpr_init(C);
    fmpr_init(fa);
    fmpr_init(fb);

    fmpr_set_d(fa, a);
    fmpr_set_d(fb, b);

    fmpr_add(fmprb_midref(t), fb, fa, FMPR_PREC_EXACT, FMPR_RND_DOWN);
    fmpr_sub(fmprb_radref(t), fb, fa, FMPR_PREC_EXACT, FMPR_RND_DOWN);
    fmprb_mul_2exp_si(t, t, -1);

    printf("interval: "); fmprb_printd(t, 15); printf("\n");
    printf("maxdepth = %ld, maxeval = %ld, maxfound = %ld, low_prec = %ld\n",
        maxdepth, maxeval, maxfound, low_prec);

    TIMEIT_ONCE_START

    num = fmprb_calc_isolate_roots(&blocks, &info, function,
        NULL, t, maxdepth, maxeval, maxfound, low_prec);

    for (i = 0; i < num; i++)
    {
        if (info[i] != 1)
        {
            if (fmprb_calc_verbose)
            {
                printf("unable to count roots in ");
                fmprb_printd(blocks + i, 15);
                printf("\n");
            }
            found_unknown++;
            continue;
        }

        found_roots++;

        if (!refine)
            continue;

        if (fmprb_calc_refine_root_bisect(t,
            function, NULL, blocks + i, 5, low_prec)
            != FMPRB_CALC_SUCCESS)
        {
            printf("warning: some bisection steps failed!\n");
        }

        if (fmprb_calc_verbose)
        {
            printf("after bisection 1: ");
            fmprb_printd(t, 15);
            printf("\n");
        }

        if (fmprb_calc_refine_root_bisect(blocks + i,
            function, NULL, t, 5, low_prec)
            != FMPRB_CALC_SUCCESS)
        {
            printf("warning: some bisection steps failed!\n");
        }

        if (fmprb_calc_verbose)
        {
            printf("after bisection 2: ");
            fmprb_printd(blocks + i, 15);
            printf("\n");
        }

        fmprb_calc_newton_conv_factor(C, function, NULL, t, low_prec);

        if (fmprb_calc_refine_root_newton(blocks + i, function, NULL,
            blocks + i, t, C, 10, high_prec) != FMPRB_CALC_SUCCESS)
        {
            printf("warning: some newton steps failed!\n");
        }

        printf("refined root:\n");
        fmprb_printd(blocks + i, digits + 2);
        printf("\n\n");
    }

    printf("---------------------------------------------------------------\n");
    printf("Found roots: %ld\n", found_roots);
    printf("Subintervals possibly containing undetected roots: %ld\n", found_unknown);
    printf("Function evaluations: %ld\n", eval_count);

    TIMEIT_ONCE_STOP
    SHOW_MEMORY_USAGE

    fmprb_clear(t);
    fmpr_clear(C);
    fmpr_clear(fa);
    fmpr_clear(fb);
    flint_cleanup();
    return 0;
}
Ejemplo n.º 16
0
int
fmprb_contains(const fmprb_t x, const fmprb_t y)
{
    fmpr_t t;
    fmpr_t u;
    fmpr_struct tmp[4];
    int left_ok, right_ok;

    if (fmprb_is_exact(y))
        return fmprb_contains_fmpr(x, fmprb_midref(y));

    if (fmpr_is_nan(fmprb_midref(y)))
        return fmpr_is_nan(fmprb_midref(x));

    fmpr_init(t);
    fmpr_init(u);

    /* fast check */
    fmpr_sub(t, fmprb_midref(x), fmprb_radref(x), 30, FMPR_RND_CEIL);
    fmpr_sub(u, fmprb_midref(y), fmprb_radref(y), 30, FMPR_RND_FLOOR);
    left_ok = fmpr_cmp(t, u) <= 0;

    /* exact check */
    if (!left_ok)
    {
        fmpr_init(tmp + 0);
        fmpr_init(tmp + 1);
        fmpr_init(tmp + 2);
        fmpr_init(tmp + 3);

        fmpr_set(tmp + 0, fmprb_midref(x));
        fmpr_neg(tmp + 1, fmprb_radref(x));
        fmpr_neg(tmp + 2, fmprb_midref(y));
        fmpr_set(tmp + 3, fmprb_radref(y));

        fmpr_sum(t, tmp, 4, 30, FMPR_RND_DOWN);
        left_ok = fmpr_sgn(t) <= 0;

        fmpr_clear(tmp + 0);
        fmpr_clear(tmp + 1);
        fmpr_clear(tmp + 2);
        fmpr_clear(tmp + 3);
    }

    /* fast check */
    fmpr_add(t, fmprb_midref(x), fmprb_radref(x), 30, FMPR_RND_FLOOR);
    fmpr_add(u, fmprb_midref(y), fmprb_radref(y), 30, FMPR_RND_CEIL);
    right_ok = (fmpr_cmp(t, u) >= 0);

    /* exact check */
    if (!right_ok)
    {
        fmpr_init(tmp + 0);
        fmpr_init(tmp + 1);
        fmpr_init(tmp + 2);
        fmpr_init(tmp + 3);

        fmpr_set(tmp + 0, fmprb_midref(x));
        fmpr_set(tmp + 1, fmprb_radref(x));
        fmpr_neg(tmp + 2, fmprb_midref(y));
        fmpr_neg(tmp + 3, fmprb_radref(y));

        fmpr_sum(t, tmp, 4, 30, FMPR_RND_DOWN);
        right_ok = fmpr_sgn(t) >= 0;

        fmpr_clear(tmp + 0);
        fmpr_clear(tmp + 1);
        fmpr_clear(tmp + 2);
        fmpr_clear(tmp + 3);
    }

    fmpr_clear(t);
    fmpr_clear(u);

    return left_ok && right_ok;
}