fixed a small bug in eval ehrhart
[polylib.git] / applications / testlib.c
blobc7cda56b1db6a83574a69107b6ff6cafba263415
1 /* main.c
2 COPYRIGHT
3 Both this software and its documentation are
5 Copyright 1993 by IRISA /Universite de Rennes I - France,
6 Copyright 1995,1996 by BYU
7 all rights reserved.
9 Permission is granted to copy, use, and distribute
10 for any commercial or noncommercial purpose under the terms
11 of the GNU General Public license, version 2, June 1991
12 (see file : LICENSING).
14 This file along with polyhedron.c and vector.c do the following functions:
15 - Extraction of a minimal set of constraints from some set of constraints
16 - Intersection of two convexes
17 - Application of a linear function to some convex
18 - Verification that a convex is included in some other convex
20 They are compiled together into an executable file called "test".
21 The file test.in contains sample input data for the program.
22 The file test.out contains the output produced by the program.
24 This directory also contains a makefile to build and run the test.
26 This file is a tutorial on how to use the library. The comments
27 explain whats going on. You can use this file as a pattern for your
28 own program. Good Luck !
30 --Doran
33 #include <stdio.h>
35 #include <polylib/polylib.h>
38 int main() {
40 Matrix *a, *b, *t;
41 Polyhedron *A, *B, *C, *D;
43 printf("Polyhedral Library Test\n\n");
45 /* read in a matrix containing your equations */
46 /* for example, run this program and type in these five lines:
47 4 4
48 1 0 1 -1
49 1 -1 0 6
50 1 0 -1 7
51 1 1 0 -2
52 This is a matrix for the following inequalities
53 1 = inequality, 0x + 1y -1 >=0 --> y >= 1
54 1 = inequality, -1x + 0y +6 >=0 --> x <= 6
55 1 = inequality, 0x + -1y +7 >=0 --> y <= 7
56 1 = inequality, 1x + 0y -2 >=0 --> x >= 2
57 If the first number is a 0 instead of a 1, then that constraint
58 is an 'equality' instead of an 'inequality'.
60 a = Matrix_Read();
62 /* read in a second matrix containing a second set of constraints:
63 for example :
64 4 4
65 1 1 0 -1
66 1 -1 0 3
67 1 0 -1 5
68 1 0 1 -2
70 b = Matrix_Read();
72 /* Convert the constraints to a Polyhedron.
73 This operation 1. Computes the dual ray/vertice form of the
74 system, and 2. Eliminates redundant constraints and reduces
75 them to a minimal form.
77 A = Constraints2Polyhedron(a, 200);
78 B = Constraints2Polyhedron(b, 200);
80 /* the 200 is the size of the working space (in terms of number
81 of rays) that is allocated temporarily
82 -- you can enlarge or reduce it as needed */
84 /* There is likewise a rays to polyhedron procedure */
86 /* Since you are done with the matrices a and b, be a good citizen
87 and clean up your garbage */
88 Matrix_Free(a);
89 Matrix_Free(b);
91 /* If you want the the reduced set of equations back, you can
92 either learn to read the polyhedron structure (not hard,
93 look in "types.h"...
94 or you can get the matrix back in the same format it started
95 in... */
96 a = Polyhedron2Constraints(A);
97 b = Polyhedron2Constraints(B);
99 /* Take a look at them if you want */
100 printf("\na =");
101 Matrix_Print(stdout,P_VALUE_FMT,a);
102 printf("\nb =");
103 Matrix_Print(stdout,P_VALUE_FMT,b);
105 /* To intersect the two systems, use the polyhedron formats... */
106 C = DomainIntersection(A, B, 200);
108 /* Again, the 200 is the size of the working space */
110 /* This time, lets look a the polyhedron itself... */
111 printf("\nC = A and B =");
112 Polyhedron_Print(stdout,P_VALUE_FMT,C);
115 * The operations DomainUnion, DomainDifference, and DomainConvex
116 * are also available
120 * Read in a third matrix containing a transformation matrix,
121 * this one swaps the indices (x,y --> y,x):
122 * 3 3
123 * 0 1 0
124 * 1 0 0
125 * 0 0 1
129 t = Matrix_Read();
131 /* Take the preimage (transform the equations) of the domain C to
132 get D. Are you catching on to this 200 thing yet ??? */
134 D = Polyhedron_Preimage(C, t, 200);
136 /* cleanup please */
137 Matrix_Free(t);
139 printf("\nD = transformed C =");
140 Polyhedron_Print(stdout,P_VALUE_FMT,D);
141 Domain_Free(D);
143 /* in a similar way, Polyhedron_Image(dom, mat, 200), takes the image
144 of dom under matrix mat (transforms the vertices/rays) */
146 /* The function PolyhedronIncludes(Pol1, Pol2) returns 1 if Pol1
147 includes (covers) Pol2, and a 0 otherwise */
149 if (PolyhedronIncludes(A,C))
150 printf("\nWe expected A to cover C since C = A intersect B\n");
151 if (!PolyhedronIncludes(C,B))
152 printf("and C does not cover B...\n");
154 /* Final note: some functions are defined for Domains, others
155 * for Polyhedrons. A domain is simply a list of polyhedrons.
156 * Every polyhedron structure has a "next" pointer used to
157 * make a list of polyhedrons... For instance, the union of
158 * two disjoint polyhedra is a domain consisting of two polyhedra.
159 * If you want the convex domain... you have to call
160 * DomainConvex(Pol1, 200) explicity.
161 * Note that includes does not work on domains, only on simple
162 * polyhedrons...
163 * Thats the end of the demo... write me if you have questions.
164 * And remember to clean up...
167 Domain_Free(A);
168 Domain_Free(B);
169 Domain_Free(C);
171 return 0;