示例#1
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;
}
/*------------------------------------------------------------*/
void check(int opt){
  long i;
  flint_rand_t state;
  flint_randinit(state);
    mp_limb_t n = 65537;

  for (i = 1; i < 200; i+=20){

    mp_ptr powers_inverse_square_q = _nmod_vec_init(2*i-1);
    nmod_poly_t S;
    nmod_poly_init(S, n);

    mp_limb_t q = nmod_poly_find_root(2*i, S->mod);
 
    if (opt == 1){
      nmod_poly_eval_geom_prepare(powers_inverse_square_q, S, q, i);

      sage_output_init(S->mod);
      printf("q = k(%lu)\n", q);
      printf("i = %lu\n", i);
      sage_output_assign_poly(S, "S");
      sage_output_assign_vec(powers_inverse_square_q, 2*i-1, "powers_inverse_square_q");
      printf("powers_inverse_square_q == [1/q^(j^2) for j in range(2*i-1)]\n");
      printf("S == U([q^(j^2) for j in range(2*i-1)])\n");
    }
    else{
      double t, u;
      long j;
      t = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_eval_geom_prepare(powers_inverse_square_q, S, q, i);
      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(S);
    _nmod_vec_clear(powers_inverse_square_q);
  }

  flint_randclear(state);
}
示例#3
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;
}
示例#4
0
int
main(void)
{
    int i, result;
    flint_rand_t state;
    flint_randinit(state);

    printf("bit_pack/bit_unpack....");
    fflush(stdout);

    /* Check aliasing of a and c */
    for (i = 0; i < 10000; i++)
    {
        nmod_poly_t a, b;
        mp_limb_t n;
        ulong bits;
        mp_ptr mpn;

        do
        {
            n = n_randtest_not_zero(state);
        } while (n == 1);
        bits = 2 * FLINT_BIT_COUNT(n) + n_randint(state, FLINT_BITS);

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        do
        {
            nmod_poly_randtest(a, state, n_randint(state, 100));
        } while (a->length == 0);

        mpn =
            malloc(sizeof(mp_limb_t) *
                   ((bits * a->length - 1) / FLINT_BITS + 1));

        _nmod_poly_bit_pack(mpn, a->coeffs, a->length, bits);
        nmod_poly_fit_length(b, a->length);
        _nmod_poly_bit_unpack(b->coeffs, a->length, mpn, bits, a->mod);
        b->length = a->length;

        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;
}
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;
}
/*------------------------------------------------------------*/
void check(int opt){
  long i;
  flint_rand_t state;
  flint_randinit(state);
  mp_limb_t n = 65537;
  nmod_t mod;
  nmod_init(&mod, n);

  for (i = 1; i < 200; i+=1){

    mp_ptr val = _nmod_vec_init(i);
    mp_limb_t q = nmod_poly_find_root(i, mod);

    if (opt == 1){
      nmod_poly_eval_geom_derivative_fanin(val, q, i, mod);

      sage_output_init(mod);
      printf("q = k(%lu)\n", q);
      printf("i = %lu\n", i);
      sage_output_assign_vec(val, i, "val");
      printf("P = mul([x-q^j for j in range(i)])\n");
      printf("val == [P.derivative()(q^j) for j in range(i)]\n");
    }
    else{
      double t, u;
      long j;
      t = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_eval_geom_derivative_fanin(val, q, i, mod);
      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_vec_clear(val);
  }

  flint_randclear(state);
}
示例#8
0
文件: det.c 项目: clear731/lattice
void
nmod_poly_mat_det(nmod_poly_t det, const nmod_poly_mat_t A)
{
    slong n = A->r;

    if (n == 0)
    {
        nmod_poly_one(det);
    }
    else if (n == 1)
    {
        nmod_poly_set(det, nmod_poly_mat_entry(A, 0, 0));
    }
    else if (n == 2)
    {
        nmod_poly_t tmp;
        nmod_poly_init(tmp, nmod_poly_mat_modulus(A));
        nmod_poly_mul(det, nmod_poly_mat_entry(A, 0, 0),
                           nmod_poly_mat_entry(A, 1, 1));
        nmod_poly_mul(tmp, nmod_poly_mat_entry(A, 0, 1),
                           nmod_poly_mat_entry(A, 1, 0));
        nmod_poly_sub(det, det, tmp);
        nmod_poly_clear(tmp);
    }
    else if (n < 15)  /* should be entry sensitive too */
    {
        nmod_poly_mat_det_fflu(det, A);
    }
    else
    {
        nmod_poly_mat_det_interpolate(det, A);
    }
}
示例#9
0
int
main(void)
{
    int i, result;
    FLINT_TEST_INIT(state);
    

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

    /* Compare with truncated product of a and b */
    for (i = 0; i < 200 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, c;
        slong trunc;
        mp_limb_t n = n_randtest_not_zero(state);

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(c, n);
        trunc = n_randint(state, 50);
        nmod_poly_randtest(b, state, trunc);
        nmod_poly_randtest(c, state, trunc);

        nmod_poly_mullow(a, b, c, trunc);
        nmod_poly_mul(b, b, c);
        nmod_poly_truncate(b, trunc);

        result = (nmod_poly_equal(a, b));
        if (!result)
        {
            flint_printf(":\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);
        nmod_poly_clear(c);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
示例#10
0
文件: t-neg.c 项目: goens/flint2
int
main(void)
{
    int i, result;
    flint_rand_t state;
    flint_randinit(state);

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

    /* Check neg neg a == a */
    for (i = 0; i < 10000; i++)
    {
        nmod_poly_t a, b;
        mp_limb_t n = n_randtest_not_zero(state);
        
        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_randtest(a, state, n_randint(state, 100));
        
        nmod_poly_neg(b, a);
        nmod_poly_neg(b, b);

        result = (nmod_poly_equal(a, b));
        if (!result)
        {
            printf("FAIL:\n");
            printf("a->length = %ld, n = %lu\n", a->length, a->mod.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;
}
示例#11
0
int main()
{
    flint_printf("hilbert_class_poly....");
    fflush(stdout);

    {
        slong i;
        mp_limb_t c;
        fmpz_poly_t hd;
        nmod_poly_t hdp;

        fmpz_poly_init(hd);
        nmod_poly_init(hdp, 31337);

        for (i = 0; i < 1000; i++)
        {
            acb_modular_hilbert_class_poly(hd, -i);
            fmpz_poly_get_nmod_poly(hdp, hd);
            c = nmod_poly_evaluate_nmod(hdp, 2015);

            if (c != hilbert_poly_values[i])
            {
                flint_printf("FAIL!\n");
                flint_printf("D = %wd\n", -i);
                flint_printf("c = %wu\n", c);
                flint_printf("expected = %d\n", (int) hilbert_poly_values[i]);
                abort();
            }
        }

        for (i = 0; hilbert_poly_values_2[2 * i] != 0; i++)
        {
            acb_modular_hilbert_class_poly(hd, hilbert_poly_values_2[2 * i]);
            fmpz_poly_get_nmod_poly(hdp, hd);
            c = nmod_poly_evaluate_nmod(hdp, 2015);

            if (c != hilbert_poly_values_2[2 * i + 1])
            {
                flint_printf("FAIL!\n");
                flint_printf("D = %wd\n", -i);
                flint_printf("c = %wu\n", c);
                flint_printf("expected = %d\n", (int) hilbert_poly_values_2[2*i+1]);
                abort();
            }
        }

        fmpz_poly_clear(hd);
        nmod_poly_clear(hdp);
    }

    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
void embeddings_isomorphism(nmod_poly_t G, mp_srcptr F, 
			    const embeddings_t FP, const embeddings_t FQ, const embeddings_t FR){

  long m = nmod_poly_degree(FP->P);
  long n = nmod_poly_degree(FQ->P);

  long i;
  nmod_poly_t tmpF, tmpG, S, X;

  nmod_t mod = FP->P->mod;
  nmod_poly_init(tmpF, mod.n);
  nmod_poly_init(tmpG, mod.n);
  nmod_poly_init(S, mod.n);
  nmod_poly_init(X, mod.n);

  nmod_poly_zero(G);
  nmod_poly_zero(X);
  nmod_poly_set_coeff_ui(X, 1, 1);
  embeddings_embed(S, X, FP, FQ, FR);
  
  for (i = m-1; i >= 0; i--){
    nmod_poly_fit_length(tmpF, n);
    long j;
    long offset = i*n;
    for (j = 0; j < n; j++)
      tmpF->coeffs[j] = F[offset+j];
    tmpF->length = n;
    _nmod_poly_normalise(tmpF);
    
    embeddings_embed(tmpG, tmpF, FQ, FP, FR);
    nmod_poly_mulmod(G, G, S, FR->P);
    nmod_poly_add(G, G, tmpG);
  }

  nmod_poly_clear(tmpF);
  nmod_poly_clear(tmpG);
  nmod_poly_clear(X);
  nmod_poly_clear(S);
}
示例#13
0
文件: ntru_poly.c 项目: hasufell/pqc
void
fmpz_poly_mod(fmpz_poly_t a,
		const uint32_t mod)
{
	nmod_poly_t nmod_tmp;

	nmod_poly_init(nmod_tmp, mod);

	fmpz_poly_get_nmod_poly(nmod_tmp, a);
	fmpz_poly_set_nmod_poly(a, nmod_tmp);

	nmod_poly_clear(nmod_tmp);
}
示例#14
0
/*------------------------------------------------------------*/
void check(int opt){
  long i;
  flint_rand_t state;
  flint_randinit(state);
  mp_limb_t n = 65537;

  for (i = 1; i < 200; i+=20){
    nmod_poly_t A, B, C;
    nmod_poly_init(A, n);
    nmod_poly_init(B, n);
    nmod_poly_init(C, n);

    nmod_poly_rand_dense_monic(A, state, i); 
    nmod_poly_rand_dense(B, state, i); 

    if (opt == 1){
      nmod_poly_invmod(C, B, A);
      sage_output_init(A->mod);
      sage_output_assign_poly(A, "A");
      sage_output_assign_poly(B, "B");
      sage_output_assign_poly(C, "C");
      printf("C*B %% A == 1\n");
    }
    else{
      double t, u;
      long j;
      t = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_invmod(C, B, A);
      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(A);
    nmod_poly_clear(B);
    nmod_poly_clear(C);
  }

  flint_randclear(state);
}
int main() {

	slong i;
	mp_limb_t p, w;
	nmod_poly_t f;
	mp_ptr res, res2, vect;

	// p premier
	// Le cardinal du groupe multiplicatif doit être une puissance de 2
	// Ne marche que pour p = 0, 1, 2, 3, 5, 17, 257, 65537.
	p = 17;

	nmod_poly_init(f, p);
	nmod_poly_set_coeff_ui(f, 0, 0);
	nmod_poly_set_coeff_ui(f, 1, 1);
	nmod_poly_set_coeff_ui(f, 2, 2);
	nmod_poly_set_coeff_ui(f, 3, 1);
	nmod_poly_set_coeff_ui(f, 4, 1);

	w = n_primitive_root_prime(p);

	res = _nmod_vec_init(p);
	nmod_poly_fft_pow2(res, f, w);

	flint_printf("w : %d\n", w);
	_fmpz_vec_print(res, p);
	flint_printf("\n");


	vect = _nmod_vec_init(p);
	for(i = 0 ; i < p ; i++) {
		vect[i] = i;
	}
	res2 = _nmod_vec_init(p);
	nmod_poly_evaluate_nmod_vec(res2, f, vect, p);

	_fmpz_vec_print(res2, p);
	flint_printf("\nBooléen d'égalité : %d\n", _nmod_vec_equal(res,res2,p));

	nmod_poly_clear(f);
	_nmod_vec_clear(res);
	_nmod_vec_clear(res2);
	_nmod_vec_clear(vect);

	return 0;
}
示例#16
0
int
main(void)
{
    int i, result;
    ulong j;
    FLINT_TEST_INIT(state);
    

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

    /* Check aliasing of a and b */
    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a;
        mp_limb_t n = n_randtest_not_zero(state);
        mp_limb_t c1 = n_randtest(state), c2;
        
        j = n_randint(state, 100);

        nmod_poly_init(a, n);
        nmod_poly_randtest(a, state, n_randint(state, 100));

        nmod_poly_set_coeff_ui(a, j, c1);
        c2 = nmod_poly_get_coeff_ui(a, j);
        
        result = (c2 == c1 % n);
        if (!result)
        {
            flint_printf("FAIL:\n");
            flint_printf("j = %wu, c1 = %wu, c2 = %wu, n = %wu\n", j, c1, c2, a->mod.n);
            nmod_poly_print(a), flint_printf("\n\n");
            abort();
        }

        nmod_poly_clear(a);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
示例#17
0
int main (void)
{

    double t;
    nmod_poly_t f, g, h;
    for (int i= 15001;i < 16000; i++)
    {
      nmod_poly_init2 (f, 17, i/2+1);
      nmod_poly_init2 (g, 17, i+1);

      nmod_poly_set_coeff_ui (f, i/2, 1);
      nmod_poly_set_coeff_ui (f, 1, 1);
      nmod_poly_set_coeff_ui (f, 0, ((i%17)*(i%17)+3) % 17);

      nmod_poly_set_coeff_ui (g, i, 1);
      nmod_poly_set_coeff_ui (g, i/2+1, 1);
      nmod_poly_set_coeff_ui (g, 1, ((i % 17)+1)%17);
      nmod_poly_set_coeff_ui (g, 0, 15);

      nmod_poly_init (h, 17);
      nmod_poly_gcd (h, f, g);

      if (!nmod_poly_is_one (h))
      {
        flint_printf ("i= %d\n", i);
        nmod_poly_factor_t factors;
        nmod_poly_factor_init (factors);
        t= clock();
        nmod_poly_factor (factors, h);
                    t = (clock() - t) / CLOCKS_PER_SEC;
                flint_printf("factorization %.2lf\n", t);
        nmod_poly_factor_clear (factors);
      }

      nmod_poly_clear (f);
      nmod_poly_clear (g);
      nmod_poly_clear (h);
    }
    return EXIT_SUCCESS;
}
示例#18
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;
}
示例#19
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);

  }
}
示例#20
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;
}
示例#21
0
int
main(void)
{
    int i, result;
    FLINT_TEST_INIT(state);
    

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

    /* Check aliasing of a and b */
    for (i = 0; i < 200 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, c;
        mp_limb_t n = n_randtest_not_zero(state);

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(c, n);
        nmod_poly_randtest(b, state, n_randint(state, 50));
        nmod_poly_randtest(c, state, n_randint(state, 50));

        nmod_poly_mul_KS(a, b, c, 0);
        nmod_poly_mul_KS(b, b, c, 0);

        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);
        nmod_poly_clear(c);
    }

    /* Check aliasing of a and c */
    for (i = 0; i < 200 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, c;
        mp_limb_t n = n_randtest_not_zero(state);

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(c, n);
        nmod_poly_randtest(b, state, n_randint(state, 50));
        nmod_poly_randtest(c, state, n_randint(state, 50));

        nmod_poly_mul_KS(a, b, c, 0);
        nmod_poly_mul_KS(c, b, c, 0);

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

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

    /* Compare with mul_classical */
    for (i = 0; i < 200 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a1, a2, b, c;
        mp_limb_t n = n_randtest_not_zero(state);

        nmod_poly_init(a1, n);
        nmod_poly_init(a2, n);
        nmod_poly_init(b, n);
        nmod_poly_init(c, n);
        nmod_poly_randtest(b, state, n_randint(state, 50));
        nmod_poly_randtest(c, state, n_randint(state, 50));

        nmod_poly_mul_classical(a1, b, c);
        nmod_poly_mul_KS(a2, b, c, 0);

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

        nmod_poly_clear(a1);
        nmod_poly_clear(a2);
        nmod_poly_clear(b);
        nmod_poly_clear(c);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
示例#22
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;
}
示例#23
0
int
main(void)
{
    int i, result = 1;
    flint_rand_t state;
    flint_randinit(state);
    
    printf("compose_divconquer....");
    fflush(stdout);

    /* Compare aliasing */
    for (i = 0; i < 5000; i++)
    {
        nmod_poly_t a, b, r1;
        mp_limb_t n = n_randtest_not_zero(state);
        
        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(r1, n);
        nmod_poly_randtest(a, state, n_randint(state, 30));
        nmod_poly_randtest(b, state, n_randint(state, 15));
        
        nmod_poly_compose_divconquer(r1, a, b);
        nmod_poly_compose_divconquer(a, a, b);
        
        result = nmod_poly_equal(r1, a);
        if (!result)
        {
            printf("FAIL:\n");
            printf("a->length = %ld, n = %lu\n", a->length, a->mod.n);
            nmod_poly_print(r1), printf("\n\n");
            nmod_poly_print(a), printf("\n\n");
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(r1);
    }
    
    /* Compare other aliasing */
    for (i = 0; i < 5000; i++)
    {
        nmod_poly_t a, b, r1;
        mp_limb_t n = n_randtest_not_zero(state);
        
        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(r1, n);
        nmod_poly_randtest(a, state, n_randint(state, 30));
        nmod_poly_randtest(b, state, n_randint(state, 15));
        
        nmod_poly_compose_divconquer(r1, a, b);
        nmod_poly_compose_divconquer(b, a, b);
        
        result = nmod_poly_equal(r1, b);
        if (!result)
        {
            printf("FAIL:\n");
            printf("a->length = %ld, n = %lu\n", a->length, a->mod.n);
            nmod_poly_print(r1), printf("\n\n");
            nmod_poly_print(b), printf("\n\n");
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(r1);
    }
    
    /* Compare with compose_horner */
    for (i = 0; i < 5000; i++)
    {
        nmod_poly_t a, b, r1, r2;
        mp_limb_t n = n_randtest_not_zero(state);
        
        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(r1, n);
        nmod_poly_init(r2, n);
        nmod_poly_randtest(a, state, n_randint(state, 30));
        nmod_poly_randtest(b, state, n_randint(state, 15));
        
        nmod_poly_compose_divconquer(r1, a, b);
        nmod_poly_compose_horner(r2, a, b);
        
        result = nmod_poly_equal(r1, r2);
        if (!result)
        {
            printf("FAIL:\n");
            printf("a->length = %ld, n = %lu\n", a->length, a->mod.n);
            nmod_poly_print(r1), printf("\n\n");
            nmod_poly_print(r2), printf("\n\n");
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(r1);
        nmod_poly_clear(r2);
    }
    
    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
示例#24
0
文件: nullspace.c 项目: goens/flint2
long
nmod_poly_mat_nullspace(nmod_poly_mat_t res, const nmod_poly_mat_t mat)
{
    long i, j, k, m, n, rank, nullity;
    long * pivots;
    long * nonpivots;
    nmod_poly_mat_t tmp;
    nmod_poly_t den;

    m = mat->r;
    n = mat->c;

    nmod_poly_init(den, nmod_poly_mat_modulus(mat));
    nmod_poly_mat_init_set(tmp, mat);
    rank = nmod_poly_mat_rref(tmp, den, NULL, tmp);
    nullity = n - rank;

    nmod_poly_mat_zero(res);

    if (rank == 0)
    {
        for (i = 0; i < nullity; i++)
            nmod_poly_one(res->rows[i] + i);
    }
    else if (nullity)
    {
        pivots = flint_malloc(rank * sizeof(long));
        nonpivots = flint_malloc(nullity * sizeof(long));

        for (i = j = k = 0; i < rank; i++)
        {
            while (nmod_poly_is_zero(tmp->rows[i] + j))
            {
                nonpivots[k] = j;
                k++;
                j++;
            }
            pivots[i] = j;
            j++;
        }
        while (k < nullity)
        {
            nonpivots[k] = j;
            k++;
            j++;
        }

        nmod_poly_set(den, tmp->rows[0] + pivots[0]);

        for (i = 0; i < nullity; i++)
        {
            for (j = 0; j < rank; j++)
                nmod_poly_set(res->rows[pivots[j]] + i,
                              tmp->rows[j] + nonpivots[i]);
            nmod_poly_neg(res->rows[nonpivots[i]] + i, den);
        }

        flint_free(pivots);
        flint_free(nonpivots);
    }

    nmod_poly_clear(den);
    nmod_poly_mat_clear(tmp);
    return nullity;
}
示例#25
0
/*------------------------------------------------------------*/
void check(int opt){
  long i;
  flint_rand_t state;
  flint_randinit(state);

  mp_limb_t n = 65537;

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

    nmod_poly_t P;
    nmod_poly_init(P, n);
    nmod_poly_rand_dense_monic(P, state, i);
    
    nmod_poly_t S0;
    nmod_poly_init(S0, n);

    if (opt == 1){
      nmod_poly_inverse_reverse(S0, P, i+10);

      sage_output_init(P->mod);
      sage_output_assign_poly(P, "P");
      sage_output_assign_poly(S0, "S0");
    
      nmod_poly_inverse_reverse_main(P, P);
      sage_output_assign_poly(P, "S1");
      printf("print (S0*P.reverse()) %% x^%lu == 1\n", i+10);
      if (i > 1)
	printf("print (S1*P.reverse()) %% x^%lu == 1\n", i-1);
    }

    else{
      double t, u;
      long j;
      t = util_gettime();
      for (j = 0; j < 10000; j++)
	nmod_poly_inverse_reverse_main(S0, 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(S0);
  }

  flint_randclear(state);
}
示例#26
0
void
nmod_poly_factor_squarefree(nmod_poly_factor_t res, const nmod_poly_t f)
{
    nmod_poly_t f_d, g, g_1;
    mp_limb_t p;
    slong deg, i;

    if (f->length <= 1) 
    {
        res->num = 0;
        return;
    }

    if (f->length == 2)
    {
        nmod_poly_factor_insert(res, f, 1);
        return;
    }

    p = nmod_poly_modulus(f);
    deg = nmod_poly_degree(f);

    
    /* Step 1, look at f', if it is zero then we are done since f = h(x)^p
       for some particular h(x), clearly f(x) = sum a_k x^kp, k <= deg(f) */

    nmod_poly_init(g_1, p);
    nmod_poly_init(f_d, p);
    nmod_poly_init(g, p);
    nmod_poly_derivative(f_d, f);

    /* Case 1 */
    if (nmod_poly_is_zero(f_d))
    {
        nmod_poly_factor_t new_res;
        nmod_poly_t h;

        nmod_poly_init(h, p);

        for (i = 0; i <= deg / p; i++) /* this will be an integer since f'=0 */
        {
            nmod_poly_set_coeff_ui(h, i, nmod_poly_get_coeff_ui(f, i * p));
        }
        
        /* Now run square-free on h, and return it to the pth power */
        nmod_poly_factor_init(new_res);

        nmod_poly_factor_squarefree(new_res, h);
        nmod_poly_factor_pow(new_res, p);

        nmod_poly_factor_concat(res, new_res);
        nmod_poly_clear(h);
        nmod_poly_factor_clear(new_res);
   }
   else 
   { 
        nmod_poly_t h, z;

        nmod_poly_gcd(g, f, f_d);
        nmod_poly_div(g_1, f, g);

        i = 1;

        nmod_poly_init(h, p);
        nmod_poly_init(z, p);

        /* Case 2 */
        while (!nmod_poly_is_one(g_1)) 
        {
            nmod_poly_gcd(h, g_1, g);
            nmod_poly_div(z, g_1, h);

            /* out <- out.z */
            if (z->length > 1)
            {
                nmod_poly_factor_insert(res, z, 1);
                nmod_poly_make_monic(res->p + (res->num - 1),
                                     res->p + (res->num - 1));
                if (res->num)
                    res->exp[res->num - 1] *= i;
            }

            i++;
            nmod_poly_set(g_1, h);
            nmod_poly_div(g, g, h);
        }

        nmod_poly_clear(h);
        nmod_poly_clear(z);
        
        nmod_poly_make_monic(g, g);

        if (!nmod_poly_is_one(g))
        {
            /* so now we multiply res with square-free(g^1/p) ^ p  */
            nmod_poly_t g_p; /* g^(1/p) */
            nmod_poly_factor_t new_res_2;

            nmod_poly_init(g_p, p);

            for (i = 0; i <= nmod_poly_degree(g) / p; i++)
                nmod_poly_set_coeff_ui(g_p, i, nmod_poly_get_coeff_ui(g, i*p));

            nmod_poly_factor_init(new_res_2);

            /* square-free(g^(1/p)) */
            nmod_poly_factor_squarefree(new_res_2, g_p);
            nmod_poly_factor_pow(new_res_2, p);

            nmod_poly_factor_concat(res, new_res_2);
            nmod_poly_clear(g_p);
            nmod_poly_factor_clear(new_res_2);
        }
   }

    nmod_poly_clear(g_1);
    nmod_poly_clear(f_d);
    nmod_poly_clear(g);
}
示例#27
0
int
main(void)
{
    int i, result;
    flint_rand_t state;
    flint_randinit(state);

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

    /* Check a << shift >> shift == a */
    for (i = 0; i < 10000; i++)
    {
        nmod_poly_t a, b;
        mp_limb_t n = n_randtest_not_zero(state);
        long shift = n_randint(state, 100);
  
        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_randtest(a, state, n_randint(state, 100));
        
        nmod_poly_shift_left(b, a, shift);
        nmod_poly_shift_right(b, b, shift);
        
        result = (nmod_poly_equal(a, b));
        if (!result)
        {
            printf("FAIL:\n");
            printf("shift = %ld, a->length = %ld, n = %lu\n", 
                shift, a->length, a->mod.n);
            nmod_poly_print(a), printf("\n\n");
            nmod_poly_print(b), printf("\n\n");
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
    }
    
    /* Check a << shift >> shift == a aliasing the other way */
    for (i = 0; i < 10000; i++)
    {
        nmod_poly_t a, b, c;
        mp_limb_t n = n_randtest_not_zero(state);
        long shift = n_randint(state, 100);

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(c, n);
        nmod_poly_randtest(c, state, n_randint(state, 100));
        
        nmod_poly_set(a, c);
        nmod_poly_shift_left(c, c, shift);
        nmod_poly_shift_right(b, c, shift);

        result = (nmod_poly_equal(a, b));
        if (!result)
        {
            printf("FAIL:\n");
            printf("shift = %ld, c->length = %ld, n = %lu\n", 
                shift, c->length, a->mod.n);
            nmod_poly_print(a), printf("\n\n");
            nmod_poly_print(b), printf("\n\n");
            abort();
        }

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

    flint_randclear(state);

    printf("PASS\n");
    return 0;
}
示例#28
0
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;
}
示例#29
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;
}
示例#30
0
int
main(void)
{
    int i, result;
    FLINT_TEST_INIT(state);
    

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

    /* 
       Compare with result from gcd and check a*s + b*t = g
    */
    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, c, g1, s, t, g2;

        mp_limb_t n;
        do n = n_randtest_not_zero(state);
        while (!n_is_probabprime(n));

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(c, n);
        nmod_poly_init(g1, n);
        nmod_poly_init(g2, n);
        nmod_poly_init(s, n);
        nmod_poly_init(t, n);
        
        nmod_poly_randtest(a, state, n_randtest(state) % 200);
        nmod_poly_randtest(b, state, n_randtest(state) % 200);
        nmod_poly_randtest(c, state, n_randtest(state) % 200);
        
        nmod_poly_mul(a, a, c);
        nmod_poly_mul(b, b, c);

        nmod_poly_gcd_euclidean(g1, a, b);
        nmod_poly_xgcd_euclidean(g2, s, t, a, b);
        
        nmod_poly_mul(s, s, a);
        nmod_poly_mul(t, t, b);
        nmod_poly_add(s, s, t);

        result = (nmod_poly_equal(g1, g2) && nmod_poly_equal(s, g1) 
            && (g1->length == 0 || g1->coeffs[g1->length - 1] == 1));
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(a), flint_printf("\n\n");
            nmod_poly_print(b), flint_printf("\n\n");
            nmod_poly_print(c), flint_printf("\n\n");
            nmod_poly_print(g1), flint_printf("\n\n");
            nmod_poly_print(g2), flint_printf("\n\n");
            nmod_poly_print(s), flint_printf("\n\n");
            nmod_poly_print(t), flint_printf("\n\n");
            flint_printf("n = %wd\n", n);
            abort();
        }
        
        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(c);
        nmod_poly_clear(g1);
        nmod_poly_clear(g2);
        nmod_poly_clear(s);
        nmod_poly_clear(t);
    }

    /* Check aliasing of a and g */
    for (i = 0; i < 20 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, g, s, t;

        mp_limb_t n;
        do n = n_randtest(state);
        while (!n_is_probabprime(n));

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(g, n);
        nmod_poly_init(s, n);
        nmod_poly_init(t, n);
        nmod_poly_randtest(a, state, n_randtest(state) % 200);
        nmod_poly_randtest(b, state, n_randtest(state) % 200);
        
        nmod_poly_xgcd_euclidean(g, s, t, a, b);
        nmod_poly_xgcd_euclidean(a, s, t, a, b);

        result = (nmod_poly_equal(a, g));
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(a), flint_printf("\n\n");
            nmod_poly_print(b), flint_printf("\n\n");
            nmod_poly_print(g), flint_printf("\n\n");
            flint_printf("n = %wd\n", n);
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(g);
        nmod_poly_clear(s);
        nmod_poly_clear(t);
    }

    /* Check aliasing of b and g */
    for (i = 0; i < 20 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, g, s, t;

        mp_limb_t n;
        do n = n_randtest(state);
        while (!n_is_probabprime(n));

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(g, n);
        nmod_poly_init(s, n);
        nmod_poly_init(t, n);
        nmod_poly_randtest(a, state, n_randtest(state) % 200);
        nmod_poly_randtest(b, state, n_randtest(state) % 200);
       
        nmod_poly_xgcd_euclidean(g, s, t, a, b);
        nmod_poly_xgcd_euclidean(b, s, t, a, b);

        result = (nmod_poly_equal(b, g));
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(a), flint_printf("\n\n");
            nmod_poly_print(b), flint_printf("\n\n");
            nmod_poly_print(g), flint_printf("\n\n");
            flint_printf("n = %wd\n", n);
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(g);
        nmod_poly_clear(s);
        nmod_poly_clear(t);
    }

    /* Check aliasing of s and a */
    for (i = 0; i < 20 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, g, s, t;

        mp_limb_t n;
        do n = n_randtest(state);
        while (!n_is_probabprime(n));

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(g, n);
        nmod_poly_init(s, n);
        nmod_poly_init(t, n);
        nmod_poly_randtest(a, state, n_randtest(state) % 200);
        nmod_poly_randtest(b, state, n_randtest(state) % 200);
       
        nmod_poly_xgcd_euclidean(g, s, t, a, b);
        nmod_poly_xgcd_euclidean(g, a, t, a, b);

        result = (nmod_poly_equal(s, a));
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(a), flint_printf("\n\n");
            nmod_poly_print(s), flint_printf("\n\n");
            flint_printf("n = %wd\n", n);
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(g);
        nmod_poly_clear(s);
        nmod_poly_clear(t);
    }

    /* Check aliasing of s and b */
    for (i = 0; i < 20 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, g, s, t;

        mp_limb_t n;
        do n = n_randtest(state);
        while (!n_is_probabprime(n));

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(g, n);
        nmod_poly_init(s, n);
        nmod_poly_init(t, n);
        nmod_poly_randtest(a, state, n_randtest(state) % 200);
        nmod_poly_randtest(b, state, n_randtest(state) % 200);
       
        nmod_poly_xgcd_euclidean(g, s, t, a, b);
        nmod_poly_xgcd_euclidean(g, b, t, a, b);

        result = (nmod_poly_equal(s, b));
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(b), flint_printf("\n\n");
            nmod_poly_print(s), flint_printf("\n\n");
            flint_printf("n = %wd\n", n);
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(g);
        nmod_poly_clear(s);
        nmod_poly_clear(t);
    }

    /* Check aliasing of t and a */
    for (i = 0; i < 20 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, g, s, t;

        mp_limb_t n;
        do n = n_randtest(state);
        while (!n_is_probabprime(n));

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(g, n);
        nmod_poly_init(s, n);
        nmod_poly_init(t, n);
        nmod_poly_randtest(a, state, n_randtest(state) % 200);
        nmod_poly_randtest(b, state, n_randtest(state) % 200);
       
        nmod_poly_xgcd_euclidean(g, s, t, a, b);
        nmod_poly_xgcd_euclidean(g, s, a, a, b);

        result = (nmod_poly_equal(t, a));
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(a), flint_printf("\n\n");
            nmod_poly_print(t), flint_printf("\n\n");
            flint_printf("n = %wd\n", n);
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(g);
        nmod_poly_clear(s);
        nmod_poly_clear(t);
    }

    /* Check aliasing of t and b */
    for (i = 0; i < 20 * flint_test_multiplier(); i++)
    {
        nmod_poly_t a, b, g, s, t;

        mp_limb_t n;
        do n = n_randtest(state);
        while (!n_is_probabprime(n));

        nmod_poly_init(a, n);
        nmod_poly_init(b, n);
        nmod_poly_init(g, n);
        nmod_poly_init(s, n);
        nmod_poly_init(t, n);
        nmod_poly_randtest(a, state, n_randtest(state) % 200);
        nmod_poly_randtest(b, state, n_randtest(state) % 200);
       
        nmod_poly_xgcd_euclidean(g, s, t, a, b);
        nmod_poly_xgcd_euclidean(g, s, b, a, b);

        result = (nmod_poly_equal(t, b));
        if (!result)
        {
            flint_printf("FAIL:\n");
            nmod_poly_print(b), flint_printf("\n\n");
            nmod_poly_print(t), flint_printf("\n\n");
            flint_printf("n = %wd\n", n);
            abort();
        }

        nmod_poly_clear(a);
        nmod_poly_clear(b);
        nmod_poly_clear(g);
        nmod_poly_clear(s);
        nmod_poly_clear(t);
    }

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