Beispiel #1
0
void manual_test(n , m)
{
  POLY a, b;
  int i;

  a.d=n;
  b.d=m;
  a.p=(dcmplx*) calloc(n+1, sizeof(dcmplx));
  b.p=(dcmplx*) calloc(m+1, sizeof(dcmplx));
  printf("please input the polynomial a\n"); 
  for(i=0; i<=n; i++)
     read_dcmplx( &(a.p[i]));
  printf("Polynomial a is:\n");
  Print_Poly(a.d, a.p);
  printf("please input the polynomial b\n");
  for(i=0; i<=m; i++)
     read_dcmplx(&(b.p[i]));
  printf("Polynomial b is:\n");
  Print_Poly(b.d, b.p);
  if( poly_divide( a, b) ) 
     printf("Polynomial a divides into b without remainder.\n");
  else
     printf("Polynomial a divides into b has remainder.\n");

  free(a.p);
  free(b.p);
}
Beispiel #2
0
void Read_Poly ( int n, dcmplx a[], int m, dcmplx b[] )
{
  int i, j;
  printf("Please input the first polynomial:\n");
  for (i=0; i<=n; i++)
	  read_dcmplx(&(a[i]));
  printf("Please input the second polynomial:\n");
  for (j=0; j<=m; j++)
	  read_dcmplx(&(b[j]));
  
  printf("The polynomial a is:\n");
  Print_Poly(n,a);
  printf("The polynomial b is:\n");
  Print_Poly(m,b);
}
Beispiel #3
0
void Call_Inverse( int n)
{
  POLY ds;
  POLY M1[n][n], t_M1[n][n], product[n][n];
  int k; 

  printf("Please choose the test matrix:  "); 
  printf("1 random matrix.\n");
  printf("2 input your own matrix.\n");
  scanf("%d", &k ); printf("%d\n", k);

  if(k==1) random_matrix ( n, n, M1);
  if(k==2) read_matrix( n, n, M1 );
  printf("the original matrix generated :\n");
  print( n, n, M1);

  copy(n, n, M1, t_M1);
  ds = Inverse_Poly ( n, M1 );
  printf(" The inverse matrix of the matrix is :\n" );
  print(n, n, M1 );
  printf(" The polynomial ds is :\n" );
  Print_Poly( ds.d, ds.p );

  printf("The product (without ds) of the original matrix");
  printf(" and the inverse matrix is:\n");
 
  Multiply(n, n, n, M1, t_M1, product);
  print(n, n, product);
  
  free_matrix(n, n, M1);
  free_matrix(n, n, t_M1);
  free_matrix(n, n, product);
 
}
Beispiel #4
0
void print ( int n, int m, POLY a[n][m] )
{
   int i,j;
   for (i=0; i<n; i++)
   {
      for (j=0; j<m; j++)
      {
        printf("( %d, %d )\n", i, j); 
        Print_Poly(a[i][j].d, a[i][j].p);
        printf("\n");
      }
   } 
}
Beispiel #5
0
void manual_test ( int n, int m, int l )
{
  double eps = 1.0e-12;
  double tol = 1.0e-8;
  double mul_tol = 1.0e-3;
  dcmplx aa[n+1], bb[m+1], c[l+1], a[n+l+1], *dcmplx_a, b[m+l+1], *dcmplx_b, ra[n+l], rb[m+l], rc[l];
  int i, j, c_num, bug, dk, dl, dgcd, k1, k2, dc_gcd;
  dcmplx **gcd_coeff;
  dcmplx *t1, *t2, *c_gcd;  
  int dbd, dad, mult[l];

  printf("please input the polynomial aa\n"); 
  for(i=0; i<=n; i++)
     read_dcmplx( &(aa[i]));
  printf("please input the polynomial bb\n");
  for(i=0; i<=m; i++)
     read_dcmplx(&(bb[i]));
  printf("please input the polynomial c, (the leading coefficient must be one!)\n");
  for(i=0; i<=l; i++)
     read_dcmplx(&(c[i]));

  for(i=0; i<=n+l; i++)
      a[i]=create1(0.0);
  for(i=0; i<=n; i++)
    for(j=0; j<=l; j++)
       a[i+j]=add_dcmplx(a[i+j], mul_dcmplx(aa[i], c[j]));     
  
  for(i=0; i<=m+l; i++)  
    b[i]=create1(0.0);
  for(i=0; i<=m; i++)
    for(j=0; j<=l; j++)
       b[i+j]=add_dcmplx(b[i+j], mul_dcmplx(bb[i], c[j]));

  gcd( n+l+1, a, m+l+1, b, &c_num, ra, rb );

  printf("the roots number of the given common diviser is: %d \n", l);
  multiple_roots(l+1, c, eps, 10*l, rc, mul_tol, mult);
  printf("the root of the gcd is:\n");
  for(i=0; i<l; i++)
     writeln_dcmplx(rc[i]);      
  
  printf("the roots number of the calculated common diviser is: %d \n", c_num); 
  
  if(l!= c_num)
  {
    printf("Bug!!\n");
    exit(1);
  }

  for(i=0; i<l; i++)
  {
    bug=1;
    for(j=0; j<l; j++)
    {
      if(equal_dcmplx(rc[i], ra[j], tol))
        bug=0;
    }
    if( bug==1 )
    {
      printf(" Bug!!\n");
      exit(1);
    }
  }
 printf("the gcd is correct !\n");

 /* now begin to test extended gcd method */
 gcd_coeff=ExtPolyGcd( n+l+1, a, m+l+1, b, &dgcd, &dk, &dl, &dbd, &dad);

 Print_Poly(dl, gcd_coeff[1]);
 printf("a is:\n"); 
 Print_Poly(n+l, a);
 dcmplx_a=(dcmplx*) calloc(n+l+1, sizeof(dcmplx));
 /* for(i=0; i<=n+l; i++)
      dcmplx_a[i]=a[i]; */
  t1=mul_poly( dk, gcd_coeff[0], n+l, a, &k1 );
  t2=mul_poly( dl, gcd_coeff[1], m+l, b, &k2 );


 /*
 printf("t1 is:\n");  
 for(i=0; i<=k1; i++)
   writeln_dcmplx(t1[i]);

 printf("t2 is:\n");
 printf("dl=%d\n", dl);
printf("m+l=%d\n", m+l);
 for(i=0; i<=k2; i++)
    writeln_dcmplx(t2[i]);
 */

 printf("\n"); 
 printf("k is:\n");
 for(i=0; i<=dk; i++)
    writeln_dcmplx(gcd_coeff[0][i]);

 printf("l is:\n");
 for(i=0; i<=dl; i++)
    writeln_dcmplx(gcd_coeff[1][i]);
 printf("\n");

 c_gcd=add_poly( k1, t1, k2, t2, &dc_gcd);

 printf("the given polynomial a is:\n"); 
 for(i=0; i<=n+l; i++)
    writeln_dcmplx(a[i]);
 printf("a's roots are:\n");
 for(i=0; i<n+l; i++)
   writeln_dcmplx(ra[i]);
 printf("\n");

 printf("the given polynomial b is:\n");
 for(i=0; i<=m+l; i++)
    writeln_dcmplx(b[i]);
 printf("b's roots are:\n");
 for(i=0; i<m+l; i++)
   writeln_dcmplx(rb[i]);
 printf("\n");

 printf("the given gcd polynomial is:\n");
 for(i=0; i<=l; i++)
    writeln_dcmplx(c[i]);
 printf("\n");

  if(l!= dc_gcd)
  {
    printf("Bug!! the given gcd is different with the gcd method result.(0 is special case).\n");
    
  }
 
 printf("the calculated gcd polynomial is:\n");
 for(i=0; i<=dc_gcd; i++)
    writeln_dcmplx(c_gcd[i]);
 printf("\n");

 printf("the gcd from common roots method is:\n");
 for(i=0; i<=dgcd; i++)
   writeln_dcmplx(gcd_coeff[2][i]);
 printf("\n");

 free(t1); free(t2);
 free(c_gcd);

 free(gcd_coeff[0]);
 free(gcd_coeff[1]);
 free(gcd_coeff[2]);
 free(gcd_coeff[3]);
 free(gcd_coeff[4]);
 free(gcd_coeff);
}