polylib 5.22.8
testCompressParms.c
Go to the documentation of this file.
1/**
2 * $Id: testCompressParms.c,v 1.4 2006/09/18 03:09:03 meister Exp $
3 *
4 * Test routines for kernel/compress_parms.c functions
5 * @author B. Meister, 3/2006
6 *
7 */
8
9#include <polylib/polylib.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14#ifdef dbg
15#undef dbg
16#endif
17#define dbg 1
18
19#define TEST(a) if ((isOk = (a))) { \
20 printf(#a" tested ok.\n"); \
21 } \
22 else { \
23 printf(#a" NOT OK\n"); \
24 }
25
26#define maxRays 200
27
31 unsigned int nbSamples);
32
33const char *origNames[] =
34 {"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
35
36int main(int argc, char ** argv) {
37 int isOk = 0;
38 Matrix * A, * B;
39 if (argc>1) {
40 printf("Warning: No arguments taken into account: testing"
41 "remove_parm_eqs().\n");
42 }
43
44 A = Matrix_Read();
45 B = Matrix_Read();
49 Matrix_Free(A);
50 Matrix_Free(B);
51 return (1-isOk);
52}
53
54
55/** extracts the equalities involving the parameters only, try to introduce
56 them back and compare the two polyhedra.
57 Reads a polyhedron and a context.
58 */
60 int isOk = 1;
61 Matrix * M, *C, *Cp, * Eqs, *M1, *C1;
62 Polyhedron *Pm, *Pc, *Pcp, *Peqs, *Pint;
63 unsigned int * elimParms;
64 printf("----- test_Constraints_Remove_parm_eqs() -----\n");
65 M1 = Matrix_Copy(A);
66 C1 = Matrix_Copy(B);
67
68 M = Matrix_Copy(M1);
69 C = Matrix_Copy(C1);
70
71 /* compute the combined polyhedron */
74 Pcp = align_context(Pc, Pm->Dimension, maxRays);
76 Pc = DomainIntersection(Pm, Pcp, maxRays);
78 Polyhedron_Free(Pcp);
79 Matrix_Free(M);
80 Matrix_Free(C);
81
82 /* extract the parm-equalities, expressed in the combined space */
83 Eqs = Constraints_Remove_parm_eqs(&M1, &C1, 1, &elimParms);
84
85 printf("Removed equalities: \n");
86 show_matrix(Eqs);
87 printf("Polyhedron without equalities involving only parameters: \n");
88 show_matrix(M1);
89 printf("Context without equalities: \n");
90 show_matrix(C1);
91
92 /* compute the supposedly-same polyhedron, using the extracted equalities */
95 Peqs = align_context(Pcp, Pm->Dimension, maxRays);
96 Polyhedron_Free(Pcp);
97 Pcp = DomainIntersection(Pm, Peqs, maxRays);
98 Polyhedron_Free(Peqs);
100 Peqs = Constraints2Polyhedron(Eqs, maxRays);
101 Matrix_Free(Eqs);
102 Matrix_Free(M1);
103 Matrix_Free(C1);
104 Pint = DomainIntersection(Pcp, Peqs, maxRays);
105 Polyhedron_Free(Pcp);
106 Polyhedron_Free(Peqs);
107
108 /* test their equality */
109 if (!PolyhedronIncludes(Pint, Pc)) {
110 isOk = 0;
111 }
112 else {
113 if (!PolyhedronIncludes(Pc, Pint)) {
114 isOk = 0;
115 }
116 }
117 Polyhedron_Free(Pc);
118 Polyhedron_Free(Pint);
119 return isOk;
120} /* test_Constraints_Remove_parm_eqs() */
121
122
123/** extracts the equalities holding on the parameters only, try to introduce
124 them back and compare the two polyhedra.
125 Reads a polyhedron and a context.
126 */
128 int isOk = 1;
129 Matrix * M, *C;
130 Polyhedron *Pm, *Pc, *Pcp, *Peqs, *Pint, *Pint1;
131 unsigned int * elimParms;
132 printf("----- test_Polyhedron_Remove_parm_eqs() -----\n");
133
134 M = Matrix_Copy(A);
135 C = Matrix_Copy(B);
136
137 /* compute the combined polyhedron */
140 Pcp = align_context(Pc, Pm->Dimension, maxRays);
141 Polyhedron_Free(Pc);
142 Pint1 = DomainIntersection(Pm, Pcp, maxRays);
143 Polyhedron_Free(Pm);
144 Polyhedron_Free(Pcp);
145 Matrix_Free(M);
146 Matrix_Free(C);
147
148 M = Matrix_Copy(A);
149 C = Matrix_Copy(B);
150 /* extract the parm-equalities, expressed in the combined space */
153 Matrix_Free(M);
154 Matrix_Free(C);
155 Peqs = Polyhedron_Remove_parm_eqs(&Pm, &Pc, 1, &elimParms, 200);
156
157 /* compute the supposedly-same polyhedron, using the extracted equalities */
158 Pcp = align_context(Pc, Pm->Dimension, maxRays);
159 Polyhedron_Free(Pc);
160 Pc = DomainIntersection(Pm, Pcp, maxRays);
161 Polyhedron_Free(Pm);
162 Polyhedron_Free(Pcp);
163
164 Pint = DomainIntersection(Pc, Peqs, maxRays);
165 Polyhedron_Free(Pc);
166 Polyhedron_Free(Peqs);
167
168 /* test their equality */
169 if (!PolyhedronIncludes(Pint, Pint1)) {
170 isOk = 0;
171 }
172 else {
173 if (!PolyhedronIncludes(Pint1, Pint)) {
174 isOk = 0;
175 }
176 }
177 Polyhedron_Free(Pint1);
178 Polyhedron_Free(Pint);
179 return isOk;
180} /* test_Polyhedron_remove_parm_eqs() */
181
182
183/**
184 * Eliminates certain parameters from a vector of values for parameters
185 * @param origParms the initial vector of values of parameters
186 * @param elimParms the list of parameters to be eliminated in the vector
187 * @param newParms the vector of values without the eliminated ones.
188 */
189void valuesWithoutElim(Matrix * origParms, unsigned int * elimParms,
190 Matrix ** newParms) {
191 unsigned int i, j=0;
192 if (*newParms==NULL) {
193 *newParms = Matrix_Alloc(1, origParms->NbColumns-elimParms[0]);
194 } /* else assume enough space is allocated */
195 if (elimParms[0] ==0) {
196 for (i=0; i< origParms->NbColumns; i++) {
197 value_assign((*newParms)->p[0][i], origParms->p[0][i]);
198 }
199 }
200 for (i=0; i< origParms->NbColumns; i++) {
201 if (i!=elimParms[j+1]) {
202 value_assign((*newParms)->p[0][i-j], origParms->p[0][i]);
203 }
204 else {
205 j++;
206 }
207 }
208}/* valuesWithoutElim */
209
210
211/**
212 * takes a list of parameter names, a list ofparameters to eliminate, and
213 * returns the list of parameters without the eliminated ones.
214 * @param parms the original parameter names
215 * @param nbParms the number of original parmaeters
216 * @param elimParms the array-list of indices of parameters to eliminate (first
217 * element set to the number of its elements)
218 * @param newParms the returned list of parm names. Allocated if set to NULL,
219 * reused if not.
220 * @return the number of names in the returned list.
221 */
222unsigned int namesWithoutElim(const char **parms, unsigned nbParms,
223 unsigned int * elimParms,
224 const char ***newParms)
225{
226 unsigned int i, j=0;
227 unsigned int newSize = nbParms -elimParms[0];
228 if (dbg) {
229 printf("Size of the new parm vector: %d\n", newSize);
230 }
231 if (*newParms==NULL) {
232 *newParms = malloc(newSize*sizeof(char *));
233 }
234 if (elimParms[0]==0) {
235 for (i=0; i< nbParms; i++) {
236 (*newParms)[i] = strdup(parms[i]);
237 }
238 return newSize;
239 }
240 for (i=0; i< nbParms; i++) {
241 if (i!=elimParms[j+1]) {
242 (*newParms)[i-j] = strdup(parms[i]);
243 }
244 else {
245 j++;
246 }
247 }
248 return newSize;
249}
250
251
252/**
253 * Tests Constraints_fullDimensionize by comparing the Ehrhart polynomials
254 * @param A the input set of constraints
255 * @param B the corresponding context
256 * @param the number of samples to generate for the test
257 * @return 1 if the Ehrhart polynomial had the same value for the
258 * full-dimensional and non-full-dimensional sets of constraints, for their
259 * corresponding sample parameters values.
260 */
262 unsigned int nbSamples) {
263 Matrix * Eqs= NULL, *ParmEqs=NULL, *VL=NULL;
264 unsigned int * elimVars=NULL, * elimParms=NULL;
265 Matrix * sample, * smallerSample=NULL;
266 Matrix * transfSample=NULL;
267 Matrix * parmVL=NULL;
268 unsigned int i, j, r, nbOrigParms, nbParms;
269 Value div, mod, *origVal=NULL, *fullVal=NULL;
270 Matrix * VLInv;
271 Polyhedron * P, *PC;
272 Matrix * M, *C;
273 Enumeration * origEP, * fullEP=NULL;
274 const char **fullNames = NULL;
275 int isOk = 1; /* holds the result */
276
277 /* compute the origial Ehrhart polynomial */
278 M = Matrix_Copy(A);
279 C = Matrix_Copy(B);
282 origEP = Polyhedron_Enumerate(P, PC, maxRays, origNames);
283 Matrix_Free(M);
284 Matrix_Free(C);
286 Polyhedron_Free(PC);
287
288 /* compute the full-dimensional polyhedron corresponding to A and its Ehrhart
289 polynomial */
290 M = Matrix_Copy(A);
291 C = Matrix_Copy(B);
292 nbOrigParms = B->NbColumns-2;
293 Constraints_fullDimensionize(&M, &C, &VL, &Eqs, &ParmEqs,
294 &elimVars, &elimParms, maxRays);
295 if ((Eqs->NbRows==0) && (ParmEqs->NbRows==0)) {
296 Matrix_Free(M);
297 Matrix_Free(C);
298 Matrix_Free(Eqs);
299 Matrix_Free(ParmEqs);
300 free(elimVars);
301 free(elimParms);
302 return 1;
303 }
304 nbParms = C->NbColumns-2;
307 namesWithoutElim(origNames, nbOrigParms, elimParms, &fullNames);
308 fullEP = Polyhedron_Enumerate(P, PC, maxRays, fullNames);
309 Matrix_Free(M);
310 Matrix_Free(C);
312 Polyhedron_Free(PC);
313
314 /* make a set of sample parameter values and compare the corresponding
315 Ehrhart polnomials */
316 sample = Matrix_Alloc(1,nbOrigParms);
317 transfSample = Matrix_Alloc(1, nbParms);
318 Lattice_extractSubLattice(VL, nbParms, &parmVL);
319 VLInv = Matrix_Alloc(parmVL->NbRows, parmVL->NbRows+1);
320 MatInverse(parmVL, VLInv);
321 if (dbg) {
322 show_matrix(parmVL);
323 show_matrix(VLInv);
324 }
325 srand(nbSamples);
326 value_init(mod);
327 value_init(div);
328 for (i = 0; i< nbSamples; i++) {
329 /* create a random sample */
330 for (j=0; j< nbOrigParms; j++) {
331 value_set_si(sample->p[0][j], rand()%100);
332 }
333 /* compute the corresponding value for the full-dimensional
334 constraints */
335 valuesWithoutElim(sample, elimParms, &smallerSample);
336 /* (N' i' 1)^T = VLinv.(N i 1)^T*/
337 for (r = 0; r < nbParms; r++) {
338 Inner_Product(&(VLInv->p[r][0]), smallerSample->p[0], nbParms,
339 &(transfSample->p[0][r]));
340 /* add the constant part */
341 value_addto(transfSample->p[0][r], transfSample->p[0][r],
342 VLInv->p[r][VLInv->NbColumns-2]);
343 value_pdivision(div, transfSample->p[0][r],
344 VLInv->p[r][VLInv->NbColumns-1]);
345 value_subtract(mod, transfSample->p[0][r], div);
346 /* if the parameters value does not belong to the validity lattice, the
347 Ehrhart polynomial is zero. */
348 if (!value_zero_p(mod)) {
349 fullEP = Enumeration_zero(nbParms, maxRays);
350 break;
351 }
352 }
353 /* compare the two forms of the Ehrhart polynomial.*/
354 if (origEP ==NULL) break; /* NULL has loose semantics for EPs */
355 origVal = compute_poly(origEP, sample->p[0]);
356 fullVal = compute_poly(fullEP, transfSample->p[0]);
357 if (!value_eq(*origVal, *fullVal)) {
358 isOk = 0;
359 printf("EPs don't match. \n Original value = ");
360 value_print(stdout, VALUE_FMT, *origVal);
361 printf("\n Original sample = [");
362 for (j=0; j<sample->NbColumns; j++) {
363 value_print(stdout, VALUE_FMT, sample->p[0][j]);
364 printf(" ");
365 }
366 printf("] \n EP = ");
367 if(origEP!=NULL) {
368 print_evalue(stdout, &(origEP->EP), origNames);
369 }
370 else {
371 printf("NULL");
372 }
373 printf(" \n Full-dimensional value = ");
374 value_print(stdout, P_VALUE_FMT, *fullVal);
375 printf("\n full-dimensional sample = [");
376 for (j=0; j<sample->NbColumns; j++) {
377 value_print(stdout, VALUE_FMT, transfSample->p[0][j]);
378 printf(" ");
379 }
380 printf("] \n EP = ");
381 if(origEP!=NULL) {
382 print_evalue(stdout, &(origEP->EP), fullNames);
383 }
384 else {
385 printf("NULL");
386 }
387 }
388 if (dbg) {
389 printf("\nOriginal value = ");
390 value_print(stdout, VALUE_FMT, *origVal);
391 printf("\nFull-dimensional value = ");
392 value_print(stdout, P_VALUE_FMT, *fullVal);
393 printf("\n");
394 }
395 value_clear(*origVal);
396 value_clear(*fullVal);
397 }
398 value_clear(mod);
399 value_clear(div);
400 Matrix_Free(sample);
401 Matrix_Free(smallerSample);
402 Matrix_Free(transfSample);
403 Enumeration_Free(origEP);
404 Enumeration_Free(fullEP);
405 return isOk;
406} /* test_Constraints_fullDimensionize */
Matrix * Matrix_Copy(Matrix const *Src)
Definition: Matop.c:98
#define value_pdivision(ref, val1, val2)
Definition: arithmetique.h:550
#define value_zero_p(val)
Definition: arithmetique.h:575
#define value_assign(v1, v2)
Definition: arithmetique.h:482
#define value_set_si(val, i)
Definition: arithmetique.h:483
#define value_eq(v1, v2)
Definition: arithmetique.h:502
#define value_clear(val)
Definition: arithmetique.h:485
#define value_print(Dst, fmt, val)
Definition: arithmetique.h:487
#define value_subtract(ref, val1, val2)
Definition: arithmetique.h:544
#define value_addto(ref, val1, val2)
Definition: arithmetique.h:537
#define value_init(val)
Definition: arithmetique.h:481
void Constraints_fullDimensionize(Matrix **M, Matrix **C, Matrix **VL, Matrix **Eqs, Matrix **ParmEqs, unsigned int **elimVars, unsigned int **elimParms, int maxRays)
Eliminates all the equalities in a set of constraints and returns the set of constraints defining a f...
Matrix * Constraints_Remove_parm_eqs(Matrix **M1, Matrix **Ctxt1, int renderSpace, unsigned int **elimParms)
Removes the equalities that involve only parameters, by eliminating some parameters in the polyhedron...
void Lattice_extractSubLattice(Matrix *lat, unsigned int k, Matrix **subLat)
Given a matrix that defines a full-dimensional affine lattice, returns the affine sub-lattice spanned...
Polyhedron * Polyhedron_Remove_parm_eqs(Polyhedron **P, Polyhedron **C, int renderSpace, unsigned int **elimParms, int maxRays)
Removes equalities involving only parameters, but starting from a Polyhedron and its context.
Enumeration * Polyhedron_Enumerate(Polyhedron *Pi, Polyhedron *C, unsigned MAXRAYS, const char **param_name)
Procedure to count points in a parameterized polytope.
Definition: ehrhart.c:1870
Enumeration * Enumeration_zero(unsigned int nbParms, unsigned int maxRays)
returns a constant Ehrhart polynomial whose value is zero for any value of the parameters.
Definition: ehrhart.c:2719
void print_evalue(FILE *DST, evalue *e, const char **pname)
Definition: ehrhart.c:169
void Enumeration_Free(Enumeration *en)
Definition: ehrhart.c:2225
Value * compute_poly(Enumeration *en, Value *list_args)
Definition: eval_ehrhart.c:137
Matrix * Matrix_Alloc(unsigned NbRows, unsigned NbColumns)
Definition: matrix.c:24
Matrix * Matrix_Read(void)
Definition: matrix.c:209
int MatInverse(Matrix *Mat, Matrix *MatInv)
Definition: matrix.c:603
void Matrix_Free(Matrix *Mat)
Definition: matrix.c:71
#define show_matrix(M)
Polylib matrix addons Mainly, deals with polyhedra represented in implicit form (set of constraints).
Definition: matrix_addon.h:15
int PolyhedronIncludes(Polyhedron *Pol1, Polyhedron *Pol2)
Definition: polyhedron.c:2404
Polyhedron * align_context(Polyhedron *Pol, int align_dimension, int NbMaxRays)
Definition: polyhedron.c:3753
void Polyhedron_Free(Polyhedron *Pol)
Definition: polyhedron.c:1614
Polyhedron * DomainIntersection(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays)
Return the intersection of two polyhedral domains 'Pol1' and 'Pol2'.
Definition: polyhedron.c:2637
Polyhedron * Constraints2Polyhedron(Matrix *Constraints, unsigned NbMaxRays)
Given a matrix of constraints ('Constraints'), construct and return a polyhedron.
Definition: polyhedron.c:1905
evalue EP
Definition: types.h:192
Definition: types.h:75
unsigned NbRows
Definition: types.h:76
Value ** p
Definition: types.h:77
unsigned NbColumns
Definition: types.h:76
unsigned Dimension
Definition: types.h:94
int test_Constraints_fullDimensionize(Matrix *A, Matrix *B, unsigned int nbSamples)
Tests Constraints_fullDimensionize by comparing the Ehrhart polynomials.
int test_Constraints_Remove_parm_eqs(Matrix *A, Matrix *B)
extracts the equalities involving the parameters only, try to introduce them back and compare the two...
int main(int argc, char **argv)
#define maxRays
#define dbg
unsigned int namesWithoutElim(const char **parms, unsigned nbParms, unsigned int *elimParms, const char ***newParms)
takes a list of parameter names, a list ofparameters to eliminate, and returns the list of parameters...
#define TEST(a)
const char * origNames[]
int test_Polyhedron_Remove_parm_eqs(Matrix *A, Matrix *B)
extracts the equalities holding on the parameters only, try to introduce them back and compare the tw...
void valuesWithoutElim(Matrix *origParms, unsigned int *elimParms, Matrix **newParms)
Eliminates certain parameters from a vector of values for parameters.
#define P_VALUE_FMT
Definition: types.h:39
void Inner_Product(Value *p1, Value *p2, unsigned length, Value *ip)
Definition: vector.c:376