예제 #1
0
/* test out_str with stream=NULL */
static void
check_stdout (mpc_ptr read_number, mpc_ptr expected)
{
  char tmp_file[] = "mpc_test";
  int fd;
  size_t sz;

  fflush(stdout);
  fd = dup(fileno(stdout));
  if (freopen(tmp_file, "w", stdout) == NULL)
  {
     printf ("mpc_inp_str cannot redirect stdout\n");
     exit (1);
  }
  mpc_out_str (NULL, 2, 0, expected, MPC_RNDNN);
  fflush(stdout);
  dup2(fd, fileno(stdout));
  close(fd);
  clearerr(stdout);

  fflush(stdin);
  fd = dup(fileno(stdin));
  if (freopen(tmp_file, "r", stdin) == NULL)
  {
     printf ("mpc_inp_str cannot redirect stdout\n");
     exit (1);
  }
  if (mpc_inp_str (read_number, NULL, &sz, 2, MPC_RNDNN) == -1)
    {
      printf ("mpc_inp_str cannot correctly re-read number "
              "in file %s\n", tmp_file);
      exit (1);
    }
  mpfr_clear_flags (); /* mpc_cmp set erange flag when an operand is
                          a NaN */
  if (mpc_cmp (read_number, expected) != 0 || mpfr_erangeflag_p())
    {
      printf ("mpc_inp_str did not read the number which was written by "
              "mpc_out_str\n");
      OUT (read_number);
      OUT (expected);
      exit (1);
    }
  fflush(stdin);
  dup2(fd, fileno(stdin));
  close(fd);
  clearerr(stdin);
}
예제 #2
0
static void
check_io_str (mpc_ptr read_number, mpc_ptr expected)
{
  char tmp_file[] = "mpc_test";
  FILE *fp;
  size_t sz;

  if (!(fp = fopen (tmp_file, "w")))
    {
      printf ("Error: Could not open file %s\n", tmp_file);

      exit (1);
    }

  mpc_out_str (fp, 10, 0, expected, MPC_RNDNN);
  fclose (fp);

  if (!(fp = fopen (tmp_file, "r")))
    {
      printf ("Error: Could not open file %s\n", tmp_file);

      exit (1);
    };
  if (mpc_inp_str (read_number, fp, &sz, 10, MPC_RNDNN) == -1)
    {
      printf ("Error: mpc_inp_str cannot correctly re-read number "
              "in file %s\n", tmp_file);

      exit (1);
    }
  fclose (fp);

  /* mpc_cmp set erange flag when an operand is a NaN */
  mpfr_clear_flags ();
  if (mpc_cmp (read_number, expected) != 0 || mpfr_erangeflag_p())
    {
      printf ("Error: inp_str o out_str <> Id\n");
      OUT (read_number);
      OUT (expected);

      exit (1);
    }
}
예제 #3
0
int 
test_mpsolve (char * pol_file, char * res_file, mps_algorithm algorithm)
{
  mpc_t root, ctmp;
  mps_boolean passed = true;
  int i, j, zero_roots = 0;
  char ch;
  rdpe_t eps;

  /* Check the roots */
  FILE* result_stream = fopen (res_file, "r"); 
  FILE* input_stream  = fopen (pol_file, "r");

  if (!result_stream) 
    {
      fprintf (stderr, "Checking \033[1m%-30s\033[0m \033[31;1mno results file found!\033[0m\n", pol_file + 9); 
      return EXIT_FAILURE;
    }
  if (!input_stream)
    {
      fprintf (stderr, "Checking \033[1m%-30s\033[0m \033[31;1mno polinomial file found!\033[0m\n", pol_file + 9); 
      return EXIT_FAILURE;
    }

  /* Create a new empty mps_context */
  mps_context * s = mps_context_new ();

  if (debug)
    mps_context_set_debug_level (s, MPS_DEBUG_TRACE);

  /* Load the polynomial that has been given to us */
  mps_parse_stream (s, input_stream);
  
  fprintf (stderr, "Checking \033[1m%-30s\033[0m [\033[34;1mchecking\033[0m]", pol_file + 9);

  mps_context_set_output_goal (s, MPS_OUTPUT_GOAL_ISOLATE);
  mps_context_set_output_prec (s, 50 * LOG2_10);
  rdpe_set_dl (eps, 1.0, -15);

  /* Solve it */
  mps_context_select_algorithm (s, algorithm);
  mps_mpsolve (s);
  
  mpc_init2 (root, mps_context_get_data_prec_max (s));
  mpc_init2 (ctmp, mps_context_get_data_prec_max (s));
    
  /* Test if roots are equal to the roots provided in the check */   
  passed = true;

  rdpe_t * drad = rdpe_valloc (mps_context_get_degree (s));
  mpc_t * mroot = mpc_valloc (mps_context_get_degree (s));
  mpc_vinit2 (mroot, mps_context_get_degree (s), 53);

  mps_context_get_roots_m (s, &mroot, &drad);

  for (i = 0; i < mps_context_get_degree (s); i++)   
    {   
      rdpe_t rtmp;   
      cdpe_t cdtmp;   
      rdpe_t min_dist;
      int found_root = 0;
      rdpe_t exp_drad;
      
      while (isspace (ch = getc (result_stream)));   
      ungetc (ch, result_stream);   
      mpc_inp_str (root, result_stream, 10);   

      if (mpc_eq_zero (root))
        {
          zero_roots++;

          /* We need to read it another time. This seems a bug in
           * mpc_inp_str, but I don't get why is necessary. */
          mpc_inp_str (root, result_stream, 10);
          continue;
        }
      
      mpc_sub (ctmp, root, mroot[0]);   
      mpc_get_cdpe (cdtmp, ctmp);   
      cdpe_mod (rtmp, cdtmp);   
      rdpe_set (min_dist, rtmp);   

      if (getenv ("MPS_VERBOSE_TEST") && (strstr (pol_file, getenv ("MPS_VERBOSE_TEST"))))
        {
          printf ("Read root_%d = ", i);
          mpc_out_str_2 (stdout, 10, mps_context_get_data_prec_max (s), mps_context_get_data_prec_max (s),
                         root);
          printf ("\n");
        }
      
      for (j = 1; j < mps_context_get_degree (s); j++)   
        {   
          mpc_sub (ctmp, root, mroot[j]);
          mpc_get_cdpe (cdtmp, ctmp);   
          cdpe_mod (rtmp, cdtmp);   
          
          if (rdpe_le (rtmp, min_dist))
            {
              rdpe_set (min_dist, rtmp);
              found_root = j;
            }
        }

      /* printf ("min_dist_%d = ", i); */
      /* rdpe_out_str (stdout, min_dist); */
      /* printf ("\nrad_%d", i); */
      /* rdpe_out_str (stdout, s->drad[i]); */
      /* printf ("\n"); */


      mpc_get_cdpe (cdtmp, mroot[found_root]);
      cdpe_mod (rtmp, cdtmp);
      rdpe_mul_eq (rtmp, eps);
      rdpe_set (exp_drad, rtmp);
      
      if ((!rdpe_le (min_dist, drad[found_root]) && !rdpe_gt (drad[found_root], exp_drad)) && !mps_context_get_over_max (s))
        {
          passed = false;
          
          if (getenv ("MPS_VERBOSE_TEST") && (strstr (pol_file, getenv ("MPS_VERBOSE_TEST"))))
            {
              printf("Failing on root %d, with min_dist = ", found_root);
              rdpe_out_str (stdout, min_dist);
              printf("\ndrad_%d", found_root);
              rdpe_out_str (stdout, drad[found_root]);
              printf("\n");
              printf("Approximation_%d = ", found_root);
              mpc_out_str_2 (stdout, 10, -rdpe_Esp (drad[found_root]), -rdpe_Esp (drad[found_root]), mroot[found_root]);
              printf("\n");
            }
        }
    }

  if (zero_roots != mps_context_get_zero_roots (s))
    passed = false;
  
  fclose (input_stream);
  fclose (result_stream);    
  
  mpc_clear (ctmp);   
  mpc_clear (root);
  mpc_vclear (mroot, mps_context_get_degree (s));
  
  free (mroot);
  free (drad);

  if (getenv ("MPS_VERBOSE_TEST") && (strstr (pol_file, getenv ("MPS_VERBOSE_TEST"))))
    {
      mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_GNUPLOT_FULL);
      mps_output (s);
    }

  mps_context_free (s);

  if (passed)
    fprintf (stderr, "\rChecking \033[1m%-30s\033[0m [\033[32;1m  done  \033[0m]\n", pol_file + 9);
  else
    fprintf (stderr, "\rChecking \033[1m%-30s\033[0m [\033[31;1m failed \033[0m]\n", pol_file + 9);

  return passed;
}
예제 #4
0
/**
 * @brief This function tests the resolution of a polynomial file
 * referenced by <code>pol</code>.
 */
int
test_secsolve_on_pol_impl (test_pol * pol, mps_output_goal goal, mps_boolean jacobi_iterations)
{
  mpc_t root, ctmp;
  mps_boolean passed = true;
  int i, j, zero_roots = 0;
  char ch;
  rdpe_t eps;
  mps_polynomial * poly = NULL;

  /* Check the roots */
  FILE* result_stream = fopen (pol->res_file, "r");
  FILE* input_stream = fopen (pol->pol_file, "r");

  rdpe_set_2dl (eps, 1.0, -pol->out_digits);

  if (!result_stream)
    {
      error_test_message ("no results file found", pol->pol_file);
      return EXIT_FAILURE;
    }
  if (!input_stream)
    {
      error_test_message ("no polynomial file found", pol->pol_file);
      return EXIT_FAILURE;
    }

  /* Create a new empty mps_context */
  mps_context * s = mps_context_new ();

  if ((getenv ("MPS_VERBOSE_TEST") && strstr (pol->pol_file, getenv ("MPS_VERBOSE_TEST"))) || pol->DOLOG)
    mps_context_set_debug_level (s, MPS_DEBUG_TRACE);

  /* Load the polynomial that has been given to us */
  poly = mps_parse_stream (s, input_stream);
  mps_context_set_input_poly (s, poly);

  starting_test_message (pol->pol_file);

  mps_context_set_output_prec (s, pol->out_digits);
  mps_context_set_output_goal (s, goal);
  mps_context_set_jacobi_iterations (s, jacobi_iterations);

  /* Solve it */
  mps_context_select_algorithm (s, (pol->ga) ? MPS_ALGORITHM_SECULAR_GA : MPS_ALGORITHM_STANDARD_MPSOLVE);
  mps_mpsolve (s);

  mpc_init2 (root, mps_context_get_data_prec_max (s));
  mpc_init2 (ctmp, mps_context_get_data_prec_max (s));

  /* Test if roots are equal to the roots provided in the check */
  passed = true;

  rdpe_t * drad = rdpe_valloc (mps_context_get_degree (s));
  mpc_t * mroot = mpc_valloc (mps_context_get_degree (s));
  mpc_vinit2 (mroot, mps_context_get_degree (s), mps_context_get_data_prec_max (s));

  mps_context_get_roots_m (s, &mroot, &drad);

  for (i = 0; i < mps_context_get_degree (s); i++)
    {
      rdpe_t rtmp;
      cdpe_t cdtmp;
      rdpe_t min_dist;
      int found_root = 0;
      rdpe_t exp_drad;

      while (isspace (ch = getc (result_stream)))
        ;
      ungetc (ch, result_stream);
      mpc_inp_str (root, result_stream, 10);

      if (mpc_eq_zero (root))
        {
          zero_roots++;

          /* We need to read it another time. This seems a bug in
           * mpc_inp_str, but I don't get why is necessary. */
          mpc_inp_str (root, result_stream, 10);
          continue;
        }

      mpc_sub (ctmp, root, mroot[0]);
      mpc_get_cdpe (cdtmp, ctmp);
      cdpe_mod (rtmp, cdtmp);
      rdpe_set (min_dist, rtmp);

      if (getenv ("MPS_VERBOSE_TEST") && (strstr (pol->pol_file, getenv ("MPS_VERBOSE_TEST"))))
        {
          printf ("Read root_%d = ", i);
          mpc_out_str_2 (stdout, 10, mps_context_get_data_prec_max (s), mps_context_get_data_prec_max (s),
                         root);
          printf ("\n");
        }

      for (j = 1; j < mps_context_get_degree (s); j++)
        {
          mpc_sub (ctmp, root, mroot[j]);
          mpc_get_cdpe (cdtmp, ctmp);
          cdpe_mod (rtmp, cdtmp);

          if (rdpe_le (rtmp, min_dist))
            {
              rdpe_set (min_dist, rtmp);
              found_root = j;
            }
        }

      mpc_get_cdpe (cdtmp, mroot[found_root]);
      cdpe_mod (rtmp, cdtmp);
      rdpe_mul_eq (rtmp, eps);
      rdpe_set (exp_drad, rtmp);
      rdpe_div_eq_d (min_dist, 1 + 4.0 * DBL_EPSILON);

      if ((!rdpe_le (min_dist, drad[found_root]) && !rdpe_gt (drad[found_root], exp_drad)) && !mps_context_get_over_max (s))
        {
          passed = false;

          if (getenv ("MPS_VERBOSE_TEST") && (strstr (pol->pol_file, getenv ("MPS_VERBOSE_TEST"))))
            {
              printf ("Failing on root %d, with min_dist = ", found_root);
              rdpe_out_str (stdout, min_dist);
              printf ("\ndrad_%d", found_root);
              rdpe_out_str (stdout, drad[found_root]);
              printf ("\n");
              printf ("Approximation_%d = ", found_root);
              mpc_out_str_2 (stdout, 10, -rdpe_Esp (drad[found_root]), -rdpe_Esp (drad[found_root]), mroot[found_root]);
              printf ("\n");
            }
        }
    }

  if (zero_roots != mps_context_get_zero_roots (s))
    passed = false;

  if ((getenv ("MPS_VERBOSE_TEST") && strstr (pol->pol_file, getenv ("MPS_VERBOSE_TEST"))) || pol->DOLOG)
    {
      /* mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_GNUPLOT_FULL); */
      mps_output (s);
    }

  fclose (result_stream);

  mpc_clear (ctmp);
  mpc_clear (root);
  mpc_vclear (mroot, mps_context_get_degree (s));

  free (mroot);
  free (drad);

  mps_polynomial_free (s, poly);
  mps_context_free (s);

  if (passed)
    success_test_message (pol->pol_file);
  else
    failed_test_message (pol->pol_file);

  if (getenv ("MPS_VERBOSE_TEST"))
    fail_unless (passed == true,
                 "Computed results are not exact to the required "
                 "precision.\n" "\n" " Dumping test configuration: \n"
                 "   => Polynomial file: %s;\n" "   => Required digits: %d\n"
                 "   => Gemignani's approach: %s;\n"
                 "   => Starting phase: %s;\n", pol->pol_file, pol->out_digits,
                 mps_boolean_to_string (pol->ga),
                 (pol->phase == float_phase) ? "float_phase" : "dpe_phase");
  else
    fail_unless (passed == true,
                 "Computed results are not exact to the required precision");

  return passed;
}
예제 #5
0
static void
check_file (const char* file_name)
{
  FILE *fp;

  int tmp;
  int base;
  int inex_re;
  int inex_im;
  mpc_t expected, got;
  mpc_rnd_t rnd = MPC_RNDNN;
  int inex = 0, expected_inex;
  size_t expected_size, size;
  known_signs_t ks = {1, 1};

  fp = open_data_file (file_name);

  mpc_init2 (expected, 53);
  mpc_init2 (got, 53);

  /* read data file */
  line_number = 1;
  nextchar = getc (fp);
  skip_whitespace_comments (fp);

  while (nextchar != EOF)
    {
      /* 1. read a line of data: expected result, base, rounding mode */
      read_ternary (fp, &inex_re);
      read_ternary (fp, &inex_im);
      read_mpc (fp, expected, &ks);
      if (inex_re == TERNARY_ERROR || inex_im == TERNARY_ERROR)
         expected_inex = -1;
      else
         expected_inex = MPC_INEX (inex_re, inex_im);
      read_int (fp, &tmp, "size");
      expected_size = (size_t)tmp;
      read_int (fp, &base, "base");
      read_mpc_rounding_mode (fp, &rnd);

      /* 2. read string at the same precision as the expected result */
      while (nextchar != '"')
        nextchar = getc (fp);
      mpfr_set_prec (MPC_RE (got), MPC_PREC_RE (expected));
      mpfr_set_prec (MPC_IM (got), MPC_PREC_IM (expected));
      inex = mpc_inp_str (got, fp, &size, base, rnd);

      /* 3. compare this result with the expected one */
      if (inex != expected_inex || !same_mpc_value (got, expected, ks)
          || size != expected_size)
        {
          printf ("mpc_inp_str failed (line %lu) with rounding mode %s\n",
                  line_number, rnd_mode[rnd]);
          if (inex != expected_inex)
            printf("     got inexact value: %d\nexpected inexact value: %d\n",
                   inex, expected_inex);
          if (size !=  expected_size)
            printf ("     got size: %lu\nexpected size: %lu\n     ",
                    (unsigned long int) size, (unsigned long int) expected_size);
          printf ("    ");
          OUT (got);
          OUT (expected);

          exit (1);
        }

      while ((nextchar = getc (fp)) != '"');
      nextchar = getc (fp);

      skip_whitespace_comments (fp);
    }

  mpc_clear (expected);
  mpc_clear (got);
  close_data_file (fp);
}