Beispiel #1
0
int
main(void)
{
    int i;
    flint_rand_t state;
    flint_randinit(state);

    printf("init/clear....");
    fflush(stdout);

    for (i = 0; i < 10000; i++)
    {
        nmod_poly_mat_t a;
        mp_limb_t mod;
        long j, k;
        long rows = n_randint(state, 100);
        long cols = n_randint(state, 100);
        mod = n_randtest_prime(state, 0);

        nmod_poly_mat_init(a, rows, cols, mod);

        for (j = 0; j < rows; j++)
            for (k = 0; k < cols; k++)
                nmod_poly_zero(nmod_poly_mat_entry(a, j, k));

        nmod_poly_mat_clear(a);
    }

    flint_randclear(state);
    printf("PASS\n");
    return 0;
}
Beispiel #2
0
int
main(void)
{
    int iter;
    FLINT_TEST_INIT(state);
    

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

    for (iter = 0; iter < 100 * flint_test_multiplier(); iter++)
    {
        nmod_poly_t poly1, poly2, poly3, xp;
        mp_limb_t modulus;
        ulong inflation;

        modulus = n_randtest_prime(state, 0);

        nmod_poly_init(poly1, modulus);
        nmod_poly_init(poly2, modulus);
        nmod_poly_init(poly3, modulus);
        nmod_poly_init(xp, modulus);

        nmod_poly_randtest(poly1, state, n_randint(state, 20));
        inflation = n_randint(state, 10);

        nmod_poly_inflate(poly2, poly1, inflation);

        nmod_poly_set_coeff_ui(xp, inflation, 1);
        nmod_poly_compose(poly3, poly1, xp);

        if (!nmod_poly_equal(poly2, poly3))
        {
            flint_printf("FAIL: not equal to compose (inflation = %wu)\n", inflation);
            flint_printf("poly1:\n"); nmod_poly_print(poly1); flint_printf("\n\n");
            flint_printf("poly2:\n"); nmod_poly_print(poly2); flint_printf("\n\n");
            flint_printf("poly3:\n"); nmod_poly_print(poly3); flint_printf("\n\n");
            abort();
        }

        nmod_poly_inflate(poly1, poly1, inflation);
        if (!nmod_poly_equal(poly1, poly2))
        {
            flint_printf("FAIL: aliasing (inflation = %wu)\n", inflation);
            flint_printf("poly1:\n"); nmod_poly_print(poly1); flint_printf("\n\n");
            flint_printf("poly2:\n"); nmod_poly_print(poly2); flint_printf("\n\n");
            abort();
        }

        nmod_poly_clear(poly1);
        nmod_poly_clear(poly2);
        nmod_poly_clear(poly3);
        nmod_poly_clear(xp);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Beispiel #3
0
int
main(void)
{
    flint_rand_t state;
    int iter;

    printf("one/is_one....");
    fflush(stdout);

    flint_randinit(state);

    for (iter = 0; iter < 1000; iter++)
    {
        nmod_poly_mat_t A;
        long m, n;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 10);
        n = n_randint(state, 10);

        nmod_poly_mat_init(A, m, n, mod);
        nmod_poly_mat_randtest(A, state, n_randint(state, 5));
        nmod_poly_mat_one(A);

        if (!nmod_poly_mat_is_one(A))
        {
            printf("FAIL: expected matrix to be one\n");
            abort();
        }

        if (m > 0 && n > 0)
        {
            m = n_randint(state, m);
            n = n_randint(state, n);

            if (m != n)
                nmod_poly_randtest_not_zero(nmod_poly_mat_entry(A, m, n),
                    state, 5);
            else
                do { nmod_poly_randtest_not_zero(nmod_poly_mat_entry(A, m, n),
                    state, 5); }
                while (nmod_poly_is_one(nmod_poly_mat_entry(A, m, n)));

            if (nmod_poly_mat_is_one(A))
            {
                printf("FAIL: expected matrix not to be one\n");
                abort();
            }
        }

        nmod_poly_mat_clear(A);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
int
main(void)
{
    int i, result = 1;
    flint_rand_t state;
    flint_randinit(state);

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

    for (i = 0; i < 10000; i++)
    {
        nmod_poly_t A, logA, res;
        long n;
        mp_limb_t mod;
        ulong power;
        mp_limb_t coeff;

        mod = n_randtest_prime(state, 0);
        n = n_randtest(state) % 100;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(logA, mod);
        nmod_poly_init(res, mod);

        coeff = n_randlimb(state) % mod;
        power = 1 + n_randint(state, 2*n + 1);

        nmod_poly_set_coeff_ui(A, 0, 1UL);
        nmod_poly_set_coeff_ui(A, power, coeff);

        nmod_poly_log_series(logA, A, n);
        nmod_poly_log_series_monomial_ui(res, coeff, power, n);

        result = nmod_poly_equal(logA, res);

        if (!result)
        {
            printf("FAIL:\n");
            printf("n = %ld, mod = %lu\n", n, mod);
            printf("power = %lu, coeff = %lu\n", power, coeff);
            printf("A: "); nmod_poly_print(A), printf("\n\n");
            printf("log(A): "); nmod_poly_print(logA), printf("\n\n");
            printf("res: "); nmod_poly_print(res), printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(logA);
        nmod_poly_clear(res);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
int
main(void)
{
    int i, result = 1;
    FLINT_TEST_INIT(state);
    

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

    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        nmod_poly_t A, expA, res;
        slong n;
        mp_limb_t mod;
        ulong power;
        mp_limb_t coeff;

        mod = n_randtest_prime(state, 0);
        n = n_randtest(state) % 100;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(expA, mod);
        nmod_poly_init(res, mod);

        coeff = n_randlimb(state) % mod;
        power = 1 + n_randint(state, 2*n + 1);

        nmod_poly_set_coeff_ui(A, power, coeff);

        nmod_poly_exp_series(expA, A, n);
        nmod_poly_exp_series_monomial_ui(res, coeff, power, n);

        result = nmod_poly_equal(expA, res);

        if (!result)
        {
            flint_printf("FAIL:\n");
            flint_printf("n = %wd, mod = %wu\n", n, mod);
            flint_printf("power = %wu, coeff = %wu\n", power, coeff);
            flint_printf("A: "); nmod_poly_print(A), flint_printf("\n\n");
            flint_printf("exp(A): "); nmod_poly_print(expA), flint_printf("\n\n");
            flint_printf("res: "); nmod_poly_print(res), flint_printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(expA);
        nmod_poly_clear(res);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
int
main(void)
{
    int i, result = 1;
    flint_rand_t state;
    flint_randinit(state);
    
    printf("interpolate_nmod_vec_fast....");
    fflush(stdout);

    for (i = 0; i < 10000; i++)
    {
        nmod_poly_t P, Q;
        mp_ptr x, y;
        mp_limb_t mod;
        long j, n, npoints;

        mod = n_randtest_prime(state, 0);
        npoints = n_randint(state, FLINT_MIN(100, mod));
        n = n_randint(state, npoints + 1);

        nmod_poly_init(P, mod);
        nmod_poly_init(Q, mod);
        x = _nmod_vec_init(npoints);
        y = _nmod_vec_init(npoints);

        nmod_poly_randtest(P, state, n);

        for (j = 0; j < npoints; j++)
            x[j] = j;

        nmod_poly_evaluate_nmod_vec_fast(y, P, x, npoints);
        nmod_poly_interpolate_nmod_vec_fast(Q, x, y, npoints);

        result = nmod_poly_equal(P, Q);

        if (!result)
        {
            printf("FAIL:\n");
            printf("mod=%lu, n=%ld, npoints=%ld\n\n", mod, n, npoints);
            nmod_poly_print(P), printf("\n\n");
            nmod_poly_print(Q), printf("\n\n");
            abort();
        }

        nmod_poly_clear(P);
        nmod_poly_clear(Q);
        _nmod_vec_clear(x);
        _nmod_vec_clear(y);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
Beispiel #7
0
int main(void)
{
    flint_rand_t state;
    long i, j;

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

    flint_randinit(state);

    for (i = 0; i < 10; i++)
    {
        mp_ptr b;
        long n;
        nmod_t mod;
        mp_limb_t p;

        n = n_randint(state, 1000);
        p = n_randtest_prime(state, 0);

        nmod_init(&mod, p);

        b = _nmod_vec_init(n + 1);
        bell_number_nmod_vec(b, n + 1, mod);

        for (j = 0; j <= n; j++)
        {
            mp_limb_t u = bell_number_nmod(j, mod);

            if (u != b[j])
            {
                printf("FAIL: p = %lu, i = %ld\n", p, j);
                abort();
            }
        }

        _nmod_vec_clear(b);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
Beispiel #8
0
int
main(void)
{
    int i;
    flint_rand_t state;

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

    flint_randinit(state);

    /* Test aliasing */
    for (i = 0; i < 2000; i++)
    {
        nmod_poly_t a, b;
        int square1, square2;
        mp_limb_t mod;
        mod = n_randtest_prime(state, 0);

        nmod_poly_init(a, mod);
        nmod_poly_init(b, mod);

        nmod_poly_randtest(a, state, 1 + n_randint(state, 50));

        if (n_randint(state, 2))
            nmod_poly_mul(a, a, a);

        square1 = nmod_poly_sqrt(b, a);
        square2 = nmod_poly_sqrt(a, a);

        if ((square1 != square2) || (square1 && !nmod_poly_equal(a, b)))
        {
            printf("FAIL: aliasing:\n");
            printf("square1 = %d, square2 = %d\n\n", square1, square2);
            printf("a: "); nmod_poly_print(a); printf("\n\n");
            printf("b: "); nmod_poly_print(b); printf("\n\n");
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
    }

    /* Test random squares */
    for (i = 0; i < 2000; i++)
    {
        nmod_poly_t a, b, c;
        int square;
        mp_limb_t mod;
        mod = n_randtest_prime(state, 0);

        nmod_poly_init(a, mod);
        nmod_poly_init(b, mod);
        nmod_poly_init(c, mod);

        nmod_poly_randtest(a, state, 1 + n_randint(state, 50));
        nmod_poly_mul(b, a, a);
        square = nmod_poly_sqrt(c, b);

        if (!square)
        {
            printf("FAIL: square reported nonsquare:\n");
            printf("a: "); nmod_poly_print(a); printf("\n\n");
            printf("b: "); nmod_poly_print(b); printf("\n\n");
            printf("c: "); nmod_poly_print(c); printf("\n\n");
            abort();
        }

        nmod_poly_mul(c, c, c);
        if (!nmod_poly_equal(c, b))
        {
            printf("FAIL: sqrt(b)^2 != b:\n");
            printf("a: "); nmod_poly_print(a); printf("\n\n");
            printf("b: "); nmod_poly_print(b); printf("\n\n");
            printf("c: "); nmod_poly_print(c); printf("\n\n");
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(c);
    }

    /* Test "almost" squares */
    for (i = 0; i < 2000; i++)
    {
        nmod_poly_t a, b, c;
        long j;
        int square;
        mp_limb_t mod;
        mod = n_randtest_prime(state, 0);

        nmod_poly_init(a, mod);
        nmod_poly_init(b, mod);
        nmod_poly_init(c, mod);

        nmod_poly_randtest_not_zero(a, state, 1 + n_randint(state, 50));
        nmod_poly_mul(b, a, a);

        j = n_randint(state, nmod_poly_length(b));
        b->coeffs[j] = n_randint(state, mod);
        _nmod_poly_normalise(b);

        square = nmod_poly_sqrt(c, b);

        if (square)
        {
            nmod_poly_mul(c, c, c);
            if (!nmod_poly_equal(c, b))
            {
                printf("FAIL: sqrt(b)^2 != b:\n");
                printf("a: "); nmod_poly_print(a); printf("\n\n");
                printf("b: "); nmod_poly_print(b); printf("\n\n");
                printf("c: "); nmod_poly_print(c); printf("\n\n");
                abort();
            }
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(c);
    }

    flint_randclear(state);
    printf("PASS\n");
    return 0;
}
int main()
{
    flint_rand_t state;
    long iter;

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

    flint_randinit(state);

    for (iter = 0; iter < 10000; iter++)
    {
        ring_t ZZ, ZZp, ZZpx[3];
        ring_struct * ring;
        elem_ptr p;
        long size[3];
        elem_poly_t A, B, C, Q, Q2, R, R2;

        ring_init_limb(ZZ);
        ELEM_TMP_INIT(p, ZZ);
        elem_set_ui(p, n_randtest_prime(state, 0), ZZ);
        ring_init_mod(ZZp, ZZ, p);
        ring_init_poly(ZZpx[0], ZZp);
        ring_init_poly(ZZpx[1], ZZpx[0]);
        ring_init_poly(ZZpx[2], ZZpx[1]);

        ring = ZZpx[n_randint(state, 2)];

        size[0] = 1 + n_randint(state, 30);
        size[1] = 1 + n_randint(state, 30);
        size[2] = 1 + n_randint(state, 30);

        elem_init(A, ring);
        elem_init(B, ring);
        elem_init(C, ring);
        elem_init(Q, ring);
        elem_init(Q2, ring);
        elem_init(R, ring);
        elem_init(R2, ring);

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_mul(C, A, B, ring);

        elem_poly_divrem_basecase(Q, R, C, B, ring);
        if (!elem_equal(Q, A, ring) || !elem_is_zero(R, ring))
        {
            printf("FAIL: (A * B) / B = A\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(C, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            abort();
        }

        elem_divrem(Q, R, A, B, ring);
        elem_mul(C, Q, B, ring);
        elem_add(C, C, R, ring);
        if (!elem_equal(C, A, ring))
        {
            printf("FAIL: Q * B + R = A\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(C, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            abort();
        }

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_poly_divrem_basecase(Q, R, A, B, ring);
        elem_poly_divrem_basecase(A, R2, A, B, ring);
        if (!elem_equal(A, Q, ring) || !elem_equal(R, R2, ring))
        {
            printf("FAIL: aliasing Q, A\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            elem_print(R2, ring); printf("\n\n");
            abort();
        }

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_poly_divrem_basecase(Q, R, A, B, ring);
        elem_poly_divrem_basecase(Q2, A, A, B, ring);
        if (!elem_equal(A, R, ring) || !elem_equal(Q, Q2, ring))
        {
            printf("FAIL: aliasing R, A\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(Q2, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            abort();
        }

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_poly_divrem_basecase(Q, R, A, B, ring);
        elem_poly_divrem_basecase(Q2, B, A, B, ring);
        if (!elem_equal(B, R, ring) || !elem_equal(Q, Q2, ring))
        {
            printf("FAIL: aliasing R, B\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(Q2, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            abort();
        }

        elem_randtest(A, state, size, ring);
        elem_randtest_not_zero(B, state, size, ring);
        elem_poly_divrem_basecase(Q, R, A, B, ring);
        elem_poly_divrem_basecase(B, R2, A, B, ring);
        if (!elem_equal(B, Q, ring) || !elem_equal(R, R2, ring))
        {
            printf("FAIL: aliasing Q, B\n");
            elem_print(A, ring); printf("\n\n");
            elem_print(B, ring); printf("\n\n");
            elem_print(Q, ring); printf("\n\n");
            elem_print(R, ring); printf("\n\n");
            elem_print(R2, ring); printf("\n\n");
            abort();
        }

        elem_clear(A, ring);
        elem_clear(B, ring);
        elem_clear(C, ring);
        elem_clear(Q, ring);
        elem_clear(Q2, ring);
        elem_clear(R, ring);
        elem_clear(R2, ring);

        ring_clear(ZZpx[2]);
        ring_clear(ZZpx[1]);
        ring_clear(ZZpx[0]);
        ring_clear(ZZp);
        ELEM_TMP_CLEAR(p, ZZ);
        ring_clear(ZZ);
    }

    printf("PASS\n");
    flint_randclear(state);
    return EXIT_SUCCESS;
}
Beispiel #10
0
/*------------------------------------------------------------*/
void check(int opt){
  long i;
  flint_rand_t state;
  flint_randinit(state);

  for (i = 1; i < 1000; i+=10){

    mp_limb_t n = 0;
    while (n < i)
      n = n_randtest_prime(state, 0);

    nmod_poly_t P;
    mp_ptr roots;
    nmod_poly_t A;
    
    nmod_poly_init(P, n);
    roots = _nmod_vec_init(i);
    _nmod_vec_randtest(roots, state, i, P->mod);
    nmod_poly_product_roots_nmod_vec(P, roots, i);
    nmod_poly_init(A, n);
    nmod_poly_rand_dense(A, state, i);

    if (opt == 1){
      mp_limb_t tr1 = nmod_poly_trace(A, P);
      mp_limb_t tr2 = 0;
      long j;
      for (j = 0; j < i; j++)
      	tr2 = nmod_add(tr2,  nmod_poly_evaluate_nmod(A, roots[j]), P->mod);

      sage_output_init(P->mod);
      sage_output_assign_poly(P, "P");
      printf("tr1 = %lu\n", tr1);
      printf("tr2 = %lu\n", tr2);
      printf("tr1 == tr2\n");
    }
    else{
      double t, u;
      long j;

      t = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_trace(A, P);
      t = util_gettime() - t;

      nmod_poly_t tmp1, tmp2, tmp3;
      nmod_poly_init(tmp1, n);
      nmod_poly_init(tmp2, n);
      nmod_poly_init(tmp3, n);

      nmod_poly_rand_dense(tmp1, state, i);
      nmod_poly_rand_dense(tmp2, state, i);

      u = util_gettime();
      for (j = 0; j < 10000; j++)
      	nmod_poly_mul(tmp3, tmp1, tmp2);
      u = util_gettime() - u;

      nmod_poly_clear(tmp1);
      nmod_poly_clear(tmp2);
      nmod_poly_clear(tmp3);

      printf("%lu %f %f\n", i, t, u);
    }

    nmod_poly_clear(P);
    nmod_poly_clear(A);
    _nmod_vec_clear(roots);

  }
}
Beispiel #11
0
int
main(void)
{
    int i, result = 1;
    FLINT_TEST_INIT(state);
    

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

    /* Check atan(tan(A)) = A */
    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        nmod_poly_t A, tanA, B;
        slong n;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        n = 1 + n_randtest(state) % 100;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(tanA, mod);
        nmod_poly_init(B, mod);

        nmod_poly_randtest(A, state, n_randint(state, 100));
        nmod_poly_set_coeff_ui(A, 0, UWORD(0));

        nmod_poly_tan_series(tanA, A, n);
        nmod_poly_atan_series(B, tanA, n);

        nmod_poly_truncate(A, n);

        result = nmod_poly_equal(A, B);

        if (!result)
        {
            flint_printf("FAIL:\n");
            flint_printf("n = %wd, mod = %wu\n", n, mod);
            flint_printf("A: "); nmod_poly_print(A), flint_printf("\n\n");
            flint_printf("tan(A): "); nmod_poly_print(tanA), flint_printf("\n\n");
            flint_printf("B: "); nmod_poly_print(B), flint_printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(tanA);
        nmod_poly_clear(B);
    }

    /* Check aliasing */
    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        nmod_poly_t A, B;
        slong n;
        mp_limb_t mod;
        mod = n_randtest_prime(state, 0);
        n = n_randtest(state) % 50;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(B, mod);
        nmod_poly_randtest(A, state, n_randint(state, 50));
        nmod_poly_set_coeff_ui(A, 0, UWORD(0));

        nmod_poly_tan_series(B, A, n);
        nmod_poly_tan_series(A, A, n);

        result = nmod_poly_equal(A, B);
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(A), flint_printf("\n\n");
            nmod_poly_print(B), flint_printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(B);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Beispiel #12
0
int
main(void)
{
    int i, result;
    flint_rand_t state;
    flint_randinit(state);

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

    /* Check 1/g^2 = h mod x^m */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_t h, g, r;
        long m;

        mp_limb_t n;
        do n = n_randtest_prime(state, 0);
        while (n == 2UL);

        nmod_poly_init(h, n);
        nmod_poly_init(g, n);
        nmod_poly_init(r, n);

        do nmod_poly_randtest(h, state, n_randint(state, 1000));
        while (h->length == 0);
        nmod_poly_set_coeff_ui(h, 0, 1UL);

        m = n_randint(state, h->length) + 1;

        nmod_poly_invsqrt_series(g, h, m);

        nmod_poly_mullow(r, g, g, m);
        nmod_poly_inv_series(r, r, m);
        nmod_poly_truncate(h, m);

        result = (nmod_poly_equal(r, h));
        if (!result)
        {
            printf("FAIL:\n");
            nmod_poly_print(h), printf("\n\n");
            nmod_poly_print(g), printf("\n\n");
            nmod_poly_print(r), printf("\n\n");
            printf("n = %ld\n", n);
            abort();
        }
        
        nmod_poly_clear(h);
        nmod_poly_clear(g);
        nmod_poly_clear(r);
    }

    /* Check aliasing of h and g */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_t g, h;
        long m;

        mp_limb_t n;
        do n = n_randtest_prime(state, 0);
        while (n == 2UL);

        nmod_poly_init(h, n);
        nmod_poly_init(g, n);
        do nmod_poly_randtest(h, state, n_randint(state, 500));
        while (h->length == 0);
        nmod_poly_set_coeff_ui(h, 0, 1UL);

        m = n_randint(state, h->length) + 1;

        nmod_poly_invsqrt_series(g, h, m);
        nmod_poly_invsqrt_series(h, h, m);

        result = (nmod_poly_equal(g, h));
        if (!result)
        {
            printf("FAIL:\n");
            nmod_poly_print(h), printf("\n\n");
            nmod_poly_print(g), printf("\n\n");
            printf("n = %ld, m = %ld\n", n, m);
            abort();
        }

        nmod_poly_clear(g);
        nmod_poly_clear(h);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
Beispiel #13
0
int
main(void)
{
    flint_rand_t state;
    long i;

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

    flint_randinit(state);

    /* Check evaluation homomorphism */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B, C;
        nmod_mat_t a, b, c, d;
        mp_limb_t mod, x;
        long m, n, k, deg;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        n = n_randint(state, 20);
        k = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, n, mod);
        nmod_poly_mat_init(B, n, k, mod);
        nmod_poly_mat_init(C, m, k, mod);

        nmod_mat_init(a, m, n, mod);
        nmod_mat_init(b, n, k, mod);
        nmod_mat_init(c, m, k, mod);
        nmod_mat_init(d, m, k, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(B, state, deg);
        nmod_poly_mat_randtest(C, state, deg);  /* noise in output */

        if (nmod_poly_mat_max_length(A)
            + nmod_poly_mat_max_length(B) - 1 <= mod)
        {
            nmod_poly_mat_mul_interpolate(C, A, B);

            x = n_randint(state, mod);

            nmod_poly_mat_evaluate_nmod(a, A, x);
            nmod_poly_mat_evaluate_nmod(b, B, x);
            nmod_poly_mat_evaluate_nmod(d, C, x);
            nmod_mat_mul(c, a, b);

            if (!nmod_mat_equal(c, d))
            {
                printf("FAIL:\n");
                printf("A:\n");
                nmod_poly_mat_print(A, "x");
                printf("B:\n");
                nmod_poly_mat_print(B, "x");
                printf("C:\n");
                nmod_poly_mat_print(C, "x");
                printf("\n");
                abort();
            }
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
        nmod_poly_mat_clear(C);

        nmod_mat_clear(a);
        nmod_mat_clear(b);
        nmod_mat_clear(c);
        nmod_mat_clear(d);
    }

    /* Check aliasing C and A */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B, C;
        long m, n, deg;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        n = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, n, mod);
        nmod_poly_mat_init(B, n, n, mod);
        nmod_poly_mat_init(C, m, n, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(B, state, deg);
        nmod_poly_mat_randtest(C, state, deg);  /* noise in output */

        if (nmod_poly_mat_max_length(A)
            + nmod_poly_mat_max_length(B) - 1 <= mod)
        {

            nmod_poly_mat_mul_interpolate(C, A, B);
            nmod_poly_mat_mul_interpolate(A, A, B);

            if (!nmod_poly_mat_equal(C, A))
            {
                printf("FAIL:\n");
                printf("A:\n");
                nmod_poly_mat_print(A, "x");
                printf("B:\n");
                nmod_poly_mat_print(B, "x");
                printf("C:\n");
                nmod_poly_mat_print(C, "x");
                printf("\n");
                abort();
            }
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
        nmod_poly_mat_clear(C);
    }

    /* Check aliasing C and B */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B, C;
        long m, n, deg;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        n = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, m, mod);
        nmod_poly_mat_init(B, m, n, mod);
        nmod_poly_mat_init(C, m, n, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(B, state, deg);
        nmod_poly_mat_randtest(C, state, deg);  /* noise in output */

        if (nmod_poly_mat_max_length(A)
            + nmod_poly_mat_max_length(B) - 1 <= mod)
        {
            nmod_poly_mat_mul_interpolate(C, A, B);
            nmod_poly_mat_mul_interpolate(B, A, B);

            if (!nmod_poly_mat_equal(C, B))
            {
                printf("FAIL:\n");
                printf("A:\n");
                nmod_poly_mat_print(A, "x");
                printf("B:\n");
                nmod_poly_mat_print(B, "x");
                printf("C:\n");
                nmod_poly_mat_print(C, "x");
                printf("\n");
                abort();
            }
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
        nmod_poly_mat_clear(C);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
Beispiel #14
0
int
main(void)
{
    int i, result = 1;
    flint_rand_t state;
    flint_randinit(state);

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

    /* Check asin(A) = atan(A/sqrt(1-A^2)) */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_t A, B, asinA, atanB;
        long n;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        n = 1 + n_randtest(state) % 100;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(B, mod);
        nmod_poly_init(asinA, mod);
        nmod_poly_init(atanB, mod);

        nmod_poly_randtest(A, state, n_randint(state, 100));
        nmod_poly_set_coeff_ui(A, 0, 0UL);

        nmod_poly_mullow(B, A, A, n);
        nmod_poly_neg(B, B);
        nmod_poly_set_coeff_ui(B, 0, 1UL);
        nmod_poly_invsqrt_series(B, B, n);
        nmod_poly_mullow(B, A, B, n);

        nmod_poly_asin_series(asinA, A, n);
        nmod_poly_atan_series(atanB, B, n);

        result = nmod_poly_equal(asinA, atanB);

        if (!result)
        {
            printf("FAIL:\n");
            printf("n = %ld, mod = %lu\n", n, mod);
            printf("A: "); nmod_poly_print(A), printf("\n\n");
            printf("B: "); nmod_poly_print(B), printf("\n\n");
            printf("asin(A): "); nmod_poly_print(asinA), printf("\n\n");
            printf("atan(B): "); nmod_poly_print(atanB), printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(B);
        nmod_poly_clear(asinA);
        nmod_poly_clear(atanB);
    }

    /* Check aliasing */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_t A, B;
        long n;
        mp_limb_t mod;
        mod = n_randtest_prime(state, 0);
        n = n_randtest(state) % 50;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(B, mod);
        nmod_poly_randtest(A, state, n_randint(state, 50));
        nmod_poly_set_coeff_ui(A, 0, 0UL);

        nmod_poly_asin_series(B, A, n);
        nmod_poly_asin_series(A, A, n);

        result = nmod_poly_equal(A, B);
        if (!result)
        {
            printf("FAIL:\n");
            nmod_poly_print(A), printf("\n\n");
            nmod_poly_print(B), printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(B);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
Beispiel #15
0
int
main(void)
{
    flint_rand_t state;
    long i;

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

    flint_randinit(state);

    /* Check evaluation homomorphism */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, C;
        nmod_mat_t a, c, d;
        mp_limb_t x, mod;
        long m, deg;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, m, mod);
        nmod_poly_mat_init(C, m, m, mod);

        nmod_mat_init(a, m, m, mod);
        nmod_mat_init(c, m, m, mod);
        nmod_mat_init(d, m, m, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(C, state, deg);  /* noise in output */

        nmod_poly_mat_sqr(C, A);

        x = n_randint(state, 0);

        nmod_poly_mat_evaluate_nmod(a, A, x);
        nmod_poly_mat_evaluate_nmod(d, C, x);
        nmod_mat_mul(c, a, a);

        if (!nmod_mat_equal(c, d))
        {
            printf("FAIL:\n");
            printf("A:\n");
            nmod_poly_mat_print(A, "x");
            printf("C:\n");
            nmod_poly_mat_print(C, "x");
            printf("\n");
            abort();
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(C);

        nmod_mat_clear(a);
        nmod_mat_clear(c);
        nmod_mat_clear(d);
    }

    /* Check aliasing B and A */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B;
        long m, deg;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 20);
        deg = 1 + n_randint(state, 10);

        nmod_poly_mat_init(A, m, m, mod);
        nmod_poly_mat_init(B, m, m, mod);

        nmod_poly_mat_randtest(A, state, deg);
        nmod_poly_mat_randtest(B, state, deg);  /* noise in output */

        nmod_poly_mat_sqr(B, A);
        nmod_poly_mat_sqr(A, A);

        if (!nmod_poly_mat_equal(B, A))
        {
            printf("FAIL (aliasing):\n");
            printf("A:\n");
            nmod_poly_mat_print(A, "x");
            printf("B:\n");
            nmod_poly_mat_print(B, "x");
            printf("\n");
            abort();
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
Beispiel #16
0
int
main(void)
{
    int iter;
    FLINT_TEST_INIT(state);
    

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

    for (iter = 0; iter < 300; iter++)
    {
        int result = 1;
        fmpz_mod_poly_t pol1, poly, quot, rem;
        fmpz_mod_poly_factor_t res;
        fmpz_t modulus;
        slong exp[5], prod1;
        slong length, i, j, num;

        fmpz_init_set_ui(modulus, n_randtest_prime(state, 0));

        fmpz_mod_poly_init(pol1, modulus);
        fmpz_mod_poly_init(poly, modulus);
        fmpz_mod_poly_init(quot, modulus);
        fmpz_mod_poly_init(rem, modulus);

        fmpz_mod_poly_zero(pol1);
        fmpz_mod_poly_set_coeff_ui(pol1, 0, 1);

        length = n_randint(state, 7) + 2;

        do
        {
            fmpz_mod_poly_randtest(poly, state, length);
            fmpz_mod_poly_make_monic(poly, poly);
        }
        while ((!fmpz_mod_poly_is_irreducible(poly)) || (poly->length < 2));
        exp[0] = n_randprime(state, 5, 0);

        prod1 = exp[0];
        for (i = 0; i < exp[0]; i++)
            fmpz_mod_poly_mul(pol1, pol1, poly);

        num = n_randint(state, 5) + 1;
        for (i = 1; i < num; i++)
        {
            do
            {
                length = n_randint(state, 7) + 2;
                fmpz_mod_poly_randtest(poly, state, length);
                if (poly->length)
                {
                    fmpz_mod_poly_make_monic(poly, poly);
                    fmpz_mod_poly_divrem(quot, rem, pol1, poly);
                }
            }
            while ((!fmpz_mod_poly_is_irreducible(poly)) ||
                   (poly->length < 2) || (rem->length == 0));

            do
                exp[i] = n_randprime(state, 5, 0);
            while (prod1 % exp[i] == 0);

            prod1 *= exp[i];
            for (j = 0; j < exp[i]; j++)
                fmpz_mod_poly_mul(pol1, pol1, poly);
        }

        fmpz_mod_poly_factor_init(res);
        fmpz_mod_poly_factor_squarefree(res, pol1);

        result &= (res->num == num);
        if (result)
        {
            ulong prod2 = 1;
            for (i = 0; i < num; i++)
                prod2 *= res->exp[i];
            result &= (prod1 == prod2);
        }

        if (!result)
        {
            flint_printf("Error: exp don't match. Modulus = ");
            fmpz_print(modulus);
            flint_printf("\n");
            for (i = 0; i < res->num; i++)
                flint_printf("%wd ", res->exp[i]);
            flint_printf("\n");
            for (i = 0; i < num; i++)
                flint_printf("%wd ", exp[i]);
            flint_printf("\n");
            abort();
        }

        fmpz_clear(modulus);
        fmpz_mod_poly_clear(quot);
        fmpz_mod_poly_clear(rem);
        fmpz_mod_poly_clear(pol1);
        fmpz_mod_poly_clear(poly);
        fmpz_mod_poly_factor_clear(res);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Beispiel #17
0
int
main(void)
{
    int i, result;

    padic_ctx_t ctx;
    fmpz_t p;
    slong N;

    FLINT_TEST_INIT(state);

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

    /* Check aliasing */
    for (i = 0; i < 1000; i++)
    {
        padic_poly_t a, b, c;
        slong n;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) 
            + PADIC_TEST_PREC_MIN;
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_poly_init2(a, 0, N);
        padic_poly_init2(b, 0, N);
        padic_poly_init2(c, 0, N);

        padic_poly_randtest(a, state, n_randint(state, 100) + 1, ctx);
        if (fmpz_is_zero(a->coeffs))
        {
            fmpz_randtest_not_zero(a->coeffs, state, 20);
            fmpz_remove(a->coeffs, a->coeffs, p);
            padic_poly_reduce(a, ctx);
        } else
            fmpz_remove(a->coeffs, a->coeffs, p);
        
        padic_poly_set(b, a, ctx);
        n = n_randint(state, 100) + 1;

        padic_poly_inv_series(c, b, n, ctx);
        padic_poly_inv_series(b, b, n, ctx);

        result = (padic_poly_equal(b, c) && padic_poly_is_reduced(b, ctx));
        if (!result)
        {
            flint_printf("FAIL:\n");
            flint_printf("a = "), padic_poly_print(a, ctx), flint_printf("\n\n");
            flint_printf("b = "), padic_poly_print(b, ctx), flint_printf("\n\n");
            flint_printf("c = "), padic_poly_print(c, ctx), flint_printf("\n\n");
            abort();
        }

        padic_poly_clear(a);
        padic_poly_clear(b);
        padic_poly_clear(c);

        padic_ctx_clear(ctx);
        fmpz_clear(p);
    }

    /*
        Check correctness: 

        If ord_p(a) = v then we can compute b = a^{-1} mod p^N 
        and we will have a b = 1 mod p^{N-|v|}.  Thus, require 
        that N - |v| > 0.
     */
    for (i = 0; i < 1000; i++)
    {
        padic_poly_t a, b, c;
        slong n, N2;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = n_randint(state, PADIC_TEST_PREC_MAX - 1) + 1;
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_poly_init2(a, 0, N);
        padic_poly_init2(b, 0, N);

        {
            slong i, len = n_randint(state, 10) + 1;
            int alloc;
            fmpz_t pow;

            padic_poly_fit_length(a, len);
            _padic_poly_set_length(a, len);
            a->val = n_randint(state, N);
            if (n_randint(state, 2))
                a->val = - a->val;

            alloc = _padic_ctx_pow_ui(pow, N - a->val, ctx);

            for (i = 0; i < len; i++)
                fmpz_randm(a->coeffs + i, state, pow);
            while (fmpz_is_zero(a->coeffs))
                fmpz_randm(a->coeffs, state, pow);
            fmpz_remove(a->coeffs, a->coeffs, p);
            _padic_poly_normalise(a);

            if (alloc)
                fmpz_clear(pow);
        }

        n = n_randint(state, 100) + 1;

        N2 = N - FLINT_ABS(a->val);
        padic_poly_init2(c, 0, N2);

        padic_poly_inv_series(b, a, n, ctx);
        padic_poly_mul(c, a, b, ctx);
        padic_poly_truncate(c, n, p);

        result = (padic_poly_is_one(c) && padic_poly_is_reduced(b, ctx));
        if (!result)
        {
            flint_printf("FAIL:\n");
            flint_printf("a = "), padic_poly_print(a, ctx), flint_printf("\n\n");
            flint_printf("b = "), padic_poly_print(b, ctx), flint_printf("\n\n");
            flint_printf("c = "), padic_poly_print(c, ctx), flint_printf("\n\n");
            flint_printf("N = %wd\n", N);
            flint_printf("N2 = %wd\n", N2);
            abort();
        }

        padic_poly_clear(a);
        padic_poly_clear(b);
        padic_poly_clear(c);

        padic_ctx_clear(ctx);
        fmpz_clear(p);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
int
main(void)
{
    int i;
    FLINT_TEST_INIT(state);

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

    

    /* Check aliasing */
    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        nmod_poly_t f, g;
        mp_limb_t c, mod;

        mod = n_randtest_prime(state, 0);

        nmod_poly_init(f, mod);
        nmod_poly_init(g, mod);

        nmod_poly_randtest(f, state, 1 + n_randint(state, 50));
        c = n_randtest(state) % mod;

        nmod_poly_taylor_shift_horner(g, f, c);
        nmod_poly_taylor_shift_horner(f, f, c);

        if (!nmod_poly_equal(g, f))
        {
            flint_printf("FAIL\n");
            nmod_poly_print(f); flint_printf("\n");
            nmod_poly_print(g); flint_printf("\n");
            abort();
        }

        nmod_poly_clear(f);
        nmod_poly_clear(g);
    }

    /* Compare with composition */
    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        nmod_poly_t f, g, h1, h2;
        mp_limb_t mod, c;

        mod = n_randtest_prime(state, 0);

        nmod_poly_init(f, mod);
        nmod_poly_init(g, mod);
        nmod_poly_init(h1, mod);
        nmod_poly_init(h2, mod);

        nmod_poly_randtest(f, state, 1 + n_randint(state, 50));
        c = n_randtest(state) % mod;

        nmod_poly_set_coeff_ui(g, 1, 1);
        nmod_poly_set_coeff_ui(g, 0, c);

        nmod_poly_taylor_shift_horner(h1, f, c);
        nmod_poly_compose(h2, f, g);

        if (!nmod_poly_equal(h1, h2))
        {
            flint_printf("FAIL\n");
            nmod_poly_print(f); flint_printf("\n");
            nmod_poly_print(g); flint_printf("\n");
            nmod_poly_print(h1); flint_printf("\n");
            nmod_poly_print(h2); flint_printf("\n");
            abort();
        }

        nmod_poly_clear(f);
        nmod_poly_clear(g);
        nmod_poly_clear(h1);
        nmod_poly_clear(h2);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Beispiel #19
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

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

    flint_randinit(state);

    /* Check aliasing */
    for (i = 0; i < 100; i++)
    {
        nmod_poly_t f, g;
        mp_limb_t m;
        long n;

        m = n_randtest_prime(state, 0);
        nmod_poly_init(f, m);
        nmod_poly_init(g, m);
        do {
            nmod_poly_randtest(g, state, n_randint(state, 100));
        } while (nmod_poly_get_coeff_ui(g, 1) == 0);
        nmod_poly_set_coeff_ui(g, 0, 0);
        do {
            n = n_randint(state, 100);
        } while (n >= m);

        nmod_poly_revert_series(f, g, n);
        nmod_poly_revert_series(g, g, n);

        result = (nmod_poly_equal(f, g));
        if (!result)
        {
            printf("FAIL (aliasing):\n");
            nmod_poly_print(f), printf("\n\n");
            nmod_poly_print(g), printf("\n\n");
            abort();
        }

        nmod_poly_clear(f);
        nmod_poly_clear(g);
    }

    /* Check f(f^(-1)) = id */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_t f, g, h;
        mp_limb_t m;
        long n;

        m = n_randtest_prime(state, 0);
        nmod_poly_init(f, m);
        nmod_poly_init(g, m);
        nmod_poly_init(h, m);
        do {
            nmod_poly_randtest(g, state, n_randint(state, 100));
        } while (nmod_poly_get_coeff_ui(g, 1) == 0);
        nmod_poly_set_coeff_ui(g, 0, 0);
        do {
            n = n_randint(state, 100);
        } while (n >= m);

        nmod_poly_revert_series(f, g, n);
        nmod_poly_compose_series(h, g, f, n);

        result = ((n <= 1 && nmod_poly_is_zero(h)) ||
            (h->length == 2 && h->coeffs[0] == 0 && h->coeffs[1] == 1));
        if (!result)
        {
            printf("FAIL (comparison):\n");
            nmod_poly_print(g), printf("\n\n");
            nmod_poly_print(f), printf("\n\n");
            nmod_poly_print(h), printf("\n\n");
            abort();
        }

        nmod_poly_clear(f);
        nmod_poly_clear(g);
        nmod_poly_clear(h);
    }

    flint_randclear(state);
    printf("PASS\n");
    return 0;
}
Beispiel #20
0
int
main(void)
{
    int i, result;
    FLINT_TEST_INIT(state);

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

    

/** p == 2 *******************************************************************/

    /* Check aliasing: a = log(a) */
    for (i = 0; i < 1000; i++)
    {
        fmpz_t p = {WORD(2)};
        slong N;
        padic_ctx_t ctx;

        padic_t a, b;
        int ans1, ans2;

        N = __rand_prec(state, i);
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_init2(a, N);
        padic_init2(b, N);

        padic_randtest(a, state, ctx);

        padic_one(b);
        padic_add(a, a, b, ctx);

        ans1 = padic_log(b, a, ctx);
        ans2 = padic_log(a, a, ctx);

        result = (ans1 == ans2) && (!ans1 || padic_equal(a, b));
        if (!result)
        {
            flint_printf("FAIL (aliasing):\n\n");
            flint_printf("a = "), padic_print(a, ctx), flint_printf("\n");
            flint_printf("b = "), padic_print(b, ctx), flint_printf("\n");
            abort();
        }

        padic_clear(a);
        padic_clear(b);

        padic_ctx_clear(ctx);
    }

    /* Check: log(a) + log(b) == log(a * b) */
    for (i = 0; i < 10000; i++)
    {
        fmpz_t p = {WORD(2)};
        slong N;
        padic_ctx_t ctx;

        padic_t a, b, c, d, e, f, g;
        int ans1, ans2, ans3;

        N = __rand_prec(state, i);
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_init2(a, N);
        padic_init2(b, N);
        padic_init2(c, N);
        padic_init2(d, N);
        padic_init2(e, N);
        padic_init2(f, N);
        padic_init2(g, N);

        padic_randtest(a, state, ctx);
        padic_randtest(b, state, ctx);

        padic_one(c);
        padic_add(a, a, c, ctx);
        padic_add(b, b, c, ctx);

        padic_mul(c, a, b, ctx);

        ans1 = padic_log(d, a, ctx);
        ans2 = padic_log(e, b, ctx);
        padic_add(f, d, e, ctx);

        ans3 = padic_log(g, c, ctx);

        result = (!ans1 || !ans2 || (ans3 && padic_equal(f, g)));
        if (!result)
        {
            flint_printf("FAIL (functional equation):\n\n");
            flint_printf("a                   = "), padic_print(a, ctx), flint_printf("\n");
            flint_printf("b                   = "), padic_print(b, ctx), flint_printf("\n");
            flint_printf("c = a * b           = "), padic_print(c, ctx), flint_printf("\n");
            flint_printf("d = log(a)          = "), padic_print(d, ctx), flint_printf("\n");
            flint_printf("e = log(b)          = "), padic_print(e, ctx), flint_printf("\n");
            flint_printf("f = log(a) + log(b) = "), padic_print(f, ctx), flint_printf("\n");
            flint_printf("g = log(a * b)      = "), padic_print(g, ctx), flint_printf("\n");
            abort();
        }

        padic_clear(a);
        padic_clear(b);
        padic_clear(c);
        padic_clear(d);
        padic_clear(e);
        padic_clear(f);
        padic_clear(g);

        padic_ctx_clear(ctx);
    }

    /* Check: log(exp(x)) == x */
    for (i = 0; i < 10000; i++)
    {
        fmpz_t p = {WORD(2)};
        slong N;
        padic_ctx_t ctx;

        padic_t a, b, c;
        int ans1, ans2;

        N = __rand_prec(state, i);
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_init2(a, N);
        padic_init2(b, N);
        padic_init2(c, N);

        padic_randtest(a, state, ctx);

        ans1 = padic_exp(b, a, ctx);
        if (ans1)
            ans2 = padic_log(c, b, ctx);

        result = !ans1 || (ans1 == ans2 && padic_equal(a, c));
        if (!result)
        {
            flint_printf("FAIL (log(exp(x)) == x):\n\n");
            flint_printf("a = "), padic_print(a, ctx), flint_printf("\n");
            flint_printf("b = "), padic_print(b, ctx), flint_printf("\n");
            flint_printf("c = "), padic_print(c, ctx), flint_printf("\n");
            flint_printf("ans1 = %d\n", ans1);
            flint_printf("ans2 = %d\n", ans2);
            abort();
        }

        padic_clear(a);
        padic_clear(b);
        padic_clear(c);

        padic_ctx_clear(ctx);
    }

/** p > 2 ********************************************************************/

    /* Check aliasing: a = log(a) */
    for (i = 0; i < 1000; i++)
    {
        fmpz_t p;
        slong N;
        padic_ctx_t ctx;

        padic_t a, b;
        int ans1, ans2;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = __rand_prec(state, i);
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_init2(a, N);
        padic_init2(b, N);

        padic_randtest(a, state, ctx);

        padic_one(b);
        padic_add(a, a, b, ctx);

        ans1 = padic_log(b, a, ctx);
        ans2 = padic_log(a, a, ctx);

        result = (ans1 == ans2) && (!ans1 || padic_equal(a, b));
        if (!result)
        {
            flint_printf("FAIL (aliasing):\n\n");
            flint_printf("a = "), padic_print(a, ctx), flint_printf("\n");
            flint_printf("b = "), padic_print(b, ctx), flint_printf("\n");
            abort();
        }

        padic_clear(a);
        padic_clear(b);

        fmpz_clear(p);
        padic_ctx_clear(ctx);
    }

    /* Check: log(a) + log(b) == log(a * b) */
    for (i = 0; i < 10000; i++)
    {
        fmpz_t p;
        slong N;
        padic_ctx_t ctx;

        padic_t a, b, c, d, e, f, g;
        int ans1, ans2, ans3;

/*      fmpz_init_set_ui(p, n_randtest_prime(state, 0)); */
        fmpz_init_set_ui(p, n_randprime(state, 5, 1));
        N = __rand_prec(state, i);
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_init2(a, N);
        padic_init2(b, N);
        padic_init2(c, N);
        padic_init2(d, N);
        padic_init2(e, N);
        padic_init2(f, N);
        padic_init2(g, N);

        padic_randtest(a, state, ctx);
        padic_randtest(b, state, ctx);

        padic_one(c);
        padic_add(a, a, c, ctx);

        padic_one(c);
        padic_add(b, b, c, ctx);

        padic_mul(c, a, b, ctx);

        ans1 = padic_log(d, a, ctx);
        ans2 = padic_log(e, b, ctx);
        padic_add(f, d, e, ctx);

        ans3 = padic_log(g, c, ctx);

        result = (!ans1 || !ans2 || (ans3 && padic_equal(f, g)));
        if (!result)
        {
            flint_printf("FAIL (functional equation):\n\n");
            flint_printf("a                   = "), padic_print(a, ctx), flint_printf("\n");
            flint_printf("b                   = "), padic_print(b, ctx), flint_printf("\n");
            flint_printf("c = a * b           = "), padic_print(c, ctx), flint_printf("\n");
            flint_printf("d = log(a)          = "), padic_print(d, ctx), flint_printf("\n");
            flint_printf("e = log(b)          = "), padic_print(e, ctx), flint_printf("\n");
            flint_printf("f = log(a) + log(b) = "), padic_print(f, ctx), flint_printf("\n");
            flint_printf("g = log(a * b)      = "), padic_print(g, ctx), flint_printf("\n");
            flint_printf("ans1 = %d\n", ans1);
            flint_printf("ans2 = %d\n", ans2);
            flint_printf("ans3 = %d\n", ans3);
            abort();
        }

        padic_clear(a);
        padic_clear(b);
        padic_clear(c);
        padic_clear(d);
        padic_clear(e);
        padic_clear(f);
        padic_clear(g);

        fmpz_clear(p);
        padic_ctx_clear(ctx);
    }

    /* Check: log(exp(x)) == x */
    for (i = 0; i < 10000; i++)
    {
        fmpz_t p;
        slong N;
        padic_ctx_t ctx;

        padic_t a, b, c;
        int ans1, ans2;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = __rand_prec(state, i);
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_init2(a, N);
        padic_init2(b, N);
        padic_init2(c, N);

        padic_randtest(a, state, ctx);

        ans1 = padic_exp(b, a, ctx);
        if (ans1)
            ans2 = padic_log(c, b, ctx);

        result = !ans1 || (ans1 == ans2 && padic_equal(a, c));
        if (!result)
        {
            flint_printf("FAIL (log(exp(x)) == x):\n\n");
            flint_printf("a = "), padic_print(a, ctx), flint_printf("\n");
            flint_printf("b = "), padic_print(b, ctx), flint_printf("\n");
            flint_printf("c = "), padic_print(c, ctx), flint_printf("\n");
            flint_printf("ans1 = %d\n", ans1);
            flint_printf("ans2 = %d\n", ans2);
            abort();
        }

        padic_clear(a);
        padic_clear(b);
        padic_clear(c);

        fmpz_clear(p);
        padic_ctx_clear(ctx);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
Beispiel #21
0
int
main(void)
{
    int i, result = 1;
    flint_rand_t state;
    flint_randinit(state);

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

    /* Check atanh(tanh(A)) = A */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_t A, tanhA, B;
        long n;
        mp_limb_t mod;

        do { mod = n_randtest_prime(state, 0); } while (mod == 2);
        n = 1 + n_randtest(state) % 100;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(tanhA, mod);
        nmod_poly_init(B, mod);

        nmod_poly_randtest(A, state, n_randint(state, 100));
        nmod_poly_set_coeff_ui(A, 0, 0UL);

        nmod_poly_tanh_series(tanhA, A, n);
        nmod_poly_atanh_series(B, tanhA, n);

        nmod_poly_truncate(A, n);

        result = nmod_poly_equal(A, B);

        if (!result)
        {
            printf("FAIL:\n");
            printf("n = %ld, mod = %lu\n", n, mod);
            printf("A: "); nmod_poly_print(A), printf("\n\n");
            printf("tanh(A): "); nmod_poly_print(tanhA), printf("\n\n");
            printf("B: "); nmod_poly_print(B), printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(tanhA);
        nmod_poly_clear(B);
    }

    /* Check aliasing */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_t A, B;
        long n;
        mp_limb_t mod;
        mod = n_randtest_prime(state, 0);
        n = n_randtest(state) % 50;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(B, mod);
        nmod_poly_randtest(A, state, n_randint(state, 50));
        nmod_poly_set_coeff_ui(A, 0, 0UL);

        nmod_poly_tanh_series(B, A, n);
        nmod_poly_tanh_series(A, A, n);

        result = nmod_poly_equal(A, B);
        if (!result)
        {
            printf("FAIL:\n");
            nmod_poly_print(A), printf("\n\n");
            nmod_poly_print(B), printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(B);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
Beispiel #22
0
int
main(void)
{
    int i, result;
    FLINT_TEST_INIT(state);

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

    

    /* Generic case, most likely co-prime arguments ******************************/

    /* Compare with result from GCD and check correctness */
    for (i = 0; i < 1000; i++)
    {
        fmpz_t p;
        fmpz_mod_poly_t a, b, d, g, s, t, v, w;

        fmpz_init(p);
        fmpz_set_ui(p, n_randtest_prime(state, 0));

        fmpz_mod_poly_init(a, p);
        fmpz_mod_poly_init(b, p);
        fmpz_mod_poly_init(d, p);
        fmpz_mod_poly_init(g, p);
        fmpz_mod_poly_init(s, p);
        fmpz_mod_poly_init(t, p);
        fmpz_mod_poly_init(v, p);
        fmpz_mod_poly_init(w, p);
        fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
        fmpz_mod_poly_randtest(b, state, n_randint(state, 100));

        fmpz_mod_poly_gcd(d, a, b);
        fmpz_mod_poly_xgcd(g, s, t, a, b);

        fmpz_mod_poly_mul(v, s, a);
        fmpz_mod_poly_mul(w, t, b);
        fmpz_mod_poly_add(w, v, w);

        result = (fmpz_mod_poly_equal(d, g) && fmpz_mod_poly_equal(g, w));
        if (!result)
        {
            flint_printf("FAIL:\n");
            fmpz_mod_poly_print(a), flint_printf("\n\n");
            fmpz_mod_poly_print(b), flint_printf("\n\n");
            fmpz_mod_poly_print(d), flint_printf("\n\n");
            fmpz_mod_poly_print(g), flint_printf("\n\n");
            fmpz_mod_poly_print(s), flint_printf("\n\n");
            fmpz_mod_poly_print(t), flint_printf("\n\n");
            fmpz_mod_poly_print(v), flint_printf("\n\n");
            fmpz_mod_poly_print(w), flint_printf("\n\n");
            abort();
        }

        fmpz_mod_poly_clear(a);
        fmpz_mod_poly_clear(b);
        fmpz_mod_poly_clear(d);
        fmpz_mod_poly_clear(g);
        fmpz_mod_poly_clear(s);
        fmpz_mod_poly_clear(t);
        fmpz_mod_poly_clear(v);
        fmpz_mod_poly_clear(w);
        fmpz_clear(p);
    }

    /* Special case, arguments share a factor ********************************/

    /* Compare with result from GCD and check correctness */
    for (i = 0; i < 1000; i++)
    {
        fmpz_t p;
        fmpz_mod_poly_t a, b, d, f, g, s, t, v, w;

        fmpz_init(p);
        fmpz_set_ui(p, n_randtest_prime(state, 0));

        fmpz_mod_poly_init(a, p);
        fmpz_mod_poly_init(b, p);
        fmpz_mod_poly_init(d, p);
        fmpz_mod_poly_init(f, p);
        fmpz_mod_poly_init(g, p);
        fmpz_mod_poly_init(s, p);
        fmpz_mod_poly_init(t, p);
        fmpz_mod_poly_init(v, p);
        fmpz_mod_poly_init(w, p);
        fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
        fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
        fmpz_mod_poly_randtest(f, state, n_randint(state, 20));
        fmpz_mod_poly_mul(a, a, f);
        fmpz_mod_poly_mul(b, b, f);

        fmpz_mod_poly_gcd(d, a, b);
        fmpz_mod_poly_xgcd(g, s, t, a, b);

        fmpz_mod_poly_mul(v, s, a);
        fmpz_mod_poly_mul(w, t, b);
        fmpz_mod_poly_add(w, v, w);

        result = (fmpz_mod_poly_equal(d, g) && fmpz_mod_poly_equal(g, w));
        if (!result)
        {
            flint_printf("FAIL:\n");
            fmpz_mod_poly_print(a), flint_printf("\n\n");
            fmpz_mod_poly_print(b), flint_printf("\n\n");
            fmpz_mod_poly_print(d), flint_printf("\n\n");
            fmpz_mod_poly_print(f), flint_printf("\n\n");
            fmpz_mod_poly_print(g), flint_printf("\n\n");
            fmpz_mod_poly_print(s), flint_printf("\n\n");
            fmpz_mod_poly_print(t), flint_printf("\n\n");
            fmpz_mod_poly_print(v), flint_printf("\n\n");
            fmpz_mod_poly_print(w), flint_printf("\n\n");
            abort();
        }

        fmpz_mod_poly_clear(a);
        fmpz_mod_poly_clear(b);
        fmpz_mod_poly_clear(d);
        fmpz_mod_poly_clear(f);
        fmpz_mod_poly_clear(g);
        fmpz_mod_poly_clear(s);
        fmpz_mod_poly_clear(t);
        fmpz_mod_poly_clear(v);
        fmpz_mod_poly_clear(w);
        fmpz_clear(p);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Beispiel #23
0
int
main(void)
{
    flint_rand_t state;
    long i;

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

    flint_randinit(state);

    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B, C;
        long m, j, exp, deg;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 6);
        deg = 1 + n_randint(state, 6);
        exp = n_randint(state, 20);

        nmod_poly_mat_init(A, m, m, mod);
        nmod_poly_mat_init(B, m, m, mod);
        nmod_poly_mat_init(C, m, m, mod);

        nmod_poly_mat_randtest(A, state, deg);

        nmod_poly_mat_pow(B, A, exp);

        nmod_poly_mat_one(C);
        for (j = 0; j < exp; j++)
            nmod_poly_mat_mul(C, C, A);

        if (!nmod_poly_mat_equal(C, B))
        {
            printf("FAIL:\n");
            printf("exp = %ld\n", exp);
            printf("A:\n");
            nmod_poly_mat_print(A, "x");
            printf("B:\n");
            nmod_poly_mat_print(B, "x");
            printf("C:\n");
            nmod_poly_mat_print(C, "x");
            printf("\n");
            abort();
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
        nmod_poly_mat_clear(C);
    }

    /* Check aliasing B and A */
    for (i = 0; i < 1000; i++)
    {
        nmod_poly_mat_t A, B;
        long m, exp, deg;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        m = n_randint(state, 6);
        deg = 1 + n_randint(state, 6);
        exp = n_randint(state, 20);

        nmod_poly_mat_init(A, m, m, mod);
        nmod_poly_mat_init(B, m, m, mod);

        nmod_poly_mat_randtest(A, state, deg);

        nmod_poly_mat_pow(B, A, exp);
        nmod_poly_mat_pow(A, A, exp);

        if (!nmod_poly_mat_equal(A, B))
        {
            printf("FAIL (aliasing)\n");
            printf("exp = %ld\n", exp);
            printf("A:\n");
            nmod_poly_mat_print(A, "x");
            printf("B:\n");
            nmod_poly_mat_print(B, "x");
            printf("\n");
            abort();
        }

        nmod_poly_mat_clear(A);
        nmod_poly_mat_clear(B);
    }

    flint_randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return 0;
}
Beispiel #24
0
int
main(void)
{
    int iter;
    flint_rand_t state;
    flint_randinit(state);

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

    /* Default algorithm */
    for (iter = 0; iter < 100; iter++)
    {
        int result = 1;
        nmod_poly_t pol1, poly, quot, rem, product;
        nmod_poly_factor_t res;
        mp_limb_t modulus, lead = 1;
        long length, num, i, j;
        ulong exp[5], prod1;

        modulus = n_randtest_prime(state, 0);

        nmod_poly_init(pol1, modulus);
        nmod_poly_init(poly, modulus);
        nmod_poly_init(quot, modulus);
        nmod_poly_init(rem, modulus);

        nmod_poly_zero(pol1);
        nmod_poly_set_coeff_ui(pol1, 0, 1);

        length = n_randint(state, 7) + 2;
        do 
        {
            nmod_poly_randtest(poly, state, length); 
            if (poly->length)
                nmod_poly_make_monic(poly, poly);
        }
        while ((!nmod_poly_is_irreducible(poly)) || (poly->length < 2));

        exp[0] = n_randint(state, 30) + 1;
        prod1 = exp[0];
        for (i = 0; i < exp[0]; i++)
            nmod_poly_mul(pol1, pol1, poly);

        num = n_randint(state, 5) + 1;
        for (i = 1; i < num; i++)
        {
            do 
            {
                length = n_randint(state, 7) + 2;
                nmod_poly_randtest(poly, state, length); 
                if (poly->length)
                {
                    nmod_poly_make_monic(poly, poly);
                    nmod_poly_divrem(quot, rem, pol1, poly);
                }
            }
            while ((!nmod_poly_is_irreducible(poly)) ||
                (poly->length < 2) || (rem->length == 0));
            exp[i] = n_randint(state, 30) + 1;
            prod1 *= exp[i];

            for (j = 0; j < exp[i]; j++)
                nmod_poly_mul(pol1, pol1, poly);
        }

        nmod_poly_factor_init(res);

        switch (n_randint(state, 3))
        {
            case 0:
                lead = nmod_poly_factor(res, pol1);
                break;
            case 1:
                lead = nmod_poly_factor_with_berlekamp(res, pol1);
                break;
            case 2:
                if (modulus == 2)
                    lead = nmod_poly_factor(res, pol1);
                else
                    lead = nmod_poly_factor_with_cantor_zassenhaus(res, pol1);
                break;
        }

        result &= (res->num == num);
        if (!result)
        {
            printf("Error: number of factors incorrect, %ld, %ld\n",
                res->num, num);
            abort();
        }

        nmod_poly_init(product, pol1->mod.n);
        nmod_poly_set_coeff_ui(product, 0, 1);
        for (i = 0; i < res->num; i++)
            for (j = 0; j < res->exp[i]; j++)
                nmod_poly_mul(product, product, res->p + i);
        nmod_poly_scalar_mul_nmod(product, product, lead);
        result &= nmod_poly_equal(pol1, product);
        if (!result)
        {
            printf("Error: product of factors does not equal original polynomial\n");
            nmod_poly_print(pol1); printf("\n");
            nmod_poly_print(product); printf("\n");
            abort();
        }
        nmod_poly_clear(product);

        nmod_poly_clear(quot);
        nmod_poly_clear(rem);
        nmod_poly_clear(pol1);
        nmod_poly_clear(poly);
        nmod_poly_factor_clear(res);
    }

    /* Test deflation trick */
    for (iter = 0; iter < 100; iter++)
    {
        nmod_poly_t pol1, poly, quot, rem;
        nmod_poly_factor_t res, res2;
        mp_limb_t modulus;
        long length, num, i, j;
        long exp[5], prod1;
        ulong inflation;
        int found;

        do {
            modulus = n_randtest_prime(state, 0);
        } while (modulus == 2); /* To compare with CZ */

        nmod_poly_init(pol1, modulus);
        nmod_poly_init(poly, modulus);
        nmod_poly_init(quot, modulus);
        nmod_poly_init(rem, modulus);

        nmod_poly_zero(pol1);
        nmod_poly_set_coeff_ui(pol1, 0, 1);

        inflation = n_randint(state, 7) + 1;

        length = n_randint(state, 7) + 2;
        do 
        {
            nmod_poly_randtest(poly, state, length); 
            if (poly->length)
                nmod_poly_make_monic(poly, poly);
        }
        while ((!nmod_poly_is_irreducible(poly)) || (poly->length < 2));
        nmod_poly_inflate(poly, poly, inflation);

        exp[0] = n_randint(state, 6) + 1;
        prod1 = exp[0];
        for (i = 0; i < exp[0]; i++)
            nmod_poly_mul(pol1, pol1, poly);

        num = n_randint(state, 5) + 1;
        for (i = 1; i < num; i++)
        {
            do
            {
                length = n_randint(state, 6) + 2;
                nmod_poly_randtest(poly, state, length); 
                if (poly->length)
                {
                    nmod_poly_make_monic(poly, poly);
                    nmod_poly_divrem(quot, rem, pol1, poly);
                }
            }
            while ((!nmod_poly_is_irreducible(poly)) ||
                (poly->length < 2) || (rem->length == 0));
            exp[i] = n_randint(state, 6) + 1;
            prod1 *= exp[i];
            nmod_poly_inflate(poly, poly, inflation);

            for (j = 0; j < exp[i]; j++)
                nmod_poly_mul(pol1, pol1, poly);
        }

        nmod_poly_factor_init(res);
        nmod_poly_factor_init(res2);

        switch (n_randint(state, 3))
        {
            case 0:
                nmod_poly_factor(res, pol1);
                break;
            case 1:
                nmod_poly_factor_with_berlekamp(res, pol1);
                break;
            case 2:
                nmod_poly_factor_with_cantor_zassenhaus(res, pol1);
                break;
        }

        nmod_poly_factor_cantor_zassenhaus(res2, pol1);

        if (res->num != res2->num)
        {
            printf("FAIL: different number of factors found\n");
            abort();
        }

        for (i = 0; i < res->num; i++)
        {
            found = 0;
            for (j = 0; j < res2->num; j++)
            {
                if (nmod_poly_equal(res->p + i, res2->p + j) &&
                        res->exp[i] == res2->exp[j])
                {
                    found = 1;
                    break;
                }
            }

            if (!found)
            {
                printf("FAIL: factor not found\n");
                abort();
            }
        }

        nmod_poly_clear(quot);
        nmod_poly_clear(rem);
        nmod_poly_clear(pol1);
        nmod_poly_clear(poly);
        nmod_poly_factor_clear(res);
        nmod_poly_factor_clear(res2);
    }

    flint_randclear(state);
    printf("PASS\n");
    return 0;
}
Beispiel #25
0
int main()
{
    long i, j;
    int sign;

    fmpz_t input;
    fmpz_t result;
    fmpz_t r1;
    fmpz_t m1;
    fmpz_t mprod;
    ulong r2, m2;

    flint_rand_t state;

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

    fmpz_init(input);
    fmpz_init(result);
    fmpz_init(r1);
    fmpz_init(m1);
    fmpz_init(mprod);
    flint_randinit(state);

    for (i = 0; i < 10000; i++)
    {
        long nprimes;

        m2 = n_randtest_prime(state, 0);
        nprimes = 1 + n_randint(state, 4);

        fmpz_set_ui(m1, 1UL);
        for (j = 0; j < nprimes; )
        {
            ulong t = n_randtest_prime(state, 0);
            if (t != m2)
            {
                fmpz_mul_ui(m1, m1, t);
                j++;
            }
        }

        fmpz_mul_ui(mprod, m1, m2);

        sign = n_randint(state, 2);

        if (sign)
            fmpz_randtest_mod_signed(input, state, mprod);
        else
            fmpz_randtest_mod(input, state, mprod);

        fmpz_mod(r1, input, m1);
        r2 = fmpz_fdiv_ui(input, m2);

        if (sign)
            fmpz_CRT_ui(result, r1, m1, r2, m2);
        else
            fmpz_CRT_ui_unsigned(result, r1, m1, r2, m2);

        if (!fmpz_equal(result, input))
        {
            printf("FAIL:\n");
            printf("m1: "); fmpz_print(m1); printf("\n");
            printf("m2: %lu\n", m2);
            printf("m1*m2: "); fmpz_print(mprod); printf("\n");
            printf("input: "); fmpz_print(input); printf("\n");
            printf("r1: "); fmpz_print(r1); printf("\n");
            printf("r2: %lu\n", r2);
            printf("result: "); fmpz_print(result); printf("\n");
            printf("%ld Equalness: %d\n", i, fmpz_equal(result, input));
            printf("\n");
            abort();
        }
    }

    fmpz_clear(input);
    fmpz_clear(result);
    fmpz_clear(r1);
    fmpz_clear(m1);
    fmpz_clear(mprod);
    flint_randclear(state);

    _fmpz_cleanup();

    printf("PASS\n");
    return 0;
}
int
main(void)
{
    int i, result;

    fmpz_t p;
    slong N;
    padic_ctx_t ctx;
    slong m, n;

    FLINT_TEST_INIT(state);

    flint_printf("get/ set_entry_padic... ");
    fflush(stdout);    

    for (i = 0; i < 10000; i++)
    {
        padic_mat_t a;
        padic_t x, y;
        slong r, c;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) 
            + PADIC_TEST_PREC_MIN;
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        m = n_randint(state, 20) + 1;
        n = n_randint(state, 20) + 1;

        padic_mat_init2(a, m, n, N);
        padic_init2(x, N);
        padic_init2(y, N);

        padic_mat_randtest(a, state, ctx);
        padic_randtest_not_zero(x, state, ctx);

        r = n_randint(state, m);
        c = n_randint(state, n);

        padic_mat_set_entry_padic(a, r, c, x, ctx);
        padic_mat_get_entry_padic(y, a, r, c, ctx);

        result = (padic_equal(x, y) && padic_mat_is_reduced(a, ctx));
        if (!result)
        {
            flint_printf("FAIL:\n\n");
            flint_printf("a = "), padic_mat_print(a, ctx), flint_printf("\n");
            flint_printf("x = "), padic_print(x, ctx), flint_printf("\n");
            flint_printf("y = "), padic_print(y, ctx), flint_printf("\n");
            abort();
        }

        padic_mat_clear(a);
        padic_clear(x);
        padic_clear(y);

        fmpz_clear(p);
        padic_ctx_clear(ctx);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
Beispiel #27
0
int
main(void)
{
    int iter;
    FLINT_TEST_INIT(state);
    

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

    for (iter = 0; iter < 200 * flint_test_multiplier(); iter++)
    {
        nmod_poly_t poly, Q, R, t;
        mp_limb_t modulus;
        slong i, num_factors, exp, max_exp;
        int v, result;

        modulus = n_randtest_prime(state, 0);

        nmod_poly_init(poly, modulus);
        nmod_poly_init(t, modulus);
        nmod_poly_init(Q, modulus);
        nmod_poly_init(R, modulus);

        nmod_poly_set_coeff_ui(poly, 0, n_randint(state, modulus));
        num_factors = n_randint(state, 5);

        max_exp = 0;
        for (i = 0; i < num_factors; i++)
        {
            do {
                nmod_poly_randtest(t, state, n_randint(state, 10));
            } while (!nmod_poly_is_irreducible(t) ||
                    (nmod_poly_length(t) < 2));

            exp = n_randint(state, 4) + 1;
            if (n_randint(state, 2) == 0)
                exp = 1;

            nmod_poly_divrem(Q, R, poly, t);
            if (!nmod_poly_is_zero(R))
            {
                nmod_poly_pow(t, t, exp);
                nmod_poly_mul(poly, poly, t);
                max_exp = FLINT_MAX(exp, max_exp);
            }
        }

        v = nmod_poly_is_squarefree(poly);

        if (v == 1)
            result = (max_exp <= 1 && !nmod_poly_is_zero(poly));
        else
            result = (max_exp > 1 || nmod_poly_is_zero(poly));

        if (!result)
        {
            flint_printf("FAIL: %wu, %wd, %d\n", modulus, max_exp, v);
            nmod_poly_print(poly); flint_printf("\n");
            abort();
        }

        nmod_poly_clear(poly);
        nmod_poly_clear(t);
        nmod_poly_clear(Q);
        nmod_poly_clear(R);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Beispiel #28
0
int
main(void)
{
    int i, result = 1;
    FLINT_TEST_INIT(state);
    

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

    /* Check 2*atanh(A) = atanh(2*A/(1+A^2)) */
    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        nmod_poly_t A, B, atanhA, atanhB;
        slong n;
        mp_limb_t mod;

        mod = n_randtest_prime(state, 0);
        n = 1 + n_randtest(state) % 100;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(B, mod);
        nmod_poly_init(atanhA, mod);
        nmod_poly_init(atanhB, mod);

        nmod_poly_randtest(A, state, n_randint(state, 100));
        nmod_poly_set_coeff_ui(A, 0, UWORD(0));

        nmod_poly_mullow(B, A, A, n);
        nmod_poly_set_coeff_ui(B, 0, UWORD(1));
        nmod_poly_div_series(B, A, B, n);
        nmod_poly_add(B, B, B);

        nmod_poly_atanh_series(atanhA, A, n);
        nmod_poly_atanh_series(atanhB, B, n);
        nmod_poly_add(atanhA, atanhA, atanhA);

        result = nmod_poly_equal(atanhA, atanhB);

        if (!result)
        {
            flint_printf("FAIL:\n");
            flint_printf("n = %wd, mod = %wu\n", n, mod);
            flint_printf("A: "); nmod_poly_print(A), flint_printf("\n\n");
            flint_printf("B: "); nmod_poly_print(B), flint_printf("\n\n");
            flint_printf("2*atanh(A): "); nmod_poly_print(atanhA), flint_printf("\n\n");
            flint_printf("atanh(B): "); nmod_poly_print(atanhB), flint_printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(B);
        nmod_poly_clear(atanhA);
        nmod_poly_clear(atanhB);
    }

    /* Check aliasing */
    for (i = 0; i < 100 * flint_test_multiplier(); i++)
    {
        nmod_poly_t A, B;
        slong n;
        mp_limb_t mod;
        mod = n_randtest_prime(state, 0);
        n = n_randtest(state) % 50;
        n = FLINT_MIN(n, mod);

        nmod_poly_init(A, mod);
        nmod_poly_init(B, mod);
        nmod_poly_randtest(A, state, n_randint(state, 50));
        nmod_poly_set_coeff_ui(A, 0, UWORD(0));

        nmod_poly_atanh_series(B, A, n);
        nmod_poly_atanh_series(A, A, n);

        result = nmod_poly_equal(A, B);
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(A), flint_printf("\n\n");
            nmod_poly_print(B), flint_printf("\n\n");
            abort();
        }

        nmod_poly_clear(A);
        nmod_poly_clear(B);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Beispiel #29
0
int
main(void)
{
    long i;
    flint_rand_t state;
    flint_randinit(state);

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

    for (i = 0; i < 1000; i++)
    {
        nmod_mat_t A, X, B, Y;
        mp_limb_t m;
        long rows, cols;
        int unit;

        m = n_randtest_prime(state, 0);
        rows = n_randint(state, 100);
        cols = n_randint(state, 100);
        unit = n_randint(state, 2);

        nmod_mat_init(A, rows, rows, m);
        nmod_mat_init(B, rows, cols, m);
        nmod_mat_init(X, rows, cols, m);
        nmod_mat_init(Y, rows, cols, m);

        nmod_mat_randtriu(A, state, unit);
        nmod_mat_randtest(X, state);
        nmod_mat_mul(B, A, X);

        /* Check Y = A^(-1) * (A * X) = X */
        nmod_mat_solve_triu_recursive(Y, A, B, unit);
        if (!nmod_mat_equal(Y, X))
        {
            printf("FAIL!\n");
            printf("A:\n");
            nmod_mat_print_pretty(A);
            printf("X:\n");
            nmod_mat_print_pretty(X);
            printf("B:\n");
            nmod_mat_print_pretty(B);
            printf("Y:\n");
            nmod_mat_print_pretty(Y);
            abort();
        }

        /* Check aliasing */
        nmod_mat_solve_triu_recursive(B, A, B, unit);
        if (!nmod_mat_equal(B, X))
        {
            printf("FAIL!\n");
            printf("aliasing test failed");
            printf("A:\n");
            nmod_mat_print_pretty(A);
            printf("B:\n");
            nmod_mat_print_pretty(B);
            abort();
        }

        nmod_mat_clear(A);
        nmod_mat_clear(B);
        nmod_mat_clear(X);
        nmod_mat_clear(Y);
    }

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
Beispiel #30
0
int
main(void)
{
    int i, result;

    padic_ctx_t ctx;
    fmpz_t p;
    slong N;

    FLINT_TEST_INIT(state);

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

    /* Check aliasing of a and b */
    for (i = 0; i < 1000; i++)
    {
        padic_poly_t a, b, c;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) 
            + PADIC_TEST_PREC_MIN;
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_poly_init2(a, 0, N);
        padic_poly_init2(b, 0, N);
        padic_poly_init2(c, 0, N);

        padic_poly_randtest(b, state, n_randint(state, 50), ctx);
        padic_poly_randtest(c, state, n_randint(state, 50), ctx);

        padic_poly_mul(a, b, c, ctx);
        padic_poly_mul(b, b, c, ctx);

        result = (padic_poly_equal(a, b) && padic_poly_is_reduced(a, ctx));
        if (!result)
        {
            flint_printf("FAIL (aliasing a and b):\n");
            padic_poly_print(a, ctx), flint_printf("\n\n");
            padic_poly_print(b, ctx), flint_printf("\n\n");
            abort();
        }

        padic_poly_clear(a);
        padic_poly_clear(b);
        padic_poly_clear(c);

        padic_ctx_clear(ctx);
        fmpz_clear(p);
    }

    /* Check aliasing of a and c */
    for (i = 0; i < 1000; i++)
    {
        padic_poly_t a, b, c;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) 
            + PADIC_TEST_PREC_MIN;
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_poly_init2(a, 0, N);
        padic_poly_init2(b, 0, N);
        padic_poly_init2(c, 0, N);

        padic_poly_randtest(b, state, n_randint(state, 50), ctx);
        padic_poly_randtest(c, state, n_randint(state, 50), ctx);

        padic_poly_mul(a, b, c, ctx);
        padic_poly_mul(c, b, c, ctx);

        result = (padic_poly_equal(a, c) && padic_poly_is_reduced(a, ctx));
        if (!result)
        {
            flint_printf("FAIL (aliasing a and c):\n");
            padic_poly_print(a, ctx), flint_printf("\n\n");
            padic_poly_print(c, ctx), flint_printf("\n\n");
            abort();
        }

        padic_poly_clear(a);
        padic_poly_clear(b);
        padic_poly_clear(c);

        padic_ctx_clear(ctx);
        fmpz_clear(p);
    }

    /* Check (b * c) + (b * d) = b * (c + d) */
    for (i = 0; i < 1000; i++)
    {
        padic_poly_t a1, a2, b, c, d, t;
        slong v;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) 
            + PADIC_TEST_PREC_MIN;
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_poly_init2(b, 0, N);
        padic_poly_init2(c, 0, N);
        padic_poly_init2(d, 0, N);
        padic_poly_init2(t, 0, N);

        padic_poly_randtest(b, state, n_randint(state, 100), ctx);
        padic_poly_randtest(c, state, n_randint(state, 100), ctx);
        padic_poly_randtest(d, state, n_randint(state, 100), ctx);

        v = FLINT_MIN(b->val, c->val);
        v = FLINT_MIN(v, d->val);
        v = FLINT_MIN(v, 0);

        if (v >= 0 || -v < N)  /* Otherwise, no precision left */
        {
            slong N2 = (v >= 0) ? N : N + v;

            padic_poly_init2(a1, 0, N2);
            padic_poly_init2(a2, 0, N2);

            padic_poly_mul(a1, b, c, ctx);
            padic_poly_mul(t, b, d, ctx);
            padic_poly_add(a1, a1, t, ctx);     /* Lower precision */

            padic_poly_add(t, c, d, ctx);
            padic_poly_mul(a2, b, t, ctx);      /* Lower precision */

            result = (padic_poly_equal(a1, a2) && padic_poly_is_reduced(a1, ctx));
            if (!result)
            {
                flint_printf("FAIL (distributivity):\n");
                flint_printf("p = "), fmpz_print(ctx->p), flint_printf("\n\n");
                flint_printf("N = %wd\n\n", N);
                flint_printf("b = "), padic_poly_print(b, ctx), flint_printf("\n\n");
                flint_printf("c = "), padic_poly_print(c, ctx), flint_printf("\n\n");
                flint_printf("d = "), padic_poly_print(d, ctx), flint_printf("\n\n");
                flint_printf("a1 = "), padic_poly_print(a1, ctx), flint_printf("\n\n");
                flint_printf("a2 = "), padic_poly_print(a2, ctx), flint_printf("\n\n");
                abort();
            }

            padic_poly_clear(a1);
            padic_poly_clear(a2);
        }

        padic_poly_clear(b);
        padic_poly_clear(c);
        padic_poly_clear(d);

        padic_ctx_clear(ctx);
        fmpz_clear(p);
    }

    /* Compare with Q */
    for (i = 0; i < 10000; i++)
    {
        padic_poly_t a, b, c, d;
        fmpq_poly_t x, y, z;

        fmpz_init_set_ui(p, n_randtest_prime(state, 0));
        N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) 
            + PADIC_TEST_PREC_MIN;
        padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES);

        padic_poly_init2(a, 0, N);
        padic_poly_init2(b, 0, N);
        padic_poly_init2(c, 0, N);
        padic_poly_init2(d, 0, N);

        fmpq_poly_init(x);
        fmpq_poly_init(y);
        fmpq_poly_init(z);

        padic_poly_randtest(b, state, n_randint(state, 50), ctx);
        padic_poly_randtest(c, state, n_randint(state, 50), ctx);

        padic_poly_mul(a, b, c, ctx);

        padic_poly_get_fmpq_poly(y, b, ctx);
        padic_poly_get_fmpq_poly(z, c, ctx);

        fmpq_poly_mul(x, y, z);
        padic_poly_set_fmpq_poly(d, x, ctx);

        result = (padic_poly_equal(a, d) && padic_poly_is_reduced(a, ctx));
        if (!result)
        {
            flint_printf("FAIL (cmp with Q):\n");
            flint_printf("N = %wd, val(b) = %wd, val(c) = %wd\n", N, b->val, c->val);
            padic_poly_print(c, ctx), flint_printf("\n\n");
            padic_poly_print(d, ctx), flint_printf("\n\n");
            abort();
        }

        padic_poly_clear(a);
        padic_poly_clear(b);
        padic_poly_clear(c);
        padic_poly_clear(d);

        fmpq_poly_clear(x);
        fmpq_poly_clear(y);
        fmpq_poly_clear(z);

        padic_ctx_clear(ctx);
        fmpz_clear(p);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}