short_rat::print: correctly print out terms with a zero coefficient
[barvinok.git] / options.c
blob563c4c03e2ba6e23ed6c28d7034361afc8a44af5
1 #include <assert.h>
2 #include <unistd.h>
3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
5 #include "argp.h"
6 #include "config.h"
8 #define MAXRAYS (POL_NO_DUAL | POL_INTEGER)
10 #define ALLOC(type) (type*)malloc(sizeof(type))
12 void barvinok_stats_clear(struct barvinok_stats *stats)
14 memset(stats, 0, sizeof(*stats));
17 void barvinok_stats_print(struct barvinok_stats *stats, FILE *out)
19 fprintf(out, "Base cones: %d\n", stats->base_cones);
20 if (stats->volume_simplices)
21 fprintf(out, "Volume simplices: %d\n", stats->volume_simplices);
22 if (stats->topcom_chambers) {
23 fprintf(out, "TOPCOM empty chambers: %d\n",
24 stats->topcom_empty_chambers);
25 fprintf(out, "TOPCOM chambers: %d\n", stats->topcom_chambers);
26 fprintf(out, "TOPCOM distinct chambers: %d\n",
27 stats->topcom_distinct_chambers);
31 struct barvinok_options *barvinok_options_new_with_defaults()
33 struct barvinok_options *options = ALLOC(struct barvinok_options);
34 if (!options)
35 return NULL;
37 options->stats = ALLOC(struct barvinok_stats);
38 if (!options->stats) {
39 free(options);
40 return NULL;
43 barvinok_stats_clear(options->stats);
45 options->LLL_a = 1;
46 options->LLL_b = 1;
48 options->MaxRays = MAXRAYS;
50 #ifdef USE_INCREMENTAL_BF
51 options->incremental_specialization = 2;
52 #elif defined USE_INCREMENTAL_DF
53 options->incremental_specialization = 1;
54 #else
55 options->incremental_specialization = 0;
56 #endif
57 options->max_index = 1;
58 options->primal = 0;
59 #ifdef USE_MODULO
60 options->lookup_table = 0;
61 #else
62 options->lookup_table = 1;
63 #endif
64 options->count_sample_infinite = 1;
65 options->try_Delaunay_triangulation = 0;
67 options->chambers = BV_CHAMBERS_POLYLIB;
69 options->polynomial_approximation = BV_APPROX_SIGN_NONE;
70 options->approximation_method = BV_APPROX_NONE;
71 options->scale_flags = 0;
72 options->volume_triangulate = BV_VOL_VERTEX;
74 #ifdef HAVE_LIBGLPK
75 options->gbr_lp_solver = BV_GBR_GLPK;
76 #elif defined HAVE_LIBCDDGMP
77 options->gbr_lp_solver = BV_GBR_CDD;
78 #else
79 options->gbr_lp_solver = BV_GBR_PIP;
80 #endif
82 #ifdef HAVE_LIBGLPK
83 options->lp_solver = BV_LP_GLPK;
84 #elif defined HAVE_LIBCDDGMP
85 options->lp_solver = BV_LP_CDD;
86 #else
87 options->lp_solver = BV_LP_POLYLIB;
88 #endif
90 options->summation = BV_SUM_BARVINOK;
92 options->bernstein_optimize = BV_BERNSTEIN_NONE;
94 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
96 return options;
99 void barvinok_options_free(struct barvinok_options *options)
101 free(options->stats);
102 free(options);
105 enum {
106 SCALE_FAST,
107 SCALE_SLOW,
108 SCALE_NARROW,
109 SCALE_NARROW2,
110 SCALE_CHAMBER,
113 const char *scale_opts[] = {
114 "fast",
115 "slow",
116 "narrow",
117 "narrow2",
118 "chamber",
119 NULL
122 static struct argp_option approx_argp_options[] = {
123 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
124 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernoulli", 0,
125 "method to use in polynomial approximation [default: drop]" },
126 { "scale-options", BV_OPT_SCALE,
127 "fast|slow,narrow|narrow2,chamber", 0 },
128 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
129 "type of triangulation to perform in volume computation [default: vertex]" },
130 { 0 }
133 static struct argp_option barvinok_argp_options[] = {
134 { "index", BV_OPT_MAXINDEX, "int", 0,
135 "maximal index of simple cones in decomposition" },
136 { "primal", BV_OPT_PRIMAL, 0, 0 },
137 { "table", BV_OPT_TABLE, 0, 0 },
138 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
139 #ifdef POINTS2TRIANGS_PATH
140 { "chamber-decomposition", BV_OPT_CHAMBERS, "polylib|topcom", 0,
141 "tool to use for chamber decomposition [default: polylib]" },
142 #endif
143 { "gbr", BV_OPT_GBR,
144 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
145 "cdd|glpk|pip|pip-dual",
146 #elif defined(HAVE_LIBGLPK)
147 "glpk|pip|pip-dual",
148 #elif defined(HAVE_LIBCDDGMP)
149 "cdd|pip|pip-dual",
150 #else
151 "pip|pip-dual",
152 #endif
153 0, "lp solver to use for basis reduction "
154 #ifdef HAVE_LIBGLPK
155 "[default: glpk]"
156 #elif defined HAVE_LIBCDDGMP
157 "[default: cdd]"
158 #else
159 "[default: pip]"
160 #endif
162 { "lp", BV_OPT_LP,
163 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
164 "cdd|cddf|glpk|polylib",
165 #elif defined(HAVE_LIBGLPK)
166 "glpk|polylib",
167 #elif defined(HAVE_LIBCDDGMP)
168 "cdd|cddf|polylib",
169 #else
170 "polylib",
171 #endif
172 0, "lp solver to use "
173 #if defined(HAVE_LIBGLPK)
174 "[default: glpk]",
175 #elif defined(HAVE_LIBCDDGMP)
176 "[default: cdd]",
177 #else
178 "[default: polylib]",
179 #endif
181 { "summation", BV_OPT_SUM, "barvinok|bernoulli|euler", 0,
182 "[default: barvinok]" },
183 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
184 "[default: factors]" },
185 { "recurse", BV_OPT_RECURSE, "",
186 OPTION_ALIAS | OPTION_HIDDEN },
187 { "version", 'V', 0, 0 },
188 { 0 }
191 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
193 struct barvinok_options *options = state->input;
194 char *subopt;
196 switch (key) {
197 case BV_OPT_POLAPPROX:
198 if (!arg) {
199 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
200 if (options->approximation_method == BV_APPROX_NONE)
201 options->approximation_method = BV_APPROX_SCALE;
202 } else {
203 if (!strcmp(arg, "lower"))
204 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
205 else if (!strcmp(arg, "upper"))
206 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
207 if (options->approximation_method == BV_APPROX_NONE)
208 options->approximation_method = BV_APPROX_DROP;
210 break;
211 case BV_OPT_APPROX:
212 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE)
213 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
214 if (!strcmp(arg, "scale"))
215 options->approximation_method = BV_APPROX_SCALE;
216 else if (!strcmp(arg, "drop"))
217 options->approximation_method = BV_APPROX_DROP;
218 else if (!strcmp(arg, "volume"))
219 options->approximation_method = BV_APPROX_VOLUME;
220 else if (!strcmp(arg, "bernoulli"))
221 options->approximation_method = BV_APPROX_BERNOULLI;
222 else
223 argp_error(state, "unknown value for --approximation-method option");
224 break;
225 case BV_OPT_SCALE:
226 options->approximation_method = BV_APPROX_SCALE;
227 while (*arg != '\0')
228 switch (getsubopt(&arg, scale_opts, &subopt)) {
229 case SCALE_FAST:
230 options->scale_flags |= BV_APPROX_SCALE_FAST;
231 break;
232 case SCALE_SLOW:
233 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
234 break;
235 case SCALE_NARROW:
236 options->scale_flags |= BV_APPROX_SCALE_NARROW;
237 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
238 break;
239 case SCALE_NARROW2:
240 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
241 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
242 break;
243 case SCALE_CHAMBER:
244 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
245 break;
246 default:
247 argp_error(state, "unknown suboption '%s'\n", subopt);
249 break;
250 case BV_OPT_VOL:
251 if (!strcmp(arg, "lift"))
252 options->volume_triangulate = BV_VOL_LIFT;
253 else if (!strcmp(arg, "vertex"))
254 options->volume_triangulate = BV_VOL_VERTEX;
255 else if (!strcmp(arg, "barycenter"))
256 options->volume_triangulate = BV_VOL_BARYCENTER;
257 break;
258 case ARGP_KEY_END:
259 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
260 options->approximation_method != BV_APPROX_NONE) {
261 fprintf(stderr,
262 "no polynomial approximation selected; reseting approximation method\n");
263 options->approximation_method = BV_APPROX_NONE;
265 break;
266 default:
267 return ARGP_ERR_UNKNOWN;
269 return 0;
272 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
274 struct barvinok_options *options = state->input;
275 char *subopt;
277 switch (key) {
278 case ARGP_KEY_INIT:
279 state->child_inputs[0] = options;
280 break;
281 case 'V':
282 printf(barvinok_version());
283 exit(0);
284 case BV_OPT_SPECIALIZATION:
285 if (!strcmp(arg, "bf"))
286 options->incremental_specialization = BV_SPECIALIZATION_BF;
287 else if (!strcmp(arg, "df"))
288 options->incremental_specialization = BV_SPECIALIZATION_DF;
289 else if (!strcmp(arg, "random"))
290 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
291 else if (!strcmp(arg, "todd"))
292 options->incremental_specialization = BV_SPECIALIZATION_TODD;
293 break;
294 case BV_OPT_PRIMAL:
295 options->primal = 1;
296 break;
297 case BV_OPT_TABLE:
298 options->lookup_table = 1;
299 break;
300 case BV_OPT_CHAMBERS:
301 if (!strcmp(arg, "polylib"))
302 options->chambers = BV_CHAMBERS_POLYLIB;
303 if (!strcmp(arg, "topcom"))
304 options->chambers = BV_CHAMBERS_TOPCOM;
305 break;
306 case BV_OPT_GBR:
307 if (!strcmp(arg, "cdd"))
308 options->gbr_lp_solver = BV_GBR_CDD;
309 if (!strcmp(arg, "glpk"))
310 options->gbr_lp_solver = BV_GBR_GLPK;
311 if (!strcmp(arg, "pip"))
312 options->gbr_lp_solver = BV_GBR_PIP;
313 if (!strcmp(arg, "pip-dual"))
314 options->gbr_lp_solver = BV_GBR_PIP_DUAL;
315 break;
316 case BV_OPT_LP:
317 if (!strcmp(arg, "cdd"))
318 options->lp_solver = BV_LP_CDD;
319 if (!strcmp(arg, "cddf"))
320 options->lp_solver = BV_LP_CDDF;
321 if (!strcmp(arg, "glpk"))
322 options->lp_solver = BV_LP_GLPK;
323 if (!strcmp(arg, "polylib"))
324 options->lp_solver = BV_LP_POLYLIB;
325 break;
326 case BV_OPT_MAXINDEX:
327 options->max_index = strtoul(arg, NULL, 0);
328 break;
329 case BV_OPT_SUM:
330 if (!strcmp(arg, "barvinok"))
331 options->summation = BV_SUM_BARVINOK;
332 if (!strcmp(arg, "euler"))
333 options->summation = BV_SUM_EULER;
334 if (!strcmp(arg, "bernoulli"))
335 options->summation = BV_SUM_BERNOULLI;
336 break;
337 case BV_OPT_RECURSE:
338 if (!strcmp(arg, "none"))
339 options->bernstein_recurse = 0;
340 else if (!strcmp(arg, "factors"))
341 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
342 else if (!strcmp(arg, "intervals"))
343 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
344 else if (!strcmp(arg, "full"))
345 options->bernstein_recurse =
346 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
347 break;
348 default:
349 return ARGP_ERR_UNKNOWN;
351 return 0;
354 static struct argp approx_argp = {
355 approx_argp_options, approx_parse_opt, 0, 0
358 static struct argp_child barvinok_children[] = {
359 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
360 { 0 }
363 struct argp barvinok_argp = {
364 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children