2 #include <bernstein/bernstein.h>
3 #include <barvinok/evalue.h>
4 #include <barvinok/options.h>
5 #include <barvinok/util.h>
6 #include <barvinok/bernstein.h>
12 using namespace GiNaC
;
13 using namespace bernstein
;
14 using namespace barvinok
;
16 #define OPT_VARS (BV_OPT_LAST+1)
18 struct argp_option argp_options
[] = {
19 { "variables", OPT_VARS
, "int", 0,
20 "number of variables over which to maximize" },
21 { "verbose", 'V', 0, 0, },
30 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
32 struct options
*options
= (struct options
*) state
->input
;
43 options
->nvar
= atoi(arg
);
46 return ARGP_ERR_UNKNOWN
;
52 #define ALLOC(type) (type*)malloc(sizeof(type))
53 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
55 enum token_type
{ TOKEN_UNKNOWN
= 256, TOKEN_VALUE
, TOKEN_IDENT
, TOKEN_GE
};
69 static struct token
*token_new(int line
, int col
)
71 struct token
*tok
= ALLOC(struct token
);
77 void token_free(struct token
*tok
)
79 if (tok
->type
== TOKEN_VALUE
)
80 value_clear(tok
->u
.v
);
81 else if (tok
->type
== TOKEN_IDENT
)
97 struct token
*tokens
[5];
101 static struct stream
* stream_new(FILE *file
)
104 struct stream
*s
= ALLOC(struct stream
);
107 s
->buffer
= (char*)malloc(s
->size
);
113 for (i
= 0; i
< 5; ++i
)
119 static void stream_free(struct stream
*s
)
122 assert(s
->n_token
== 0);
126 static int stream_getc(struct stream
*s
)
145 static void stream_ungetc(struct stream
*s
, int c
)
151 static void stream_push_char(struct stream
*s
, int c
)
153 if (s
->len
>= s
->size
) {
154 s
->size
= (3*s
->size
)/2;
155 s
->buffer
= (char*)realloc(s
->buffer
, s
->size
);
157 s
->buffer
[s
->len
++] = c
;
160 static struct token
*stream_push_token(struct stream
*s
, struct token
*tok
)
162 assert(s
->n_token
< 5);
163 s
->tokens
[s
->n_token
++] = tok
;
166 static struct token
*stream_next_token(struct stream
*s
)
173 return s
->tokens
[--s
->n_token
];
178 while ((c
= stream_getc(s
)) != -1 && isspace(c
))
200 tok
= token_new(line
, col
);
201 tok
->type
= (token_type
)c
;
204 if (c
== '-' || isdigit(c
)) {
205 tok
= token_new(line
, col
);
206 tok
->type
= TOKEN_VALUE
;
207 value_init(tok
->u
.v
);
208 stream_push_char(s
, c
);
209 while ((c
= stream_getc(s
)) != -1 && isdigit(c
))
210 stream_push_char(s
, c
);
213 if (s
->len
== 1 && s
->buffer
[0] == '-')
214 value_set_si(tok
->u
.v
, -1);
216 stream_push_char(s
, '\0');
217 mpz_set_str(tok
->u
.v
, s
->buffer
, 0);
222 tok
= token_new(line
, col
);
223 tok
->type
= TOKEN_IDENT
;
224 stream_push_char(s
, c
);
225 while ((c
= stream_getc(s
)) != -1 && isalpha(c
))
226 stream_push_char(s
, c
);
229 stream_push_char(s
, '\0');
230 tok
->u
.s
= strdup(s
->buffer
);
234 if ((c
= stream_getc(s
)) == '=') {
235 tok
= token_new(line
, col
);
236 tok
->type
= TOKEN_GE
;
243 tok
= token_new(line
, col
);
244 tok
->type
= TOKEN_UNKNOWN
;
248 void stream_error(struct stream
*s
, struct token
*tok
, char *msg
)
250 int line
= tok
? tok
->line
: s
->line
;
251 int col
= tok
? tok
->col
: s
->col
;
252 fprintf(stderr
, "syntax error (%d, %d): %s\n", line
, col
, msg
);
255 fprintf(stderr
, "got '%c'\n", tok
->type
);
262 struct parameter
*next
;
265 struct parameter
*parameter_new(char *name
, int pos
, struct parameter
*next
)
267 struct parameter
*p
= ALLOC(struct parameter
);
268 p
->name
= strdup(name
);
274 static int parameter_pos(struct parameter
**p
, char *s
)
276 int pos
= *p
? (*p
)->pos
+1 : 0;
279 for (q
= *p
; q
; q
= q
->next
) {
280 if (strcmp(q
->name
, s
) == 0)
286 *p
= parameter_new(s
, pos
, *p
);
290 static int optional_power(struct stream
*s
)
294 tok
= stream_next_token(s
);
297 if (tok
->type
!= '^') {
298 stream_push_token(s
, tok
);
302 tok
= stream_next_token(s
);
303 if (!tok
|| tok
->type
!= TOKEN_VALUE
) {
304 stream_error(s
, tok
, "expecting exponent");
306 stream_push_token(s
, tok
);
309 pow
= VALUE_TO_INT(tok
->u
.v
);
314 static evalue
*evalue_read_factor(struct stream
*s
, struct parameter
**p
);
315 static evalue
*evalue_read_term(struct stream
*s
, struct parameter
**p
);
317 static evalue
*create_fract_like(struct stream
*s
, evalue
*arg
, enode_type type
,
318 struct parameter
**p
)
322 pow
= optional_power(s
);
326 e
->x
.p
= new_enode(type
, pow
+2, -1);
327 value_clear(e
->x
.p
->arr
[0].d
);
328 e
->x
.p
->arr
[0] = *arg
;
330 evalue_set_si(&e
->x
.p
->arr
[1+pow
], 1, 1);
332 evalue_set_si(&e
->x
.p
->arr
[1+pow
], 0, 1);
337 static evalue
*read_fract(struct stream
*s
, struct token
*tok
, struct parameter
**p
)
341 tok
= stream_next_token(s
);
343 assert(tok
->type
== '{');
346 arg
= evalue_read_term(s
, p
);
347 tok
= stream_next_token(s
);
348 if (!tok
|| tok
->type
!= '}') {
349 stream_error(s
, tok
, "expecting \"}\"");
351 stream_push_token(s
, tok
);
355 return create_fract_like(s
, arg
, fractional
, p
);
358 static evalue
*read_periodic(struct stream
*s
, struct parameter
**p
)
366 tok
= stream_next_token(s
);
367 assert(tok
&& tok
->type
== '[');
371 list
= (evalue
**)malloc(len
* sizeof(evalue
*));
375 evalue
*e
= evalue_read_term(s
, p
);
377 stream_error(s
, NULL
, "missing argument or list element");
382 list
= (evalue
**)realloc(list
, len
* sizeof(evalue
*));
386 tok
= stream_next_token(s
);
388 stream_error(s
, NULL
, "unexpected EOF");
391 if (tok
->type
!= ',')
396 if (tok
->type
!= ']') {
397 stream_error(s
, tok
, "expecting \"]\"");
398 stream_push_token(s
, tok
);
404 tok
= stream_next_token(s
);
405 if (tok
->type
== '_') {
408 tok
= stream_next_token(s
);
409 if (!tok
|| tok
->type
!= TOKEN_IDENT
) {
410 stream_error(s
, tok
, "expecting identifier");
412 stream_push_token(s
, tok
);
417 pos
= parameter_pos(p
, tok
->u
.s
);
419 e
->x
.p
= new_enode(periodic
, n
, pos
+1);
421 value_clear(e
->x
.p
->arr
[n
].d
);
422 e
->x
.p
->arr
[n
] = *list
[n
];
426 stream_push_token(s
, tok
);
427 e
= create_fract_like(s
, list
[0], flooring
, p
);
430 stream_error(s
, tok
, "unexpected token");
431 stream_push_token(s
, tok
);
436 free_evalue_refs(list
[n
]);
443 static evalue
*evalue_read_factor(struct stream
*s
, struct parameter
**p
)
448 tok
= stream_next_token(s
);
452 if (tok
->type
== '(') {
454 e
= evalue_read_term(s
, p
);
455 tok
= stream_next_token(s
);
456 if (!tok
|| tok
->type
!= ')') {
457 stream_error(s
, tok
, "expecting \")\"");
459 stream_push_token(s
, tok
);
462 } else if (tok
->type
== TOKEN_VALUE
) {
465 value_set_si(e
->d
, 1);
467 value_assign(e
->x
.n
, tok
->u
.v
);
469 tok
= stream_next_token(s
);
470 if (tok
&& tok
->type
== '/') {
472 tok
= stream_next_token(s
);
473 if (!tok
|| tok
->type
!= TOKEN_VALUE
) {
474 stream_error(s
, tok
, "expecting denominator");
476 stream_push_token(s
, tok
);
479 value_assign(e
->d
, tok
->u
.v
);
482 stream_push_token(s
, tok
);
483 } else if (tok
->type
== TOKEN_IDENT
) {
484 int pos
= parameter_pos(p
, tok
->u
.s
);
485 int pow
= optional_power(s
);
489 e
->x
.p
= new_enode(polynomial
, pow
+1, pos
+1);
490 evalue_set_si(&e
->x
.p
->arr
[pow
], 1, 1);
492 evalue_set_si(&e
->x
.p
->arr
[pow
], 0, 1);
493 } else if (tok
->type
== '[') {
494 stream_push_token(s
, tok
);
495 e
= read_periodic(s
, p
);
496 } else if (tok
->type
== '{') {
497 stream_push_token(s
, tok
);
498 e
= read_fract(s
, tok
, p
);
501 tok
= stream_next_token(s
);
502 if (tok
&& tok
->type
== '*') {
505 e2
= evalue_read_factor(s
, p
);
507 stream_error(s
, NULL
, "unexpected EOF");
511 free_evalue_refs(e2
);
514 stream_push_token(s
, tok
);
519 static evalue
*evalue_read_term(struct stream
*s
, struct parameter
**p
)
524 e
= evalue_read_factor(s
, p
);
528 tok
= stream_next_token(s
);
532 if (tok
->type
== '+') {
535 e2
= evalue_read_term(s
, p
);
537 stream_error(s
, NULL
, "unexpected EOF");
541 free_evalue_refs(e2
);
544 stream_push_token(s
, tok
);
552 struct constraint
*next
;
555 static struct constraint
*constraint_new()
557 struct constraint
*c
= ALLOC(struct constraint
);
559 c
->v
= Vector_Alloc(16);
564 static void constraint_free(struct constraint
*c
)
567 struct constraint
*next
= c
->next
;
574 static void constraint_extend(struct constraint
*c
, int pos
)
577 if (pos
< c
->v
->Size
)
580 v
= Vector_Alloc((3*c
->v
->Size
)/2);
581 Vector_Copy(c
->v
->p
, v
->p
, c
->v
->Size
);
586 static int evalue_read_constraint(struct stream
*s
, struct parameter
**p
,
587 struct constraint
**constraints
)
590 struct constraint
*c
= NULL
;
592 while ((tok
= stream_next_token(s
))) {
595 if (tok
->type
== '+')
597 else if (tok
->type
== TOKEN_IDENT
) {
599 c
= constraint_new();
600 pos
= parameter_pos(p
, tok
->u
.s
);
601 constraint_extend(c
, 1+pos
);
602 value_set_si(c
->v
->p
[1+pos
], 1);
604 } else if (tok
->type
== TOKEN_VALUE
) {
606 c
= constraint_new();
607 tok2
= stream_next_token(s
);
608 if (tok2
&& tok2
->type
== TOKEN_IDENT
) {
609 pos
= parameter_pos(p
, tok2
->u
.s
);
610 constraint_extend(c
, 1+pos
);
611 value_assign(c
->v
->p
[1+pos
], tok
->u
.v
);
616 stream_push_token(s
, tok2
);
617 value_assign(c
->v
->p
[0], tok
->u
.v
);
620 } else if (tok
->type
== TOKEN_GE
|| tok
->type
== '=') {
621 int type
= tok
->type
== TOKEN_GE
;
623 tok
= stream_next_token(s
);
624 if (!tok
|| tok
->type
!= TOKEN_VALUE
|| value_notzero_p(tok
->u
.v
)) {
625 stream_error(s
, tok
, "expecting \"0\"");
627 stream_push_token(s
, tok
);
631 c
->next
= *constraints
;
638 stream_push_token(s
, tok
);
640 stream_error(s
, tok
, "unexpected token");
649 static struct constraint
*evalue_read_domain(struct stream
*s
, struct parameter
**p
,
652 struct constraint
*constraints
= NULL
;
656 tok
= stream_next_token(s
);
659 stream_push_token(s
, tok
);
662 while (evalue_read_constraint(s
, p
, &constraints
)) {
663 tok
= stream_next_token(s
);
665 stream_push_token(s
, tok
);
666 /* empty line separates domain from evalue */
667 if (tok
->line
> line
+1)
676 struct constraint
*constraints
;
678 struct section
*next
;
681 static char **extract_parameters(struct parameter
*p
, unsigned *nparam
)
686 *nparam
= p
? p
->pos
+1 : 0;
687 params
= ALLOCN(char *, *nparam
);
688 for (i
= 0; i
< *nparam
; ++i
) {
689 struct parameter
*next
= p
->next
;
690 params
[p
->pos
] = p
->name
;
697 static evalue
*evalue_read_partition(struct stream
*s
, char ***ppp
,
698 unsigned *nparam
, unsigned MaxRays
)
700 struct section
*part
= NULL
;
701 struct constraint
*constraints
;
704 struct parameter
*p
= NULL
;
706 while ((constraints
= evalue_read_domain(s
, &p
, MaxRays
))) {
707 evalue
*e
= evalue_read_term(s
, &p
);
709 stream_error(s
, NULL
, "missing evalue");
712 struct section
*sect
= ALLOC(struct section
);
713 sect
->constraints
= constraints
;
725 *ppp
= extract_parameters(p
, nparam
);
728 e
->x
.p
= new_enode(partition
, 2*m
, *nparam
);
730 for (j
= 0; j
< m
; ++j
) {
732 struct section
*next
= part
->next
;
733 struct constraint
*c
;
734 constraints
= part
->constraints
;
735 for (n
= 0, c
= constraints
; c
; ++n
, c
= c
->next
)
737 M
= Matrix_Alloc(n
, 1+*nparam
+1);
739 struct constraint
*next
= constraints
->next
;
740 Vector_Copy(constraints
->v
->p
+1, M
->p
[n
]+1, *nparam
);
741 if (constraints
->type
)
742 value_set_si(M
->p
[n
][0], 1);
743 value_assign(M
->p
[n
][1+*nparam
], constraints
->v
->p
[0]);
744 constraints
->next
= NULL
;
745 constraint_free(constraints
);
748 D
= Constraints2Polyhedron(M
, MaxRays
);
750 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[2*j
], D
);
751 value_clear(e
->x
.p
->arr
[2*j
+1].d
);
752 e
->x
.p
->arr
[2*j
+1] = *part
->e
;
761 static evalue
*evalue_read(FILE *in
, char ***ppp
, unsigned *nparam
,
764 struct stream
*s
= stream_new(in
);
767 struct parameter
*p
= NULL
;
769 if (!(tok
= stream_next_token(s
)))
772 if (tok
->type
== TOKEN_VALUE
) {
773 struct token
*tok2
= stream_next_token(s
);
775 stream_push_token(s
, tok2
);
776 stream_push_token(s
, tok
);
777 if (tok2
&& (tok2
->type
== TOKEN_IDENT
|| tok2
->type
== TOKEN_GE
))
778 e
= evalue_read_partition(s
, ppp
, nparam
, MaxRays
);
780 e
= evalue_read_term(s
, &p
);
781 *ppp
= extract_parameters(p
, nparam
);
783 } else if (tok
->type
== TOKEN_IDENT
) {
784 stream_push_token(s
, tok
);
785 e
= evalue_read_partition(s
, ppp
, nparam
, MaxRays
);
791 int main(int argc
, char **argv
)
794 char **all_vars
= NULL
;
796 struct options options
;
797 struct barvinok_options
*bv_options
= barvinok_options_new_with_defaults();
798 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, 0 };
800 piecewise_lst
*pl
= NULL
;
802 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
804 EP
= evalue_read(stdin
, &all_vars
, &ntotal
, bv_options
->MaxRays
);
808 print_evalue(stderr
, EP
, all_vars
);
810 if (options
.nvar
== -1)
811 options
.nvar
= ntotal
;
813 U
= Universe_Polyhedron(ntotal
- options
.nvar
);
816 params
= constructParameterVector(all_vars
+options
.nvar
, ntotal
-options
.nvar
);
818 pl
= evalue_bernstein_coefficients(NULL
, EP
, U
, params
, bv_options
);
824 free_evalue_refs(EP
);
828 Free_ParamNames(all_vars
, ntotal
);
829 barvinok_options_free(bv_options
);