void cdpe_set_dl (cdpe_t c, double dr, long int lr, double di, long int li) /* c = dr*10^lr + I di*10^li */ { rdpe_set_dl (cdpe_Re (c), dr, lr); rdpe_set_dl (cdpe_Im (c), di, li); }
int cdpe_set_str (cdpe_t c, const char *s) /* set from string as (re , im) */ { if (sscanf (s, CDPE_INP_FMT, &rdpe_Mnt (cdpe_Re (c)), &rdpe_Esp (cdpe_Re (c)), &rdpe_Mnt (cdpe_Im (c)), &rdpe_Esp (cdpe_Im (c))) != 4) return 0; rdpe_set_dl (cdpe_Re (c), rdpe_Mnt (cdpe_Re (c)), rdpe_Esp (cdpe_Re (c))); rdpe_set_dl (cdpe_Im (c), rdpe_Mnt (cdpe_Im (c)), rdpe_Esp (cdpe_Im (c))); return 1; }
int cdpe_inp_str (cdpe_t c, FILE * f) /* input from file as (mant x exp , mant x exp) */ { double dr, di; long int lr, li; if (f == NULL) f = stdin; if (fscanf (f, CDPE_INP_FMT, &dr, &lr, &di, &li) != 4) return 0; rdpe_set_dl (cdpe_Re (c), dr, lr); rdpe_set_dl (cdpe_Im (c), di, li); return 1; }
int rdpe_set_str (rdpe_t e, const char *s) /* input from string as mant x exp) */ { if (sscanf (s, RDPE_INP_FMT, &rdpe_Mnt (e), &rdpe_Esp (e)) != 2) return 0; rdpe_set_dl (e, rdpe_Mnt (e), rdpe_Esp (e)); return 1; }
void rdpe_add (rdpe_t re, const rdpe_t e1, const rdpe_t e2) /* re = e1 + e2 */ { long delta; /* Check for overflows */ if (rdpe_Mnt (e1) > 0 && rdpe_Mnt (e2) > 0 && rdpe_Esp (e1) == LONG_MAX && rdpe_Esp (e2) == LONG_MAX) { rdpe_set (re, RDPE_MAX); return; } if (rdpe_Mnt (e1) < 0 && rdpe_Mnt (e2) < 0 && rdpe_Esp (e1) == LONG_MAX && rdpe_Esp (e2) == LONG_MAX) { rdpe_set_dl (re, -0.5, LONG_MAX); return; } if (rdpe_Mnt (e2) == 0.0) { rdpe_Move (re, e1); return; } if (rdpe_Mnt (e1) == 0.0) { rdpe_Move (re, e2); return; } delta = rdpe_Esp (e1) - rdpe_Esp (e2); if (delta > NBT) rdpe_Move (re, e1); else if (delta < -NBT) rdpe_Move (re, e2); else if (delta == 0) { rdpe_Mnt (re) = rdpe_Mnt (e1) + rdpe_Mnt (e2); rdpe_Esp (re) = rdpe_Esp (e1); rdpe_Norm (re); } else if (delta > 0) { rdpe_Mnt (re) = rdpe_Mnt (e1) + ldexp (rdpe_Mnt (e2), (int)-delta); rdpe_Esp (re) = rdpe_Esp (e1); rdpe_Norm (re); } else { /* delta < 0 */ rdpe_Mnt (re) = ldexp (rdpe_Mnt (e1), (int)delta) + rdpe_Mnt (e2); rdpe_Esp (re) = rdpe_Esp (e2); rdpe_Norm (re); } }
int rdpe_inp_sstr_flex (rdpe_t e, char *f) /* More flexible input for rdpe */ { double d; long int l = 0; if (sscanf (f, RDPE_INP_FMT, &d, &l) < 1) return 0; rdpe_set_dl (e, d, l); return 1; }
int rdpe_inp_str (rdpe_t e, FILE * f) /* input from file as mant x exp */ { double d; long int l; if (f == NULL) f = stdin; if (fscanf (f, RDPE_INP_FMT, &d, &l) != 2) return 0; rdpe_set_dl (e, d, l); return 1; }
int rdpe_inp_str_flex (rdpe_t e, FILE * f) /* More flexible input for rdpe */ { double d; long int l = 0; if (f == NULL) f = stdin; if (fscanf (f, RDPE_INP_FMT, &d, &l) < 1) return 0; rdpe_set_dl (e, d, l); return 1; }
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; }