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 <tropicalCurves.h> |
---|
11 | #include <set> |
---|
12 | #ifndef NDEBUG |
---|
13 | #include <bbfan.h> |
---|
14 | #endif |
---|
15 | |
---|
16 | std::set<gfan::ZCone> intersect(const std::set<gfan::ZCone> setA, const std::set<gfan::ZCone> setB, int d) |
---|
17 | { |
---|
18 | std::set<gfan::ZCone> setAB; |
---|
19 | for (std::set<gfan::ZCone>::iterator coneOfA=setA.begin(); coneOfA!=setA.end(); coneOfA++) |
---|
20 | { |
---|
21 | for (std::set<gfan::ZCone>::iterator coneOfB=setB.begin(); coneOfB!=setB.end(); coneOfB++) |
---|
22 | { |
---|
23 | gfan::ZCone coneOfIntersection = gfan::intersection(*coneOfA,*coneOfB); |
---|
24 | if (coneOfIntersection.dimension()>=d) |
---|
25 | { |
---|
26 | coneOfIntersection.canonicalize(); |
---|
27 | setAB.insert(coneOfIntersection); |
---|
28 | } |
---|
29 | } |
---|
30 | } |
---|
31 | return setAB; |
---|
32 | } |
---|
33 | |
---|
34 | /*** |
---|
35 | * Given a relative interior point w of a cone in the weight space |
---|
36 | * and generators E of its span, returns a ring with an ordering that coincides |
---|
37 | * with a weighted ordering on the ideal with respect to a weight in the cone |
---|
38 | * which does not lie on a Groebner cone of lower dimension than that of the cone. |
---|
39 | **/ |
---|
40 | static ring ringWithGenericlyWeightedOrdering(const ring r, |
---|
41 | const gfan::ZVector w, |
---|
42 | const gfan::ZMatrix E, |
---|
43 | const tropicalStrategy currentCase) |
---|
44 | { |
---|
45 | gfan::ZVector (*adjustWeight)(gfan::ZVector v, gfan::ZVector w); |
---|
46 | adjustWeight = currentCase.adjustWeightUnderHomogeneity; |
---|
47 | |
---|
48 | int n = r->N; |
---|
49 | int h = E.getHeight(); |
---|
50 | |
---|
51 | // create a copy of r and delete its old ordering |
---|
52 | ring s = rCopy0(r); |
---|
53 | omFree(s->order); |
---|
54 | s->order = (int*) omAlloc0((h+3)*sizeof(int)); |
---|
55 | omFree(s->block0); |
---|
56 | s->block0 = (int*) omAlloc0((h+3)*sizeof(int)); |
---|
57 | omFree(s->block1); |
---|
58 | s->block1 = (int*) omAlloc0((h+3)*sizeof(int)); |
---|
59 | for (int j=0; s->wvhdl[j]; j++) omFree(s->wvhdl[j]); |
---|
60 | omFree(s->wvhdl); |
---|
61 | s->wvhdl = (int**) omAlloc0((h+3)*sizeof(int*)); |
---|
62 | |
---|
63 | // construct a new ordering and keep an eye out for weight overflows |
---|
64 | bool overflow; |
---|
65 | s->order[0] = ringorder_a; |
---|
66 | s->block0[0] = 1; |
---|
67 | s->block1[0] = n; |
---|
68 | s->wvhdl[0] = ZVectorToIntStar(w,overflow); |
---|
69 | for (int j=1; j<h; j++) |
---|
70 | { |
---|
71 | s->order[j] = ringorder_a; |
---|
72 | s->block0[j] = 1; |
---|
73 | s->block1[j] = n; |
---|
74 | s->wvhdl[j] = ZVectorToIntStar(adjustWeight(E[j-1],w),overflow); |
---|
75 | } |
---|
76 | s->order[h] = ringorder_wp; |
---|
77 | s->block0[h] = 1; |
---|
78 | s->block1[h] = n; |
---|
79 | s->wvhdl[h] = ZVectorToIntStar(adjustWeight(E[h-1],w),overflow); |
---|
80 | s->order[h+1] = ringorder_C; |
---|
81 | |
---|
82 | rComplete(s,1); |
---|
83 | // Return s. |
---|
84 | if (overflow) |
---|
85 | throw 0; //weightOverflow; |
---|
86 | return s; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /*** |
---|
91 | * Computes the tropical curve of an x-homogeneous ideal I |
---|
92 | * which is weighted homogeneous with respect to weight w in ring r |
---|
93 | **/ |
---|
94 | std::set<gfan::ZCone> tropicalCurve(const ideal I, const ring r, const int d, |
---|
95 | const tropicalStrategy currentCase) |
---|
96 | { |
---|
97 | int k = idSize(I); |
---|
98 | std::set<gfan::ZCone> (*tropicalVariety)(const poly &g, const ring &r); |
---|
99 | tropicalVariety = currentCase.tropicalVarietyOfPolynomial; |
---|
100 | // Compute the common refinement of the tropical varieties |
---|
101 | // of the generating set |
---|
102 | std::set<gfan::ZCone> C = tropicalVariety(I->m[0],r); |
---|
103 | for (int i=1; i<k; i++) |
---|
104 | C = intersect(C,tropicalVariety(I->m[i],r),d); |
---|
105 | |
---|
106 | // cycle through all maximal cones of the refinement |
---|
107 | // pick a monomial ordering corresponding to a generic weight vector in it |
---|
108 | // check if the initial ideal is monomial free |
---|
109 | // if the initial ideal is not monomial free, pick a witness for the monomial |
---|
110 | // and intersect with its corresponding tropical variety |
---|
111 | // if all initial ideals are monomial free, the we have our tropical curve |
---|
112 | poly mon = NULL; |
---|
113 | do |
---|
114 | { |
---|
115 | for (std::set<gfan::ZCone>::iterator zc=C.begin(); zc!=C.end(); zc++) |
---|
116 | { |
---|
117 | gfan::ZVector v = zc->getRelativeInteriorPoint(); |
---|
118 | gfan::ZMatrix E = zc->generatorsOfSpan(); |
---|
119 | |
---|
120 | // std::cout << "interiorPoint: " << v << std::endl; |
---|
121 | // std::cout << "generators of span: " << E << std::endl; |
---|
122 | // ideal inIr = initial(I,r,E[E.getHeight()-1]); |
---|
123 | ring s = ringWithGenericlyWeightedOrdering(r,v,E,currentCase); |
---|
124 | nMapFunc nMap = n_SetMap(r->cf,s->cf); |
---|
125 | ideal Is = idInit(k); |
---|
126 | for (int j=0; j<k; j++) |
---|
127 | { |
---|
128 | Is->m[j] = p_PermPoly(I->m[j],NULL,r,s,nMap,NULL,0); |
---|
129 | p_Test(Is->m[j],s); |
---|
130 | } |
---|
131 | ideal inIs = initial(Is,s,E[E.getHeight()-1]); |
---|
132 | id_Delete(&Is,s); |
---|
133 | // id_Delete(&inIr,r); |
---|
134 | |
---|
135 | mon = checkForMonomialViaSuddenSaturation(inIs,s); |
---|
136 | if (mon) |
---|
137 | { |
---|
138 | ideal Is = idInit(k); |
---|
139 | for (int j=0; j<k; j++) |
---|
140 | { |
---|
141 | Is->m[j] = p_PermPoly(I->m[j],NULL,r,s,nMap,NULL,0); |
---|
142 | p_Test(Is->m[j],s); |
---|
143 | } |
---|
144 | poly g = witness(mon,Is,inIs,s); |
---|
145 | C = intersect(C,tropicalVariety(g,s),d); |
---|
146 | p_Delete(&mon,s); |
---|
147 | p_Delete(&g,s); |
---|
148 | id_Delete(&inIs,s); |
---|
149 | id_Delete(&Is,s); |
---|
150 | rDelete(s); |
---|
151 | break; |
---|
152 | } |
---|
153 | id_Delete(&inIs,s); |
---|
154 | rDelete(s); |
---|
155 | } |
---|
156 | } while (mon); |
---|
157 | return C; |
---|
158 | } |
---|
159 | /*** |
---|
160 | * Computes the tropical curve of an x-homogeneous ideal I |
---|
161 | * which is weighted homogeneous with respect to weight w in ring r |
---|
162 | **/ |
---|
163 | |
---|
164 | #ifndef NDEBUG |
---|
165 | BOOLEAN tropicalCurve0(leftv res, leftv args) |
---|
166 | { |
---|
167 | leftv u = args; |
---|
168 | ideal I = (ideal) u->CopyD(); |
---|
169 | leftv v = u->next; |
---|
170 | int d = (long)(int) v->CopyD(); |
---|
171 | tropicalStrategy currentCase = nonValuedCase; |
---|
172 | std::set<gfan::ZCone> C = tropicalCurve(I,currRing,d,currentCase); |
---|
173 | id_Delete(&I,currRing); |
---|
174 | omUpdateInfo(); |
---|
175 | Print("usedBytesBefore=%ld\n",om_Info.UsedBytes); |
---|
176 | res->rtyp = fanID; |
---|
177 | res->data = (char*) toFanStar(C); |
---|
178 | return FALSE; |
---|
179 | } |
---|
180 | BOOLEAN tropicalCurve1(leftv res, leftv args) |
---|
181 | { |
---|
182 | leftv u = args; |
---|
183 | ideal I = (ideal) u->CopyD(); |
---|
184 | leftv v = u->next; |
---|
185 | int d = (long)(int) v->CopyD(); |
---|
186 | tropicalStrategy currentCase = valuedCase; |
---|
187 | std::set<gfan::ZCone> C = tropicalCurve(I,currRing,d,currentCase); |
---|
188 | id_Delete(&I,currRing); |
---|
189 | omUpdateInfo(); |
---|
190 | Print("usedBytesBefore=%ld\n",om_Info.UsedBytes); |
---|
191 | res->rtyp = fanID; |
---|
192 | res->data = (char*) toFanStar(C); |
---|
193 | return FALSE; |
---|
194 | } |
---|
195 | #endif |
---|