ats_void_type
atslib_mpq_inp_str_exn (
  ats_mpq_ptr_type x
, ats_ptr_type file
, ats_int_type base
) {
  size_t n = mpq_inp_str(x, (FILE*)file, base) ;
  if (n == 0) {
    ats_exit_errmsg (1, "exit(ATS): [mpq_inp_str] failed.\n") ;
  } // end of [if]
  return ;
} // end of [atslib_mpq_inp_str_exn]
Beispiel #2
0
int
main (int argc, char **argv)
{
  mpq_t  op1, op2;
  mp_size_t size;
  int i;
  int reps = 10000;
  FILE *fp;
  int base;
  gmp_randstate_ptr rands;
  mpz_t bs;
  unsigned long bsi, size_range;
  size_t nread;

  tests_start ();
  rands = RANDS;

  mpz_init (bs);

  if (argc == 2)
    reps = atoi (argv[1]);

  mpq_init (op1);
  mpq_init (op2);

  fp = fopen (FILENAME, "w+");

  for (i = 0; i < reps; i++)
    {
      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 10 + 2;

      mpz_urandomb (bs, rands, size_range);
      size = mpz_get_ui (bs);
      mpz_errandomb (mpq_numref(op1), rands, 512L);
      mpz_errandomb_nonzero (mpq_denref(op1), rands, 512L);
      mpq_canonicalize (op1);

      mpz_urandomb (bs, rands, 1);
      bsi = mpz_get_ui (bs);
      if ((bsi & 1) != 0)
	mpq_neg (op1, op1);

      mpz_urandomb (bs, rands, 16);
      bsi = mpz_get_ui (bs);
      base = bsi % 36 + 1;
      if (base == 1)
	base = 0;

      rewind (fp);
      if (mpq_out_str (fp, base, op1) == 0
	  || putc (' ', fp) == EOF
	  || fflush (fp) != 0)
	{
	  printf ("mpq_out_str write error\n");
	  abort ();
	}

      rewind (fp);
      nread = mpq_inp_str (op2, fp, base);
      if (nread == 0)
	{
	  if (ferror (fp))
	    printf ("mpq_inp_str stream read error\n");
	  else
	    printf ("mpq_inp_str data conversion error\n");
	  abort ();
	}

      if (nread != ftell(fp))
	{
	  printf ("mpq_inp_str nread doesn't match ftell\n");
	  printf ("  nread  %lu\n", (unsigned long) nread);
	  printf ("  ftell  %ld\n", ftell(fp));
	  abort ();
	}

      if (mpq_cmp (op1, op2))
	{
	  printf ("ERROR\n");
	  printf ("op1  = "); debug_mp (op1, -16);
	  printf ("op2  = "); debug_mp (op2, -16);
	  printf ("base = %d\n", base);
	  abort ();
	}
    }

  fclose (fp);

  unlink (FILENAME);

  mpz_clear (bs);
  mpq_clear (op1);
  mpq_clear (op2);

  tests_end ();
  exit (0);
}
Beispiel #3
0
void
check_data (void)
{
  static const struct {
    const char  *inp;
    int         base;
    const char  *want;
    int         want_nread;

  } data[] = {

    { "0",   10, "0", 1 },
    { "0/1", 10, "0", 3 },

    { "0/",   10, "0", 0 },
    { "/123", 10, "0", 0 },
    { "blah", 10, "0", 0 },
    { "123/blah", 10, "0", 0 },
    { "5 /8", 10, "5", 1 },
    { "5/ 8", 10, "0", 0 },

    {  "ff", 16,  "255", 2 },
    { "-ff", 16, "-255", 3 },
    {  "FF", 16,  "255", 2 },
    { "-FF", 16, "-255", 3 },

    { "z", 36, "35", 1 },
    { "Z", 36, "35", 1 },

    {  "0x0",    0,   "0", 3 },
    {  "0x10",   0,  "16", 4 },
    { "-0x0",    0,   "0", 4 },
    { "-0x10",   0, "-16", 5 },
    { "-0x10/5", 0, "-16/5", 7 },

    {  "00",   0,  "0", 2 },
    {  "010",  0,  "8", 3 },
    { "-00",   0,  "0", 3 },
    { "-010",  0, "-8", 4 },
  };

  mpq_t  got, want;
  long   ftell_nread;
  int    i, post, j, got_nread;
  FILE   *fp;

  mpq_init (got);
  mpq_init (want);

  for (i = 0; i < numberof (data); i++)
    {
      for (post = 0; post <= 2; post++)
	{
	  mpq_set_str_or_abort (want, data[i].want, 0);
	  MPQ_CHECK_FORMAT (want);

	  fp = fopen (FILENAME, "w+");
	  ASSERT_ALWAYS (fp != NULL);
	  fputs (data[i].inp, fp);
	  for (j = 0; j < post; j++)
	    putc (' ', fp);
	  fflush (fp);
	  ASSERT_ALWAYS (! ferror(fp));

	  rewind (fp);
	  got_nread = mpq_inp_str (got, fp, data[i].base);

	  if (got_nread != 0)
	    {
	      ftell_nread = ftell (fp);
	      if (got_nread != ftell_nread)
		{
		  printf ("mpq_inp_str nread wrong\n");
		  printf ("  inp          \"%s\"\n", data[i].inp);
		  printf ("  base         %d\n", data[i].base);
		  printf ("  got_nread    %d\n", got_nread);
		  printf ("  ftell_nread  %ld\n", ftell_nread);
		  abort ();
		}
	    }

	  if (post == 0 && data[i].want_nread == strlen(data[i].inp))
	    {
	      int  c = getc(fp);
	      if (c != EOF)
		{
		  printf ("mpq_inp_str didn't read to EOF\n");
		  printf ("  inp         \"%s\"\n", data[i].inp);
		  printf ("  base        %d\n", data[i].base);
		  printf ("  c '%c' %#x\n", c, c);
		  abort ();
		}
	    }

	  if (got_nread != data[i].want_nread)
	    {
	      printf ("mpq_inp_str nread wrong\n");
	      printf ("  inp         \"%s\"\n", data[i].inp);
	      printf ("  base        %d\n", data[i].base);
	      printf ("  got_nread   %d\n", got_nread);
	      printf ("  want_nread  %d\n", data[i].want_nread);
	      abort ();
	    }

	  MPQ_CHECK_FORMAT (got);

	  if (! mpq_equal (got, want))
	    {
	      printf ("mpq_inp_str wrong result\n");
	      printf ("  inp   \"%s\"\n", data[i].inp);
	      printf ("  base  %d\n", data[i].base);
	      mpq_trace ("  got ",  got);
	      mpq_trace ("  want", want);
	      abort ();
	    }

	  ASSERT_ALWAYS (fclose (fp) == 0);
	}
    }

  mpq_clear (got);
  mpq_clear (want);
}
Beispiel #4
0
void load_rational_points(int *numPoints, rational_complex_vector **points, int numVars, char *PtsFile)
/***************************************************************\
* USAGE: load rational points from PtsFile                      *
\***************************************************************/
{
  int i, j, rV, base = 10;
  FILE *IN = fopen(PtsFile, "r");

  // error checking - file must exist
  if (IN == NULL)
  {
    printf("\nERROR: '%s' does not exist!\n", PtsFile);
    errExit(ERROR_FILE_NOT_EXIST); 
  }

  // read in the number of points
  rV = fscanf(IN, "%d", numPoints);

  // error checking
  if (rV != 1)
  { 
    printf("\nERROR: Unable to read the number of points stored in '%s'.\n", PtsFile);
    errExit(ERROR_FILE_NOT_EXIST); 
  }
  else if (*numPoints <= 0)
  {
    printf("ERROR: The number of points in '%s' must be positive!\n", PtsFile);
    errExit(ERROR_CONFIGURATION);
  }

  // allocate points
  *points = (rational_complex_vector *)errMalloc((*numPoints) * sizeof(rational_complex_vector));
  
  // read in the points
  for (i = 0; i < *numPoints; i++)
  { // setup points[i]
    initialize_rational_vector((*points)[i], numVars);
  
    for (j = 0; j < numVars; j++)
    { // setup real part
      rV = mpq_inp_str((*points)[i]->coord[j]->re, IN, base);

      // error checking
      if (rV == 0)
      { 
        printf("\nERROR: Unable to read in %d rational vectors from '%s'.\n", *numPoints, PtsFile);
        errExit(ERROR_FILE_NOT_EXIST); 
      }

      // setup imag part
      rV = mpq_inp_str((*points)[i]->coord[j]->im, IN, base);

      // error checking
      if (rV == 0)
      { 
        printf("\nERROR: Unable to read in %d rational vectors from '%s'.\n", *numPoints, PtsFile);
        errExit(ERROR_FILE_NOT_EXIST); 
      }

      // canonicalize rational numbers
      mpq_canonicalize((*points)[i]->coord[j]->re);
      mpq_canonicalize((*points)[i]->coord[j]->im);
    }  
  }

  // close file
  fclose(IN);

  return;
}
Beispiel #5
0
void setup_polynomial(polynomial *F, int numVars, FILE *IN, int polyNumber)
/***************************************************************\
* USAGE: setup the next polynomial described in IN              *
\***************************************************************/
{
  int i, j, rV, max, base = 10;

  // initialize the number of variables, degree & isReal
  F->numVariables = numVars;
  F->degree = 0;
  F->isReal = 1;

  // read in the number of terms
  fscanf(IN, "%d\n", &F->numTerms);

  // error checking - want number of terms >= 0
  if (F->numTerms <= 0)
  { // error
    printf("ERROR: The number of terms (%d) must be positive.\n", F->numTerms);
    // close file and exit
    fclose(IN);
    errExit(ERROR_INPUT_SYSTEM);
  }

  // allocate memory
  mpq_init(F->norm_sqr);
  F->coeff = (rational_complex_number *)errMalloc(F->numTerms * sizeof(rational_complex_number));
  F->exponents = (int **)errMalloc(F->numTerms * sizeof(int *));

  // setup the terms
  for (i = 0; i < F->numTerms; i++)
  { // allocate & initialize memory
    F->exponents[i] = (int *)errMalloc(numVars * sizeof(int));
    initialize_rational_number(F->coeff[i]);

    // read in exponents, compute degree, and perform error checking - want exponents >= 0
    max = 0;
    for (j = 0; j < numVars; j++)
    {
      fscanf(IN, "%d", &F->exponents[i][j]);
      if (F->exponents[i][j] < 0)
      { // error
        printf("ERROR: The exponent for variable %d in monomial %d of polynomial %d must be nonnegative (%d).\n", j+1, i+1, polyNumber+1, F->exponents[i][j]);
        // close file and exit
        fclose(IN);
        errExit(ERROR_INPUT_SYSTEM);
      }
      max += F->exponents[i][j];
    }

    // update degree, if needed
    if (max > F->degree)
      F->degree = max;

    // read in real & imaginary part of coefficient
    rV = mpq_inp_str(F->coeff[i]->re, IN, base);
    if (rV == 0)
    { // error in reading the coefficient
      printf("ERROR: There appears to be an error when reading in the real part of the\n       coefficient for monomial %d of polynomial %d.\n", i+1, polyNumber+1);
      // close file and exit
      fclose(IN);
      errExit(ERROR_INPUT_SYSTEM);
    } 
    rV = mpq_inp_str(F->coeff[i]->im, IN, base);
    if (rV == 0)
    { // error in reading the coefficient
      printf("ERROR: There appears to be an error when reading in the imaginary part of the\n       coefficient for monomial %d of polynomial %d.\n", i+1, polyNumber+1);
      // close file and exit
      fclose(IN);
      errExit(ERROR_INPUT_SYSTEM);
    } 

    // setup in canonical form
    mpq_canonicalize(F->coeff[i]->re);
    mpq_canonicalize(F->coeff[i]->im);

    // update isReal, if needed
    if (F->isReal && mpq_cmp_ui(F->coeff[i]->im, 0, 1) != 0)
      F->isReal = 0;
  }

  // compute norm_sqr
  norm_sqr_polynomial(F->norm_sqr, F);
  
  return;
}
Beispiel #6
0
void setup_exponential(exponential *F, int numVars, FILE *IN, int expNumber, int yIndex)
/***************************************************************\
* USAGE: setup the next exponential described in IN             *
\***************************************************************/
{
  int rV, base = 10;

  // read in the x variable index
  rV = fscanf(IN, "%d", &F->xIndex);
  if (rV == 0)
  { // error in reading the x variable index
    printf("ERROR: There appears to be an error when reading in the variable index\n       for exponential %d.\n", expNumber+1);
    // close file and exit
    fclose(IN);
    errExit(ERROR_INPUT_SYSTEM);
  } 

  // error checking - want variable index to be in 1,2,..,numVars
  if (F->xIndex < 1 || F->xIndex > numVars)
  { // error
    printf("ERROR: The variable index for exponential %d must be between 1 and %d.\n", expNumber+1, numVars);
    // close file and exit
    fclose(IN);
    errExit(ERROR_INPUT_SYSTEM);
  }

  // setup x & y variable index
  F->xIndex--;
  F->yIndex = yIndex;

  // read in the type of exponential function: exp, sin, or cos
  rV = readInExpFunction(IN, &F->expFunction, &F->isHyperbolic);
  if (rV == 0)
  { // error in reading the exponential function
    printf("ERROR: There appears to be an error when reading in the exponential function\n       for exponential %d.\n", expNumber+1);
    // close file and exit
    fclose(IN);
    errExit(ERROR_INPUT_SYSTEM);
  } 

  // error checking
  if (F->expFunction != 'X' && F->expFunction != 'S' && F->expFunction != 'C')
  { // error
    printf("ERROR: The exponential function for exponential %d appears to be incorrect.\n", expNumber+1);
    // close file and exit
    fclose(IN);
    errExit(ERROR_INPUT_SYSTEM);
  }

  // read in real & imaginary part of beta
  initialize_rational_number(F->beta);
  rV = mpq_inp_str(F->beta->re, IN, base);
  if (rV == 0)
  { // error in reading the coefficient
    printf("ERROR: There appears to be an error when reading in the real part of the\n       exponential constant for exponential %d.\n", expNumber+1);
    // close file and exit
    fclose(IN);
    errExit(ERROR_INPUT_SYSTEM);
  } 
  rV = mpq_inp_str(F->beta->im, IN, base);
  if (rV == 0)
  { // error in reading the coefficient
    printf("ERROR: There appears to be an error when reading in the imaginary part of the\n       exponential constant exponential %d.\n", expNumber+1);
    // close file and exit
    fclose(IN);
    errExit(ERROR_INPUT_SYSTEM);
  } 

  // setup in canonical form
  mpq_canonicalize(F->beta->re);
  mpq_canonicalize(F->beta->im);

  // update isReal
  F->isReal = !mpq_cmp_ui(F->beta->im, 0, 1);

  return;
}