1 | #include <gfanlib/gfanlib.h> |
---|
2 | |
---|
3 | #include <kernel/ideals.h> |
---|
4 | #include <Singular/subexpr.h> |
---|
5 | #include <libpolys/polys/monomials/p_polys.h> |
---|
6 | #include <libpolys/polys/simpleideals.h> |
---|
7 | |
---|
8 | #include <callgfanlib_conversion.h> |
---|
9 | #include <gfanlib_exceptions.h> |
---|
10 | |
---|
11 | #include <exception> |
---|
12 | |
---|
13 | /*** |
---|
14 | * Computes the weighted degree of the leading term of p with respect to w |
---|
15 | **/ |
---|
16 | long wDeg(const poly p, const ring r, const gfan::ZVector w) |
---|
17 | { |
---|
18 | long d=0; |
---|
19 | for (unsigned i=0; i<w.size(); i++) |
---|
20 | { |
---|
21 | if (!w[i].fitsInInt()) |
---|
22 | throw 0; //weightOverflow; |
---|
23 | d += p_GetExp(p,i+1,r)*w[i].toInt(); |
---|
24 | } |
---|
25 | return d; |
---|
26 | } |
---|
27 | |
---|
28 | /*** |
---|
29 | * Computes the weighted multidegree of the leading term of p with respect to W. |
---|
30 | * The weighted multidegree is a vector whose i-th entry is the weighted degree |
---|
31 | * with respect to the i-th row vector of W. |
---|
32 | **/ |
---|
33 | gfan::ZVector WDeg(const poly p, const ring r, const gfan::ZMatrix W) |
---|
34 | { |
---|
35 | gfan::ZVector d = gfan::ZVector(W.getHeight()); |
---|
36 | for (int i=0; i<W.getHeight(); i++) |
---|
37 | d[i] = wDeg(p,r,W[i]); |
---|
38 | return d; |
---|
39 | } |
---|
40 | |
---|
41 | /*** |
---|
42 | * Checks if p is sorted with respect to w. |
---|
43 | **/ |
---|
44 | static bool checkSloppyInput(const poly p, const ring r, const gfan::ZVector w) |
---|
45 | { |
---|
46 | long d = wDeg(p,r,w); |
---|
47 | for (poly currentTerm = p->next; currentTerm; pIter(currentTerm)) |
---|
48 | { |
---|
49 | long e = wDeg(currentTerm,r,w); |
---|
50 | if (e>d) |
---|
51 | return false; |
---|
52 | } |
---|
53 | return true; |
---|
54 | } |
---|
55 | |
---|
56 | /*** |
---|
57 | * Returns the terms of p of same weighted degree under w as the leading term. |
---|
58 | * Coincides with the initial form of p with respect to w if and only if p was already |
---|
59 | * sorted with respect to w in the sense that the leading term is of highest w-degree. |
---|
60 | **/ |
---|
61 | poly sloppyInitial(const poly p, const ring r, const gfan::ZVector w) |
---|
62 | { |
---|
63 | assume(checkSloppyInput(p,r,w)); |
---|
64 | int n = rVar(r); |
---|
65 | int* expv = (int*) omAlloc(n*sizeof(int)); |
---|
66 | poly q0 = p_Head(p,r); |
---|
67 | poly q1 = q0; |
---|
68 | long d = wDeg(p,r,w); |
---|
69 | for (poly currentTerm = p->next; currentTerm; pIter(currentTerm)) |
---|
70 | { |
---|
71 | if (wDeg(currentTerm,r,w) == d) |
---|
72 | { |
---|
73 | pNext(q1) = p_Head(currentTerm,r); |
---|
74 | pIter(q1); |
---|
75 | } |
---|
76 | } |
---|
77 | omFreeSize(expv,n*sizeof(int)); |
---|
78 | return q0; |
---|
79 | } |
---|
80 | |
---|
81 | /*** |
---|
82 | * Runs the above procedure over all generators of an ideal. |
---|
83 | * Coincides with the initial ideal of I with respect to w if and only if |
---|
84 | * the elements of I were already sorted with respect to w and |
---|
85 | * I is a standard basis form with respect to w. |
---|
86 | **/ |
---|
87 | ideal sloppyInitial(const ideal I, const ring r, const gfan::ZVector w) |
---|
88 | { |
---|
89 | int k = idSize(I); ideal inI = idInit(k); |
---|
90 | for (int i=0; i<k; i++) |
---|
91 | inI->m[i] = sloppyInitial(I->m[i],r,w); |
---|
92 | return inI; |
---|
93 | } |
---|
94 | |
---|
95 | /*** |
---|
96 | * Returns the initial form of p with respect to w |
---|
97 | **/ |
---|
98 | poly initial(const poly p, const ring r, const gfan::ZVector w) |
---|
99 | { |
---|
100 | poly q0 = p_Head(p,r); |
---|
101 | poly q1 = q0; |
---|
102 | long d = wDeg(p,r,w); |
---|
103 | for (poly currentTerm = p->next; currentTerm; pIter(currentTerm)) |
---|
104 | { |
---|
105 | long e = wDeg(currentTerm,r,w); |
---|
106 | if (e>d) |
---|
107 | { |
---|
108 | p_Delete(&q0,r); |
---|
109 | q0 = p_Head(p,r); |
---|
110 | q1 = q0; |
---|
111 | d = e; |
---|
112 | } |
---|
113 | else |
---|
114 | if (e==d) |
---|
115 | { |
---|
116 | pNext(q1) = p_Head(currentTerm,r); |
---|
117 | pIter(q1); |
---|
118 | } |
---|
119 | } |
---|
120 | return q0; |
---|
121 | } |
---|
122 | |
---|
123 | /*** |
---|
124 | * Runs the above procedure over all generators of an ideal. |
---|
125 | * Returns the initial ideal if and only if the weight is in the maximal Groebner cone |
---|
126 | * of the current ordering. |
---|
127 | **/ |
---|
128 | ideal initial(const ideal I, const ring r, const gfan::ZVector w) |
---|
129 | { |
---|
130 | idSkipZeroes(I); |
---|
131 | int k = idSize(I); ideal inI = idInit(k); |
---|
132 | for (int i=0; i<k; i++) |
---|
133 | inI->m[i] = initial(I->m[i],r,w); |
---|
134 | return inI; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | /*** |
---|
139 | * Returns the initial form of p with respect to W, |
---|
140 | * i.e. the sum over all terms of p with highest multidegree with respect to W. |
---|
141 | **/ |
---|
142 | poly initial(const poly p, const ring r, const gfan::ZMatrix W) |
---|
143 | { |
---|
144 | int n = rVar(r); |
---|
145 | poly q0 = p_Head(p,r); |
---|
146 | poly q1 = q0; |
---|
147 | gfan::ZVector d = WDeg(p,r,W); |
---|
148 | for (poly currentTerm = p->next; currentTerm; pIter(currentTerm)) |
---|
149 | { |
---|
150 | gfan::ZVector e = WDeg(currentTerm,r,W); |
---|
151 | if (d<e) |
---|
152 | { |
---|
153 | p_Delete(&q0,r); |
---|
154 | q0 = p_Head(p,r); |
---|
155 | q1 = q0; |
---|
156 | d = e; |
---|
157 | } |
---|
158 | else |
---|
159 | if (d==e) |
---|
160 | { |
---|
161 | pNext(q1) = p_Head(currentTerm,r); |
---|
162 | pIter(q1); |
---|
163 | } |
---|
164 | } |
---|
165 | return q0; |
---|
166 | } |
---|
167 | |
---|
168 | /*** |
---|
169 | * Runs the above procedure over all generators of an ideal. |
---|
170 | * Returns the initial ideal if and only if the weight is in the maximal Groebner cone |
---|
171 | * of the current ordering. |
---|
172 | **/ |
---|
173 | ideal initial(const ideal I, const ring r, const gfan::ZMatrix W) |
---|
174 | { |
---|
175 | int k = idSize(I); ideal inI = idInit(k); |
---|
176 | for (int i=0; i<k; i++) |
---|
177 | inI->m[i] = initial(I->m[i],r,W); |
---|
178 | return inI; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | #ifndef NDEBUG |
---|
183 | BOOLEAN initial0(leftv res, leftv args) |
---|
184 | { |
---|
185 | leftv u = args; |
---|
186 | ideal I = (ideal) u->CopyD(); |
---|
187 | leftv v = u->next; |
---|
188 | bigintmat* w0 = (bigintmat*) v->Data(); |
---|
189 | gfan::ZVector* w = bigintmatToZVector(w0); |
---|
190 | omUpdateInfo(); |
---|
191 | Print("usedBytesBefore=%ld\n",om_Info.UsedBytes); |
---|
192 | ideal inI = initial(I,currRing,*w); |
---|
193 | id_Delete(&I,currRing); |
---|
194 | delete w; |
---|
195 | res->rtyp = IDEAL_CMD; |
---|
196 | res->data = (char*) inI; |
---|
197 | return FALSE; |
---|
198 | } |
---|
199 | #endif |
---|
200 | |
---|
201 | |
---|
202 | /*** |
---|
203 | * Computes the initial form of p with respect to the first row in the order matrix |
---|
204 | **/ |
---|
205 | poly initial(const poly p, const ring r) |
---|
206 | { |
---|
207 | long d = p_Deg(p,r); |
---|
208 | poly initialForm0 = p_Head(p,r); |
---|
209 | poly initialForm1 = initialForm0; |
---|
210 | poly currentTerm = p->next; |
---|
211 | while (currentTerm && p_Deg(currentTerm,r)==d) |
---|
212 | { |
---|
213 | pNext(initialForm1) = p_Head(currentTerm,r); |
---|
214 | pIter(currentTerm); pIter(initialForm1); |
---|
215 | } |
---|
216 | return initialForm0; |
---|
217 | } |
---|
218 | |
---|
219 | /*** |
---|
220 | * Computes the initial form of all generators of I. |
---|
221 | * If I is a standard basis, then this is a standard basis |
---|
222 | * of the initial ideal. |
---|
223 | **/ |
---|
224 | ideal initial(const ideal I, const ring r) |
---|
225 | { |
---|
226 | int k = idSize(I); ideal inI = idInit(k); |
---|
227 | for (int i=0; i<k; i++) |
---|
228 | inI->m[i] = initial(I->m[i],r); |
---|
229 | return inI; |
---|
230 | } |
---|
231 | |
---|
232 | BOOLEAN initial(leftv res, leftv args) |
---|
233 | { |
---|
234 | leftv u = args; |
---|
235 | if ((u != NULL) && (u->Typ() == POLY_CMD) && (u->next == NULL)) |
---|
236 | { |
---|
237 | poly p = (poly) u->Data(); |
---|
238 | res->rtyp = POLY_CMD; |
---|
239 | res->data = (void*) initial(p, currRing); |
---|
240 | return FALSE; |
---|
241 | } |
---|
242 | if ((u != NULL) && (u->Typ() == IDEAL_CMD) && (u->next == NULL)) |
---|
243 | { |
---|
244 | ideal I = (ideal) u->Data(); |
---|
245 | res->rtyp = IDEAL_CMD; |
---|
246 | res->data = (void*) initial(I, currRing); |
---|
247 | return FALSE; |
---|
248 | } |
---|
249 | WerrorS("initial: unexpected parameters"); |
---|
250 | return TRUE; |
---|
251 | } |
---|