Пример #1
0
void
fmpq_mat_mul_fmpz_mat(fmpq_mat_t C, const fmpq_mat_t A, const fmpz_mat_t B)
{
    slong i, j;

    fmpz_mat_t Aclear;
    fmpz_mat_t Cclear;

    fmpz * Aden;

    fmpz_mat_init(Aclear, A->r, A->c);
    fmpz_mat_init(Cclear, A->r, B->c);

    Aden = _fmpz_vec_init(A->r);

    fmpq_mat_get_fmpz_mat_rowwise(Aclear, Aden, A);
    fmpz_mat_mul(Cclear, Aclear, B);

    for (i = 0; i < C->r; i++)
    {
        for (j = 0; j < C->c; j++)
        {
            fmpz_set(fmpq_mat_entry_num(C, i, j), fmpz_mat_entry(Cclear, i, j));
            fmpz_set(fmpq_mat_entry_den(C, i, j), Aden + i);
            fmpq_canonicalise(fmpq_mat_entry(C, i, j));
        }
    }

    fmpz_mat_clear(Aclear);
    fmpz_mat_clear(Cclear);

    _fmpz_vec_clear(Aden, A->r);
}
Пример #2
0
void
fmprb_get_rand_fmpq(fmpq_t q, flint_rand_t state, const fmprb_t x, long bits)
{
    /* there is only one rational */
    if (fmprb_is_exact(x))
    {
        fmpr_get_fmpq(q, fmprb_midref(x));
        return;
    }

    /* pick a denominator */
    fmpz_randbits(fmpq_denref(q), state, n_randint(state, bits + 1));
    fmpz_abs(fmpq_denref(q), fmpq_denref(q));
    if (fmpz_is_zero(fmpq_denref(q)))
        fmpz_one(fmpq_denref(q));

    _fmprb_get_rand_fmpq(fmpq_numref(q), fmpq_denref(q), state, fmpq_denref(q), x);
    fmpq_canonicalise(q);
}
Пример #3
0
void
fmpq_mat_set_fmpz_mat_div_fmpz(fmpq_mat_t X, const fmpz_mat_t Xnum,
                                                const fmpz_t den)
{
    slong i, j;

    if (fmpz_is_one(den))
    {
        fmpq_mat_set_fmpz_mat(X, Xnum);
    }
    else if (*den == WORD(-1))
    {
        fmpz_t t;
        fmpz_init(t);
        fmpz_set(t, den);

        for (i = 0; i < Xnum->r; i++)
        {
            for (j = 0; j < Xnum->c; j++)
            {
                fmpz_neg(fmpq_mat_entry_num(X, i, j),
                    fmpz_mat_entry(Xnum, i, j));
                fmpz_one(fmpq_mat_entry_den(X, i, j));
            }
        }

        fmpz_clear(t);
    }
    else
    {
        for (i = 0; i < Xnum->r; i++)
        {
            for (j = 0; j < Xnum->c; j++)
            {
                fmpz_set(fmpq_mat_entry_num(X, i, j),
                                fmpz_mat_entry(Xnum, i, j));
                fmpz_set(fmpq_mat_entry_den(X, i, j), den);
                fmpq_canonicalise(fmpq_mat_entry(X, i, j));
            }
        }
    }
}
Пример #4
0
void
dedekind_sum_coprime(fmpq_t s, const fmpz_t h, const fmpz_t k)
{
    if (fmpz_cmp_ui(k, DOUBLE_CUTOFF) < 0)
    {
        double t;

        t = dedekind_sum_coprime_d(*h, *k) * (6 * (*k));

        /* Round to nearest after truncation */
        if (t > 0)
            t += 0.5;
        else
            t -= 0.5;

        fmpz_set_d(fmpq_numref(s), t);
        fmpz_set_ui(fmpq_denref(s), 6UL * (*k));
        fmpq_canonicalise(s);
    }
    else
    {
        dedekind_sum_coprime_large(s, h, k);
    }
}
Пример #5
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("evaluate_fmpq....");
    fflush(stdout);

    flint_randinit(state);

    /* Check aliasing */
    for (i = 0; i < 1000; i++)
    {
        fmpz_t a, b;
        fmpq_t x, y;
        fmpq_poly_t f;

        fmpz_init(a);
        fmpz_init(b);
        fmpq_init(x);
        fmpq_init(y);
        fmpq_poly_init(f);
        fmpq_poly_randtest(f, state, n_randint(state, 80), 100);
        fmpz_randtest(a, state, 80);
        fmpz_randtest_not_zero(b, state, 80);
        fmpz_set(fmpq_numref(x), a);
        fmpz_set(fmpq_denref(x), a);
        fmpq_canonicalise(x);

        fmpq_poly_evaluate_fmpq(y, f, x);
        fmpq_poly_evaluate_fmpq(x, f, x);

        result = (fmpq_equal(x, y));
        if (!result)
        {
            printf("FAIL:\n");
            fmpz_print(a), printf("\n\n");
            fmpz_print(b), printf("\n\n");
            fmpq_poly_debug(f), printf("\n\n");
            abort();
        }

        fmpz_clear(a);
        fmpz_clear(b);
        fmpq_clear(x);
        fmpq_clear(y);
        fmpq_poly_clear(f);
    }

    /* Check that (f+g)(a) = f(a) + g(a) */
    for (i = 0; i < 1000; i++)
    {
        fmpz_t a, b;
        fmpq_t x, y, z;
        fmpq_poly_t f, g;

        fmpz_init(a);
        fmpz_init(b);
        fmpq_init(x);
        fmpq_init(y);
        fmpq_init(z);
        fmpq_poly_init(f);
        fmpq_poly_init(g);
        fmpq_poly_randtest(f, state, n_randint(state, 80), 100);
        fmpq_poly_randtest(g, state, n_randint(state, 80), 100);
        fmpz_randtest(a, state, 80);
        fmpz_randtest_not_zero(b, state, 80);
        fmpz_set(fmpq_numref(x), a);
        fmpz_set(fmpq_denref(x), a);
        fmpq_canonicalise(x);

        fmpq_poly_evaluate_fmpq(y, f, x);
        fmpq_poly_evaluate_fmpq(z, g, x);
        fmpq_add(y, y, z);
        fmpq_poly_add(f, f, g);
        fmpq_poly_evaluate_fmpq(z, f, x);

        result = (fmpq_equal(y, z));
        if (!result)
        {
            printf("FAIL:\n");
            fmpz_print(a), printf("\n\n");
            fmpz_print(b), printf("\n\n");
            abort();
        }

        fmpz_clear(a);
        fmpz_clear(b);
        fmpq_clear(x);
        fmpq_clear(y);
        fmpq_clear(z);
        fmpq_poly_clear(f);
        fmpq_poly_clear(g);
    }

    /* Check that (f*g)(a) = f(a) * g(a) */
    for (i = 0; i < 1000; i++)
    {
        fmpz_t a, b;
        fmpq_t x, y, z;
        fmpq_poly_t f, g;

        fmpz_init(a);
        fmpz_init(b);
        fmpq_init(x);
        fmpq_init(y);
        fmpq_init(z);
        fmpq_poly_init(f);
        fmpq_poly_init(g);
        fmpq_poly_randtest(f, state, n_randint(state, 50), 80);
        fmpq_poly_randtest(g, state, n_randint(state, 50), 80);
        fmpz_randtest(a, state, 80);
        fmpz_randtest_not_zero(b, state, 80);
        fmpz_set(fmpq_numref(x), a);
        fmpz_set(fmpq_denref(x), a);
        fmpq_canonicalise(x);

        fmpq_poly_evaluate_fmpq(y, f, x);
        fmpq_poly_evaluate_fmpq(z, g, x);
        fmpq_mul(y, y, z);
        fmpq_poly_mul(f, f, g);
        fmpq_poly_evaluate_fmpq(z, f, x);

        result = (fmpq_equal(y, z));
        if (!result)
        {
            printf("FAIL:\n");
            fmpz_print(a), printf("\n\n");
            fmpz_print(b), printf("\n\n");
            abort();
        }

        fmpz_clear(a);
        fmpz_clear(b);
        fmpq_clear(x);
        fmpq_clear(y);
        fmpq_clear(z);
        fmpq_poly_clear(f);
        fmpq_poly_clear(g);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
Пример #6
0
int
main(void)
{
    int i;
    flint_rand_t state;
    flint_randinit(state);

    printf("canonicalise....");
    fflush(stdout);

    for (i = 0; i < 10000; i++)
    {
        fmpq_t x;
        fmpz_t mult;

        fmpq_init(x);
        fmpq_randtest(x, state, 200);

        if (!fmpq_is_canonical(x))
        {
            printf("FAIL: expected fmpq_randtest output to be canonical\n");
            fmpq_print(x);
            printf("\n");
            abort();
        }

        fmpz_init(mult);
        fmpz_randtest_not_zero(mult, state, 200);
        fmpz_add_ui(mult, mult, 1UL);

        fmpz_mul(&x->num, &x->num, mult);
        fmpz_mul(&x->den, &x->den, mult);

        if (fmpq_is_canonical(x))
        {
            printf("FAIL: expected fmpq_is_canonical to detect common factor\n");
            fmpq_print(x);
            printf("\n");
            abort();
        }

        fmpq_canonicalise(x);

        if (!fmpq_is_canonical(x))
        {
            printf("FAIL: result not canonical after calling fmpq_canonicalise\n");
            fmpq_print(x);
            printf("\n");
            abort();
        }

        fmpz_neg(&x->den, &x->den);

        if (fmpq_is_canonical(x))
        {
            printf("FAIL: negative denominator reported as being canonical\n");
            fmpq_print(x);
            printf("\n");
            abort();
        }

        fmpq_canonicalise(x);

        if (!fmpq_is_canonical(x))
        {
            printf("FAIL: result not canonical after calling fmpq_canonicalise\n");
            fmpq_print(x);
            printf("\n");
            abort();
        }

        fmpz_clear(mult);
        fmpq_clear(x);
    }

    flint_randclear(state);

    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
Пример #7
0
void
fmpz_holonomic_get_nth_fmpq(fmpq_t res, const fmpz_holonomic_t op, const fmpq * initial, long n0, long n)
{
    long r = fmpz_holonomic_order(op);

    if (r == 0)
    {
        fmpq_zero(res);
        return;
    }
    else if (n < n0)
    {
        printf("not implemented\n");
        abort();
    }
    else if (n - n0 < r)
    {
        fmpq_set(res, initial + n - n0);
        return;
    }
    else
    {
        fmpz_mat_t M;
        long i;
        fmpz_t Q;
        fmpz_mat_init(M, r, r);
        fmpz_init(Q);

        fmpz_holonomic_forward_fmpz_mat(M, Q, op, n0, n - n0 - r + 1);

        {
            fmpz_t g, t;
            fmpz_init(g);
            fmpz_init(t);

            fmpz_one(g);
            for (i = 0; i < r; i++)
                fmpz_lcm(g, g, fmpq_denref(initial + i));

            fmpz_divexact(t, g, fmpq_denref(initial + 0));
            fmpz_mul(t, t, fmpq_numref(initial + 0));
            fmpz_mul(fmpz_mat_entry(M, r - 1, 0), fmpz_mat_entry(M, r - 1, 0), t);

            for (i = 1; i < r; i++)
            {
                fmpz_divexact(t, g, fmpq_denref(initial + i));
                fmpz_mul(t, t, fmpq_numref(initial + i));
                fmpz_addmul(fmpz_mat_entry(M, r - 1, 0), fmpz_mat_entry(M, r - 1, i), t);
            }

            fmpz_set(fmpq_numref(res), fmpz_mat_entry(M, r - 1, 0));
            fmpz_mul(fmpq_denref(res), Q, g);
            fmpq_canonicalise(res);

            fmpz_clear(g);
            fmpz_clear(t);
        }

        fmpz_mat_clear(M);
        fmpz_clear(Q);
    }
}