Merge ssh://repo.or.cz/srv/git/polylib
[polylib.git] / applications / testehrhart.c
blobeb444cf0d26696598ef2d903265d04d819476b98
1 /***********************************************************************/
2 /* Ehrhart V4.20 */
3 /* copyright 1997, Doran Wilde */
4 /* copyright 1997-2000, Vincent Loechner */
5 /***********************************************************************/
6 /*
7 This file is part of PolyLib.
9 PolyLib is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 PolyLib is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with PolyLib. If not, see <http://www.gnu.org/licenses/>.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <assert.h>
30 #include <polylib/polylib.h>
31 #include <polylib/homogenization.h>
32 #include "config.h"
34 #define EP_EVALUATION
36 #ifndef HAVE_GETOPT_H
37 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
38 #else
39 #include <getopt.h>
40 struct option options[] = {
41 { "homogenized", no_argument, 0, 'h' },
42 { 0, 0, 0, 0 }
44 #endif
46 #define WS 0
48 /**
50 define this to print all constraints on the validity domains if not
51 defined, only new constraints (not in validity domain given by the
52 user) are printed
55 #define EPRINT_ALL_VALIDITY_CONSTRAINTS
57 /**
59 The following are mainly for debug purposes. You shouldn't need to
60 change anything for daily usage...
64 /** you may define each macro independently
65 <ol>
66 <li> #define EDEBUG minimal debug
67 <li> #define EDEBUG1 prints enumeration points
68 <li> #define EDEBUG11 prints number of points
69 <li> #define EDEBUG2 prints domains
70 <li> #define EDEBUG21 prints more domains
71 <li> #define EDEBUG3 prints systems of equations that are solved
72 <li> #define EDEBUG4 prints message for degree reduction
73 <li> #define EDEBUG5 prints result before simplification
74 <li> #define EDEBUG6 prints domains in Preprocess
75 <li> #define EDEBUG61 prints even more in Preprocess
76 <li> #define EDEBUG62 prints domains in Preprocess2
77 </ol>
80 /* #define EDEBUG */ /* minimal debug */
81 /* #define EDEBUG1 */ /* prints enumeration points */
82 /* #define EDEBUG11 */ /* prints number of points */
83 /* #define EDEBUG2 */ /* prints domains */
84 /* #define EDEBUG21 */ /* prints more domains */
85 /* #define EDEBUG3 */ /* prints systems of equations that are solved */
86 /* #define EDEBUG4 */ /* prints message for degree reduction */
87 /* #define EDEBUG5 */ /* prints result before simplification */
88 /* #define EDEBUG6 */ /* prints domains in Preprocess */
89 /* #define EDEBUG61 */ /* prints even more in Preprocess */
90 /* #define EDEBUG62 */ /* prints domains in Preprocess2 */
93 /**
95 Reduce the degree of resulting polynomials
98 #define REDUCE_DEGREE
100 /**
102 define this to print one warning message per domain overflow these
103 overflows should no longer happen since version 4.20
106 #define ALL_OVERFLOW_WARNINGS
110 EPRINT : print results while computing the ehrhart polynomial. this
111 is done by default if you build the executable ehrhart. (If EMAIN is
112 defined). Don't define EMAIN here, it is defined when necessary in
113 the makefile.
117 Notice: you may however define EPRINT without defining EMAIN, but in
118 this case, you have to initialize the global variable param_name by
119 calling Read_ParamNames before any call to ehrhart. This is NOT
120 recommanded, unless you know what you do. EPRINT causes more debug
121 messages to be printed.
124 /* #define EPRINT */
126 int main(int argc, char **argv)
128 int i;
129 char str[1024];
130 Matrix *C1, *P1;
131 Polyhedron *C, *P;
132 Enumeration *en;
133 const char **param_name;
134 int c, ind = 0;
135 int hom = 0;
137 #ifdef EP_EVALUATION
138 Value *p, *tmp;
139 int k;
140 #endif
142 while ((c = getopt_long(argc, argv, "h", options, &ind)) != -1) {
143 switch (c) {
144 case 'h':
145 hom = 1;
146 break;
150 P1 = Matrix_Read();
151 C1 = Matrix_Read();
152 if(C1->NbColumns < 2) {
153 fprintf( stderr, "Not enough parameters !\n" );
154 exit(0);
156 if (hom) {
157 Matrix *C2, *P2;
158 P2 = AddANullColumn(P1);
159 Matrix_Free(P1);
160 P1 = P2;
161 C2 = AddANullColumn(C1);
162 Matrix_Free(C1);
163 C1 = C2;
165 P = Constraints2Polyhedron(P1,WS);
166 C = Constraints2Polyhedron(C1,WS);
167 Matrix_Free(P1);
168 Matrix_Free(C1);
170 /* Read the name of the parameters */
171 param_name = Read_ParamNames(stdin,C->Dimension - hom);
172 if (hom) {
173 const char **param_name2;
174 param_name2 = (const char**)malloc(sizeof(char*) * (C->Dimension));
175 for (i = 0; i < C->Dimension - 1; i++)
176 param_name2[i] = param_name[i];
177 param_name2[C->Dimension-1] = "_H";
178 free(param_name);
179 param_name=param_name2;
182 en = Polyhedron_Enumerate(P,C,WS,param_name);
184 if (hom) {
185 Enumeration *en2;
187 printf("inhomogeneous form:\n");
189 dehomogenize_enumeration(en, C->Dimension, WS);
190 for (en2 = en; en2; en2 = en2->next) {
191 Print_Domain(stdout, en2->ValidityDomain, param_name);
192 print_evalue(stdout, &en2->EP, param_name);
196 #ifdef EP_EVALUATION
197 if( isatty(0) && C->Dimension != 0)
198 { /* no tty input or no polyhedron -> no evaluation. */
199 printf("Evaluation of the Ehrhart polynomial :\n");
200 p = (Value *)malloc(sizeof(Value) * (C->Dimension));
201 for(i=0;i<C->Dimension;i++)
202 value_init(p[i]);
203 FOREVER {
204 fflush(stdin);
205 printf("Enter %d parameters : ",C->Dimension);
206 for(k=0;k<C->Dimension;++k) {
207 scanf("%s",str);
208 value_read(p[k],str);
210 fprintf(stdout,"EP( ");
211 value_print(stdout,VALUE_FMT,p[0]);
212 for(k=1;k<C->Dimension;++k) {
213 fprintf(stdout,",");
214 value_print(stdout,VALUE_FMT,p[k]);
216 fprintf(stdout," ) = ");
217 value_print(stdout,VALUE_FMT,*(tmp=compute_poly(en,p)));
218 free(tmp);
219 fprintf(stdout,"\n");
222 #endif /* EP_EVALUATION */
224 Enumeration_Free(en);
225 Free_ParamNames(param_name, C->Dimension-hom);
226 Polyhedron_Free( P );
227 Polyhedron_Free( C );
229 return 0;