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