1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: shiftgb.cc,v 1.6 2008-02-24 17:41:32 levandov Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: kernel: utils for shift GB and free GB |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | |
---|
11 | #ifdef HAVE_PLURAL |
---|
12 | #include "febase.h" |
---|
13 | #include "ring.h" |
---|
14 | #include "polys.h" |
---|
15 | #include "numbers.h" |
---|
16 | #include "ideals.h" |
---|
17 | #include "matpol.h" |
---|
18 | #include "kbuckets.h" |
---|
19 | #include "kstd1.h" |
---|
20 | #include "sbuckets.h" |
---|
21 | #include "p_Mult_q.h" |
---|
22 | #include "kutil.h" |
---|
23 | #include "structs.h" |
---|
24 | #include "omalloc.h" |
---|
25 | #include "khstd.h" |
---|
26 | #include "kbuckets.h" |
---|
27 | #include "weight.h" |
---|
28 | #include "intvec.h" |
---|
29 | #include "structs.h" |
---|
30 | #include "kInline.cc" |
---|
31 | #include "stairc.h" |
---|
32 | #include "weight.h" |
---|
33 | #include "intvec.h" |
---|
34 | #include "timer.h" |
---|
35 | #include "shiftgb.h" |
---|
36 | #include "sca.h" |
---|
37 | |
---|
38 | |
---|
39 | #define freeT(A,v) omFreeSize((ADDRESS)A,(v+1)*sizeof(int)) |
---|
40 | |
---|
41 | |
---|
42 | /* TODO: write p* stuff as instances of p_* for all the functions */ |
---|
43 | |
---|
44 | poly p_LPshiftT(poly p, int sh, int uptodeg, int lV, kStrategy strat, const ring r) |
---|
45 | { |
---|
46 | /* assume shift takes place, shifts the poly p by sh */ |
---|
47 | /* p is like TObject: lm in currRing = r, tail in tailRing */ |
---|
48 | |
---|
49 | if (p==NULL) return(p); |
---|
50 | |
---|
51 | assume(p_LmCheckIsFromRing(p,r)); |
---|
52 | assume(p_CheckIsFromRing(pNext(p),strat->tailRing)); |
---|
53 | |
---|
54 | /* assume sh and uptodeg agree */ |
---|
55 | |
---|
56 | if (sh == 0) return(p); /* the zero shift */ |
---|
57 | |
---|
58 | poly q = NULL; |
---|
59 | poly s = p_mLPshift(p, sh, uptodeg, lV, r); // lm in currRing |
---|
60 | poly pp = pNext(p); |
---|
61 | |
---|
62 | while (pp != NULL) |
---|
63 | { |
---|
64 | q = p_Add_q(q, p_mLPshift(pp,sh,uptodeg,lV,strat->tailRing),strat->tailRing); |
---|
65 | pIter(pp); |
---|
66 | } |
---|
67 | pNext(s) = q; |
---|
68 | /* int version: returns TRUE if it was successful */ |
---|
69 | return(s); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | poly p_LPshift(poly p, int sh, int uptodeg, int lV, const ring r) |
---|
74 | { |
---|
75 | /* assume shift takes place */ |
---|
76 | /* shifts the poly p by sh */ |
---|
77 | /* deletes p */ |
---|
78 | |
---|
79 | /* assume sh and uptodeg agree */ |
---|
80 | |
---|
81 | if (p==NULL) return(p); |
---|
82 | if (sh == 0) return(p); /* the zero shift */ |
---|
83 | |
---|
84 | poly q = NULL; |
---|
85 | poly pp = p; // pCopy(p); |
---|
86 | while (pp!=NULL) |
---|
87 | { |
---|
88 | q = p_Add_q(q, p_mLPshift(pp,sh,uptodeg,lV,r),r); |
---|
89 | pIter(pp); |
---|
90 | } |
---|
91 | /* delete pp? */ |
---|
92 | /* int version: returns TRUE if it was successful */ |
---|
93 | return(q); |
---|
94 | } |
---|
95 | |
---|
96 | poly p_mLPshift(poly p, int sh, int uptodeg, int lV, const ring r) |
---|
97 | { |
---|
98 | /* pm is a monomial */ |
---|
99 | |
---|
100 | if (sh == 0) return(p); /* the zero shift */ |
---|
101 | |
---|
102 | if (sh < 0 ) |
---|
103 | { |
---|
104 | #ifdef PDEBUG |
---|
105 | Print("pmLPshift: negative shift requested"); |
---|
106 | #endif |
---|
107 | return(NULL); /* violation, 2check */ |
---|
108 | } |
---|
109 | |
---|
110 | int L = p_mLastVblock(p,lV,r); |
---|
111 | if (L+sh-1 > uptodeg) |
---|
112 | { |
---|
113 | #ifdef PDEBUG |
---|
114 | Print("p_mLPshift: too big shift requested"); |
---|
115 | #endif |
---|
116 | return(NULL); /* violation, 2check */ |
---|
117 | } |
---|
118 | int *e=(int *)omAlloc0((r->N+1)*sizeof(int)); |
---|
119 | int *s=(int *)omAlloc0((r->N+1)*sizeof(int)); |
---|
120 | p_GetExpV(p,e,r); |
---|
121 | number c = pGetCoeff(p); |
---|
122 | int j; |
---|
123 | // for (j=1; j<=r->N; j++) |
---|
124 | // L*lV gives the last position of the last block |
---|
125 | for (j=1; j<= L*lV ; j++) |
---|
126 | { |
---|
127 | if (e[j]==1) |
---|
128 | { |
---|
129 | s[j + (sh*lV)] = e[j]; /* actually 1 */ |
---|
130 | omCheckAddr(s); |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | if (e[j]!=0) |
---|
135 | { |
---|
136 | #ifdef PDEBUG |
---|
137 | Print("p_mLPshift: ex[%d]=%d\n",j,e[j]); |
---|
138 | #endif |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | poly m = p_ISet(1,r); |
---|
143 | p_SetExpV(m,s,r); |
---|
144 | /* pSetm(m); */ /* done in the pSetExpV */ |
---|
145 | /* think on the component */ |
---|
146 | pSetCoeff0(m,c); |
---|
147 | freeT(e, r->N); |
---|
148 | freeT(s, r->N); |
---|
149 | return(m); |
---|
150 | } |
---|
151 | |
---|
152 | poly pLPshift(poly p, int sh, int uptodeg, int lV) |
---|
153 | { |
---|
154 | /* assume shift takes place */ |
---|
155 | /* shifts the poly p by sh */ |
---|
156 | /* deletes p */ |
---|
157 | |
---|
158 | /* assume sh and uptodeg agree */ |
---|
159 | |
---|
160 | if (sh == 0) return(p); /* the zero shift */ |
---|
161 | |
---|
162 | poly q = NULL; |
---|
163 | poly pp = p; // pCopy(p); |
---|
164 | while (pp!=NULL) |
---|
165 | { |
---|
166 | q = p_Add_q(q, pmLPshift(pp,sh,uptodeg,lV),currRing); |
---|
167 | pIter(pp); |
---|
168 | } |
---|
169 | /* delete pp? */ |
---|
170 | /* int version: returns TRUE if it was successful */ |
---|
171 | return(q); |
---|
172 | } |
---|
173 | |
---|
174 | poly pmLPshift(poly p, int sh, int uptodeg, int lV) |
---|
175 | { |
---|
176 | /* TODO: use a shortcut with p_ version */ |
---|
177 | /* pm is a monomial */ |
---|
178 | |
---|
179 | if (sh == 0) return(p); /* the zero shift */ |
---|
180 | |
---|
181 | if (sh < 0 ) |
---|
182 | { |
---|
183 | #ifdef PDEBUG |
---|
184 | Print("pmLPshift: negative shift requested"); |
---|
185 | #endif |
---|
186 | return(NULL); /* violation, 2check */ |
---|
187 | } |
---|
188 | |
---|
189 | int L = pmLastVblock(p,lV); |
---|
190 | if (L+sh-1 > uptodeg) |
---|
191 | { |
---|
192 | #ifdef PDEBUG |
---|
193 | Print("pmLPshift: too big shift requested"); |
---|
194 | #endif |
---|
195 | return(NULL); /* violation, 2check */ |
---|
196 | } |
---|
197 | int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int)); |
---|
198 | int *s=(int *)omAlloc0((currRing->N+1)*sizeof(int)); |
---|
199 | pGetExpV(p,e); |
---|
200 | number c = pGetCoeff(p); |
---|
201 | int j; |
---|
202 | for (j=1; j<=currRing->N; j++) |
---|
203 | { |
---|
204 | if (e[j]) |
---|
205 | { |
---|
206 | s[j + (sh*lV)] = e[j]; /* actually 1 */ |
---|
207 | } |
---|
208 | } |
---|
209 | poly m = pOne(); |
---|
210 | pSetExpV(m,s); |
---|
211 | /* pSetm(m); */ /* done in the pSetExpV */ |
---|
212 | /* think on the component */ |
---|
213 | pSetCoeff0(m,c); |
---|
214 | freeT(e, currRing->N); |
---|
215 | freeT(s, currRing->N); |
---|
216 | return(m); |
---|
217 | } |
---|
218 | |
---|
219 | int pLastVblock(poly p, int lV) |
---|
220 | { |
---|
221 | /* returns the number of maximal block */ |
---|
222 | /* appearing among the monomials of p */ |
---|
223 | /* the 0th block is the 1st one */ |
---|
224 | poly q = p; //p_Copy(p,currRing); /* need it ? */ |
---|
225 | int ans = 0; |
---|
226 | int ansnew = 0; |
---|
227 | while (q!=NULL) |
---|
228 | { |
---|
229 | ansnew = pmLastVblock(q,lV); |
---|
230 | ans = si_max(ans,ansnew); |
---|
231 | pIter(q); |
---|
232 | } |
---|
233 | /* do not need to delete q */ |
---|
234 | return(ans); |
---|
235 | } |
---|
236 | |
---|
237 | int pmLastVblock(poly p, int lV) |
---|
238 | { |
---|
239 | /* for a monomial p, returns the number of the last block */ |
---|
240 | /* where a nonzero exponent is sitting */ |
---|
241 | if (pIsConstantPoly(p)) |
---|
242 | { |
---|
243 | return(int(0)); |
---|
244 | } |
---|
245 | int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int)); |
---|
246 | pGetExpV(p,e); |
---|
247 | int j,b; |
---|
248 | j = currRing->N; |
---|
249 | while ( (!e[j]) && (j>=1) ) j--; |
---|
250 | if (j==0) |
---|
251 | { |
---|
252 | #ifdef PDEBUG |
---|
253 | Print("pmLastVblock: unexpected zero exponent vector"); |
---|
254 | PrintLn(); |
---|
255 | #endif |
---|
256 | return(j); |
---|
257 | } |
---|
258 | b = (int)(j/lV) + 1; /* the number of the block, >=1 */ |
---|
259 | return (b); |
---|
260 | } |
---|
261 | |
---|
262 | int p_LastVblockT(poly p, int lV, kStrategy strat, const ring r) |
---|
263 | { |
---|
264 | /* returns the number of maximal block */ |
---|
265 | /* appearing among the monomials of p */ |
---|
266 | /* the 0th block is the 1st one */ |
---|
267 | |
---|
268 | /* p is like TObject: lm in currRing = r, tail in tailRing */ |
---|
269 | assume(p_LmCheckIsFromRing(p,r)); |
---|
270 | assume(p_CheckIsFromRing(pNext(p),strat->tailRing)); |
---|
271 | |
---|
272 | int ans = p_mLastVblock(p, lV, r); // Block of LM |
---|
273 | poly q = pNext(p); |
---|
274 | int ansnew = 0; |
---|
275 | while (q != NULL) |
---|
276 | { |
---|
277 | ansnew = p_mLastVblock(q, lV, strat->tailRing); |
---|
278 | ans = si_max(ans,ansnew); |
---|
279 | pIter(q); |
---|
280 | } |
---|
281 | /* do not need to delete q */ |
---|
282 | return(ans); |
---|
283 | } |
---|
284 | |
---|
285 | int p_LastVblock(poly p, int lV, const ring r) |
---|
286 | { |
---|
287 | /* returns the number of maximal block */ |
---|
288 | /* appearing among the monomials of p */ |
---|
289 | /* the 0th block is the 1st one */ |
---|
290 | poly q = p; //p_Copy(p,currRing); /* need it ? */ |
---|
291 | int ans = 0; |
---|
292 | int ansnew = 0; |
---|
293 | while (q!=NULL) |
---|
294 | { |
---|
295 | ansnew = p_mLastVblock(q, lV, r); |
---|
296 | ans = si_max(ans,ansnew); |
---|
297 | pIter(q); |
---|
298 | } |
---|
299 | /* do not need to delete q */ |
---|
300 | return(ans); |
---|
301 | } |
---|
302 | |
---|
303 | int p_mLastVblock(poly p, int lV, const ring r) |
---|
304 | { |
---|
305 | /* for a monomial p, returns the number of the last block */ |
---|
306 | /* where a nonzero exponent is sitting */ |
---|
307 | if (p_LmIsConstant(p,r)) |
---|
308 | { |
---|
309 | return(0); |
---|
310 | } |
---|
311 | int *e=(int *)omAlloc0((r->N+1)*sizeof(int)); |
---|
312 | p_GetExpV(p,e,r); |
---|
313 | int j,b; |
---|
314 | j = r->N; |
---|
315 | while ( (!e[j]) && (j>=1) ) j--; |
---|
316 | if (j==0) |
---|
317 | { |
---|
318 | #ifdef PDEBUG |
---|
319 | Print("pmLastVblock: unexpected zero exponent vector"); |
---|
320 | PrintLn(); |
---|
321 | #endif |
---|
322 | return(j); |
---|
323 | } |
---|
324 | b = (int)((j+lV-1)/lV); /* the number of the block, >=1 */ |
---|
325 | freeT(e,r->N); |
---|
326 | return (b); |
---|
327 | } |
---|
328 | |
---|
329 | int pFirstVblock(poly p, int lV) |
---|
330 | { |
---|
331 | /* returns the number of maximal block */ |
---|
332 | /* appearing among the monomials of p */ |
---|
333 | /* the 0th block is the 1st one */ |
---|
334 | poly q = p; //p_Copy(p,currRing); /* need it ? */ |
---|
335 | int ans = 0; |
---|
336 | int ansnew = 0; |
---|
337 | while (q!=NULL) |
---|
338 | { |
---|
339 | ansnew = pmFirstVblock(q,lV); |
---|
340 | ans = si_min(ans,ansnew); |
---|
341 | pIter(q); |
---|
342 | } |
---|
343 | /* do not need to delete q */ |
---|
344 | return(ans); |
---|
345 | } |
---|
346 | |
---|
347 | int pmFirstVblock(poly p, int lV) |
---|
348 | { |
---|
349 | if (pIsConstantPoly(p)) |
---|
350 | { |
---|
351 | return(int(0)); |
---|
352 | } |
---|
353 | /* for a monomial p, returns the number of the first block */ |
---|
354 | /* where a nonzero exponent is sitting */ |
---|
355 | int *e=(int *)omAlloc0((currRing->N+1)*sizeof(int)); |
---|
356 | pGetExpV(p,e); |
---|
357 | int j,b; |
---|
358 | j = 1; |
---|
359 | while ( (!e[j]) && (j<=currRing->N-1) ) j++; |
---|
360 | if (j==currRing->N + 1) |
---|
361 | { |
---|
362 | #ifdef PDEBUG |
---|
363 | Print("pmFirstVblock: unexpected zero exponent vector"); |
---|
364 | PrintLn(); |
---|
365 | #endif |
---|
366 | return(j); |
---|
367 | } |
---|
368 | b = (int)(j/lV)+1; /* the number of the block, 1<= N <= currRing->N */ |
---|
369 | return (b); |
---|
370 | } |
---|
371 | |
---|
372 | /* there should be two routines: */ |
---|
373 | /* 1. tests squarefreeness: in homog this suffices */ |
---|
374 | /* 2. test the presence of a hole -> in the tail??? */ |
---|
375 | |
---|
376 | int isInV(poly p, int lV) |
---|
377 | { |
---|
378 | |
---|
379 | if (lV <= 0) return(0); |
---|
380 | /* returns 1 iff p is in V */ |
---|
381 | /* that is in each block up to a certain one there is only one nonzero exponent */ |
---|
382 | /* lV = the length of V = the number of orig vars */ |
---|
383 | int *e = (int *)omAlloc0((currRing->N+1)*sizeof(int)); |
---|
384 | int b = (int)(currRing->N)/lV; /* the number of blocks */ |
---|
385 | int *B = (int *)omAlloc0((b+1)*sizeof(int)); /* the num of elements in a block */ |
---|
386 | pGetExpV(p,e); |
---|
387 | int i,j; |
---|
388 | for (j=1; j<=b; j++) |
---|
389 | { |
---|
390 | /* we go through all the vars */ |
---|
391 | /* by blocks in lV vars */ |
---|
392 | for (i=(j-1)*lV + 1; i<= j*lV; i++) |
---|
393 | { |
---|
394 | if (e[i]) B[j] = B[j]+1; |
---|
395 | } |
---|
396 | } |
---|
397 | j = b; |
---|
398 | // while ( (!B[j]) && (j>=1)) j--; |
---|
399 | for (j=b; j>=1; j--) |
---|
400 | { |
---|
401 | if (B[j]!=0) break; |
---|
402 | } |
---|
403 | |
---|
404 | if (j==0) |
---|
405 | { |
---|
406 | /* it is a zero exp vector, which is in V */ |
---|
407 | return(1); |
---|
408 | } |
---|
409 | /* now B[j] != 0 */ |
---|
410 | for (j; j>=1; j--) |
---|
411 | { |
---|
412 | if (B[j]!=1) |
---|
413 | { |
---|
414 | return(0); |
---|
415 | } |
---|
416 | } |
---|
417 | return(1); |
---|
418 | } |
---|
419 | |
---|
420 | int itoInsert(poly p, int uptodeg, int lV, const ring r) |
---|
421 | { |
---|
422 | /* for poly in lmCR/tailTR presentation */ |
---|
423 | /* the below situation might happen! */ |
---|
424 | // if (r == currRing) |
---|
425 | // { |
---|
426 | // "Current ring is not expected in toInsert"; |
---|
427 | // return(0); |
---|
428 | // } |
---|
429 | /* compute the number of insertions */ |
---|
430 | int i = p_mLastVblock(p, lV, currRing); |
---|
431 | if (pNext(p) != NULL) |
---|
432 | { |
---|
433 | i = si_max(i, p_LastVblock(pNext(p), lV, r) ); |
---|
434 | } |
---|
435 | // i = uptodeg - i +1; |
---|
436 | i = uptodeg - i; |
---|
437 | // p_wrp(p,currRing,r); Print("----i:%d",i); PrintLn(); |
---|
438 | return(i); |
---|
439 | } |
---|
440 | |
---|
441 | |
---|
442 | /* shiftgb stuff */ |
---|
443 | |
---|
444 | |
---|
445 | /* remarks: cleanT : just deletion |
---|
446 | enlargeT: just reallocation */ |
---|
447 | |
---|
448 | #endif |
---|