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