-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcadenza.c
354 lines (287 loc) · 9.7 KB
/
cadenza.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
/*
* Cadenza
*
* Jonathan Hauenstein <[email protected]>
* Alan Liddell <[email protected]>
* Ian Haywood <[email protected]>
*
* cadenza.c: Main file for Cadenza
*/
#include "cadenza.h"
int main(int argc, char *argv[]) {
int num_var = 0, num_points = 0, num_sing = 0, tested = 0, succeeded = 0, failed = 0;
/* set termwidth for maximum string length */
termwidth = set_termwidth();
error_string = malloc(termwidth * sizeof(char));
/* polynomial system */
polynomial_system F;
/* vector v */
rational_complex_vector v_rational;
complex_vector v_float;
void *v = NULL;
/* initial vector t */
mpq_t *t_rational = NULL;
mpf_t *t_float = NULL;
void *t = NULL;
/* final vector t */
void *t_final = NULL;
/* initial vector x */
rational_complex_vector *x_rational = NULL;
complex_vector *x_float = NULL;
void *x = NULL;
/* final vector x */
void *x_final = NULL;
/* singularities array */
void *sing = NULL;
/* init random seed */
srand(time(NULL));
/* get command-line arguments before anything else happens */
getargs(argc, argv);
/* tell em who we are */
fputs("\n", stderr);
prog_info(stderr);
/* do this before checking filenames */
if (help_flag) {
usage();
exit(BH_EXIT_SUCCESS);
}
/* ensure filenames are properly defined */
if (strcmp(sysfile, "") == 0) {
print_error("You need to define a system file", stderr);
usage();
exit(BH_EXIT_BADFILE);
} else if (strcmp(pointsfile, "") == 0) {
print_error("You need to define a points file", stderr);
usage();
exit(BH_EXIT_BADFILE);
}
/* display the options the user has selected */
if (verbosity > BH_LACONIC) {
fputs("\n", stderr);
display_config(stderr);
}
/* set void and function pointers */
set_function_pointers();
if (arithmetic_type == BH_USE_FLOAT) {
mpf_set_default_prec(default_precision);
v = (void *) &v_float;
} else {
v = (void *) &v_rational;
}
read_system_file(&F, v);
num_var = F.numVariables;
num_points = read_points_file(&t, &x, num_var);
/* print out the system, vector and points */
if (verbosity > BH_CHATTY) {
fprint_input(stdout, &F, v, t, x, num_points);
}
initialize_output_files(&F, v, t, x, num_points);
test_system(&F, v, t, x, num_points, &t_final, &x_final, &sing, &tested, &succeeded, &failed, &num_sing);
/* print an output file only if all intervals certified continuous */
if (tested == succeeded)
fprint_solutions(t_final, x_final, succeeded + 1);
summarize(tested, succeeded, failed, num_sing);
/* clean up */
clear_polynomial_system(&F);
free_v(v);
free_t(t, num_points);
free_t(t_final, succeeded + 1);
free_x(x, num_points);
free_x(x_final, succeeded + 1);
free_x(sing, num_sing);
free(error_string);
exit(BH_EXIT_SUCCESS);
}
/*******************************************************
* parse command-line args and set flags and filenames *
*******************************************************/
void getargs(int argc, char *argv[]) {
/* define default values */
help_flag = 0;
verbosity = BH_UNSET;
arithmetic_type = BH_UNSET;
default_precision = BH_UNSET;
newton_tolerance = BH_UNSET;
subd_tolerance = BH_UNSET;
sort_order = BH_DESCENDING;
strcpy(sysfile, "");
strcpy(pointsfile, "");
strcpy(configfile, "");
int c = 0;
while (c != -1) {
static struct option long_options[] = {
/* These options set a flag */
{"laconic", no_argument, &verbosity, BH_LACONIC},
{"chatty", no_argument, &verbosity, BH_CHATTY},
{"verbose", no_argument, &verbosity, BH_VERBOSE},
{"loquacious", no_argument, &verbosity, BH_LOQUACIOUS},
{"help", no_argument, &help_flag, 1},
{"float", no_argument, &arithmetic_type, BH_USE_FLOAT},
{"rational", no_argument, &arithmetic_type, BH_USE_RATIONAL},
{"ascending", no_argument, &sort_order, BH_ASCENDING},
/* These options set a global */
{"points", required_argument, 0, 'p'},
{"system", required_argument, 0, 's'},
{"precision", required_argument, 0, 'm'},
{"newtons", required_argument, 0, 'n'},
{"subdivisions", required_argument, 0, 'd'},
{"config", required_argument, 0, 'c'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "ahvfqp:s:m:n:d:c:", long_options, &option_index);
if (c == -1) break;
switch (c) {
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0) break;
printf ("option %s", long_options[option_index].name);
if (optarg) printf (" with arg %s", optarg);
puts("\n");
break;
case 'a':
sort_order = BH_ASCENDING;
break;
case 'c':
strcpy(configfile, optarg);
break;
case 'f':
arithmetic_type = BH_USE_FLOAT;
break;
case 'h':
help_flag = 1;
break;
case 'm':
default_precision = atoi(optarg);
break;
case 'p':
strcpy(pointsfile, optarg);
break;
case 'q':
arithmetic_type = BH_USE_RATIONAL;
break;
case 's':
strcpy(sysfile, optarg);
break;
case 'n':
newton_tolerance = atoi(optarg);
break;
case 'd':
subd_tolerance = atoi(optarg);
break;
case 'v':
if (verbosity == BH_UNSET)
verbosity = BH_LACONIC;
verbosity++;
if (verbosity > BH_LOQUACIOUS)
verbosity = BH_LOQUACIOUS;
break;
}
}
if (strcmp(configfile, "") != 0)
read_config_file();
/* if after reading flags and config file, options are still unset */
if (verbosity == BH_UNSET)
verbosity = BH_LACONIC;
if (arithmetic_type == BH_UNSET)
arithmetic_type = BH_USE_RATIONAL;
if (default_precision == BH_UNSET)
default_precision = BH_PREC_MIN;
if (newton_tolerance == BH_UNSET)
newton_tolerance = BH_NEWT_TOLERANCE;
if (subd_tolerance == BH_UNSET)
subd_tolerance = BH_SUB_TOLERANCE;
/* set significant digits */
if (default_precision < 32)
sigdig = 15;
else
sigdig = default_precision / 3;
}
/**************************
* set the terminal width *
**************************/
int set_termwidth() {
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
return w.ws_col;
}
/**********************************************************************************
* set function pointers to use the proper arithmetic type with a minimum of fuss *
**********************************************************************************/
void set_function_pointers() {
/* floating point functions and data types */
if (arithmetic_type == BH_USE_FLOAT) {
read_system_file = &read_system_file_float;
read_points_file = &read_points_file_float;
fprint_input = &fprint_input_float;
test_system = &test_system_float;
fprint_solutions = &fprint_solutions_float;
}
/* rational functions */
else {
read_system_file = &read_system_file_rational;
read_points_file = &read_points_file_rational;
fprint_input = &fprint_input_rational;
test_system = &test_system_rational;
fprint_solutions = &fprint_solutions_rational;
}
}
/************************************
* free [rational_]complex_vector v *
************************************/
void free_v(void *v) {
if (arithmetic_type == BH_USE_FLOAT) {
complex_vector *v_float = (complex_vector *) v;
clear_vector(*v_float);
v_float = NULL;
v = NULL;
} else {
rational_complex_vector *v_rational = (rational_complex_vector *) v;
clear_rational_vector(*v_rational);
v_rational = NULL;
v = NULL;
}
}
/********************
* free mp[qf]_t *t *
********************/
void free_t(void *t, int num_points) {
int i;
if (arithmetic_type == BH_USE_FLOAT) {
mpf_t *t_float = (mpf_t *) t;
for (i = 0; i < num_points; i++)
mpf_clear(t_float[i]);
free(t_float);
t_float = NULL;
t = NULL;
} else {
mpq_t *t_rational = (mpq_t *) t;
for (i = 0; i < num_points; i++)
mpq_clear(t_rational[i]);
free(t_rational);
t_rational = NULL;
t = NULL;
}
}
/*************************************
* free [rational_]complex_vector *x *
*************************************/
void free_x(void *x, int num_points) {
int i;
if (arithmetic_type == BH_USE_FLOAT) {
complex_vector *x_float = (complex_vector *) x;
for (i = 0; i < num_points; i++)
clear_vector(x_float[i]);
free(x_float);
x_float = NULL;
x = NULL;
} else {
rational_complex_vector *x_rational = (rational_complex_vector *) x;
for (i = 0; i < num_points; i++)
clear_rational_vector(x_rational[i]);
free(x_rational);
x_rational = NULL;
x = NULL;
}
}