polylib 5.22.8
ehrhart_union.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5
6#include <polylib/polylib.h>
7
8
9void Union_Read(Polyhedron **P, Polyhedron **C, const char ***param_name)
10{
11 Matrix *pm;
12 Polyhedron *ptmp;
13 unsigned NbRows, NbColumns;
14 char s[1025], param[1025];
15 int i, j, c, f;
16
17 *P = NULL;
18 pm = Matrix_Read();
19 f=1;
20 while( f )
21 {
22 do
23 {
24 if( fgets(s, 1024, stdin) == 0 )
25 f=0;
26 }
27 while ( (*s=='#' || *s=='\n') && f );
28
29 if( f && sscanf(s, "%d %d", &NbRows, &NbColumns)==2 )
30 {
31 /* gets old pm and add it to the union */
32 if( *P )
33 if( pm->NbColumns != ((*P)->Dimension)+2 )
34 {
35 fprintf( stderr,
36 "Polyhedra must be in the same dimension space !\n");
37 exit(0);
38 }
39 ptmp = Constraints2Polyhedron(pm, 200);
40 ptmp->next = *P;
41 *P = ptmp;
42 Matrix_Free(pm);
43
44 /* reads the new pm */
45 pm = Matrix_Alloc(NbRows, NbColumns);
47 }
48 else
49 break;
50 }
51
52 /* Context : last read pm */
53 *C = Constraints2Polyhedron(pm, 200);
54 Matrix_Free(pm);
55
56
57 if( f )
58 {
59 char **pp = (char **)malloc((*C)->Dimension*sizeof(char *));
60 *param_name = (const char **)pp;
61 /* read the parameter names */
62 c = 0;
63 for( i=0 ; i<(*C)->Dimension ; ++i )
64 {
65 j=0;
66 for( ; ; ++c )
67 {
68 if( s[c]==' ' || s[c]=='\n' || s[c]==0 ) {
69 if( j==0 )
70 continue;
71 else
72 break;
73 }
74 param[j++] = s[c];
75 }
76
77 /* else, no parameters (use default) */
78 if( j==0 )
79 break;
80 param[j] = 0;
81 pp[i] = (char *)malloc(j);
82 strcpy(pp[i], param);
83 }
84 if( i != (*C)->Dimension )
85 {
86 free( *param_name );
87 *param_name = Read_ParamNames(NULL,(*C)->Dimension);
88 }
89 }
90 else
91 *param_name = Read_ParamNames(NULL,(*C)->Dimension);
92
93}
94
95void recurse(Polyhedron *C, const char **param_name, Enumeration *e,
96 Value *pmin, Value *pmax, Value *p, int l )
97{
98 Value z, *tmp; int k;
99 value_init( z );
100 if( l == C->Dimension )
101 {
102 fprintf(stdout,"EP( ");
103 value_print(stdout,VALUE_FMT,p[0]);
104 for(k=1;k<C->Dimension;++k) {
105 fprintf(stdout,",");
106 value_print(stdout,VALUE_FMT,p[k]);
107 }
108 fprintf(stdout," ) = ");
109 value_print(stdout,VALUE_FMT,*(tmp=compute_poly(e,p)));
110 value_clear( *tmp );
111 free(tmp);
112 fprintf(stdout,"\n");
113 }
114 else
115 {
116 for( value_assign( z, pmin[l]) ; value_le(z,pmax[l]) ; value_increment(z,z) )
117 {
118 value_assign( p[l], z );
119 recurse ( C, param_name, e, pmin, pmax, p, l+1 );
120 }
121 }
122
123}
124
125
126
127int main( int argc, char **argv)
128{
129 Polyhedron *P, *C;
130 const char **param_name;
131 Enumeration *e, *en;
132 Value *pmin, *pmax, *p; int i, k; char str[256], *s;
133
134 if( argc != 1 )
135 {
136 fprintf( stderr, " Usage : %s [< file]\n", argv[0] );
137 return( -1 );
138 }
139
140 Union_Read( &P, &C, &param_name );
141
142 e = Domain_Enumerate( P, C, 200, param_name );
143
144 for( en=e ; en ; en=en->next )
145 {
146 Print_Domain(stdout,en->ValidityDomain, param_name);
147 print_evalue(stdout,&en->EP, param_name);
148 printf( "\n-----------------------------------\n" );
149 }
150
151 if( isatty(0) && C->Dimension != 0)
152 { /* no tty input or no polyhedron -> no evaluation. */
153 printf("Evaluation of the Ehrhart polynomial :\n");
154 pmin = (Value *)malloc(sizeof(Value) * (C->Dimension));
155 pmax = (Value *)malloc(sizeof(Value) * (C->Dimension));
156 p = (Value *)malloc(sizeof(Value) * (C->Dimension));
157 for(i=0;i<C->Dimension;i++)
158 {
159 value_init(pmin[i]);
160 value_init(pmax[i]);
161 value_init(p[i]);
162 }
163 FOREVER {
164 fflush(stdin);
165 printf("Enter %d parameters (or intervals, comma separated) : ",C->Dimension);
166 for(k=0;k<C->Dimension;++k)
167 {
168 scanf("%s",str);
169 if( (s=strpbrk(str,",")) )
170 { *s = 0;
171 value_read(pmin[k],str);
172 value_read(pmax[k],(s+1));
173 }
174 else
175 { value_read(pmin[k],str);
176 value_assign(pmax[k],pmin[k]);
177 }
178 }
179
180 recurse( C, param_name, e, pmin, pmax, p, 0 );
181
182 }
183 }
184
185 return( 0 );
186}
187
#define value_le(v1, v2)
Definition: arithmetique.h:507
#define value_assign(v1, v2)
Definition: arithmetique.h:482
#define value_increment(ref, val)
Definition: arithmetique.h:540
#define value_read(val, str)
Definition: arithmetique.h:486
#define value_clear(val)
Definition: arithmetique.h:485
#define value_print(Dst, fmt, val)
Definition: arithmetique.h:487
#define value_init(val)
Definition: arithmetique.h:481
void print_evalue(FILE *DST, evalue *e, const char **pname)
Definition: ehrhart.c:169
int main(int argc, char **argv)
void Union_Read(Polyhedron **P, Polyhedron **C, const char ***param_name)
Definition: ehrhart_union.c:9
void recurse(Polyhedron *C, const char **param_name, Enumeration *e, Value *pmin, Value *pmax, Value *p, int l)
Definition: ehrhart_union.c:95
Value * compute_poly(Enumeration *en, Value *list_args)
Definition: eval_ehrhart.c:137
Enumeration * Domain_Enumerate(Polyhedron *D, Polyhedron *C, unsigned MAXRAYS, const char **pn)
Definition: ext_ehrhart.c:671
Matrix * Matrix_Alloc(unsigned NbRows, unsigned NbColumns)
Definition: matrix.c:24
Matrix * Matrix_Read_Input(Matrix *Mat)
Definition: matrix.c:141
Matrix * Matrix_Read(void)
Definition: matrix.c:209
void Matrix_Free(Matrix *Mat)
Definition: matrix.c:71
const char ** Read_ParamNames(FILE *in, int m)
Definition: param.c:14
Polyhedron * Constraints2Polyhedron(Matrix *Constraints, unsigned NbMaxRays)
Given a matrix of constraints ('Constraints'), construct and return a polyhedron.
Definition: polyhedron.c:1905
void Print_Domain(FILE *DST, Polyhedron *D, const char **pname)
Definition: polyparam.c:1680
char s[128]
Definition: polytest.c:8
Polyhedron * ValidityDomain
Definition: types.h:191
evalue EP
Definition: types.h:192
struct _enumeration * next
Definition: types.h:193
Definition: types.h:75
unsigned NbColumns
Definition: types.h:76
unsigned Dimension
Definition: types.h:94
struct polyhedron * next
Definition: types.h:99
#define FOREVER
Definition: types.h:232