1 | #include <callgfanlib_conversion.h> |
---|
2 | #include <containsMonomial.h> |
---|
3 | #include <tropical.h> |
---|
4 | #include <initial.h> |
---|
5 | #include <lift.h> |
---|
6 | #include <groebnerCone.h> |
---|
7 | #include <neighbours.h> |
---|
8 | #include <tropicalStrategy.h> |
---|
9 | #include <tropicalCurves.h> |
---|
10 | #include <bbcone.h> |
---|
11 | #include <tropicalVarietyOfPolynomials.h> |
---|
12 | #include <tropicalVariety.h> |
---|
13 | |
---|
14 | |
---|
15 | /*** |
---|
16 | * checks whether sigma is contained in the tropical variety |
---|
17 | * by testing whether the initial Ideal with respect to the interior point |
---|
18 | * is monomial free. |
---|
19 | **/ |
---|
20 | static bool checkContainmentInTropicalVariety(const groebnerCone sigma) |
---|
21 | { |
---|
22 | gfan::ZVector w = sigma.getInteriorPoint(); |
---|
23 | ideal I = sigma.getPolynomialIdeal(); |
---|
24 | ring r = sigma.getPolynomialRing(); |
---|
25 | ideal inI = sloppyInitial(I,r,w); |
---|
26 | poly s = checkForMonomialViaSuddenSaturation(inI,r); |
---|
27 | id_Delete(&inI,r); |
---|
28 | if (s) |
---|
29 | { |
---|
30 | p_Delete(&s,r); |
---|
31 | return false; |
---|
32 | } |
---|
33 | return true; |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | static bool checkOneCodimensionalLinealitySpace(const groebnerCone sigma) |
---|
38 | { |
---|
39 | gfan::ZCone zc = sigma.getPolyhedralCone(); |
---|
40 | int linDim = zc.dimensionOfLinealitySpace(); |
---|
41 | int dim = zc.dimension(); |
---|
42 | return (linDim+1)==dim; |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | /*** |
---|
47 | * Computes a starting point by traversing the Groebner fan, |
---|
48 | * checking each cone whether it contains a ray in the tropical variety. |
---|
49 | * Returns a point in the tropical variety and a maximal Groebner cone |
---|
50 | * containing the point. |
---|
51 | **/ |
---|
52 | std::pair<gfan::ZVector,groebnerCone> tropicalStartingDataViaGroebnerFan(const ideal I, const ring r, const tropicalStrategy& currentStrategy) |
---|
53 | { |
---|
54 | currentStrategy.reduce(I,r); |
---|
55 | |
---|
56 | const groebnerCone sigma(I,r,currentStrategy); |
---|
57 | gfan::ZVector startingPoint = sigma.tropicalPoint(); |
---|
58 | if (startingPoint.size() > 0) |
---|
59 | return std::make_pair(startingPoint,sigma); |
---|
60 | groebnerCones groebnerFan; |
---|
61 | groebnerCones workingList; |
---|
62 | workingList.insert(sigma); |
---|
63 | |
---|
64 | while (!workingList.empty()) |
---|
65 | { |
---|
66 | const groebnerCone sigma = *(workingList.begin()); |
---|
67 | groebnerCones neighbours = sigma.groebnerNeighbours(); |
---|
68 | for (groebnerCones::iterator tau = neighbours.begin(); tau!=neighbours.end(); tau++) |
---|
69 | { |
---|
70 | if (groebnerFan.count(*tau) == 0) |
---|
71 | { |
---|
72 | if (workingList.count(*tau) == 0) |
---|
73 | { |
---|
74 | startingPoint = tau->tropicalPoint(); |
---|
75 | if (startingPoint.size() > 0) |
---|
76 | return std::make_pair(startingPoint,*tau); |
---|
77 | } |
---|
78 | workingList.insert(*tau); |
---|
79 | } |
---|
80 | } |
---|
81 | groebnerFan.insert(sigma); |
---|
82 | workingList.erase(sigma); |
---|
83 | } |
---|
84 | gfan::ZVector emptyVector = gfan::ZVector(0); |
---|
85 | groebnerCone emptyCone = groebnerCone(); |
---|
86 | return std::pair<gfan::ZVector,groebnerCone>(emptyVector,emptyCone); |
---|
87 | } |
---|
88 | |
---|
89 | BOOLEAN positiveTropicalStartingPoint(leftv res, leftv args) |
---|
90 | { |
---|
91 | leftv u = args; |
---|
92 | if ((u!=NULL) && (u->Typ()==IDEAL_CMD)) |
---|
93 | { |
---|
94 | ideal I = (ideal) u->Data(); |
---|
95 | if (idSize(I)==1) |
---|
96 | { |
---|
97 | tropicalStrategy currentStrategy(I,currRing); |
---|
98 | poly g = I->m[0]; |
---|
99 | std::set<gfan::ZCone> Tg = tropicalVariety(g,currRing,currentStrategy); |
---|
100 | for (std::set<gfan::ZCone>::iterator zc=Tg.begin(); zc!=Tg.end(); zc++) |
---|
101 | { |
---|
102 | gfan::ZMatrix ray = zc->extremeRays(); |
---|
103 | for (int i=0; i<ray.getHeight(); i++) |
---|
104 | { |
---|
105 | if (ray[i].isPositive()) |
---|
106 | { |
---|
107 | res->rtyp = BIGINTMAT_CMD; |
---|
108 | res->data = (void*) zVectorToBigintmat(ray[i]); |
---|
109 | return FALSE; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | res->rtyp = BIGINTMAT_CMD; |
---|
114 | res->data = (void*) zVectorToBigintmat(gfan::ZVector(0)); |
---|
115 | return FALSE; |
---|
116 | } |
---|
117 | WerrorS("positiveTropicalStartingPoint: ideal not principal"); |
---|
118 | return TRUE; |
---|
119 | } |
---|
120 | WerrorS("positiveTropicalStartingPoint: unexpected parameters"); |
---|
121 | return TRUE; |
---|
122 | } |
---|
123 | |
---|
124 | BOOLEAN nonNegativeTropicalStartingPoint(leftv res, leftv args) |
---|
125 | { |
---|
126 | leftv u = args; |
---|
127 | if ((u!=NULL) && (u->Typ()==IDEAL_CMD)) |
---|
128 | { |
---|
129 | ideal I = (ideal) u->Data(); |
---|
130 | if (idSize(I)==1) |
---|
131 | { |
---|
132 | tropicalStrategy currentStrategy(I,currRing); |
---|
133 | poly g = I->m[0]; |
---|
134 | std::set<gfan::ZCone> Tg = tropicalVariety(g,currRing,currentStrategy); |
---|
135 | for (std::set<gfan::ZCone>::iterator zc=Tg.begin(); zc!=Tg.end(); zc++) |
---|
136 | { |
---|
137 | gfan::ZMatrix ray = zc->extremeRays(); |
---|
138 | for (int i=0; i<ray.getHeight(); i++) |
---|
139 | { |
---|
140 | if (ray[i].isNonNegative()) |
---|
141 | { |
---|
142 | res->rtyp = BIGINTMAT_CMD; |
---|
143 | res->data = (void*) zVectorToBigintmat(ray[i]); |
---|
144 | return FALSE; |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | res->rtyp = BIGINTMAT_CMD; |
---|
149 | res->data = (void*) zVectorToBigintmat(gfan::ZVector(0)); |
---|
150 | return FALSE; |
---|
151 | } |
---|
152 | WerrorS("nonNegativeTropicalStartingPoint: ideal not principal"); |
---|
153 | return TRUE; |
---|
154 | } |
---|
155 | WerrorS("nonNegativeTropicalStartingPoint: unexpected parameters"); |
---|
156 | return TRUE; |
---|
157 | } |
---|
158 | |
---|
159 | BOOLEAN negativeTropicalStartingPoint(leftv res, leftv args) |
---|
160 | { |
---|
161 | leftv u = args; |
---|
162 | if ((u!=NULL) && (u->Typ()==IDEAL_CMD)) |
---|
163 | { |
---|
164 | ideal I = (ideal) u->Data(); |
---|
165 | if (idSize(I)==1) |
---|
166 | { |
---|
167 | tropicalStrategy currentStrategy(I,currRing); |
---|
168 | poly g = I->m[0]; |
---|
169 | std::set<gfan::ZCone> Tg = tropicalVariety(g,currRing,currentStrategy); |
---|
170 | for (std::set<gfan::ZCone>::iterator zc=Tg.begin(); zc!=Tg.end(); zc++) |
---|
171 | { |
---|
172 | gfan::ZMatrix ray = zc->extremeRays(); |
---|
173 | for (int i=0; i<ray.getHeight(); i++) |
---|
174 | { |
---|
175 | gfan::ZVector negatedRay = gfan::Integer(-1)*ray[i]; |
---|
176 | if (negatedRay.isPositive()) |
---|
177 | { |
---|
178 | res->rtyp = BIGINTMAT_CMD; |
---|
179 | res->data = (void*) zVectorToBigintmat(ray[i]); |
---|
180 | return FALSE; |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | res->rtyp = BIGINTMAT_CMD; |
---|
185 | res->data = (void*) zVectorToBigintmat(gfan::ZVector(0)); |
---|
186 | return FALSE; |
---|
187 | } |
---|
188 | WerrorS("negativeTropicalStartingPoint: ideal not principal"); |
---|
189 | return TRUE; |
---|
190 | } |
---|
191 | WerrorS("negativeTropicalStartingPoint: unexpected parameters"); |
---|
192 | return TRUE; |
---|
193 | } |
---|
194 | |
---|
195 | BOOLEAN nonPositiveTropicalStartingPoint(leftv res, leftv args) |
---|
196 | { |
---|
197 | leftv u = args; |
---|
198 | if ((u!=NULL) && (u->Typ()==IDEAL_CMD)) |
---|
199 | { |
---|
200 | ideal I = (ideal) u->Data(); |
---|
201 | if (idSize(I)==1) |
---|
202 | { |
---|
203 | tropicalStrategy currentStrategy(I,currRing); |
---|
204 | poly g = I->m[0]; |
---|
205 | std::set<gfan::ZCone> Tg = tropicalVariety(g,currRing,currentStrategy); |
---|
206 | for (std::set<gfan::ZCone>::iterator zc=Tg.begin(); zc!=Tg.end(); zc++) |
---|
207 | { |
---|
208 | gfan::ZMatrix ray = zc->extremeRays(); |
---|
209 | for (int i=0; i<ray.getHeight(); i++) |
---|
210 | { |
---|
211 | gfan::ZVector negatedRay = gfan::Integer(-1)*ray[i]; |
---|
212 | if (negatedRay.isNonNegative()) |
---|
213 | { |
---|
214 | res->rtyp = BIGINTMAT_CMD; |
---|
215 | res->data = (void*) zVectorToBigintmat(ray[i]); |
---|
216 | return FALSE; |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | res->rtyp = BIGINTMAT_CMD; |
---|
221 | res->data = (void*) zVectorToBigintmat(gfan::ZVector(0)); |
---|
222 | return FALSE; |
---|
223 | } |
---|
224 | WerrorS("nonPositiveTropicalStartingPoint: ideal not principal"); |
---|
225 | return TRUE; |
---|
226 | } |
---|
227 | WerrorS("nonPositiveTropicalStartingPoint: unexpected parameters"); |
---|
228 | return TRUE; |
---|
229 | } |
---|
230 | |
---|
231 | BOOLEAN tropicalStartingPoint(leftv res, leftv args) |
---|
232 | { |
---|
233 | leftv u = args; |
---|
234 | if ((u!=NULL) && (u->Typ()==IDEAL_CMD)) |
---|
235 | { |
---|
236 | ideal I = (ideal) u->Data(); |
---|
237 | tropicalStrategy currentStrategy(I,currRing); |
---|
238 | if (idSize(I)==1) |
---|
239 | { |
---|
240 | poly g = I->m[0]; |
---|
241 | std::set<gfan::ZCone> Tg = tropicalVariety(g,currRing,currentStrategy); |
---|
242 | if (Tg.empty()) |
---|
243 | { |
---|
244 | res->rtyp = BIGINTMAT_CMD; |
---|
245 | res->data = (void*) zVectorToBigintmat(gfan::ZVector(0)); |
---|
246 | return FALSE; |
---|
247 | } |
---|
248 | gfan::ZCone C = *(Tg.begin()); |
---|
249 | gfan::ZMatrix rays = C.extremeRays(); |
---|
250 | if (rays.getHeight()==0) |
---|
251 | { |
---|
252 | gfan::ZMatrix lin = C.generatorsOfLinealitySpace(); |
---|
253 | res->rtyp = BIGINTMAT_CMD; |
---|
254 | res->data = (void*) zVectorToBigintmat(lin[0]); |
---|
255 | return FALSE; |
---|
256 | } |
---|
257 | res->rtyp = BIGINTMAT_CMD; |
---|
258 | res->data = (void*) zVectorToBigintmat(rays[0]); |
---|
259 | return FALSE; |
---|
260 | } |
---|
261 | gfan::ZCone C0 = currentStrategy.getHomogeneitySpace(); |
---|
262 | if (C0.dimension()==currentStrategy.getDimensionOfIdeal()) |
---|
263 | { |
---|
264 | gfan::ZMatrix lin = C0.generatorsOfLinealitySpace(); |
---|
265 | res->rtyp = BIGINTMAT_CMD; |
---|
266 | res->data = (void*) zVectorToBigintmat(lin[0]); |
---|
267 | return FALSE; |
---|
268 | } |
---|
269 | std::pair<gfan::ZVector,groebnerCone> startingData = tropicalStartingDataViaGroebnerFan(I,currRing,currentStrategy); |
---|
270 | gfan::ZVector startingPoint = startingData.first; |
---|
271 | res->rtyp = BIGINTMAT_CMD; |
---|
272 | res->data = (void*) zVectorToBigintmat(startingPoint); |
---|
273 | return FALSE; |
---|
274 | } |
---|
275 | WerrorS("tropicalStartingPoint: unexpected parameters"); |
---|
276 | return TRUE; |
---|
277 | } |
---|
278 | |
---|
279 | /*** |
---|
280 | * returs the lineality space of the Groebner fan |
---|
281 | **/ |
---|
282 | static gfan::ZCone linealitySpaceOfGroebnerFan(const ideal I, const ring r) |
---|
283 | { |
---|
284 | int n = rVar(r); |
---|
285 | gfan::ZMatrix equations = gfan::ZMatrix(0,n); |
---|
286 | int* expv = (int*) omAlloc((n+1)*sizeof(int)); |
---|
287 | int k = idSize(I); |
---|
288 | for (int i=0; i<k; i++) |
---|
289 | { |
---|
290 | poly g = I->m[i]; |
---|
291 | p_GetExpV(g,expv,r); |
---|
292 | gfan::ZVector leadexp = intStar2ZVector(n,expv); |
---|
293 | for (pIter(g); g; pIter(g)) |
---|
294 | { |
---|
295 | p_GetExpV(g,expv,r); |
---|
296 | equations.appendRow(leadexp-intStar2ZVector(n,expv)); |
---|
297 | } |
---|
298 | } |
---|
299 | omFreeSize(expv,(n+1)*sizeof(int)); |
---|
300 | return gfan::ZCone(gfan::ZMatrix(0,n),equations); |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | /*** |
---|
305 | * Computes a starting cone in the tropical variety. |
---|
306 | **/ |
---|
307 | groebnerCone tropicalStartingCone(const ideal I, const ring r, const tropicalStrategy& currentStrategy) |
---|
308 | { |
---|
309 | ring s = rCopy(r); |
---|
310 | int k = idSize(I); ideal inI = idInit(k); |
---|
311 | nMapFunc identityMap = n_SetMap(r->cf,s->cf); |
---|
312 | for (int i=0; i<k; i++) |
---|
313 | inI->m[i] = p_PermPoly(I->m[i],NULL,r,s,identityMap,NULL,0); |
---|
314 | |
---|
315 | gfan::ZCone zc = linealitySpaceOfGroebnerFan(inI,s); |
---|
316 | gfan::ZVector startingPoint; groebnerCone ambientMaximalCone; |
---|
317 | while (zc.dimension()<currentStrategy.getDimensionOfIdeal()) |
---|
318 | { |
---|
319 | std::pair<gfan::ZVector,groebnerCone> startingData = tropicalStartingDataViaGroebnerFan(inI,s,currentStrategy); |
---|
320 | startingPoint = startingData.first; |
---|
321 | ambientMaximalCone = groebnerCone(startingData.second); |
---|
322 | |
---|
323 | id_Delete(&inI,s); rDelete(s); |
---|
324 | inI = ambientMaximalCone.getPolynomialIdeal(); |
---|
325 | s = ambientMaximalCone.getPolynomialRing(); |
---|
326 | |
---|
327 | inI = sloppyInitial(inI,s,startingPoint); |
---|
328 | zc = linealitySpaceOfGroebnerFan(inI,s); |
---|
329 | } |
---|
330 | |
---|
331 | ideal J = lift(I,r,inI,s); |
---|
332 | groebnerCone tropicalStartingCone(J,inI,s,currentStrategy); |
---|
333 | id_Delete(&inI,s); |
---|
334 | id_Delete(&J,s); |
---|
335 | |
---|
336 | assume(checkContainmentInTropicalVariety(tropicalStartingCone)); |
---|
337 | assume(checkOneCodimensionalLinealitySpace(tropicalStartingCone)); |
---|
338 | return tropicalStartingCone; |
---|
339 | } |
---|
340 | |
---|
341 | |
---|
342 | BOOLEAN tropicalStartingCone(leftv res, leftv args) |
---|
343 | { |
---|
344 | leftv u = args; |
---|
345 | if ((u != NULL) && (u->Typ() == IDEAL_CMD)) |
---|
346 | { |
---|
347 | ideal I = (ideal) u->CopyD(); |
---|
348 | leftv v = u->next; |
---|
349 | if ((v != NULL) && (v->Typ() == NUMBER_CMD)) |
---|
350 | { |
---|
351 | number p = (number) v->Data(); |
---|
352 | leftv w = v->next; |
---|
353 | if (w==NULL) |
---|
354 | { |
---|
355 | tropicalStrategy valuedCase(I,p,currRing); |
---|
356 | groebnerCone sigma = tropicalStartingCone(I,currRing,valuedCase); |
---|
357 | res->rtyp = coneID; |
---|
358 | res->data = (char*) new gfan::ZCone(sigma.getPolyhedralCone()); |
---|
359 | return FALSE; |
---|
360 | } |
---|
361 | } |
---|
362 | else |
---|
363 | { |
---|
364 | if (v==NULL) |
---|
365 | { |
---|
366 | tropicalStrategy nonValuedCase(I,currRing); |
---|
367 | groebnerCone sigma = tropicalStartingCone(I,currRing,nonValuedCase); |
---|
368 | res->rtyp = coneID; |
---|
369 | res->data = (char*) new gfan::ZCone(sigma.getPolyhedralCone()); |
---|
370 | return FALSE; |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | WerrorS("tropicalStartingCone: unexpected parameters"); |
---|
375 | return TRUE; |
---|
376 | } |
---|