3 #include <barvinok/barvinok.h>
4 #include <barvinok/options.h>
5 #include <barvinok/util.h>
8 #include "evalue_convert.h"
9 #include "evalue_read.h"
16 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
18 #define OPT_VARS (BV_OPT_LAST+1)
20 struct argp_option argp_options
[] = {
21 { "variables", OPT_VARS
, "list", 0,
22 "comma separated list of variables over which to sum" },
27 struct convert_options convert
;
28 struct verify_options verify
;
32 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
34 struct options
*options
= (struct options
*) state
->input
;
38 state
->child_inputs
[0] = &options
->convert
;
39 state
->child_inputs
[1] = &options
->verify
;
40 state
->child_inputs
[2] = options
->verify
.barvinok
;
41 options
->var_list
= NULL
;
44 options
->var_list
= strdup(arg
);
47 return ARGP_ERR_UNKNOWN
;
52 static int check_poly_sum(const struct check_poly_data
*data
,
54 const struct verify_options
*options
);
56 struct check_poly_sum_data
: public check_EP_data
{
59 check_poly_sum_data(const evalue
*EP
, evalue
*sum
) : sum(sum
) {
61 this->cp
.check
= check_poly_sum
;
65 static void sum(Polyhedron
*S
, int pos
, const check_poly_sum_data
*data
,
66 evalue
*s
, const struct verify_options
*options
)
69 evalue
*e
= evalue_eval(data
->EP
, data
->cp
.z
+1);
77 ok
= !(lower_upper_bounds(1+pos
, S
, data
->cp
.z
, &LB
, &UB
));
79 for (; value_le(LB
, UB
); value_increment(LB
, LB
)) {
80 value_assign(data
->cp
.z
[1+pos
], LB
);
81 sum(S
->next
, pos
+1, data
, s
, options
);
83 value_set_si(data
->cp
.z
[1+pos
], 0);
89 static evalue
*sum(const check_poly_sum_data
*data
,
90 const struct verify_options
*options
)
92 evalue
*s
= evalue_zero();
93 for (int i
= 0; i
< data
->n_S
; ++i
)
94 if (!emptyQ2(data
->S
[i
]))
95 sum(data
->S
[i
], 0, data
, s
, options
);
99 static int check_poly_sum(const struct check_poly_data
*data
,
100 int nparam
, Value
*z
,
101 const struct verify_options
*options
)
103 const check_poly_sum_data
*sum_data
;
104 sum_data
= (const check_poly_sum_data
*)data
;
109 e
= evalue_eval(sum_data
->sum
, z
);
110 s
= sum(sum_data
, options
);
114 check_poly_print(ok
, nparam
, z
, s
->x
.n
, s
->d
, e
->x
.n
, e
->d
,
115 "sum", "sum(EP)", "summation", options
);
123 static int verify(evalue
*EP
, evalue
*sum
, unsigned nvar
, unsigned nparam
,
124 struct verify_options
*options
)
126 check_poly_sum_data
data(EP
, sum
);
127 return !check_EP(&data
, nvar
, nparam
, options
);
130 int main(int argc
, char **argv
)
133 char **all_vars
= NULL
;
136 struct options options
;
137 struct barvinok_options
*bv_options
= barvinok_options_new_with_defaults();
138 static struct argp_child argp_children
[] = {
139 { &convert_argp
, 0, "input conversion", 1 },
140 { &verify_argp
, 0, "verification", 2 },
141 { &barvinok_argp
, 0, "barvinok options", 3 },
144 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, argp_children
};
147 options
.verify
.barvinok
= bv_options
;
148 set_program_name(argv
[0]);
149 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
151 EP
= evalue_read_from_file(stdin
, options
.var_list
, &all_vars
,
152 &nvar
, &nparam
, bv_options
->MaxRays
);
155 if (options
.verify
.verify
)
156 verify_options_set_range(&options
.verify
, nvar
+nparam
);
158 evalue_convert(EP
, &options
.convert
, bv_options
->verbose
, nparam
, all_vars
);
160 if (EVALUE_IS_ZERO(*EP
))
161 print_evalue(stdout
, EP
, all_vars
);
163 evalue
*sum
= barvinok_summate(EP
, nvar
, bv_options
);
165 if (options
.verify
.verify
)
166 result
= verify(EP
, sum
, nvar
, nparam
, &options
.verify
);
168 print_evalue(stdout
, sum
, all_vars
+nvar
);
174 if (options
.var_list
)
175 free(options
.var_list
);
176 Free_ParamNames(all_vars
, nvar
+nparam
);
177 barvinok_options_free(bv_options
);