예제 #1
0
파일: t-set_fmpq.c 프로젝트: argriffing/arb
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    /* test exact roundtrip R -> Q -> R */
    for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++)
    {
        slong bits, res;
        arf_t x, z;
        fmpq_t y;

        bits = 2 + n_randint(state, 200);

        arf_init(x);
        arf_init(z);
        fmpq_init(y);

        arf_randtest(x, state, bits, 10);
        arf_randtest(z, state, bits, 10);

        arf_get_fmpq(y, x);
        res = arf_set_fmpq(z, y, bits, ARF_RND_DOWN);

        if (!arf_equal(x, z) || res != 0)
        {
            flint_printf("FAIL\n\n");
            flint_printf("bits: %wd\n", bits);
            flint_printf("x = "); arf_print(x); flint_printf("\n\n");
            flint_printf("y = "); fmpq_print(y); flint_printf("\n\n");
            flint_printf("z = "); arf_print(z); flint_printf("\n\n");
            abort();
        }

        arf_clear(x);
        arf_clear(z);
        fmpq_clear(y);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
예제 #2
0
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
    {
        arb_t a;
        arf_t b;
        fmpq_t am, ar, bm, t;
        int c1, c2;

        arb_init(a);
        arf_init(b);

        fmpq_init(am);
        fmpq_init(ar);
        fmpq_init(bm);
        fmpq_init(t);

        arb_randtest(a, state, 1 + n_randint(state, 500), 14);
        arf_randtest(b, state, 1 + n_randint(state, 500), 14);

        arf_get_fmpq(am, arb_midref(a));
        mag_get_fmpq(ar, arb_radref(a));
        arf_get_fmpq(bm, b);

        c1 = arb_contains_arf(a, b);

        fmpq_sub(t, am, ar);
        c2 = fmpq_cmp(t, bm) <= 0;

        fmpq_add(t, am, ar);
        c2 = c2 && (fmpq_cmp(t, bm) >= 0);

        if (c1 != c2)
        {
            flint_printf("FAIL:\n\n");
            flint_printf("a = "); arb_print(a); flint_printf("\n\n");
            flint_printf("b = "); arf_print(b); flint_printf("\n\n");
            flint_printf("am = "); fmpq_print(am); flint_printf("\n\n");
            flint_printf("ar = "); fmpq_print(ar); flint_printf("\n\n");
            flint_printf("bm = "); fmpq_print(bm); flint_printf("\n\n");
            flint_printf("t = "); fmpq_print(t); flint_printf("\n\n");
            flint_printf("c1 = %d, c2 = %d\n\n", c1, c2);
            abort();
        }

        arb_clear(a);
        arf_clear(b);

        fmpq_clear(am);
        fmpq_clear(ar);
        fmpq_clear(bm);
        fmpq_clear(t);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
예제 #3
0
int main()
{
    slong iter;
    flint_rand_t state;

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

    flint_randinit(state);

    for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++)
    {
        arb_t a, b;
        fmpq_t am, ar, bm, br, t, u;
        int c1, c2;

        arb_init(a);
        arb_init(b);

        fmpq_init(am);
        fmpq_init(ar);
        fmpq_init(bm);
        fmpq_init(br);
        fmpq_init(t);
        fmpq_init(u);

        arb_randtest(a, state, 1 + n_randint(state, 500), 14);
        arb_randtest(b, state, 1 + n_randint(state, 500), 14);

        arf_get_fmpq(am, arb_midref(a));
        mag_get_fmpq(ar, arb_radref(a));
        arf_get_fmpq(bm, arb_midref(b));
        mag_get_fmpq(br, arb_radref(b));

        fmpq_sub(t, am, bm);
        fmpz_abs(fmpq_numref(t), fmpq_numref(t));
        fmpq_add(u, ar, br);

        c1 = arb_overlaps(a, b);

        c2 = (fmpq_cmp(t, u) <= 0);

        if (c1 != c2)
        {
            flint_printf("FAIL:\n\n");
            flint_printf("a = "); arb_print(a); flint_printf("\n\n");
            flint_printf("b = "); arb_print(b); flint_printf("\n\n");
            flint_printf("am = "); fmpq_print(am); flint_printf("\n\n");
            flint_printf("ar = "); fmpq_print(ar); flint_printf("\n\n");
            flint_printf("bm = "); fmpq_print(bm); flint_printf("\n\n");
            flint_printf("br = "); fmpq_print(br); flint_printf("\n\n");
            flint_printf("t = "); fmpq_print(t); flint_printf("\n\n");
            flint_printf("u = "); fmpq_print(u); flint_printf("\n\n");
            flint_printf("c1 = %d, c2 = %d\n\n", c1, c2);
            flint_abort();
        }

        arb_clear(a);
        arb_clear(b);

        fmpq_clear(am);
        fmpq_clear(ar);
        fmpq_clear(bm);
        fmpq_clear(br);
        fmpq_clear(t);
        fmpq_clear(u);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
예제 #4
0
int fmpq_poly_check_unique_real_root(const fmpq_poly_t pol, const arb_t a, slong prec)
{
    if (pol->length < 2)
        return 0;
    else if (pol->length == 2)
    {
        /* linear polynomial */
        fmpq_t root;
        int ans;

        fmpq_init(root);
        fmpq_set_fmpz_frac(root, fmpq_poly_numref(pol), fmpq_poly_numref(pol) + 1);
        fmpq_neg(root, root);
        ans = arb_contains_fmpq(a, root);
        fmpq_clear(root);
        return ans;
    }
    else
    {
        arb_t b, c;
        arf_t l, r;

        fmpz * der;
        int lsign, rsign;
        fmpz_poly_t pol2;
        slong n;

        /* 1 - cheap test:                    */
        /*   - sign(left) * sign(right) = -1  */
        /*   - no zero of the derivative      */
        arb_init(b);
        arb_init(c);
        arf_init(l);
        arf_init(r);
        arb_get_interval_arf(l, r, a, prec);
        arb_set_arf(b, l);
        _fmpz_poly_evaluate_arb(c, pol->coeffs, pol->length, b, 2*prec);
        lsign = arb_sgn2(c);

        arb_set_arf(b, r);
        _fmpz_poly_evaluate_arb(c, pol->coeffs, pol->length, b, 2*prec);
        rsign = arb_sgn2(c);

        arb_clear(c);
        if (lsign * rsign == -1)
        {
            der = _fmpz_vec_init(pol->length - 1);
            _fmpz_poly_derivative(der, pol->coeffs, pol->length);
            _fmpz_poly_evaluate_arb(b, der, pol->length - 1, a, prec);
            _fmpz_vec_clear(der, pol->length - 1);

            if (!arb_contains_zero(b))
            {
                arf_clear(l);
                arf_clear(r);
                arb_clear(b);
                return 1;
            }
        }
        else
            return 0;
        arb_clear(b);

        /* 2 - expensive testing                                        */
        fmpq_t ql, qr;

        fmpq_init(ql);
        fmpq_init(qr);
        arf_get_fmpq(ql, l);
        arf_get_fmpq(qr, r);

        fmpz_poly_init(pol2);
        fmpz_poly_fit_length(pol2, pol->length);
        _fmpz_vec_set(pol2->coeffs, pol->coeffs, pol->length);
        pol2->length = pol->length;
        _fmpz_poly_scale_0_1_fmpq(pol2->coeffs, pol2->length, ql, qr);

        n = fmpz_poly_num_real_roots_0_1(pol2);

        fmpz_poly_clear(pol2);
        fmpq_clear(ql);
        fmpq_clear(qr);

        return (n == 1);
    }
}