-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_float.c
577 lines (451 loc) · 19.5 KB
/
io_float.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
* Cadenza
*
* Jonathan Hauenstein <[email protected]>
* Alan Liddell <[email protected]>
* Ian Haywood <[email protected]>
*
* io_float.c: Process system and points file using floating-point arithmetic
*/
#include "cadenza.h"
/**********************************************
* function to compare mpf_t needed for qsort *
**********************************************/
int compare_mpf(const void *a, const void *b) {
const mpf_t *fa = (const mpf_t *) a;
const mpf_t *fb = (const mpf_t *) b;
if (sort_order == BH_ASCENDING)
return (int) mpf_cmp(*fa, *fb);
else
return (int) mpf_cmp(*fb, *fa);
}
/********************************************************************
* sort the t array from highest to lowest and adjust x accordingly *
********************************************************************/
void sort_points_float(mpf_t *t, complex_vector *x, int num_points) {
int i, j, k, *indices = malloc(num_points * sizeof(int));
mpf_t *t_cpy = malloc(num_points * sizeof(mpf_t));
complex_vector *x_cpy = malloc(num_points * sizeof(complex_vector));
/* make copies of t and x */
for (i = 0; i < num_points; i++) {
mpf_init(t_cpy[i]);
mpf_set(t_cpy[i], t[i]);
initialize_vector(x_cpy[i], x[i]->size);
copy_vector(x_cpy[i], x[i]);
}
/* sort t, leave t_cpy intact */
qsort((void *) t, (size_t) num_points, sizeof(mpf_t), &compare_mpf);
/* collect the indices of the unsorted t-values as they relate to the sorted ones */
for (i = 0; i < num_points; i++) {
for (j = 0; j < num_points; j++) {
if (mpf_cmp(t_cpy[j], t[i]) == 0) {
int found = 0;
for (k = 0; k < i; k++)
if (j == indices[k])
found = 1;
if (!found) {
indices[i] = j;
break;
}
}
}
}
/* make a `sorted' copy of x */
for (i = 0; i < num_points; i++)
copy_vector(x_cpy[i], x[indices[i]]);
/* copy the sorted copy back into x */
for (i = 0; i < num_points; i++)
copy_vector(x[i], x_cpy[i]);
/* clean up */
for (i = 0; i < num_points; i++) {
mpf_clear(t_cpy[i]);
clear_vector(x_cpy[i]);
}
free(indices);
indices = NULL;
free(t_cpy);
t_cpy = NULL;
free(x_cpy);
x_cpy = NULL;
}
/***********************************
* read the polynomial system file *
***********************************/
void read_system_file_float(polynomial_system *system, void *v) {
int i, res, num_var = 0, num_poly = 0, base = 10;
complex_vector *v_float = (complex_vector *) v;
/* sanity-check the system file */
errno = 0;
FILE *sysfh = fopen(sysfile, "r");
if (sysfh == NULL) {
snprintf(error_string, (size_t) termwidth, "Couldn't open system file `%s': %s", sysfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADFILE);
}
/* gather number of variables */
errno = 0;
res = fscanf(sysfh, "%d", &num_var);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': unexpected EOF", sysfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (res == 0) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", sysfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (errno == EILSEQ) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", sysfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADPARSE);
}
num_poly = num_var; /* square system */
system->numVariables = num_var;
system->numPolynomials = num_poly;
system->maximumDegree = 0;
system->isReal = 0;
mpq_init(system->norm_sqr);
system->numExponentials = 0;
system->polynomials = malloc(num_poly * sizeof(polynomial));
system->exponentials = NULL;
/* read in each polynomial piece by piece */
for (i = 0; i < num_poly; i++) {
system->polynomials[i] = parse_polynomial(sysfh, num_var);
if (system->polynomials[i].degree > system->maximumDegree)
system->maximumDegree = system->polynomials[i].degree;
}
initialize_vector(*v_float, num_var);
for (i = 0; i < num_var; i++) {
/* get (real & imag) parts for each v_i */
errno = 0;
res = mpf_inp_str((*v_float)->coord[i]->re, sysfh, base);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': unexpected EOF", sysfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (res == 0) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", sysfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (errno == EILSEQ) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", sysfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADPARSE);
}
errno = 0;
res = mpf_inp_str((*v_float)->coord[i]->im, sysfh, base);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': unexpected EOF", sysfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (res == 0) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", sysfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (errno == EILSEQ) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", sysfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADPARSE);
}
}
/* clean up the file */
errno = 0;
res = fclose(sysfh);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Couldn't close `%s': %s", sysfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
}
v = (void *) v_float;
}
/********************
* read points file *
********************/
int read_points_file_float(void **t, void **x, int num_var) {
int i, j, num_points, res, base = 10;
mpf_t *t_float;
complex_vector *x_float;
/* sanity-check the points file */
errno = 0;
FILE *pointsfh = fopen(pointsfile, "r");
if (pointsfh == NULL) {
snprintf(error_string, (size_t) termwidth, "Couldn't open points file `%s': %s", pointsfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADFILE);
}
/* get number of points */
errno = 0;
res = fscanf(pointsfh, "%d", &num_points);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': unexpected EOF", pointsfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (res == 0) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", pointsfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (errno == EILSEQ) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", pointsfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADPARSE);
}
/* check that we have enought points to test */
if (num_points < 2) {
char err_string[] = "You must define 2 or more points to test";
print_error(err_string, stderr);
exit(BH_EXIT_BADDEF);
}
t_float = malloc(num_points * sizeof(mpf_t));
x_float = malloc(num_points * sizeof(complex_vector));
for (i = 0; i < num_points; i++) {
/* read in each t */
mpf_init(t_float[i]);
initialize_vector(x_float[i], num_var);
errno = 0;
res = mpf_inp_str(t_float[i], pointsfh, base);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': unexpected EOF", pointsfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (res == 0) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': are you using rational input?", pointsfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (errno == EILSEQ) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", pointsfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADPARSE);
}
/* check if 0 < t < 1 */
if (mpf_cmp_ui(t_float[i], 0) < 0 || mpf_cmp_ui(t_float[i], 1) > 0) {
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "Value for t not between 0 and 1: %%.%dRe", sigdig);
mpfr_snprintf(error_string, (size_t) termwidth, msg, t_float[i]);
print_error(error_string, stderr);
exit(BH_EXIT_BADDEF);
}
/* get real and imag points */
for (j = 0; j < num_var; j++) {
errno = 0;
res = mpf_inp_str(x_float[i]->coord[j]->re, pointsfh, base);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': unexpected EOF", pointsfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (res == 0) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': are you using rational input?", pointsfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (errno == EILSEQ) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", pointsfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADPARSE);
}
errno = 0;
res = mpf_inp_str(x_float[i]->coord[j]->im, pointsfh, base);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': unexpected EOF", pointsfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (res == 0) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': are you using rational input?", pointsfile);
print_error(error_string, stderr);
exit(BH_EXIT_BADREAD);
} else if (errno == EILSEQ) {
snprintf(error_string, (size_t) termwidth, "Error reading `%s': %s", pointsfile, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADPARSE);
}
}
}
/* clean up the file */
errno = 0;
res = fclose(pointsfh);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Couldn't close `%s': %s", pointsfile, strerror(errno));
exit(BH_EXIT_BADREAD);
}
sort_points_float(t_float, x_float, num_points);
*x = (void *) x_float;
*t = (void *) t_float;
return num_points;
}
/**************************************************************
* print the file input back to stdout for debugging purposes *
**************************************************************/
void fprint_input_float(FILE *outfile, polynomial_system *system, void *v, void *t, void *x, int num_points) {
int i, num_var = system->numVariables;
complex_vector *v_float = (complex_vector *) v;
mpf_t *t_float = (mpf_t *) t;
complex_vector *x_float = (complex_vector *) x;
fputs("F:\n", outfile);
print_system(outfile, system);
fputs("\n", outfile);
fputs("v:\n", outfile);
fprintf(outfile, "[");
for (i = 0; i < num_var - 1; i++) {
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe + I * %%.%dRe, ", sigdig, sigdig);
mpfr_fprintf(outfile, msg, (*v_float)->coord[i]->re, (*v_float)->coord[i]->im);
}
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe + I * %%.%dRe]\n", sigdig, sigdig);
mpfr_fprintf(outfile, msg, (*v_float)->coord[i]->re, (*v_float)->coord[i]->im);
fputs("\n", outfile);
fputs("(t, x)\n", outfile);
for (i = 0; i < num_points; i++) {
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe, ", sigdig);
mpfr_fprintf(outfile, msg, t_float[i]);
print_points_float(outfile, x_float[i]);
}
}
/*********************************
* print a test point to outfile *
*********************************/
void print_points_float(FILE *outfile, complex_vector points) {
int i, num_var = points->size;
fprintf(outfile, "[");
for (i = 0; i < num_var - 1; i++) {
if (mpf_cmp_ui(points->coord[i]->im, 0) == 0) {
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe, ", sigdig);
mpfr_fprintf(outfile, msg, points->coord[i]->re);
}
else {
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe + I * %%.%dRe, ", sigdig, sigdig);
mpfr_fprintf(outfile, msg, points->coord[i]->re, points->coord[i]->im);
}
}
if (mpf_cmp_ui(points->coord[i]->im, 0) == 0) {
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe]\n", sigdig);
mpfr_fprintf(outfile, msg, points->coord[i]->re);
}
else {
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe + I * %%.%dRe]\n", sigdig, sigdig);
mpfr_fprintf(outfile, msg, points->coord[i]->re, points->coord[i]->im);
}
}
/*****************************************
* print interval information to outfile *
*****************************************/
void fprint_interval_float(FILE *outfile, mpf_t t_left, mpf_t t_right, complex_vector x_left, complex_vector x_right, mpf_t alpha_left, mpf_t alpha_right, mpf_t beta_left, mpf_t beta_right, mpf_t gamma_left, mpf_t gamma_right) {
int i;
char msg[BH_MAX_STRING];
for (i = 0; i < BH_TERMWIDTH; i++)
fprintf(outfile, "=");
fprintf(outfile, "\n");
snprintf(msg, (size_t) BH_MAX_STRING, "t interval: [%%.%dRe, %%.%dRe]\n", sigdig, sigdig);
mpfr_fprintf(outfile, msg, t_left, t_right);
mpfr_fprintf(outfile, "x_left: ");
print_points_float(outfile, x_left);
snprintf(msg, (size_t) BH_MAX_STRING, "alpha (x_left): %%.%dRe\n", sigdig);
mpfr_fprintf(outfile, msg, alpha_left);
snprintf(msg, (size_t) BH_MAX_STRING, "beta (x_left): %%.%dRe\n", sigdig);
mpfr_fprintf(outfile, msg, beta_left);
snprintf(msg, (size_t) BH_MAX_STRING, "gamma (x_left): %%.%dRe\n", sigdig);
mpfr_fprintf(outfile, msg, gamma_left);
mpfr_fprintf(outfile, "x_right: ");
print_points_float(outfile, x_right);
snprintf(msg, (size_t) BH_MAX_STRING, "alpha (x_right): %%.%dRe\n", sigdig);
mpfr_fprintf(outfile, msg, alpha_right);
snprintf(msg, (size_t) BH_MAX_STRING, "beta (x_right): %%.%dRe\n", sigdig);
mpfr_fprintf(outfile, msg, beta_right);
snprintf(msg, (size_t) BH_MAX_STRING, "gamma (x_right): %%.%dRe\n", sigdig);
mpfr_fprintf(outfile, msg, gamma_right);
}
/**************************************
* print continuous intervals to file *
**************************************/
void fprint_continuous_float(mpf_t t_left, mpf_t t_right, complex_vector x_left, complex_vector x_right, mpf_t alpha_left, mpf_t alpha_right, mpf_t beta_left, mpf_t beta_right, mpf_t gamma_left, mpf_t gamma_right) {
int res;
errno = 0;
FILE *fh = fopen(BH_FCONT, "a");
if (fh == NULL) {
snprintf(error_string, (size_t) termwidth, "Couldn't open output file `%s': %s", BH_FCONT, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADFILE);
}
fprint_interval_float(fh, t_left, t_right, x_left, x_right, alpha_left, alpha_right, beta_left, beta_right, gamma_left, gamma_right);
errno = 0;
res = fclose(fh);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Couldn't close `%s': %s", BH_FCONT, strerror(errno));
exit(BH_EXIT_BADREAD);
}
}
/*****************************************
* print discontinuous intervals to file *
*****************************************/
void fprint_discontinuous_float(mpf_t t_left, mpf_t t_right, complex_vector x_left, complex_vector x_right, mpf_t alpha_left, mpf_t alpha_right, mpf_t beta_left, mpf_t beta_right, mpf_t gamma_left, mpf_t gamma_right) {
int res;
errno = 0;
FILE *fh = fopen(BH_FDISCONT, "a");
if (fh == NULL) {
snprintf(error_string, (size_t) termwidth, "Couldn't open output file `%s': %s", BH_FDISCONT, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADFILE);
}
fprint_interval_float(fh, t_left, t_right, x_left, x_right, alpha_left, alpha_right, beta_left, beta_right, gamma_left, gamma_right);
errno = 0;
res = fclose(fh);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Couldn't close `%s': %s", BH_FDISCONT, strerror(errno));
exit(BH_EXIT_BADREAD);
}
}
/*********************************************
* print uncertain intervals to summary file *
*********************************************/
void fprint_uncertain_float(mpf_t t_left, mpf_t t_right, complex_vector x_left, complex_vector x_right, mpf_t alpha_left, mpf_t alpha_right, mpf_t beta_left, mpf_t beta_right, mpf_t gamma_left, mpf_t gamma_right) {
int res;
errno = 0;
FILE *fh = fopen(BH_FUNCERTIFIED, "a");
if (fh == NULL) {
snprintf(error_string, (size_t) termwidth, "Couldn't open output file `%s': %s", BH_FUNCERTIFIED, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADFILE);
}
fprintf(fh, "Uncertain of interval:\n");
fprint_interval_float(fh, t_left, t_right, x_left, x_right, alpha_left, alpha_right, beta_left, beta_right, gamma_left, gamma_right);
fputs("\n", fh);
errno = 0;
res = fclose(fh);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Couldn't close `%s': %s", BH_FUNCERTIFIED, strerror(errno));
exit(BH_EXIT_BADREAD);
}
}
/*****************************************************
* print solutions in input format to an output file *
*****************************************************/
void fprint_solutions_float(void *t, void *x, int num_points) {
int i, j, res;
mpf_t *t_float = (mpf_t *) t;
complex_vector *x_float = (complex_vector *) x;
errno = 0;
FILE *outfile = fopen(BH_FPTSOUT, "w");
if (outfile == NULL) {
snprintf(error_string, (size_t) termwidth, "Couldn't open output file `%s': %s", BH_FPTSOUT, strerror(errno));
print_error(error_string, stderr);
exit(BH_EXIT_BADFILE);
}
fprintf(outfile, "%d\n", num_points);
for (i = 0; i < num_points; i++) {
char msg[BH_MAX_STRING];
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe\n", sigdig);
mpfr_fprintf(outfile, msg, t_float[i]);
for (j = 0; j < x_float[i]->size; j++) {
snprintf(msg, (size_t) BH_MAX_STRING, "%%.%dRe\t%%.%dRe\n", sigdig, sigdig);
mpfr_fprintf(outfile, msg, x_float[i]->coord[j]->re, x_float[i]->coord[j]->im);
}
}
errno = 0;
res = fclose(outfile);
if (res == EOF) {
snprintf(error_string, (size_t) termwidth, "Couldn't close `%s': %s", BH_FPTSOUT, strerror(errno));
exit(BH_EXIT_BADREAD);
}
}