1 | #ifndef GFANLIB_TROPICALSTRATEGY_H |
---|
2 | #define GFANLIB_TROPICALSTRATEGY_H |
---|
3 | |
---|
4 | #include <gfanlib/gfanlib_vector.h> |
---|
5 | #include <gfanlib/gfanlib_zcone.h> |
---|
6 | #include <libpolys/polys/simpleideals.h> |
---|
7 | #include <kernel/ideals.h> // for idSize |
---|
8 | #include <set> |
---|
9 | #include <callgfanlib_conversion.h> |
---|
10 | #include <containsMonomial.h> |
---|
11 | #include <flip.h> |
---|
12 | #include <initial.h> |
---|
13 | |
---|
14 | /** \file |
---|
15 | * implementation of the class tropicalStrategy |
---|
16 | * |
---|
17 | * tropicalStrategy is a class that contains information relevant for |
---|
18 | * computing tropical varieties that is dependent on the valuation of the coefficient field. |
---|
19 | * It represents the mutable part of an overall framework that is capable of |
---|
20 | * computing tropical varieties both over coefficient fields without valuation and |
---|
21 | * with valuation (currently: only p-adic valuation over rational numbers) |
---|
22 | */ |
---|
23 | |
---|
24 | typedef gfan::ZVector (*wAdjAlg1)(gfan::ZVector); |
---|
25 | typedef gfan::ZVector (*wAdjAlg2)(gfan::ZVector,gfan::ZVector); |
---|
26 | typedef bool (*redAlg)(ideal,ring,number); |
---|
27 | |
---|
28 | class tropicalStrategy |
---|
29 | { |
---|
30 | private: |
---|
31 | /** |
---|
32 | * polynomial ring over a field with valuation |
---|
33 | */ |
---|
34 | ring originalRing; |
---|
35 | /** |
---|
36 | * input ideal, assumed to be a homogeneous prime ideal |
---|
37 | */ |
---|
38 | ideal originalIdeal; |
---|
39 | /** |
---|
40 | * the expected Dimension of the polyhedral output, |
---|
41 | * i.e. the dimension of the ideal if trivial valuation |
---|
42 | * or the dimension of the ideal plus one if non-trivial valuation |
---|
43 | * (as the output is supposed to be intersected with a hyperplane) |
---|
44 | */ |
---|
45 | int expectedDimension; |
---|
46 | /** |
---|
47 | * the homogeneity space of the Grobner fan |
---|
48 | */ |
---|
49 | gfan::ZCone linealitySpace; |
---|
50 | /** |
---|
51 | * polynomial ring over the valuation ring extended by one extra variable t |
---|
52 | */ |
---|
53 | ring startingRing; |
---|
54 | /** |
---|
55 | * preimage of the input ideal under the map that sends t to the uniformizing parameter |
---|
56 | */ |
---|
57 | ideal startingIdeal; |
---|
58 | /** |
---|
59 | * uniformizing parameter in the valuation ring |
---|
60 | */ |
---|
61 | number uniformizingParameter; |
---|
62 | /** |
---|
63 | * polynomial ring over the residue field |
---|
64 | */ |
---|
65 | ring shortcutRing; |
---|
66 | |
---|
67 | /** |
---|
68 | * true if valuation non-trivial, false otherwise |
---|
69 | */ |
---|
70 | bool onlyLowerHalfSpace; |
---|
71 | |
---|
72 | /** |
---|
73 | * A function such that: |
---|
74 | * Given weight w, returns a strictly positive weight u such that an ideal satisfying |
---|
75 | * the valuation-sepcific homogeneity conditions is weighted homogeneous with respect to w |
---|
76 | * if and only if it is homogeneous with respect to u |
---|
77 | */ |
---|
78 | gfan::ZVector (*weightAdjustingAlgorithm1) (gfan::ZVector w); |
---|
79 | /** |
---|
80 | * A function such that: |
---|
81 | * Given strictly positive weight w and weight v, |
---|
82 | * returns a strictly positive weight u such that on an ideal that is weighted homogeneous |
---|
83 | * with respect to w the weights u and v coincide |
---|
84 | */ |
---|
85 | gfan::ZVector (*weightAdjustingAlgorithm2) (gfan::ZVector v, gfan::ZVector w); |
---|
86 | /** |
---|
87 | * A function that reduces the generators of an ideal I so that |
---|
88 | * the inequalities and equations of the Groebner cone can be read off. |
---|
89 | */ |
---|
90 | bool (*extraReductionAlgorithm) (ideal I, ring r, number p); |
---|
91 | |
---|
92 | public: |
---|
93 | |
---|
94 | /** |
---|
95 | * Constructor for the trivial valuation case |
---|
96 | */ |
---|
97 | tropicalStrategy(const ideal I, const ring r, const bool completelyHomogeneous=true, const bool completeSpace=true); |
---|
98 | /** |
---|
99 | * Constructor for the non-trivial valuation case |
---|
100 | * p is the uniformizing parameter of the valuation |
---|
101 | */ |
---|
102 | tropicalStrategy(const ideal J, const number p, const ring s); |
---|
103 | /** |
---|
104 | * copy constructor |
---|
105 | */ |
---|
106 | tropicalStrategy(const tropicalStrategy& currentStrategy); |
---|
107 | /** |
---|
108 | * destructor |
---|
109 | */ |
---|
110 | ~tropicalStrategy(); |
---|
111 | /** |
---|
112 | * assignment operator |
---|
113 | **/ |
---|
114 | tropicalStrategy& operator=(const tropicalStrategy& currentStrategy); |
---|
115 | |
---|
116 | bool isConstantCoefficientCase() const |
---|
117 | { |
---|
118 | bool b = (uniformizingParameter==NULL); |
---|
119 | return b; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * returns the polynomial ring over the field with valuation |
---|
124 | */ |
---|
125 | ring getOriginalRing() const |
---|
126 | { |
---|
127 | rTest(originalRing); |
---|
128 | return originalRing; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * returns the input ideal over the field with valuation |
---|
133 | */ |
---|
134 | ideal getOriginalIdeal() const |
---|
135 | { |
---|
136 | if (originalIdeal) id_Test(originalIdeal,originalRing); |
---|
137 | return originalIdeal; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * returns the polynomial ring over the valuation ring |
---|
142 | */ |
---|
143 | ring getStartingRing() const |
---|
144 | { |
---|
145 | rTest(startingRing); |
---|
146 | return startingRing; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * returns the input ideal |
---|
151 | */ |
---|
152 | ideal getStartingIdeal() const |
---|
153 | { |
---|
154 | if (startingIdeal) id_Test(startingIdeal,startingRing); |
---|
155 | return startingIdeal; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * returns the expected Dimension of the polyhedral output |
---|
160 | */ |
---|
161 | int getExpectedDimension() const |
---|
162 | { |
---|
163 | return expectedDimension; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * returns the uniformizing parameter in the valuation ring |
---|
168 | */ |
---|
169 | number getUniformizingParameter() const |
---|
170 | { |
---|
171 | if (uniformizingParameter) n_Test(uniformizingParameter,startingRing->cf); |
---|
172 | return uniformizingParameter; |
---|
173 | } |
---|
174 | |
---|
175 | ring getShortcutRing() const |
---|
176 | { |
---|
177 | rTest(shortcutRing); |
---|
178 | return shortcutRing; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * returns the homogeneity space of the preimage ideal |
---|
183 | */ |
---|
184 | gfan::ZCone getHomogeneitySpace() const |
---|
185 | { |
---|
186 | return linealitySpace; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * returns the dimension of the homogeneity space |
---|
191 | */ |
---|
192 | int getDimensionOfHomogeneitySpace() const |
---|
193 | { |
---|
194 | return linealitySpace.dimension(); |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * returns true, if valuation non-trivial, false otherwise |
---|
199 | */ |
---|
200 | bool restrictToLowerHalfSpace() const |
---|
201 | { |
---|
202 | return onlyLowerHalfSpace; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Given weight w, returns a strictly positive weight u such that an ideal satisfying |
---|
207 | * the valuation-sepcific homogeneity conditions is weighted homogeneous with respect to w |
---|
208 | * if and only if it is homogeneous with respect to u |
---|
209 | */ |
---|
210 | gfan::ZVector adjustWeightForHomogeneity(gfan::ZVector w) const |
---|
211 | { |
---|
212 | return this->weightAdjustingAlgorithm1(w); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Given strictly positive weight w and weight v, |
---|
217 | * returns a strictly positive weight u such that on an ideal that is weighted homogeneous |
---|
218 | * with respect to w the weights u and v coincide |
---|
219 | */ |
---|
220 | gfan::ZVector adjustWeightUnderHomogeneity(gfan::ZVector v, gfan::ZVector w) const |
---|
221 | { |
---|
222 | return this->weightAdjustingAlgorithm2(v,w); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * reduces the generators of an ideal I so that |
---|
227 | * the inequalities and equations of the Groebner cone can be read off. |
---|
228 | */ |
---|
229 | bool reduce(ideal I, const ring r) const |
---|
230 | { |
---|
231 | rTest(r); id_Test(I,r); |
---|
232 | nMapFunc nMap = n_SetMap(startingRing->cf,r->cf); |
---|
233 | number p = nMap(uniformizingParameter,startingRing->cf,r->cf); |
---|
234 | bool b = this->extraReductionAlgorithm(I,r,p); |
---|
235 | n_Delete(&p,r->cf); |
---|
236 | return b; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * returns true, if I contains a monomial. |
---|
241 | * returns false otherwise. |
---|
242 | **/ |
---|
243 | poly checkInitialIdealForMonomial(const ideal I, const ring r, const gfan::ZVector w) const |
---|
244 | { |
---|
245 | gfan::ZVector v = adjustWeightForHomogeneity(w); |
---|
246 | if (isConstantCoefficientCase()) |
---|
247 | { |
---|
248 | ring rShortcut = rCopy0(r); |
---|
249 | bool overflow; |
---|
250 | /** |
---|
251 | * prepend extra weight vector for homogeneity |
---|
252 | */ |
---|
253 | int* order = rShortcut->order; |
---|
254 | int* block0 = rShortcut->block0; |
---|
255 | int* block1 = rShortcut->block1; |
---|
256 | int** wvhdl = rShortcut->wvhdl; |
---|
257 | int h = rBlocks(r); int n = rVar(r); |
---|
258 | rShortcut->order = (int*) omAlloc0((h+1)*sizeof(int)); |
---|
259 | rShortcut->block0 = (int*) omAlloc0((h+1)*sizeof(int)); |
---|
260 | rShortcut->block1 = (int*) omAlloc0((h+1)*sizeof(int)); |
---|
261 | rShortcut->wvhdl = (int**) omAlloc0((h+1)*sizeof(int*)); |
---|
262 | rShortcut->order[0] = ringorder_a; |
---|
263 | rShortcut->block0[0] = 1; |
---|
264 | rShortcut->block1[0] = n; |
---|
265 | rShortcut->wvhdl[0] = ZVectorToIntStar(v,overflow); |
---|
266 | for (int i=1; i<=h; i++) |
---|
267 | { |
---|
268 | rShortcut->order[i] = order[i-1]; |
---|
269 | rShortcut->block0[i] = block0[i-1]; |
---|
270 | rShortcut->block1[i] = block1[i-1]; |
---|
271 | rShortcut->wvhdl[i] = wvhdl[i-1]; |
---|
272 | } |
---|
273 | rComplete(rShortcut); |
---|
274 | rTest(rShortcut); |
---|
275 | omFree(order); |
---|
276 | omFree(block0); |
---|
277 | omFree(block1); |
---|
278 | omFree(wvhdl); |
---|
279 | |
---|
280 | ideal inI = initial(I,r,w); |
---|
281 | int k = idSize(inI); |
---|
282 | ideal inIShortcut = idInit(k); |
---|
283 | nMapFunc identity = n_SetMap(r->cf,rShortcut->cf); |
---|
284 | for (int i=0; i<k; i++) |
---|
285 | inIShortcut->m[i] = p_PermPoly(inI->m[i],NULL,r,rShortcut,identity,NULL,0); |
---|
286 | |
---|
287 | poly p = checkForMonomialViaSuddenSaturation(inIShortcut,rShortcut); |
---|
288 | poly monomial = NULL; |
---|
289 | if (p!=NULL) |
---|
290 | { |
---|
291 | monomial=p_One(r); |
---|
292 | for (int i=1; i<n; i++) |
---|
293 | p_SetExp(monomial,i,p_GetExp(p,i,rShortcut),r); |
---|
294 | p_Delete(&p,rShortcut); |
---|
295 | } |
---|
296 | id_Delete(&inI,r); |
---|
297 | id_Delete(&inIShortcut,rShortcut); |
---|
298 | rDelete(rShortcut); |
---|
299 | return monomial; |
---|
300 | } |
---|
301 | else |
---|
302 | { |
---|
303 | ring rShortcut = rCopy0(r); |
---|
304 | bool overflow; |
---|
305 | /** |
---|
306 | * prepend extra weight vector for homogeneity |
---|
307 | */ |
---|
308 | int* order = rShortcut->order; |
---|
309 | int* block0 = rShortcut->block0; |
---|
310 | int* block1 = rShortcut->block1; |
---|
311 | int** wvhdl = rShortcut->wvhdl; |
---|
312 | int h = rBlocks(r); int n = rVar(r); |
---|
313 | rShortcut->order = (int*) omAlloc0((h+1)*sizeof(int)); |
---|
314 | rShortcut->block0 = (int*) omAlloc0((h+1)*sizeof(int)); |
---|
315 | rShortcut->block1 = (int*) omAlloc0((h+1)*sizeof(int)); |
---|
316 | rShortcut->wvhdl = (int**) omAlloc0((h+1)*sizeof(int*)); |
---|
317 | rShortcut->order[0] = ringorder_a; |
---|
318 | rShortcut->block0[0] = 1; |
---|
319 | rShortcut->block1[0] = n; |
---|
320 | rShortcut->wvhdl[0] = ZVectorToIntStar(v,overflow); |
---|
321 | for (int i=1; i<=h; i++) |
---|
322 | { |
---|
323 | rShortcut->order[i] = order[i-1]; |
---|
324 | rShortcut->block0[i] = block0[i-1]; |
---|
325 | rShortcut->block1[i] = block1[i-1]; |
---|
326 | rShortcut->wvhdl[i] = wvhdl[i-1]; |
---|
327 | } |
---|
328 | omFree(order); |
---|
329 | omFree(block0); |
---|
330 | omFree(block1); |
---|
331 | omFree(wvhdl); |
---|
332 | /** |
---|
333 | * change ground domain into finite field |
---|
334 | */ |
---|
335 | nKillChar(rShortcut->cf); |
---|
336 | rShortcut->cf = nCopyCoeff(shortcutRing->cf); |
---|
337 | rComplete(rShortcut); |
---|
338 | rTest(rShortcut); |
---|
339 | |
---|
340 | ideal inI = initial(I,r,w); |
---|
341 | int k = idSize(inI); |
---|
342 | ideal inIShortcut = idInit(k); |
---|
343 | nMapFunc takingResidues = n_SetMap(r->cf,rShortcut->cf); |
---|
344 | for (int i=0; i<k; i++) |
---|
345 | inIShortcut->m[i] = p_PermPoly(inI->m[i],NULL,r,rShortcut,takingResidues,NULL,0); |
---|
346 | |
---|
347 | idSkipZeroes(inIShortcut); |
---|
348 | poly p = checkForMonomialViaSuddenSaturation(inIShortcut,rShortcut); |
---|
349 | poly monomial = NULL; |
---|
350 | if (p!=NULL) |
---|
351 | { |
---|
352 | monomial=p_One(r); |
---|
353 | for (int i=1; i<n; i++) |
---|
354 | p_SetExp(monomial,i,p_GetExp(p,i,rShortcut),r); |
---|
355 | p_Delete(&p,rShortcut); |
---|
356 | } |
---|
357 | id_Delete(&inI,r); |
---|
358 | id_Delete(&inIShortcut,rShortcut); |
---|
359 | rDelete(rShortcut); |
---|
360 | return monomial; |
---|
361 | } |
---|
362 | } |
---|
363 | std::pair<ideal,ring> flip(const ideal I, const ring r, |
---|
364 | const gfan::ZVector interiorPoint, |
---|
365 | const gfan::ZVector facetNormal) const |
---|
366 | { |
---|
367 | gfan::ZVector adjustedInteriorPoint = adjustWeightForHomogeneity(interiorPoint); |
---|
368 | gfan::ZVector adjustedFacetNormal = adjustWeightUnderHomogeneity(facetNormal,adjustedInteriorPoint); |
---|
369 | return flip0(I,r,interiorPoint,facetNormal,adjustedInteriorPoint,adjustedFacetNormal); |
---|
370 | } |
---|
371 | }; |
---|
372 | |
---|
373 | int dim(ideal I, ring r); |
---|
374 | |
---|
375 | #endif |
---|