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)
17 #define OPT_SPLIT (BV_OPT_LAST+2)
19 struct argp_option argp_options
[] = {
20 { "split", OPT_SPLIT
, "int" },
21 { "variables", OPT_VARS
, "int", 0,
22 "number of variables over which to maximize" },
23 { "verbose", 'V', 0, 0, },
33 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
35 struct options
*options
= (struct options
*) state
->input
;
47 options
->nvar
= atoi(arg
);
50 options
->split
= atoi(arg
);
53 return ARGP_ERR_UNKNOWN
;
59 #define ALLOC(type) (type*)malloc(sizeof(type))
60 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
62 enum token_type
{ TOKEN_UNKNOWN
= 256, TOKEN_VALUE
, TOKEN_IDENT
, TOKEN_GE
};
76 static struct token
*token_new(int line
, int col
)
78 struct token
*tok
= ALLOC(struct token
);
84 void token_free(struct token
*tok
)
86 if (tok
->type
== TOKEN_VALUE
)
87 value_clear(tok
->u
.v
);
88 else if (tok
->type
== TOKEN_IDENT
)
104 struct token
*tokens
[5];
108 static struct stream
* stream_new(FILE *file
)
111 struct stream
*s
= ALLOC(struct stream
);
114 s
->buffer
= (char*)malloc(s
->size
);
120 for (i
= 0; i
< 5; ++i
)
126 static void stream_free(struct stream
*s
)
129 assert(s
->n_token
== 0);
133 static int stream_getc(struct stream
*s
)
152 static void stream_ungetc(struct stream
*s
, int c
)
158 static void stream_push_char(struct stream
*s
, int c
)
160 if (s
->len
>= s
->size
) {
161 s
->size
= (3*s
->size
)/2;
162 s
->buffer
= (char*)realloc(s
->buffer
, s
->size
);
164 s
->buffer
[s
->len
++] = c
;
167 static struct token
*stream_push_token(struct stream
*s
, struct token
*tok
)
169 assert(s
->n_token
< 5);
170 s
->tokens
[s
->n_token
++] = tok
;
173 static struct token
*stream_next_token(struct stream
*s
)
180 return s
->tokens
[--s
->n_token
];
185 while ((c
= stream_getc(s
)) != -1 && isspace(c
))
207 tok
= token_new(line
, col
);
208 tok
->type
= (token_type
)c
;
211 if (c
== '-' || isdigit(c
)) {
212 tok
= token_new(line
, col
);
213 tok
->type
= TOKEN_VALUE
;
214 value_init(tok
->u
.v
);
215 stream_push_char(s
, c
);
216 while ((c
= stream_getc(s
)) != -1 && isdigit(c
))
217 stream_push_char(s
, c
);
220 if (s
->len
== 1 && s
->buffer
[0] == '-')
221 value_set_si(tok
->u
.v
, -1);
223 stream_push_char(s
, '\0');
224 mpz_set_str(tok
->u
.v
, s
->buffer
, 0);
229 tok
= token_new(line
, col
);
230 tok
->type
= TOKEN_IDENT
;
231 stream_push_char(s
, c
);
232 while ((c
= stream_getc(s
)) != -1 && isalpha(c
))
233 stream_push_char(s
, c
);
236 stream_push_char(s
, '\0');
237 tok
->u
.s
= strdup(s
->buffer
);
241 if ((c
= stream_getc(s
)) == '=') {
242 tok
= token_new(line
, col
);
243 tok
->type
= TOKEN_GE
;
250 tok
= token_new(line
, col
);
251 tok
->type
= TOKEN_UNKNOWN
;
255 void stream_error(struct stream
*s
, struct token
*tok
, char *msg
)
257 int line
= tok
? tok
->line
: s
->line
;
258 int col
= tok
? tok
->col
: s
->col
;
259 fprintf(stderr
, "syntax error (%d, %d): %s\n", line
, col
, msg
);
262 fprintf(stderr
, "got '%c'\n", tok
->type
);
269 struct parameter
*next
;
272 struct parameter
*parameter_new(char *name
, int pos
, struct parameter
*next
)
274 struct parameter
*p
= ALLOC(struct parameter
);
275 p
->name
= strdup(name
);
281 static int parameter_pos(struct parameter
**p
, char *s
)
283 int pos
= *p
? (*p
)->pos
+1 : 0;
286 for (q
= *p
; q
; q
= q
->next
) {
287 if (strcmp(q
->name
, s
) == 0)
293 *p
= parameter_new(s
, pos
, *p
);
297 static int optional_power(struct stream
*s
)
301 tok
= stream_next_token(s
);
304 if (tok
->type
!= '^') {
305 stream_push_token(s
, tok
);
309 tok
= stream_next_token(s
);
310 if (!tok
|| tok
->type
!= TOKEN_VALUE
) {
311 stream_error(s
, tok
, "expecting exponent");
313 stream_push_token(s
, tok
);
316 pow
= VALUE_TO_INT(tok
->u
.v
);
321 static evalue
*evalue_read_factor(struct stream
*s
, struct parameter
**p
);
322 static evalue
*evalue_read_term(struct stream
*s
, struct parameter
**p
);
324 static evalue
*create_fract_like(struct stream
*s
, evalue
*arg
, enode_type type
,
325 struct parameter
**p
)
329 pow
= optional_power(s
);
333 e
->x
.p
= new_enode(type
, pow
+2, -1);
334 value_clear(e
->x
.p
->arr
[0].d
);
335 e
->x
.p
->arr
[0] = *arg
;
337 evalue_set_si(&e
->x
.p
->arr
[1+pow
], 1, 1);
339 evalue_set_si(&e
->x
.p
->arr
[1+pow
], 0, 1);
344 static evalue
*read_fract(struct stream
*s
, struct token
*tok
, struct parameter
**p
)
348 tok
= stream_next_token(s
);
350 assert(tok
->type
== '{');
353 arg
= evalue_read_term(s
, p
);
354 tok
= stream_next_token(s
);
355 if (!tok
|| tok
->type
!= '}') {
356 stream_error(s
, tok
, "expecting \"}\"");
358 stream_push_token(s
, tok
);
362 return create_fract_like(s
, arg
, fractional
, p
);
365 static evalue
*read_periodic(struct stream
*s
, struct parameter
**p
)
373 tok
= stream_next_token(s
);
374 assert(tok
&& tok
->type
== '[');
378 list
= (evalue
**)malloc(len
* sizeof(evalue
*));
382 evalue
*e
= evalue_read_term(s
, p
);
384 stream_error(s
, NULL
, "missing argument or list element");
389 list
= (evalue
**)realloc(list
, len
* sizeof(evalue
*));
393 tok
= stream_next_token(s
);
395 stream_error(s
, NULL
, "unexpected EOF");
398 if (tok
->type
!= ',')
403 if (tok
->type
!= ']') {
404 stream_error(s
, tok
, "expecting \"]\"");
405 stream_push_token(s
, tok
);
411 tok
= stream_next_token(s
);
412 if (tok
->type
== '_') {
415 tok
= stream_next_token(s
);
416 if (!tok
|| tok
->type
!= TOKEN_IDENT
) {
417 stream_error(s
, tok
, "expecting identifier");
419 stream_push_token(s
, tok
);
424 pos
= parameter_pos(p
, tok
->u
.s
);
426 e
->x
.p
= new_enode(periodic
, n
, pos
+1);
428 value_clear(e
->x
.p
->arr
[n
].d
);
429 e
->x
.p
->arr
[n
] = *list
[n
];
433 stream_push_token(s
, tok
);
434 e
= create_fract_like(s
, list
[0], flooring
, p
);
437 stream_error(s
, tok
, "unexpected token");
438 stream_push_token(s
, tok
);
443 free_evalue_refs(list
[n
]);
450 static evalue
*evalue_read_factor(struct stream
*s
, struct parameter
**p
)
455 tok
= stream_next_token(s
);
459 if (tok
->type
== '(') {
461 e
= evalue_read_term(s
, p
);
462 tok
= stream_next_token(s
);
463 if (!tok
|| tok
->type
!= ')') {
464 stream_error(s
, tok
, "expecting \")\"");
466 stream_push_token(s
, tok
);
469 } else if (tok
->type
== TOKEN_VALUE
) {
472 value_set_si(e
->d
, 1);
474 value_assign(e
->x
.n
, tok
->u
.v
);
476 tok
= stream_next_token(s
);
477 if (tok
&& tok
->type
== '/') {
479 tok
= stream_next_token(s
);
480 if (!tok
|| tok
->type
!= TOKEN_VALUE
) {
481 stream_error(s
, tok
, "expecting denominator");
483 stream_push_token(s
, tok
);
486 value_assign(e
->d
, tok
->u
.v
);
489 stream_push_token(s
, tok
);
490 } else if (tok
->type
== TOKEN_IDENT
) {
491 int pos
= parameter_pos(p
, tok
->u
.s
);
492 int pow
= optional_power(s
);
496 e
->x
.p
= new_enode(polynomial
, pow
+1, pos
+1);
497 evalue_set_si(&e
->x
.p
->arr
[pow
], 1, 1);
499 evalue_set_si(&e
->x
.p
->arr
[pow
], 0, 1);
500 } else if (tok
->type
== '[') {
501 stream_push_token(s
, tok
);
502 e
= read_periodic(s
, p
);
503 } else if (tok
->type
== '{') {
504 stream_push_token(s
, tok
);
505 e
= read_fract(s
, tok
, p
);
508 tok
= stream_next_token(s
);
509 if (tok
&& tok
->type
== '*') {
512 e2
= evalue_read_factor(s
, p
);
514 stream_error(s
, NULL
, "unexpected EOF");
518 free_evalue_refs(e2
);
521 stream_push_token(s
, tok
);
526 static evalue
*evalue_read_term(struct stream
*s
, struct parameter
**p
)
531 e
= evalue_read_factor(s
, p
);
535 tok
= stream_next_token(s
);
539 if (tok
->type
== '+') {
542 e2
= evalue_read_term(s
, p
);
544 stream_error(s
, NULL
, "unexpected EOF");
548 free_evalue_refs(e2
);
551 stream_push_token(s
, tok
);
559 struct constraint
*next
;
562 static struct constraint
*constraint_new()
564 struct constraint
*c
= ALLOC(struct constraint
);
566 c
->v
= Vector_Alloc(16);
571 static void constraint_free(struct constraint
*c
)
574 struct constraint
*next
= c
->next
;
581 static void constraint_extend(struct constraint
*c
, int pos
)
584 if (pos
< c
->v
->Size
)
587 v
= Vector_Alloc((3*c
->v
->Size
)/2);
588 Vector_Copy(c
->v
->p
, v
->p
, c
->v
->Size
);
593 static int evalue_read_constraint(struct stream
*s
, struct parameter
**p
,
594 struct constraint
**constraints
)
597 struct constraint
*c
= NULL
;
599 while ((tok
= stream_next_token(s
))) {
602 if (tok
->type
== '+')
604 else if (tok
->type
== TOKEN_IDENT
) {
606 c
= constraint_new();
607 pos
= parameter_pos(p
, tok
->u
.s
);
608 constraint_extend(c
, 1+pos
);
609 value_set_si(c
->v
->p
[1+pos
], 1);
611 } else if (tok
->type
== TOKEN_VALUE
) {
613 c
= constraint_new();
614 tok2
= stream_next_token(s
);
615 if (tok2
&& tok2
->type
== TOKEN_IDENT
) {
616 pos
= parameter_pos(p
, tok2
->u
.s
);
617 constraint_extend(c
, 1+pos
);
618 value_assign(c
->v
->p
[1+pos
], tok
->u
.v
);
623 stream_push_token(s
, tok2
);
624 value_assign(c
->v
->p
[0], tok
->u
.v
);
627 } else if (tok
->type
== TOKEN_GE
|| tok
->type
== '=') {
628 int type
= tok
->type
== TOKEN_GE
;
630 tok
= stream_next_token(s
);
631 if (!tok
|| tok
->type
!= TOKEN_VALUE
|| value_notzero_p(tok
->u
.v
)) {
632 stream_error(s
, tok
, "expecting \"0\"");
634 stream_push_token(s
, tok
);
638 c
->next
= *constraints
;
645 stream_push_token(s
, tok
);
647 stream_error(s
, tok
, "unexpected token");
656 static struct constraint
*evalue_read_domain(struct stream
*s
, struct parameter
**p
,
659 struct constraint
*constraints
= NULL
;
663 tok
= stream_next_token(s
);
666 stream_push_token(s
, tok
);
669 while (evalue_read_constraint(s
, p
, &constraints
)) {
670 tok
= stream_next_token(s
);
672 stream_push_token(s
, tok
);
673 /* empty line separates domain from evalue */
674 if (tok
->line
> line
+1)
683 struct constraint
*constraints
;
685 struct section
*next
;
688 static char **extract_parameters(struct parameter
*p
, unsigned *nparam
)
693 *nparam
= p
? p
->pos
+1 : 0;
694 params
= ALLOCN(char *, *nparam
);
695 for (i
= 0; i
< *nparam
; ++i
) {
696 struct parameter
*next
= p
->next
;
697 params
[p
->pos
] = p
->name
;
704 static evalue
*evalue_read_partition(struct stream
*s
, char ***ppp
,
705 unsigned *nparam
, unsigned MaxRays
)
707 struct section
*part
= NULL
;
708 struct constraint
*constraints
;
711 struct parameter
*p
= NULL
;
713 while ((constraints
= evalue_read_domain(s
, &p
, MaxRays
))) {
714 evalue
*e
= evalue_read_term(s
, &p
);
716 stream_error(s
, NULL
, "missing evalue");
719 struct section
*sect
= ALLOC(struct section
);
720 sect
->constraints
= constraints
;
732 *ppp
= extract_parameters(p
, nparam
);
735 e
->x
.p
= new_enode(partition
, 2*m
, *nparam
);
737 for (j
= 0; j
< m
; ++j
) {
739 struct section
*next
= part
->next
;
740 struct constraint
*c
;
741 constraints
= part
->constraints
;
742 for (n
= 0, c
= constraints
; c
; ++n
, c
= c
->next
)
744 M
= Matrix_Alloc(n
, 1+*nparam
+1);
746 struct constraint
*next
= constraints
->next
;
747 Vector_Copy(constraints
->v
->p
+1, M
->p
[n
]+1, *nparam
);
748 if (constraints
->type
)
749 value_set_si(M
->p
[n
][0], 1);
750 value_assign(M
->p
[n
][1+*nparam
], constraints
->v
->p
[0]);
751 constraints
->next
= NULL
;
752 constraint_free(constraints
);
755 D
= Constraints2Polyhedron(M
, MaxRays
);
757 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[2*j
], D
);
758 value_clear(e
->x
.p
->arr
[2*j
+1].d
);
759 e
->x
.p
->arr
[2*j
+1] = *part
->e
;
768 static evalue
*evalue_read(FILE *in
, char ***ppp
, unsigned *nparam
,
771 struct stream
*s
= stream_new(in
);
774 struct parameter
*p
= NULL
;
776 if (!(tok
= stream_next_token(s
)))
779 if (tok
->type
== TOKEN_VALUE
) {
780 struct token
*tok2
= stream_next_token(s
);
782 stream_push_token(s
, tok2
);
783 stream_push_token(s
, tok
);
784 if (tok2
&& (tok2
->type
== TOKEN_IDENT
|| tok2
->type
== TOKEN_GE
))
785 e
= evalue_read_partition(s
, ppp
, nparam
, MaxRays
);
787 e
= evalue_read_term(s
, &p
);
788 *ppp
= extract_parameters(p
, nparam
);
790 } else if (tok
->type
== TOKEN_IDENT
) {
791 stream_push_token(s
, tok
);
792 e
= evalue_read_partition(s
, ppp
, nparam
, MaxRays
);
798 int main(int argc
, char **argv
)
801 char **all_vars
= NULL
;
803 struct options options
;
804 struct barvinok_options
*bv_options
= barvinok_options_new_with_defaults();
805 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, 0 };
807 piecewise_lst
*pl
= NULL
;
809 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
811 EP
= evalue_read(stdin
, &all_vars
, &ntotal
, bv_options
->MaxRays
);
815 evalue_split_periods(EP
, options
.split
, bv_options
->MaxRays
);
818 print_evalue(stderr
, EP
, all_vars
);
820 if (options
.nvar
== -1)
821 options
.nvar
= ntotal
;
823 U
= Universe_Polyhedron(ntotal
- options
.nvar
);
826 params
= constructParameterVector(all_vars
+options
.nvar
, ntotal
-options
.nvar
);
828 pl
= evalue_bernstein_coefficients(NULL
, EP
, U
, params
, bv_options
);
834 free_evalue_refs(EP
);
838 Free_ParamNames(all_vars
, ntotal
);
839 barvinok_options_free(bv_options
);