Main Page | Class List | File List | Class Members | File Members

testlib.c

Go to the documentation of this file.
00001 /* main.c
00002      COPYRIGHT
00003           Both this software and its documentation are
00004 
00005               Copyright 1993 by IRISA /Universite de Rennes I - France,
00006               Copyright 1995,1996 by BYU
00007                          all rights reserved.
00008 
00009           Permission is granted to copy, use, and distribute
00010           for any commercial or noncommercial purpose under the terms
00011           of the GNU General Public license, version 2, June 1991
00012           (see file : LICENSING).
00013 
00014    This file along with polyhedron.c and vector.c do the following functions:
00015     - Extraction of a minimal set of constraints from some set of constraints
00016     - Intersection of two convexes
00017     - Application of a linear function to some convex
00018     - Verification that a convex is included in some other convex
00019 
00020    They are compiled together into an executable file called "test".
00021    The file test.in contains sample input data for the program.
00022    The file test.out contains the output produced by the program.
00023 
00024    This directory also contains a makefile to build and run the test.
00025 
00026    This file is a tutorial on how to use the library.  The comments
00027    explain whats going on.  You can use this file as a pattern for your
00028    own program.  Good Luck !
00029 
00030    --Doran
00031 */
00032 
00033 #include <stdio.h>
00034 
00035 #include <polylib/polylib.h>
00036 
00037 
00038 int main() { 
00039   
00040   Matrix *a, *b, *t;
00041   Polyhedron *A, *B, *C, *D;
00042   
00043   printf("Polyhedral Library Test\n\n");
00044   
00045   /* read in a matrix containing your equations */
00046   /* for example, run this program and type in these five  lines:
00047      4 4
00048      1 0 1 -1
00049      1 -1 0 6
00050      1 0 -1 7
00051      1 1 0 -2
00052      This is a matrix for the following inequalities
00053      1 = inequality,  0x +  1y -1 >=0  -->      y >= 1
00054      1 = inequality, -1x +  0y +6 >=0  -->      x <= 6
00055      1 = inequality,  0x + -1y +7 >=0  -->      y <= 7
00056      1 = inequality,  1x +  0y -2 >=0  -->      x >= 2
00057      If the first number is a 0 instead of a 1, then that constraint
00058      is an 'equality' instead of an 'inequality'.
00059   */
00060   a = Matrix_Read();
00061   
00062   /* read in a second matrix containing a second set of constraints:
00063      for example :
00064      4 4
00065      1 1 0 -1
00066      1 -1 0 3
00067      1 0 -1 5
00068      1 0 1 -2
00069   */
00070   b = Matrix_Read();
00071 
00072   /* Convert the constraints to a Polyhedron.
00073      This operation 1. Computes the dual ray/vertice form of the
00074      system, and 2. Eliminates redundant constraints and reduces
00075      them to a minimal form.
00076   */
00077   A = Constraints2Polyhedron(a, 200);
00078   B = Constraints2Polyhedron(b, 200);
00079 
00080   /* the 200 is the size of the working space (in terms of number
00081      of rays) that is allocated temporarily
00082      -- you can enlarge or reduce it as needed */
00083   
00084   /* There is likewise a rays to polyhedron procedure */
00085   
00086   /* Since you are done with the matrices a and b, be a good citizen
00087      and clean up your garbage */
00088   Matrix_Free(a);
00089   Matrix_Free(b);
00090   
00091   /* If you want the the reduced set of equations back, you can
00092      either learn to read the polyhedron structure (not hard,
00093      look in "types.h"...
00094      or you can get the matrix back in the same format it started
00095      in... */
00096   a = Polyhedron2Constraints(A);
00097   b = Polyhedron2Constraints(B);
00098 
00099   /* Take a look at them if you want */
00100   printf("\na =");
00101   Matrix_Print(stdout,P_VALUE_FMT,a);
00102   printf("\nb =");
00103   Matrix_Print(stdout,P_VALUE_FMT,b);
00104   
00105   /* To intersect the two systems, use the polyhedron formats... */
00106   C = DomainIntersection(A, B, 200);
00107   
00108   /* Again, the 200 is the size of the working space */
00109   
00110   /* This time, lets look a the polyhedron itself... */
00111   printf("\nC = A and B =");
00112   Polyhedron_Print(stdout,P_VALUE_FMT,C);
00113   
00114   /* 
00115    * The operations DomainUnion, DomainDifference, and DomainConvex
00116    * are also available 
00117    */
00118   
00119   /* 
00120    * Read in a third matrix containing a transformation matrix,
00121    * this one swaps the indices (x,y --> y,x):
00122    * 3 3
00123    * 0 1 0
00124    * 1 0 0
00125    * 0 0 1
00126    */
00127   
00128   
00129   t = Matrix_Read();
00130   
00131   /* Take the preimage (transform the equations) of the domain C to
00132      get D.  Are you catching on to this 200 thing yet ??? */
00133   
00134   D = Polyhedron_Preimage(C, t, 200);
00135   
00136   /* cleanup please */
00137   Matrix_Free(t);
00138   
00139   printf("\nD = transformed C =");
00140   Polyhedron_Print(stdout,P_VALUE_FMT,D);
00141   Domain_Free(D);
00142   
00143   /* in a similar way, Polyhedron_Image(dom, mat, 200), takes the image
00144      of dom under matrix mat  (transforms the vertices/rays) */
00145   
00146   /* The function PolyhedronIncludes(Pol1, Pol2) returns 1 if Pol1
00147      includes (covers) Pol2, and a 0 otherwise */
00148   
00149   if (PolyhedronIncludes(A,C))
00150     printf("\nWe expected A to cover C since C = A intersect B\n");
00151   if (!PolyhedronIncludes(C,B))
00152     printf("and C does not cover B...\n");
00153   
00154   /* Final note:  some functions are defined for Domains, others
00155    * for Polyhedrons.  A domain is simply a list of polyhedrons.
00156    * Every polyhedron structure has a "next" pointer used to 
00157    * make a list of polyhedrons...  For instance, the union of
00158    * two disjoint polyhedra is a domain consisting of two polyhedra.
00159    * If you want the convex domain... you have to call 
00160    * DomainConvex(Pol1, 200) explicity.   
00161    * Note that includes does not work on domains, only on simple
00162    * polyhedrons...
00163    * Thats the end of the demo...  write me if you have questions.
00164    * And remember to clean up... 
00165    */
00166   
00167   Domain_Free(A);
00168   Domain_Free(B);
00169   Domain_Free(C);
00170   
00171   return 0;
00172 }
00173 
00174 

Generated on Mon Sep 12 14:48:29 2005 for polylib by doxygen 1.3.5