1 | #include <gfanlib/gfanlib_matrix.h> |
---|
2 | #include <gfanlib/gfanlib_zcone.h> |
---|
3 | #include <libpolys/polys/monomials/p_polys.h> |
---|
4 | #include <callgfanlib_conversion.h> |
---|
5 | #include <gfanlib_exceptions.h> |
---|
6 | #include <std_wrapper.h> |
---|
7 | #include <containsMonomial.h> |
---|
8 | #include <initial.h> |
---|
9 | #include <witness.h> |
---|
10 | #include <tropicalStrategy.h> |
---|
11 | #include <tropicalVarietyOfPolynomials.h> |
---|
12 | #include <set> |
---|
13 | #ifndef NDEBUG |
---|
14 | #include <bbfan.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | /*** |
---|
18 | * Given two sets of cones A,B and a dimensional bound d, |
---|
19 | * computes the intersections of all cones of A with all cones of B, |
---|
20 | * and throws away those of lower dimension than d. |
---|
21 | **/ |
---|
22 | static std::set<gfan::ZCone> intersect(const std::set<gfan::ZCone> setA, |
---|
23 | const std::set<gfan::ZCone> setB, |
---|
24 | int d=0) |
---|
25 | { |
---|
26 | if (setA.empty()) |
---|
27 | return setB; |
---|
28 | if (setB.empty()) |
---|
29 | return setA; |
---|
30 | std::set<gfan::ZCone> setAB; |
---|
31 | for (std::set<gfan::ZCone>::iterator coneOfA=setA.begin(); coneOfA!=setA.end(); coneOfA++) |
---|
32 | { |
---|
33 | for (std::set<gfan::ZCone>::iterator coneOfB=setB.begin(); coneOfB!=setB.end(); coneOfB++) |
---|
34 | { |
---|
35 | gfan::ZCone coneOfIntersection = gfan::intersection(*coneOfA,*coneOfB); |
---|
36 | if (coneOfIntersection.dimension()>=d) |
---|
37 | { |
---|
38 | coneOfIntersection.canonicalize(); |
---|
39 | setAB.insert(coneOfIntersection); |
---|
40 | } |
---|
41 | } |
---|
42 | } |
---|
43 | return setAB; |
---|
44 | } |
---|
45 | |
---|
46 | /*** |
---|
47 | * Given a ring r, weights u, w, and a matrix E, returns a copy of r whose ordering is, |
---|
48 | * for any ideal homogeneous with respect to u, weighted with respect to u and |
---|
49 | * whose tiebreaker is genericly weighted with respect to v and E in the following sense: |
---|
50 | * the ordering "lies" on the affine space A running through v and spanned by the row vectors of E, |
---|
51 | * and it lies in a Groebner cone of dimension at least rank(E)=dim(A). |
---|
52 | **/ |
---|
53 | static ring genericlyWeightedOrdering(const ring r, const gfan::ZVector u, const gfan::ZVector w, |
---|
54 | const gfan::ZMatrix W, const tropicalStrategy& currentStrategy) |
---|
55 | { |
---|
56 | int n = rVar(r); |
---|
57 | int h = W.getHeight(); |
---|
58 | |
---|
59 | /* create a copy s of r and delete its ordering */ |
---|
60 | ring s = rCopy0(r); |
---|
61 | omFree(s->order); |
---|
62 | s->order = (int*) omAlloc0((h+4)*sizeof(int)); |
---|
63 | omFree(s->block0); |
---|
64 | s->block0 = (int*) omAlloc0((h+4)*sizeof(int)); |
---|
65 | omFree(s->block1); |
---|
66 | s->block1 = (int*) omAlloc0((h+4)*sizeof(int)); |
---|
67 | for (int j=0; s->wvhdl[j]; j++) omFree(s->wvhdl[j]); |
---|
68 | omFree(s->wvhdl); |
---|
69 | s->wvhdl = (int**) omAlloc0((h+4)*sizeof(int*)); |
---|
70 | |
---|
71 | /* construct a new ordering as describe above */ |
---|
72 | bool overflow; |
---|
73 | s->order[0] = ringorder_a; |
---|
74 | s->block0[0] = 1; |
---|
75 | s->block1[0] = n; |
---|
76 | gfan::ZVector uAdjusted = currentStrategy.adjustWeightForHomogeneity(u); |
---|
77 | s->wvhdl[0] = ZVectorToIntStar(uAdjusted,overflow); |
---|
78 | s->order[1] = ringorder_a; |
---|
79 | s->block0[1] = 1; |
---|
80 | s->block1[1] = n; |
---|
81 | gfan::ZVector wAdjusted = currentStrategy.adjustWeightUnderHomogeneity(w,uAdjusted); |
---|
82 | s->wvhdl[1] = ZVectorToIntStar(wAdjusted,overflow); |
---|
83 | for (int j=0; j<h-1; j++) |
---|
84 | { |
---|
85 | s->order[j+2] = ringorder_a; |
---|
86 | s->block0[j+2] = 1; |
---|
87 | s->block1[j+2] = n; |
---|
88 | wAdjusted = currentStrategy.adjustWeightUnderHomogeneity(W[j],uAdjusted); |
---|
89 | s->wvhdl[j+2] = ZVectorToIntStar(wAdjusted,overflow); |
---|
90 | } |
---|
91 | s->order[h+1] = ringorder_wp; |
---|
92 | s->block0[h+1] = 1; |
---|
93 | s->block1[h+1] = n; |
---|
94 | wAdjusted = currentStrategy.adjustWeightUnderHomogeneity(W[h-1],uAdjusted); |
---|
95 | s->wvhdl[h+1] = ZVectorToIntStar(wAdjusted,overflow); |
---|
96 | s->order[h+2] = ringorder_C; |
---|
97 | |
---|
98 | if (overflow) |
---|
99 | throw 0; //weightOverflow; |
---|
100 | |
---|
101 | /* complete the ring and return it */ |
---|
102 | rComplete(s); |
---|
103 | rTest(s); |
---|
104 | return s; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /*** |
---|
109 | * Let I be an ideal. Given a weight vector u in the relative interior |
---|
110 | * of a one-codimensional cone of the tropical variety of I and |
---|
111 | * the initial ideal inI with respect to it, computes the star of the tropical variety in u. |
---|
112 | **/ |
---|
113 | std::set<gfan::ZCone> tropicalStar(ideal inI, const ring r, const gfan::ZVector u, |
---|
114 | const tropicalStrategy currentStrategy) |
---|
115 | { |
---|
116 | int k = idSize(inI); |
---|
117 | int d = currentStrategy.getExpectedDimension(); |
---|
118 | |
---|
119 | /* Compute the common refinement over all tropical varieties |
---|
120 | * of the polynomials in the generating set */ |
---|
121 | std::set<gfan::ZCone> C = tropicalVariety(inI->m[0],r,currentStrategy); |
---|
122 | for (int i=1; i<k; i++) |
---|
123 | C = intersect(C,tropicalVariety(inI->m[i],r,currentStrategy),d); |
---|
124 | |
---|
125 | /* Cycle through all maximal cones of the refinement. |
---|
126 | * Pick a monomial ordering corresponding to a generic weight vector in it |
---|
127 | * and check if the initial ideal is monomial free, generic meaning |
---|
128 | * that it lies in a maximal Groebner cone in the maximal cone of the refinement. |
---|
129 | * If the initial ideal is not monomial free, compute a witness for the monomial |
---|
130 | * and compute the common refinement with its tropical variety. |
---|
131 | * If all initial ideals are monomial free, then we have our tropical curve */ |
---|
132 | // gfan::ZFan* zf = toFanStar(C); |
---|
133 | // std::cout << zf->toString(); |
---|
134 | // delete zf; |
---|
135 | for (std::set<gfan::ZCone>::iterator zc=C.begin(); zc!=C.end();) |
---|
136 | { |
---|
137 | gfan::ZVector w = zc->getRelativeInteriorPoint(); |
---|
138 | gfan::ZMatrix W = zc->generatorsOfSpan(); |
---|
139 | |
---|
140 | ring s = genericlyWeightedOrdering(r,u,w,W,currentStrategy); |
---|
141 | nMapFunc identity = n_SetMap(r->cf,s->cf); |
---|
142 | ideal inIs = idInit(k); |
---|
143 | for (int j=0; j<k; j++) |
---|
144 | inIs->m[j] = p_PermPoly(inI->m[j],NULL,r,s,identity,NULL,0); |
---|
145 | |
---|
146 | ideal inIsSTD = gfanlib_kStd_wrapper(inIs,s,isHomog); |
---|
147 | ideal ininIs = initial(inIsSTD,s,w,W); |
---|
148 | |
---|
149 | poly mons = currentStrategy.checkInitialIdealForMonomial(ininIs,s,w); |
---|
150 | // poly mons = checkForMonomialViaSuddenSaturation(ininIs,s); |
---|
151 | if (mons) |
---|
152 | { |
---|
153 | poly gs = witness(mons,inIsSTD,ininIs,s); |
---|
154 | C = intersect(C,tropicalVariety(gs,s,currentStrategy),d); |
---|
155 | nMapFunc mMap = n_SetMap(s->cf,r->cf); |
---|
156 | poly gr = p_PermPoly(gs,NULL,s,r,mMap,NULL,0); |
---|
157 | idInsertPoly(inI,gr); |
---|
158 | k++; |
---|
159 | p_Delete(&mons,s); |
---|
160 | p_Delete(&gs,s); |
---|
161 | zc = C.begin(); |
---|
162 | // gfan::ZFan* zf = toFanStar(C); |
---|
163 | // std::cout << zf->toString(); |
---|
164 | // delete zf; |
---|
165 | id_Delete(&inIs,s); |
---|
166 | id_Delete(&inIsSTD,s); |
---|
167 | id_Delete(&ininIs,s); |
---|
168 | rDelete(s); |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | gfan::ZVector wNeg = -w; |
---|
173 | if (zc->contains(wNeg)) |
---|
174 | { |
---|
175 | s = genericlyWeightedOrdering(r,u,wNeg,W,currentStrategy); |
---|
176 | identity = n_SetMap(r->cf,s->cf); |
---|
177 | inIs = idInit(k); |
---|
178 | for (int j=0; j<k; j++) |
---|
179 | inIs->m[j] = p_PermPoly(inI->m[j],NULL,r,s,identity,NULL,0); |
---|
180 | |
---|
181 | inIsSTD = gfanlib_kStd_wrapper(inIs,s,isHomog); |
---|
182 | ininIs = initial(inIsSTD,s,wNeg,W); |
---|
183 | |
---|
184 | mons = currentStrategy.checkInitialIdealForMonomial(ininIs,s,wNeg); |
---|
185 | // mons = checkForMonomialViaSuddenSaturation(ininIs,s); |
---|
186 | if (mons) |
---|
187 | { |
---|
188 | poly gs = witness(mons,inIsSTD,ininIs,s); |
---|
189 | C = intersect(C,tropicalVariety(gs,s,currentStrategy),d); |
---|
190 | nMapFunc mMap = n_SetMap(s->cf,r->cf); |
---|
191 | poly gr = p_PermPoly(gs,NULL,s,r,mMap,NULL,0); |
---|
192 | idInsertPoly(inI,gr); |
---|
193 | k++; |
---|
194 | p_Delete(&mons,s); |
---|
195 | p_Delete(&gs,s); |
---|
196 | zc = C.begin(); |
---|
197 | // gfan::ZFan* zf = toFanStar(C); |
---|
198 | // std::cout << zf->toString(); |
---|
199 | // delete zf; |
---|
200 | } |
---|
201 | else |
---|
202 | zc++; |
---|
203 | id_Delete(&inIs,s); |
---|
204 | id_Delete(&inIsSTD,s); |
---|
205 | id_Delete(&ininIs,s); |
---|
206 | rDelete(s); |
---|
207 | } |
---|
208 | else |
---|
209 | zc++; |
---|
210 | } |
---|
211 | } |
---|
212 | return C; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | gfan::ZMatrix raysOfTropicalStar(ideal I, const ring r, const gfan::ZVector u, const tropicalStrategy& currentStrategy) |
---|
217 | { |
---|
218 | std::set<gfan::ZCone> C = tropicalStar(I,r,u,currentStrategy); |
---|
219 | gfan::ZFan* zf = toFanStar(C); |
---|
220 | // std::cout << zf->toString(); |
---|
221 | delete zf; |
---|
222 | gfan::ZMatrix raysOfC(0,u.size()); |
---|
223 | if (!currentStrategy.restrictToLowerHalfSpace()) |
---|
224 | { |
---|
225 | for (std::set<gfan::ZCone>::iterator zc=C.begin(); zc!=C.end(); zc++) |
---|
226 | { |
---|
227 | assume(zc->dimensionOfLinealitySpace()+1 == zc->dimension()); |
---|
228 | gfan::ZMatrix ray = zc->extremeRays(); |
---|
229 | raysOfC.appendRow(ray[0]); |
---|
230 | } |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | for (std::set<gfan::ZCone>::iterator zc=C.begin(); zc!=C.end(); zc++) |
---|
235 | { |
---|
236 | assume(zc->dimensionOfLinealitySpace()+2 == zc->dimension()); |
---|
237 | raysOfC.appendRow(zc->getRelativeInteriorPoint()); |
---|
238 | } |
---|
239 | } |
---|
240 | return raysOfC; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | /*** |
---|
245 | * Computes the tropical curve of an x-homogeneous ideal I |
---|
246 | * which is weighted homogeneous with respect to weight w in ring r |
---|
247 | **/ |
---|
248 | #ifndef NDEBUG |
---|
249 | BOOLEAN tropicalStarDebug(leftv res, leftv args) |
---|
250 | { |
---|
251 | leftv u = args; |
---|
252 | if ((u!=NULL) && (u->Typ()==IDEAL_CMD)) |
---|
253 | { |
---|
254 | leftv v = u->next; |
---|
255 | if ((v!=NULL) && (v->Typ()==BIGINTMAT_CMD)) |
---|
256 | { |
---|
257 | omUpdateInfo(); |
---|
258 | Print("usedBytesBefore=%ld\n",om_Info.UsedBytes); |
---|
259 | ideal inI = (ideal) u->CopyD(); |
---|
260 | bigintmat* u = (bigintmat*) v->CopyD(); |
---|
261 | tropicalStrategy currentCase(inI,currRing); |
---|
262 | gfan::ZVector* v = bigintmatToZVector(u); |
---|
263 | std::set<gfan::ZCone> C = tropicalStar(inI,currRing,*v,currentCase); |
---|
264 | id_Delete(&inI,currRing); |
---|
265 | delete u; |
---|
266 | delete v; |
---|
267 | res->rtyp = fanID; |
---|
268 | res->data = (char*) toFanStar(C); |
---|
269 | return FALSE; |
---|
270 | } |
---|
271 | } |
---|
272 | WerrorS("tropicalStarDebug: unexpected parameters"); |
---|
273 | return TRUE; |
---|
274 | } |
---|
275 | #endif |
---|