예제 #1
0
void F_mpz_mod_poly_left_shift(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t poly, const ulong n)
{
   if (n == 0) // special case, no shift
	{
		if (res != poly) F_mpz_mod_poly_set(res, poly);
		return;
	}
	
	if (poly->length == 0) // nothing to shift
	{
		_F_mpz_mod_poly_set_length(res, 0);
		return;
	}
	
	F_mpz_mod_poly_fit_length(res, poly->length + n);
	
	// copy in reverse order to avoid writing over unshifted coeffs
	for (long i = poly->length - 1; i >= 0; i--) 
		F_mpz_set(res->coeffs + i + n, poly->coeffs + i);

   // insert n zeroes
	for (ulong i = 0; i < n; i++) F_mpz_zero(res->coeffs + i);
   
   _F_mpz_mod_poly_set_length(res, poly->length + n);
}
예제 #2
0
void F_mpz_mod_poly_init(F_mpz_mod_poly_t poly, const F_mpz_t P)
{
   poly->coeffs = NULL;
   F_mpz_init(poly->P);
   F_mpz_set(poly->P, P);
   poly->alloc = 0;
   poly->length = 0;
}
예제 #3
0
파일: F_mpzmod_mat.c 프로젝트: hperl/flint
void F_mpzmod_mat_set(F_mpzmod_mat_t res, F_mpzmod_mat_t mat1)
{
	for (ulong i = 0; i < mat1->r; i++)
	{
		for (ulong j = 0; j < mat1->c; j++)
			F_mpz_set(res->rows[i] + j, mat1->rows[i] + j);
	}
}
예제 #4
0
void F_mpz_mod_poly_to_F_mpz_poly(F_mpz_poly_t poly, const F_mpz_mod_poly_t F_poly)
{
	F_mpz_poly_fit_length(poly, F_poly->length);

   poly->length = F_poly->length;
   
   for (ulong i = 0; i < F_poly->length; i++)
	   F_mpz_set(poly->coeffs + i, F_poly->coeffs + i);
}
예제 #5
0
void F_mpz_poly_to_F_mpz_mod_poly(F_mpz_mod_poly_t F_poly, const F_mpz_poly_t poly)
{
	F_mpz_mod_poly_fit_length(F_poly, poly->length);

	_F_mpz_mod_poly_set_length(F_poly, poly->length);
   
	for (ulong i = 0; i < poly->length; i++)
   {
      F_mpz_set(F_poly->coeffs + i, poly->coeffs + i);
      F_mpz_mod(F_poly->coeffs + i, F_poly->coeffs + i, F_poly->P);
   }
   _F_mpz_mod_poly_normalise(F_poly);
}
예제 #6
0
void F_mpz_mod_poly_init2(F_mpz_mod_poly_t poly, const F_mpz_t P, const ulong alloc)
{
   if (alloc) // allocate space for alloc small coeffs
   {
      poly->coeffs = (F_mpz *) flint_heap_alloc(alloc);
		F_mpn_clear(poly->coeffs, alloc);
   }
   else poly->coeffs = NULL;

   F_mpz_init(poly->P);
   F_mpz_set(poly->P, P);   
   poly->alloc = alloc;
   poly->length = 0;
}
예제 #7
0
void F_mpz_mod_poly_set(F_mpz_mod_poly_t poly1, const F_mpz_mod_poly_t poly2)
{
	if (poly1 != poly2) // aliasing is trivial
	{
		ulong length = poly2->length;
	
	   F_mpz_mod_poly_fit_length(poly1, poly2->length);

		for (ulong i = 0; i < poly2->length; i++)
			F_mpz_set(poly1->coeffs + i, poly2->coeffs + i);
		
		_F_mpz_mod_poly_set_length(poly1, poly2->length);
	}
}
예제 #8
0
void F_mpz_mod_poly_right_shift(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t poly, const ulong n)
{
   if (poly->length <= n) 
   {
      F_mpz_mod_poly_zero(res);
      return;
   }

   F_mpz_mod_poly_fit_length(res, poly->length - n);
	
	// copy in forward order to avoid writing over unshifted coeffs
	for (ulong i = 0; i < poly->length - n; i++) 
		F_mpz_set(res->coeffs + i, poly->coeffs + i + n);
	
	_F_mpz_mod_poly_set_length(res, poly->length - n);
}
예제 #9
0
파일: F_mpzmod_mat.c 프로젝트: hperl/flint
void F_mpzmod_mat_init(F_mpzmod_mat_t mat, F_mpz_t p, ulong rows, ulong cols)
{
   mat->entries = (F_mpz *) flint_heap_alloc(rows*cols);
	mat->rows = (F_mpz **) flint_heap_alloc(rows);
   
   // Set up the rows
   for (ulong i = 0; i < rows; i++)
      mat->rows[i] = mat->entries + i*cols;

	for (ulong i = 0; i < rows*cols; i++)
		F_mpz_init(mat->entries + i);

   F_mpz_set(mat->p, p);
	mat->r = rows;
   mat->c = cols;
}
예제 #10
0
void F_mpz_mod_poly_divrem_basecase(F_mpz_mod_poly_t Q, F_mpz_mod_poly_t R, const F_mpz_mod_poly_t A, const F_mpz_mod_poly_t B)
{
   if (B->length == 0)
   {
      printf("Error: Divide by zero\n");
      abort();      
   }
   
   if (A->length < B->length)
   {
      F_mpz_mod_poly_set(R, A);
      F_mpz_mod_poly_zero(Q);
      
      return;
   }

   F_mpz_t lead_inv;
   F_mpz_init(lead_inv);
   F_mpz_invert(lead_inv, B->coeffs + B->length - 1, B->P);
   
   F_mpz * coeff_Q;
   
   F_mpz_mod_poly_t qB;
   F_mpz_mod_poly_init2(qB, B->P, B->length);
   
   F_mpz_mod_poly_t Bm1;
   _F_mpz_mod_poly_attach_truncate(Bm1, B, B->length - 1);
   
   long coeff = A->length - 1;
   
   F_mpz_mod_poly_set(R, A);
   
   if (A->length >= B->length)
   {
      F_mpz_mod_poly_fit_length(Q, A->length - B->length + 1);
      _F_mpz_mod_poly_set_length(Q, A->length - B->length + 1);
   } else F_mpz_mod_poly_zero(Q); 

   coeff_Q = Q->coeffs - B->length + 1;
   
   while (coeff >= (long) B->length - 1)
   {
      while ((coeff >= (long) B->length - 1) && (F_mpz_is_zero(R->coeffs + coeff)))
      {
         F_mpz_zero(coeff_Q + coeff);
         coeff--;
      }
      
      if (coeff >= (long) B->length - 1)
      {
         F_mpz_mulmod2(coeff_Q + coeff, R->coeffs + coeff, lead_inv, B->P);
         
         F_mpz_mod_poly_scalar_mul(qB, Bm1, coeff_Q + coeff);
         
         F_mpz_mod_poly_t R_sub;
         F_mpz_init(R_sub->P);
         F_mpz_set(R_sub->P, B->P);
         R_sub->coeffs = R->coeffs + coeff - B->length + 1;
         R_sub->length = B->length - 1;
         _F_mpz_mod_poly_sub(R_sub, R_sub, qB);
         F_mpz_clear(R_sub->P);
         
         coeff--;
      }
   }
   
   _F_mpz_mod_poly_set_length(R, B->length - 1);
   _F_mpz_mod_poly_normalise(R);
   F_mpz_mod_poly_clear(qB);
   F_mpz_clear(lead_inv);
}
예제 #11
0
int main(int argc, char * argv[]){

   long alpha, Xpow, Ypow, i, j;

   if (argc == 1){
      alpha = 3;
      Xpow = 20;
      Ypow = 220;
   }
   else{
      alpha = atoi(argv[1]);
      Xpow = atoi(argv[2]);
      Ypow = atoi(argv[3]);
   }
   
   F_mpz_poly_t f;
   F_mpz_poly_init(f);
   FILE * inst = fopen("pol_in","r");
   F_mpz_poly_fread(f, inst);
   fclose(inst);

   F_mpz_t Modulus;
   F_mpz_init(Modulus);

   F_mpz_read(Modulus);

//   F_mpz_print(Modulus); printf(" was modulus\n");
//   F_mpz_poly_print(f); printf(" was f\n");
   long d = f->length - 1;

   long ypow = d*(alpha) + 1;
   long maxlength = ypow*alpha + 1;
//   printf("ypow = %ld\n", ypow);

   F_mpz_poly_t temp;
   F_mpz_poly_init(temp);
   F_mpz_poly_t P[alpha + 1];
   F_mpz_mat_t M;
   F_mpz_mat_init(M, ((alpha+1)*(alpha+2))/2 , maxlength);
   long row = 0;

   F_mpz_poly_t m_arr; //an array of powers of the modulus, m_arr->coeffs + i should be Modulus^i
   F_mpz_poly_init(m_arr);
   F_mpz_poly_set_coeff_ui(m_arr, 0, 1);

   F_mpz_t temp_m;
   F_mpz_init(temp_m);

   F_mpz_set_ui(temp_m, 1);

   for (j = 1; j < alpha + 1; j++)
   {
      F_mpz_mul2(temp_m, temp_m, Modulus);
      F_mpz_poly_set_coeff(m_arr, j, temp_m);
   } 

   F_mpz_mat_t row_scale;
   F_mpz_mat_init(row_scale, M->r, M->r);

   for (j = 0; j < alpha + 1; j++)
   {
      if (j == 0){
         F_mpz_poly_init(P[0]);
         F_mpz_poly_set_coeff_ui(P[0], 0, 1);
      }
      else if (j == 1){
         F_mpz_poly_init(P[1]);
         F_mpz_poly_set(P[1], f);
//      printf("i is %ld and j is %ld\n", i, j);
         F_mpz_poly_set_coeff_si(P[1], ypow, -1);
      }
      else if (j > 1){
         F_mpz_poly_init(P[j]);
         F_mpz_poly_mul(P[j], P[j-1], P[1]);
      }

      for (i = 0; i < alpha + 1 - j; i++){
         F_mpz_poly_left_shift(temp, P[j], i);
//         F_mpz_poly_print(temp); printf(" was P with i = %ld and j = %ld\n", i, j);
         long k;
         for (k = 0; k < maxlength; k++){
            if (k < temp->length)
               F_mpz_set(M->rows[row]+k, temp->coeffs + k);
            else
               F_mpz_zero(M->rows[row]+k);
         }
         F_mpz_set(row_scale->rows[row] + row, m_arr->coeffs + alpha - j);          
         row++;
      }
   } 
   F_mpz_clear(temp_m);
   F_mpz_poly_clear(m_arr);

   F_mpz_t X;
   F_mpz_t Y;
   F_mpz_init(X);
   F_mpz_init(Y);

   F_mpz_set_ui(X, 2);
   F_mpz_pow_ui(X, X, Xpow);
   F_mpz_set_ui(Y, 2);
   F_mpz_pow_ui(Y, Y, Ypow);

   F_mpz_mat_t col_scale;
   F_mpz_mat_init(col_scale, maxlength, maxlength);

   F_mpz_set_ui(col_scale->rows[0], 1);
   for (i = 1; i < ypow; i++){
      F_mpz_mul2(col_scale->rows[i] + i, col_scale->rows[i-1] + i-1, X);
   }

   for (j = 1; j < alpha; j++){
      for (i = ypow*(j-1); i < j*ypow; i++){
         F_mpz_mul2(col_scale->rows[i + ypow] + i + ypow, col_scale->rows[i] + i, Y);
      }
   }
//using X bounds and Y bounds as powers of 2 but not taking advantage of 
//that via cheap multiplication (or to save in LLL costs) and we should


   F_mpz_mul2(col_scale->rows[maxlength-1] + maxlength -1 , col_scale->rows[maxlength - ypow - 1] + maxlength - ypow - 1, Y);

//   F_mpz_mat_print_pretty(col_scale);

   F_mpz_mat_mul_classical(M, M, col_scale);

   F_mpz_mat_mul_classical(M, row_scale, M);

   FILE * fpre = fopen("pre_LLL","w");
   F_mpz_mat_fprint_pretty(M, fpre);
   fclose(fpre);

//   LLL(M);

//   FILE * fpost = fopen("post_LLL","w");
//   F_mpz_mat_fprint_pretty(M, fpost);
//   fclose(fpost);

//   F_mpz_poly_clear(temp);
//   F_mpz_poly_init(temp);

//   for (i = 0; i < maxlength; i++){
//      F_mpz_poly_set_coeff(temp, i, M->rows[0]+i);
//      F_mpz_divexact(temp->coeffs + i, temp->coeffs + i, col_scale->rows[i] + i); //undo col_scale
//   }

//   FILE * fpoly1 = fopen("poly1","w");
//   F_mpz_poly_fprint(temp, fpoly1);
//   fclose(fpoly1);
//   F_mpz_poly_print(temp); printf(" was the first row after LLL as a polynomial\n");

//   F_mpz_poly_clear(temp);

   F_mpz_clear(X);
   F_mpz_clear(Y);
   F_mpz_clear(Modulus);

   for (j = 0; j < alpha + 1; j++){
      F_mpz_poly_clear(P[j]);
   }
   F_mpz_poly_clear(temp);
   F_mpz_mat_clear(col_scale);
   F_mpz_mat_clear(row_scale);
   F_mpz_mat_clear(M);
   F_mpz_poly_clear(f);
   flint_stack_cleanup();
   return 0;
}
예제 #12
0
void F_mpz_mod_poly_gcd_euclidean(F_mpz_mod_poly_t res, F_mpz_mod_poly_t poly1, F_mpz_mod_poly_t poly2)
{
	F_mpz_mod_poly_t R, A, B;
	F_mpz_poly_t r;
	int steps = 0;
	
	if (poly1->length == 0) 
	{
		if (poly2->length == 0)
			F_mpz_mod_poly_zero(res);
		else F_mpz_mod_poly_make_monic(res, poly2);
		return;
	}
	
	if (poly2->length == 0) 
	{
		F_mpz_mod_poly_make_monic(res, poly1);
		return;
	}
	
	if ((poly1->length == 1) || (poly2->length == 1))
	{
		_F_mpz_poly_attach_F_mpz_mod_poly(r, res);
		F_mpz_poly_set_coeff_ui(r, 0, 1L);
		_F_mpz_mod_poly_attach_F_mpz_poly(res, r);
		_F_mpz_mod_poly_normalise(res);
		return;
	}
	
	F_mpz_t P;
	F_mpz_init(P);
	F_mpz_set(P, poly1->P);
	
	F_mpz_mod_poly_init(R, P);
	
	if (poly1->length > poly2->length)
	{
		_F_mpz_mod_poly_attach(A, poly1);
		_F_mpz_mod_poly_attach(B, poly2);
	} else
	{
		_F_mpz_mod_poly_attach(A, poly2);
		_F_mpz_mod_poly_attach(B, poly1);
	}
	
	F_mpz_mod_poly_rem(R, A, B);
	
	F_mpz_mod_poly_swap(A, B);
	F_mpz_mod_poly_swap(B, R);
	F_mpz_mod_poly_init(R, P); 
	
	if (B->length > 1)
	{
		F_mpz_mod_poly_rem(R, A, B);
		F_mpz_mod_poly_swap(A, B);
		F_mpz_mod_poly_swap(B, R);
		F_mpz_mod_poly_init(R, P);
		steps = 1;
	}
	
	while (B->length > 1)
	{
		F_mpz_mod_poly_rem(A, A, B);
		F_mpz_mod_poly_swap(A, B); 
	}
	
	if  (B->length == 1) 
	{
		_F_mpz_poly_attach_F_mpz_mod_poly(r, res);
		F_mpz_poly_set_coeff_ui(r, 0, 1L);
		_F_mpz_mod_poly_attach_F_mpz_poly(res, r);
		res->length = 1;
	}
	else F_mpz_mod_poly_make_monic(res, A);
	
	if (steps) 
	{
		F_mpz_mod_poly_clear(A);
	} 
	
	F_mpz_mod_poly_clear(B);
	F_mpz_mod_poly_clear(R);
}