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