1 | /* |
---|
2 | Compute the Groebner fan of an ideal |
---|
3 | Author: $Author: monerjan $ |
---|
4 | Date: $Date: 2009-02-25 14:30:25 $ |
---|
5 | Header: $Header: /exports/cvsroot-2/cvsroot/kernel/gfan.cc,v 1.16 2009-02-25 14:30:25 monerjan Exp $ |
---|
6 | Id: $Id: gfan.cc,v 1.16 2009-02-25 14:30:25 monerjan Exp $ |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | |
---|
11 | #ifdef HAVE_GFAN |
---|
12 | |
---|
13 | #include "kstd1.h" |
---|
14 | #include "intvec.h" |
---|
15 | #include "polys.h" |
---|
16 | #include "ideals.h" |
---|
17 | #include "kmatrix.h" |
---|
18 | #include "/users/urmel/alggeom/monerjan/cddlib/include/setoper.h" //Support for cddlib. Dirty hack |
---|
19 | #include "/users/urmel/alggeom/monerjan/cddlib/include/cdd.h" |
---|
20 | |
---|
21 | #ifndef gfan_DEBUG |
---|
22 | #define gfan_DEBUG |
---|
23 | #endif |
---|
24 | |
---|
25 | class facet |
---|
26 | { |
---|
27 | private: |
---|
28 | intvec fnormal; //inner normal, describing the facet uniquely |
---|
29 | public: |
---|
30 | facet(); //constructor |
---|
31 | bool isflippable; //flippable facet? |
---|
32 | }; |
---|
33 | |
---|
34 | /*class gcone |
---|
35 | finally this should become s.th. like gconelib.{h,cc} to provide an API |
---|
36 | */ |
---|
37 | class gcone |
---|
38 | { |
---|
39 | public: |
---|
40 | gcone(int); //constructor with dimension |
---|
41 | poly gc_marked_term; //marked terms of the cone's Gröbner basis |
---|
42 | ideal gc_basis; //GB of the cone |
---|
43 | gcone *next; //Pointer to *previous* cone in search tree |
---|
44 | |
---|
45 | void flip(); //Compute "the other side" |
---|
46 | |
---|
47 | };//class gcone |
---|
48 | |
---|
49 | ideal getGB(ideal inputIdeal) |
---|
50 | { |
---|
51 | #ifdef gfan_DEBUG |
---|
52 | printf("Computing a groebner basis...\n"); |
---|
53 | #endif |
---|
54 | |
---|
55 | ideal gb; |
---|
56 | gb=kStd(inputIdeal,NULL,testHomog,NULL); //Possible to call without testHomog/isHomog? |
---|
57 | idSkipZeroes(gb); //Get rid of zero entries |
---|
58 | |
---|
59 | return gb; |
---|
60 | } |
---|
61 | |
---|
62 | /****** getWallIneq computes the inequalities ***/ |
---|
63 | /*INPUT_TYPE: ideal */ |
---|
64 | /*RETURN_TYPE: matrix */ |
---|
65 | /************************************************/ |
---|
66 | void getWallIneq(ideal I) |
---|
67 | { |
---|
68 | #ifdef gfan_DEBUG |
---|
69 | printf("*** Computing Inequalities... ***\n"); |
---|
70 | #endif |
---|
71 | |
---|
72 | //All variables go here - except ineq matrix and *v, see below |
---|
73 | int lengthGB=IDELEMS(I); // # of polys in the groebner basis |
---|
74 | int pCompCount; // # of terms in a poly |
---|
75 | poly aktpoly; |
---|
76 | int numvar = pVariables; // # of variables in a polynomial (or ring?) |
---|
77 | int leadexp[numvar]; // dirty hack of exp.vects |
---|
78 | int aktexp[numvar]; |
---|
79 | int cols,rows; // will contain the dimensions of the ineq matrix - deprecated by |
---|
80 | dd_rowrange ddrows; |
---|
81 | dd_colrange ddcols; |
---|
82 | dd_rowset ddredrows; // # of redundant rows in ddineq |
---|
83 | dd_rowset ddlinset; // the opposite |
---|
84 | dd_rowindex ddnewpos; // all to make dd_Canonicalize happy |
---|
85 | dd_NumberType ddnumb=dd_Real; //Number type |
---|
86 | dd_ErrorType dderr=dd_NoError; // |
---|
87 | // End of var declaration |
---|
88 | |
---|
89 | printf("The Groebner basis has %i elements\n",lengthGB); |
---|
90 | printf("The current ring has %i variables\n",numvar); |
---|
91 | cols = numvar; |
---|
92 | |
---|
93 | //Compute the # inequalities i.e. rows of the matrix |
---|
94 | rows=0; //Initialization |
---|
95 | for (int ii=0;ii<IDELEMS(I);ii++) |
---|
96 | { |
---|
97 | aktpoly=(poly)I->m[ii]; |
---|
98 | rows=rows+pLength(aktpoly)-1; |
---|
99 | } |
---|
100 | printf("rows=%i\n",rows); |
---|
101 | printf("Will create a %i x %i - matrix to store inequalities\n",rows,cols); |
---|
102 | |
---|
103 | dd_rowrange aktmatrixrow=0; // needed to store the diffs of the expvects in the rows of ddineq |
---|
104 | dd_set_global_constants(); |
---|
105 | ddrows=rows; |
---|
106 | ddcols=cols; |
---|
107 | dd_MatrixPtr ddineq; //Matrix to store the inequalities |
---|
108 | ddineq=dd_CreateMatrix(ddrows,ddcols+1); //The first col has to be 0 since cddlib checks for additive consts there |
---|
109 | |
---|
110 | // We loop through each g\in GB and compute the resulting inequalities |
---|
111 | for (int i=0; i<IDELEMS(I); i++) |
---|
112 | { |
---|
113 | aktpoly=(poly)I->m[i]; //get aktpoly as i-th component of I |
---|
114 | pCompCount=pLength(aktpoly); //How many terms does aktpoly consist of? |
---|
115 | printf("Poly No. %i has %i components\n",i,pCompCount); |
---|
116 | |
---|
117 | int *v=(int *)omAlloc((numvar+1)*sizeof(int)); |
---|
118 | pGetExpV(aktpoly,v); //find the exp.vect in v[1],...,v[n], use pNext(p) |
---|
119 | |
---|
120 | //Store leadexp for aktpoly |
---|
121 | for (int kk=0;kk<numvar;kk++) |
---|
122 | { |
---|
123 | leadexp[kk]=v[kk+1]; |
---|
124 | //printf("Leadexpcomp=%i\n",leadexp[kk]); |
---|
125 | //Since we need to know the difference of leadexp with the other expvects we do nothing here |
---|
126 | //but compute the diff below |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | while (pNext(aktpoly)!=NULL) //move to next term until NULL |
---|
131 | { |
---|
132 | aktpoly=pNext(aktpoly); |
---|
133 | pSetm(aktpoly); //doesn't seem to help anything |
---|
134 | pGetExpV(aktpoly,v); |
---|
135 | for (int kk=0;kk<numvar;kk++) |
---|
136 | { |
---|
137 | //The ordering somehow gets lost here but this is acceptable, since we are only interested in the inequalities |
---|
138 | aktexp[kk]=v[kk+1]; |
---|
139 | //printf("aktexpcomp=%i\n",aktexp[kk]); |
---|
140 | //ineq[aktmatrixrow][kk]=leadexp[kk]-aktexp[kk]; //dito |
---|
141 | dd_set_si(ddineq->matrix[(dd_rowrange)aktmatrixrow][kk+1],leadexp[kk]-aktexp[kk]); //because of the 1st col being const 0 |
---|
142 | } |
---|
143 | aktmatrixrow=aktmatrixrow+1; |
---|
144 | }//while |
---|
145 | |
---|
146 | } //for |
---|
147 | |
---|
148 | //Maybe add another row to contain the constraints of the standard simplex? |
---|
149 | |
---|
150 | #ifdef gfan_DEBUG |
---|
151 | printf("The inequality matrix is:\n"); |
---|
152 | dd_WriteMatrix(stdout, ddineq); |
---|
153 | #endif |
---|
154 | |
---|
155 | // The inequalities are now stored in ddineq |
---|
156 | // Next we check for superflous rows |
---|
157 | ddredrows = dd_RedundantRows(ddineq, &dderr); |
---|
158 | if (dderr!=dd_NoError) // did an error occur? |
---|
159 | { |
---|
160 | dd_WriteErrorMessages(stderr,dderr); //if so tell us |
---|
161 | } else |
---|
162 | { |
---|
163 | printf("Redundant rows: "); |
---|
164 | set_fwrite(stdout, ddredrows); //otherwise print the redundant rows |
---|
165 | }//if dd_Error |
---|
166 | |
---|
167 | //Remove reduntant rows here! |
---|
168 | dd_MatrixCanonicalize(&ddineq, &ddlinset, &ddredrows, &ddnewpos, &dderr); |
---|
169 | #ifdef gfan_DEBUG |
---|
170 | printf("Having removed redundant rows, the inequalities now read:\n"); |
---|
171 | dd_WriteMatrix(stdout,ddineq); |
---|
172 | #endif |
---|
173 | |
---|
174 | dd_PolyhedraPtr ddpolyh; |
---|
175 | dd_MatrixPtr G; |
---|
176 | ddpolyh=dd_DDMatrix2Poly(ddineq, &dderr); |
---|
177 | G=dd_CopyGenerators(ddpolyh); |
---|
178 | printf("\nSpanning vectors = rows:\n"); |
---|
179 | dd_WriteMatrix(stdout, G); |
---|
180 | |
---|
181 | //ddineq->representation=dd_Inequality; //We want our LP to be Ax>=0 |
---|
182 | |
---|
183 | //Clean up but don't delete the return value! (Whatever it will turn out to be) |
---|
184 | dd_FreeMatrix(ddineq); |
---|
185 | //set_free(ddrows); |
---|
186 | //set_free(ddcols); |
---|
187 | set_free(ddredrows); |
---|
188 | //set_free(ddnumb); |
---|
189 | //set_free(dderr); |
---|
190 | free(ddnewpos); |
---|
191 | //set_free(ddlinset); |
---|
192 | dd_free_global_constants(); |
---|
193 | |
---|
194 | //res=(ideal)aktpoly; |
---|
195 | //return res; |
---|
196 | } |
---|
197 | |
---|
198 | ideal gfan(ideal inputIdeal) |
---|
199 | { |
---|
200 | #ifdef gfan_DEBUG |
---|
201 | printf("Now in subroutine gfan\n"); |
---|
202 | #endif |
---|
203 | ideal res; |
---|
204 | matrix ineq; //Matrix containing the boundary inequalities |
---|
205 | |
---|
206 | res=getGB(inputIdeal); |
---|
207 | getWallIneq(res); |
---|
208 | return res; |
---|
209 | } |
---|
210 | #endif |
---|