1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | /* |
---|
6 | * ABSTRACT - all basic methods to manipulate ideals |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | /* includes */ |
---|
11 | #include "config.h" |
---|
12 | #include <misc/auxiliary.h> |
---|
13 | |
---|
14 | #include <omalloc/omalloc.h> |
---|
15 | |
---|
16 | #include <misc/options.h> |
---|
17 | #include <misc/intvec.h> |
---|
18 | |
---|
19 | // #include <coeffs/longrat.h> |
---|
20 | #include "matpol.h" |
---|
21 | |
---|
22 | #include "monomials/p_polys.h" |
---|
23 | #include "weight.h" |
---|
24 | #include "sbuckets.h" |
---|
25 | #include "clapsing.h" |
---|
26 | |
---|
27 | #include "simpleideals.h" |
---|
28 | |
---|
29 | omBin sip_sideal_bin = omGetSpecBin(sizeof(sip_sideal)); |
---|
30 | |
---|
31 | static poly * idpower; |
---|
32 | /*collects the monomials in makemonoms, must be allocated befor*/ |
---|
33 | static int idpowerpoint; |
---|
34 | /*index of the actual monomial in idpower*/ |
---|
35 | static poly * givenideal; |
---|
36 | /*the ideal from which a power is computed*/ |
---|
37 | |
---|
38 | /*2 |
---|
39 | * initialise an ideal |
---|
40 | */ |
---|
41 | ideal idInit(int idsize, int rank) |
---|
42 | { |
---|
43 | /*- initialise an ideal -*/ |
---|
44 | ideal hh = (ideal )omAllocBin(sip_sideal_bin); |
---|
45 | hh->nrows = 1; |
---|
46 | hh->rank = rank; |
---|
47 | IDELEMS(hh) = idsize; |
---|
48 | if (idsize>0) |
---|
49 | { |
---|
50 | hh->m = (poly *)omAlloc0(idsize*sizeof(poly)); |
---|
51 | } |
---|
52 | else |
---|
53 | hh->m=NULL; |
---|
54 | return hh; |
---|
55 | } |
---|
56 | |
---|
57 | #ifdef PDEBUG |
---|
58 | // this is only for outputting an ideal within the debugger |
---|
59 | void idShow(const ideal id, const ring lmRing, const ring tailRing, const int debugPrint) |
---|
60 | { |
---|
61 | assume( debugPrint >= 0 ); |
---|
62 | |
---|
63 | if( id == NULL ) |
---|
64 | PrintS("(NULL)"); |
---|
65 | else |
---|
66 | { |
---|
67 | Print("Module of rank %ld,real rank %ld and %d generators.\n", |
---|
68 | id->rank,id_RankFreeModule(id, lmRing, tailRing),IDELEMS(id)); |
---|
69 | |
---|
70 | int j = (id->ncols*id->nrows) - 1; |
---|
71 | while ((j > 0) && (id->m[j]==NULL)) j--; |
---|
72 | for (int i = 0; i <= j; i++) |
---|
73 | { |
---|
74 | Print("generator %d: ",i); p_DebugPrint(id->m[i], lmRing, tailRing, debugPrint); |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | #endif |
---|
79 | |
---|
80 | /* index of generator with leading term in ground ring (if any); |
---|
81 | otherwise -1 */ |
---|
82 | int id_PosConstant(ideal id, const ring r) |
---|
83 | { |
---|
84 | int k; |
---|
85 | for (k = IDELEMS(id)-1; k>=0; k--) |
---|
86 | { |
---|
87 | if (p_LmIsConstantComp(id->m[k], r) == TRUE) |
---|
88 | return k; |
---|
89 | } |
---|
90 | return -1; |
---|
91 | } |
---|
92 | |
---|
93 | /*2 |
---|
94 | * initialise the maximal ideal (at 0) |
---|
95 | */ |
---|
96 | ideal id_MaxIdeal (const ring r) |
---|
97 | { |
---|
98 | int l; |
---|
99 | ideal hh=NULL; |
---|
100 | |
---|
101 | hh=idInit(rVar(r),1); |
---|
102 | for (l=0; l<rVar(r); l++) |
---|
103 | { |
---|
104 | hh->m[l] = p_One(r); |
---|
105 | p_SetExp(hh->m[l],l+1,1,r); |
---|
106 | p_Setm(hh->m[l],r); |
---|
107 | } |
---|
108 | return hh; |
---|
109 | } |
---|
110 | |
---|
111 | /*2 |
---|
112 | * deletes an ideal/matrix |
---|
113 | */ |
---|
114 | void id_Delete (ideal * h, ring r) |
---|
115 | { |
---|
116 | int j,elems; |
---|
117 | if (*h == NULL) |
---|
118 | return; |
---|
119 | elems=j=(*h)->nrows*(*h)->ncols; |
---|
120 | if (j>0) |
---|
121 | { |
---|
122 | do |
---|
123 | { |
---|
124 | p_Delete(&((*h)->m[--j]), r); |
---|
125 | } |
---|
126 | while (j>0); |
---|
127 | omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems); |
---|
128 | } |
---|
129 | omFreeBin((ADDRESS)*h, sip_sideal_bin); |
---|
130 | *h=NULL; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /*2 |
---|
135 | * Shallowdeletes an ideal/matrix |
---|
136 | */ |
---|
137 | void id_ShallowDelete (ideal *h, ring r) |
---|
138 | { |
---|
139 | int j,elems; |
---|
140 | if (*h == NULL) |
---|
141 | return; |
---|
142 | elems=j=(*h)->nrows*(*h)->ncols; |
---|
143 | if (j>0) |
---|
144 | { |
---|
145 | do |
---|
146 | { |
---|
147 | p_ShallowDelete(&((*h)->m[--j]), r); |
---|
148 | } |
---|
149 | while (j>0); |
---|
150 | omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems); |
---|
151 | } |
---|
152 | omFreeBin((ADDRESS)*h, sip_sideal_bin); |
---|
153 | *h=NULL; |
---|
154 | } |
---|
155 | |
---|
156 | /*2 |
---|
157 | *gives an ideal the minimal possible size |
---|
158 | */ |
---|
159 | void idSkipZeroes (ideal ide) |
---|
160 | { |
---|
161 | int k; |
---|
162 | int j = -1; |
---|
163 | BOOLEAN change=FALSE; |
---|
164 | for (k=0; k<IDELEMS(ide); k++) |
---|
165 | { |
---|
166 | if (ide->m[k] != NULL) |
---|
167 | { |
---|
168 | j++; |
---|
169 | if (change) |
---|
170 | { |
---|
171 | ide->m[j] = ide->m[k]; |
---|
172 | } |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | change=TRUE; |
---|
177 | } |
---|
178 | } |
---|
179 | if (change) |
---|
180 | { |
---|
181 | if (j == -1) |
---|
182 | j = 0; |
---|
183 | else |
---|
184 | { |
---|
185 | for (k=j+1; k<IDELEMS(ide); k++) |
---|
186 | ide->m[k] = NULL; |
---|
187 | } |
---|
188 | pEnlargeSet(&(ide->m),IDELEMS(ide),j+1-IDELEMS(ide)); |
---|
189 | IDELEMS(ide) = j+1; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /*2 |
---|
194 | * copies the first k (>= 1) entries of the given ideal |
---|
195 | * and returns these as a new ideal |
---|
196 | * (Note that the copied polynomials may be zero.) |
---|
197 | */ |
---|
198 | ideal id_CopyFirstK (const ideal ide, const int k,const ring r) |
---|
199 | { |
---|
200 | ideal newI = idInit(k, 0); |
---|
201 | for (int i = 0; i < k; i++) |
---|
202 | newI->m[i] = p_Copy(ide->m[i],r); |
---|
203 | return newI; |
---|
204 | } |
---|
205 | |
---|
206 | /*2 |
---|
207 | * ideal id = (id[i]) |
---|
208 | * result is leadcoeff(id[i]) = 1 |
---|
209 | */ |
---|
210 | void id_Norm(ideal id, const ring r) |
---|
211 | { |
---|
212 | for (int i=IDELEMS(id)-1; i>=0; i--) |
---|
213 | { |
---|
214 | if (id->m[i] != NULL) |
---|
215 | { |
---|
216 | p_Norm(id->m[i],r); |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /*2 |
---|
222 | * ideal id = (id[i]), c any unit |
---|
223 | * if id[i] = c*id[j] then id[j] is deleted for j > i |
---|
224 | */ |
---|
225 | void id_DelMultiples(ideal id, const ring r) |
---|
226 | { |
---|
227 | int i, j; |
---|
228 | int k = IDELEMS(id)-1; |
---|
229 | for (i=k; i>=0; i--) |
---|
230 | { |
---|
231 | if (id->m[i]!=NULL) |
---|
232 | { |
---|
233 | for (j=k; j>i; j--) |
---|
234 | { |
---|
235 | if (id->m[j]!=NULL) |
---|
236 | { |
---|
237 | #ifdef HAVE_RINGS |
---|
238 | if (rField_is_Ring(r)) |
---|
239 | { |
---|
240 | /* if id[j] = c*id[i] then delete id[j]. |
---|
241 | In the below cases of a ground field, we |
---|
242 | check whether id[i] = c*id[j] and, if so, |
---|
243 | delete id[j] for historical reasons (so |
---|
244 | that previous output does not change) */ |
---|
245 | if (p_ComparePolys(id->m[j], id->m[i],r)) p_Delete(&id->m[j],r); |
---|
246 | } |
---|
247 | else |
---|
248 | { |
---|
249 | if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r); |
---|
250 | } |
---|
251 | #else |
---|
252 | if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r); |
---|
253 | #endif |
---|
254 | } |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | /*2 |
---|
261 | * ideal id = (id[i]) |
---|
262 | * if id[i] = id[j] then id[j] is deleted for j > i |
---|
263 | */ |
---|
264 | void id_DelEquals(ideal id, const ring r) |
---|
265 | { |
---|
266 | int i, j; |
---|
267 | int k = IDELEMS(id)-1; |
---|
268 | for (i=k; i>=0; i--) |
---|
269 | { |
---|
270 | if (id->m[i]!=NULL) |
---|
271 | { |
---|
272 | for (j=k; j>i; j--) |
---|
273 | { |
---|
274 | if ((id->m[j]!=NULL) |
---|
275 | && (p_EqualPolys(id->m[i], id->m[j],r))) |
---|
276 | { |
---|
277 | p_Delete(&id->m[j],r); |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | // |
---|
285 | // Delete id[j], if Lm(j) == Lm(i) and both LC(j), LC(i) are units and j > i |
---|
286 | // |
---|
287 | void id_DelLmEquals(ideal id, const ring r) |
---|
288 | { |
---|
289 | int i, j; |
---|
290 | int k = IDELEMS(id)-1; |
---|
291 | for (i=k; i>=0; i--) |
---|
292 | { |
---|
293 | if (id->m[i] != NULL) |
---|
294 | { |
---|
295 | for (j=k; j>i; j--) |
---|
296 | { |
---|
297 | if ((id->m[j] != NULL) |
---|
298 | && p_LmEqual(id->m[i], id->m[j],r) |
---|
299 | #ifdef HAVE_RINGS |
---|
300 | && n_IsUnit(pGetCoeff(id->m[i]),r->cf) && n_IsUnit(pGetCoeff(id->m[j]),r->cf) |
---|
301 | #endif |
---|
302 | ) |
---|
303 | { |
---|
304 | p_Delete(&id->m[j],r); |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | // |
---|
312 | // delete id[j], if LT(j) == coeff*mon*LT(i) and vice versa, i.e., |
---|
313 | // delete id[i], if LT(i) == coeff*mon*LT(j) |
---|
314 | // |
---|
315 | void id_DelDiv(ideal id, const ring r) |
---|
316 | { |
---|
317 | int i, j; |
---|
318 | int k = IDELEMS(id)-1; |
---|
319 | for (i=k; i>=0; i--) |
---|
320 | { |
---|
321 | if (id->m[i] != NULL) |
---|
322 | { |
---|
323 | for (j=k; j>i; j--) |
---|
324 | { |
---|
325 | if (id->m[j]!=NULL) |
---|
326 | { |
---|
327 | #ifdef HAVE_RINGS |
---|
328 | if (rField_is_Ring(r)) |
---|
329 | { |
---|
330 | if (p_DivisibleByRingCase(id->m[i], id->m[j],r)) |
---|
331 | { |
---|
332 | p_Delete(&id->m[j],r); |
---|
333 | } |
---|
334 | else if (p_DivisibleByRingCase(id->m[j], id->m[i],r)) |
---|
335 | { |
---|
336 | p_Delete(&id->m[i],r); |
---|
337 | break; |
---|
338 | } |
---|
339 | } |
---|
340 | else |
---|
341 | { |
---|
342 | #endif |
---|
343 | /* the case of a ground field: */ |
---|
344 | if (p_DivisibleBy(id->m[i], id->m[j],r)) |
---|
345 | { |
---|
346 | p_Delete(&id->m[j],r); |
---|
347 | } |
---|
348 | else if (p_DivisibleBy(id->m[j], id->m[i],r)) |
---|
349 | { |
---|
350 | p_Delete(&id->m[i],r); |
---|
351 | break; |
---|
352 | } |
---|
353 | #ifdef HAVE_RINGS |
---|
354 | } |
---|
355 | #endif |
---|
356 | } |
---|
357 | } |
---|
358 | } |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | /*2 |
---|
363 | *test if the ideal has only constant polynomials |
---|
364 | */ |
---|
365 | BOOLEAN id_IsConstant(ideal id, const ring r) |
---|
366 | { |
---|
367 | int k; |
---|
368 | for (k = IDELEMS(id)-1; k>=0; k--) |
---|
369 | { |
---|
370 | if (!p_IsConstantPoly(id->m[k],r)) |
---|
371 | return FALSE; |
---|
372 | } |
---|
373 | return TRUE; |
---|
374 | } |
---|
375 | |
---|
376 | /*2 |
---|
377 | * copy an ideal |
---|
378 | */ |
---|
379 | ideal id_Copy(ideal h1, const ring r) |
---|
380 | { |
---|
381 | int i; |
---|
382 | ideal h2; |
---|
383 | |
---|
384 | //#ifdef TEST |
---|
385 | if (h1 == NULL) |
---|
386 | { |
---|
387 | h2=idInit(1,1); |
---|
388 | } |
---|
389 | else |
---|
390 | //#endif |
---|
391 | { |
---|
392 | h2=idInit(IDELEMS(h1),h1->rank); |
---|
393 | for (i=IDELEMS(h1)-1; i>=0; i--) |
---|
394 | h2->m[i] = p_Copy(h1->m[i],r); |
---|
395 | } |
---|
396 | return h2; |
---|
397 | } |
---|
398 | |
---|
399 | #ifdef PDEBUG |
---|
400 | void id_DBTest(ideal h1, int level, const char *f,const int l, const ring r) |
---|
401 | { |
---|
402 | int i; |
---|
403 | |
---|
404 | if (h1 != NULL) |
---|
405 | { |
---|
406 | // assume(IDELEMS(h1) > 0); for ideal/module, does not apply to matrix |
---|
407 | omCheckAddrSize(h1,sizeof(*h1)); |
---|
408 | omdebugAddrSize(h1->m,h1->ncols*h1->nrows*sizeof(poly)); |
---|
409 | /* to be able to test matrices: */ |
---|
410 | for (i=(h1->ncols*h1->nrows)-1; i>=0; i--) |
---|
411 | _p_Test(h1->m[i], r, level); |
---|
412 | int new_rk=id_RankFreeModule(h1,r); |
---|
413 | if(new_rk > h1->rank) |
---|
414 | { |
---|
415 | dReportError("wrong rank %d (should be %d) in %s:%d\n", |
---|
416 | h1->rank, new_rk, f,l); |
---|
417 | omPrintAddrInfo(stderr, h1, " for ideal"); |
---|
418 | h1->rank=new_rk; |
---|
419 | } |
---|
420 | } |
---|
421 | } |
---|
422 | #endif |
---|
423 | |
---|
424 | /*3 |
---|
425 | * for idSort: compare a and b revlex inclusive module comp. |
---|
426 | */ |
---|
427 | static int p_Comp_RevLex(poly a, poly b,BOOLEAN nolex, const ring R) |
---|
428 | { |
---|
429 | if (b==NULL) return 1; |
---|
430 | if (a==NULL) return -1; |
---|
431 | |
---|
432 | if (nolex) |
---|
433 | { |
---|
434 | int r=p_LmCmp(a,b,R); |
---|
435 | if (r!=0) return r; |
---|
436 | number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf); |
---|
437 | r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */ |
---|
438 | n_Delete(&h, R->cf); |
---|
439 | return r; |
---|
440 | } |
---|
441 | int l=rVar(R); |
---|
442 | while ((l>0) && (p_GetExp(a,l,R)==p_GetExp(b,l,R))) l--; |
---|
443 | if (l==0) |
---|
444 | { |
---|
445 | if (p_GetComp(a,R)==p_GetComp(b,R)) |
---|
446 | { |
---|
447 | number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf); |
---|
448 | int r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */ |
---|
449 | n_Delete(&h,R->cf); |
---|
450 | return r; |
---|
451 | } |
---|
452 | if (p_GetComp(a,R)>p_GetComp(b,R)) return 1; |
---|
453 | } |
---|
454 | else if (p_GetExp(a,l,R)>p_GetExp(b,l,R)) |
---|
455 | return 1; |
---|
456 | return -1; |
---|
457 | } |
---|
458 | |
---|
459 | /*2 |
---|
460 | *sorts the ideal w.r.t. the actual ringordering |
---|
461 | *uses lex-ordering when nolex = FALSE |
---|
462 | */ |
---|
463 | intvec *id_Sort(ideal id,BOOLEAN nolex, const ring r) |
---|
464 | { |
---|
465 | poly p,q; |
---|
466 | intvec * result = new intvec(IDELEMS(id)); |
---|
467 | int i, j, actpos=0, newpos, l; |
---|
468 | int diff, olddiff, lastcomp, newcomp; |
---|
469 | BOOLEAN notFound; |
---|
470 | |
---|
471 | for (i=0;i<IDELEMS(id);i++) |
---|
472 | { |
---|
473 | if (id->m[i]!=NULL) |
---|
474 | { |
---|
475 | notFound = TRUE; |
---|
476 | newpos = actpos / 2; |
---|
477 | diff = (actpos+1) / 2; |
---|
478 | diff = (diff+1) / 2; |
---|
479 | lastcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r); |
---|
480 | if (lastcomp<0) |
---|
481 | { |
---|
482 | newpos -= diff; |
---|
483 | } |
---|
484 | else if (lastcomp>0) |
---|
485 | { |
---|
486 | newpos += diff; |
---|
487 | } |
---|
488 | else |
---|
489 | { |
---|
490 | notFound = FALSE; |
---|
491 | } |
---|
492 | //while ((newpos>=0) && (newpos<actpos) && (notFound)) |
---|
493 | while (notFound && (newpos>=0) && (newpos<actpos)) |
---|
494 | { |
---|
495 | newcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r); |
---|
496 | olddiff = diff; |
---|
497 | if (diff>1) |
---|
498 | { |
---|
499 | diff = (diff+1) / 2; |
---|
500 | if ((newcomp==1) |
---|
501 | && (actpos-newpos>1) |
---|
502 | && (diff>1) |
---|
503 | && (newpos+diff>=actpos)) |
---|
504 | { |
---|
505 | diff = actpos-newpos-1; |
---|
506 | } |
---|
507 | else if ((newcomp==-1) |
---|
508 | && (diff>1) |
---|
509 | && (newpos<diff)) |
---|
510 | { |
---|
511 | diff = newpos; |
---|
512 | } |
---|
513 | } |
---|
514 | if (newcomp<0) |
---|
515 | { |
---|
516 | if ((olddiff==1) && (lastcomp>0)) |
---|
517 | notFound = FALSE; |
---|
518 | else |
---|
519 | newpos -= diff; |
---|
520 | } |
---|
521 | else if (newcomp>0) |
---|
522 | { |
---|
523 | if ((olddiff==1) && (lastcomp<0)) |
---|
524 | { |
---|
525 | notFound = FALSE; |
---|
526 | newpos++; |
---|
527 | } |
---|
528 | else |
---|
529 | { |
---|
530 | newpos += diff; |
---|
531 | } |
---|
532 | } |
---|
533 | else |
---|
534 | { |
---|
535 | notFound = FALSE; |
---|
536 | } |
---|
537 | lastcomp = newcomp; |
---|
538 | if (diff==0) notFound=FALSE; /*hs*/ |
---|
539 | } |
---|
540 | if (newpos<0) newpos = 0; |
---|
541 | if (newpos>actpos) newpos = actpos; |
---|
542 | while ((newpos<actpos) && (p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r)==0)) |
---|
543 | newpos++; |
---|
544 | for (j=actpos;j>newpos;j--) |
---|
545 | { |
---|
546 | (*result)[j] = (*result)[j-1]; |
---|
547 | } |
---|
548 | (*result)[newpos] = i; |
---|
549 | actpos++; |
---|
550 | } |
---|
551 | } |
---|
552 | for (j=0;j<actpos;j++) (*result)[j]++; |
---|
553 | return result; |
---|
554 | } |
---|
555 | |
---|
556 | /*2 |
---|
557 | * concat the lists h1 and h2 without zeros |
---|
558 | */ |
---|
559 | ideal id_SimpleAdd (ideal h1,ideal h2, const ring R) |
---|
560 | { |
---|
561 | int i,j,r,l; |
---|
562 | ideal result; |
---|
563 | |
---|
564 | if (h1==NULL) return id_Copy(h2,R); |
---|
565 | if (h2==NULL) return id_Copy(h1,R); |
---|
566 | j = IDELEMS(h1)-1; |
---|
567 | while ((j >= 0) && (h1->m[j] == NULL)) j--; |
---|
568 | i = IDELEMS(h2)-1; |
---|
569 | while ((i >= 0) && (h2->m[i] == NULL)) i--; |
---|
570 | r = si_max(h1->rank,h2->rank); |
---|
571 | if (i+j==(-2)) |
---|
572 | return idInit(1,r); |
---|
573 | else |
---|
574 | result=idInit(i+j+2,r); |
---|
575 | for (l=j; l>=0; l--) |
---|
576 | { |
---|
577 | result->m[l] = p_Copy(h1->m[l],R); |
---|
578 | } |
---|
579 | r = i+j+1; |
---|
580 | for (l=i; l>=0; l--, r--) |
---|
581 | { |
---|
582 | result->m[r] = p_Copy(h2->m[l],R); |
---|
583 | } |
---|
584 | return result; |
---|
585 | } |
---|
586 | |
---|
587 | /*2 |
---|
588 | * insert h2 into h1 (if h2 is not the zero polynomial) |
---|
589 | * return TRUE iff h2 was indeed inserted |
---|
590 | */ |
---|
591 | BOOLEAN idInsertPoly (ideal h1, poly h2) |
---|
592 | { |
---|
593 | if (h2==NULL) return FALSE; |
---|
594 | int j = IDELEMS(h1)-1; |
---|
595 | while ((j >= 0) && (h1->m[j] == NULL)) j--; |
---|
596 | j++; |
---|
597 | if (j==IDELEMS(h1)) |
---|
598 | { |
---|
599 | pEnlargeSet(&(h1->m),IDELEMS(h1),16); |
---|
600 | IDELEMS(h1)+=16; |
---|
601 | } |
---|
602 | h1->m[j]=h2; |
---|
603 | return TRUE; |
---|
604 | } |
---|
605 | |
---|
606 | /*2 |
---|
607 | * insert h2 into h1 depending on the two boolean parameters: |
---|
608 | * - if zeroOk is true, then h2 will also be inserted when it is zero |
---|
609 | * - if duplicateOk is true, then h2 will also be inserted when it is |
---|
610 | * already present in h1 |
---|
611 | * return TRUE iff h2 was indeed inserted |
---|
612 | */ |
---|
613 | BOOLEAN id_InsertPolyWithTests (ideal h1, const int validEntries, |
---|
614 | const poly h2, const bool zeroOk, const bool duplicateOk, const ring r) |
---|
615 | { |
---|
616 | if ((!zeroOk) && (h2 == NULL)) return FALSE; |
---|
617 | if (!duplicateOk) |
---|
618 | { |
---|
619 | bool h2FoundInH1 = false; |
---|
620 | int i = 0; |
---|
621 | while ((i < validEntries) && (!h2FoundInH1)) |
---|
622 | { |
---|
623 | h2FoundInH1 = p_EqualPolys(h1->m[i], h2,r); |
---|
624 | i++; |
---|
625 | } |
---|
626 | if (h2FoundInH1) return FALSE; |
---|
627 | } |
---|
628 | if (validEntries == IDELEMS(h1)) |
---|
629 | { |
---|
630 | pEnlargeSet(&(h1->m), IDELEMS(h1), 16); |
---|
631 | IDELEMS(h1) += 16; |
---|
632 | } |
---|
633 | h1->m[validEntries] = h2; |
---|
634 | return TRUE; |
---|
635 | } |
---|
636 | |
---|
637 | /*2 |
---|
638 | * h1 + h2 |
---|
639 | */ |
---|
640 | ideal id_Add (ideal h1,ideal h2, const ring r) |
---|
641 | { |
---|
642 | ideal result = id_SimpleAdd(h1,h2,r); |
---|
643 | id_Compactify(result,r); |
---|
644 | return result; |
---|
645 | } |
---|
646 | |
---|
647 | /*2 |
---|
648 | * h1 * h2 |
---|
649 | */ |
---|
650 | ideal id_Mult (ideal h1,ideal h2, const ring r) |
---|
651 | { |
---|
652 | int i,j,k; |
---|
653 | ideal hh; |
---|
654 | |
---|
655 | j = IDELEMS(h1); |
---|
656 | while ((j > 0) && (h1->m[j-1] == NULL)) j--; |
---|
657 | i = IDELEMS(h2); |
---|
658 | while ((i > 0) && (h2->m[i-1] == NULL)) i--; |
---|
659 | j = j * i; |
---|
660 | if (j == 0) |
---|
661 | hh = idInit(1,1); |
---|
662 | else |
---|
663 | hh=idInit(j,1); |
---|
664 | if (h1->rank<h2->rank) |
---|
665 | hh->rank = h2->rank; |
---|
666 | else |
---|
667 | hh->rank = h1->rank; |
---|
668 | if (j==0) return hh; |
---|
669 | k = 0; |
---|
670 | for (i=0; i<IDELEMS(h1); i++) |
---|
671 | { |
---|
672 | if (h1->m[i] != NULL) |
---|
673 | { |
---|
674 | for (j=0; j<IDELEMS(h2); j++) |
---|
675 | { |
---|
676 | if (h2->m[j] != NULL) |
---|
677 | { |
---|
678 | hh->m[k] = pp_Mult_qq(h1->m[i],h2->m[j],r); |
---|
679 | k++; |
---|
680 | } |
---|
681 | } |
---|
682 | } |
---|
683 | } |
---|
684 | { |
---|
685 | id_Compactify(hh,r); |
---|
686 | return hh; |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | /*2 |
---|
691 | *returns true if h is the zero ideal |
---|
692 | */ |
---|
693 | BOOLEAN idIs0 (ideal h) |
---|
694 | { |
---|
695 | int i; |
---|
696 | |
---|
697 | if (h == NULL) return TRUE; |
---|
698 | i = IDELEMS(h)-1; |
---|
699 | while ((i >= 0) && (h->m[i] == NULL)) |
---|
700 | { |
---|
701 | i--; |
---|
702 | } |
---|
703 | if (i < 0) |
---|
704 | return TRUE; |
---|
705 | else |
---|
706 | return FALSE; |
---|
707 | } |
---|
708 | |
---|
709 | /*2 |
---|
710 | * return the maximal component number found in any polynomial in s |
---|
711 | */ |
---|
712 | long id_RankFreeModule (ideal s, ring lmRing, ring tailRing) |
---|
713 | { |
---|
714 | if (s!=NULL) |
---|
715 | { |
---|
716 | int j=0; |
---|
717 | |
---|
718 | if (rRing_has_Comp(tailRing) && rRing_has_Comp(lmRing)) |
---|
719 | { |
---|
720 | int l=IDELEMS(s); |
---|
721 | poly *p=s->m; |
---|
722 | int k; |
---|
723 | for (; l != 0; l--) |
---|
724 | { |
---|
725 | if (*p!=NULL) |
---|
726 | { |
---|
727 | pp_Test(*p, lmRing, tailRing); |
---|
728 | k = p_MaxComp(*p, lmRing, tailRing); |
---|
729 | if (k>j) j = k; |
---|
730 | } |
---|
731 | p++; |
---|
732 | } |
---|
733 | } |
---|
734 | return j; |
---|
735 | } |
---|
736 | return -1; |
---|
737 | } |
---|
738 | |
---|
739 | BOOLEAN idIsModule(ideal id, ring r) |
---|
740 | { |
---|
741 | if (id != NULL && rRing_has_Comp(r)) |
---|
742 | { |
---|
743 | int j, l = IDELEMS(id); |
---|
744 | for (j=0; j<l; j++) |
---|
745 | { |
---|
746 | if (id->m[j] != NULL && p_GetComp(id->m[j], r) > 0) return TRUE; |
---|
747 | } |
---|
748 | } |
---|
749 | return FALSE; |
---|
750 | } |
---|
751 | |
---|
752 | |
---|
753 | /*2 |
---|
754 | *returns true if id is homogenous with respect to the aktual weights |
---|
755 | */ |
---|
756 | BOOLEAN id_HomIdeal (ideal id, ideal Q, const ring r) |
---|
757 | { |
---|
758 | int i; |
---|
759 | BOOLEAN b; |
---|
760 | if ((id == NULL) || (IDELEMS(id) == 0)) return TRUE; |
---|
761 | i = 0; |
---|
762 | b = TRUE; |
---|
763 | while ((i < IDELEMS(id)) && b) |
---|
764 | { |
---|
765 | b = p_IsHomogeneous(id->m[i],r); |
---|
766 | i++; |
---|
767 | } |
---|
768 | if ((b) && (Q!=NULL) && (IDELEMS(Q)>0)) |
---|
769 | { |
---|
770 | i=0; |
---|
771 | while ((i < IDELEMS(Q)) && b) |
---|
772 | { |
---|
773 | b = p_IsHomogeneous(Q->m[i],r); |
---|
774 | i++; |
---|
775 | } |
---|
776 | } |
---|
777 | return b; |
---|
778 | } |
---|
779 | |
---|
780 | /*2 |
---|
781 | *initialized a field with r numbers between beg and end for the |
---|
782 | *procedure idNextChoise |
---|
783 | */ |
---|
784 | void idInitChoise (int r,int beg,int end,BOOLEAN *endch,int * choise) |
---|
785 | { |
---|
786 | /*returns the first choise of r numbers between beg and end*/ |
---|
787 | int i; |
---|
788 | for (i=0; i<r; i++) |
---|
789 | { |
---|
790 | choise[i] = 0; |
---|
791 | } |
---|
792 | if (r <= end-beg+1) |
---|
793 | for (i=0; i<r; i++) |
---|
794 | { |
---|
795 | choise[i] = beg+i; |
---|
796 | } |
---|
797 | if (r > end-beg+1) |
---|
798 | *endch = TRUE; |
---|
799 | else |
---|
800 | *endch = FALSE; |
---|
801 | } |
---|
802 | |
---|
803 | /*2 |
---|
804 | *returns the next choise of r numbers between beg and end |
---|
805 | */ |
---|
806 | void idGetNextChoise (int r,int end,BOOLEAN *endch,int * choise) |
---|
807 | { |
---|
808 | int i = r-1,j; |
---|
809 | while ((i >= 0) && (choise[i] == end)) |
---|
810 | { |
---|
811 | i--; |
---|
812 | end--; |
---|
813 | } |
---|
814 | if (i == -1) |
---|
815 | *endch = TRUE; |
---|
816 | else |
---|
817 | { |
---|
818 | choise[i]++; |
---|
819 | for (j=i+1; j<r; j++) |
---|
820 | { |
---|
821 | choise[j] = choise[i]+j-i; |
---|
822 | } |
---|
823 | *endch = FALSE; |
---|
824 | } |
---|
825 | } |
---|
826 | |
---|
827 | /*2 |
---|
828 | *takes the field choise of d numbers between beg and end, cancels the t-th |
---|
829 | *entree and searches for the ordinal number of that d-1 dimensional field |
---|
830 | * w.r.t. the algorithm of construction |
---|
831 | */ |
---|
832 | int idGetNumberOfChoise(int t, int d, int begin, int end, int * choise) |
---|
833 | { |
---|
834 | int * localchoise,i,result=0; |
---|
835 | BOOLEAN b=FALSE; |
---|
836 | |
---|
837 | if (d<=1) return 1; |
---|
838 | localchoise=(int*)omAlloc((d-1)*sizeof(int)); |
---|
839 | idInitChoise(d-1,begin,end,&b,localchoise); |
---|
840 | while (!b) |
---|
841 | { |
---|
842 | result++; |
---|
843 | i = 0; |
---|
844 | while ((i<t) && (localchoise[i]==choise[i])) i++; |
---|
845 | if (i>=t) |
---|
846 | { |
---|
847 | i = t+1; |
---|
848 | while ((i<d) && (localchoise[i-1]==choise[i])) i++; |
---|
849 | if (i>=d) |
---|
850 | { |
---|
851 | omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int)); |
---|
852 | return result; |
---|
853 | } |
---|
854 | } |
---|
855 | idGetNextChoise(d-1,end,&b,localchoise); |
---|
856 | } |
---|
857 | omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int)); |
---|
858 | return 0; |
---|
859 | } |
---|
860 | |
---|
861 | /*2 |
---|
862 | *computes the binomial coefficient |
---|
863 | */ |
---|
864 | int binom (int n,int r) |
---|
865 | { |
---|
866 | int i,result; |
---|
867 | |
---|
868 | if (r==0) return 1; |
---|
869 | if (n-r<r) return binom(n,n-r); |
---|
870 | result = n-r+1; |
---|
871 | for (i=2;i<=r;i++) |
---|
872 | { |
---|
873 | result *= n-r+i; |
---|
874 | if (result<0) |
---|
875 | { |
---|
876 | WarnS("overflow in binomials"); |
---|
877 | return 0; |
---|
878 | } |
---|
879 | result /= i; |
---|
880 | } |
---|
881 | return result; |
---|
882 | } |
---|
883 | |
---|
884 | /*2 |
---|
885 | *the free module of rank i |
---|
886 | */ |
---|
887 | ideal id_FreeModule (int i, const ring r) |
---|
888 | { |
---|
889 | int j; |
---|
890 | ideal h; |
---|
891 | |
---|
892 | h=idInit(i,i); |
---|
893 | for (j=0; j<i; j++) |
---|
894 | { |
---|
895 | h->m[j] = p_One(r); |
---|
896 | p_SetComp(h->m[j],j+1,r); |
---|
897 | p_SetmComp(h->m[j],r); |
---|
898 | } |
---|
899 | return h; |
---|
900 | } |
---|
901 | |
---|
902 | /*2 |
---|
903 | *computes recursively all monomials of a certain degree |
---|
904 | *in every step the actvar-th entry in the exponential |
---|
905 | *vector is incremented and the other variables are |
---|
906 | *computed by recursive calls of makemonoms |
---|
907 | *if the last variable is reached, the difference to the |
---|
908 | *degree is computed directly |
---|
909 | *vars is the number variables |
---|
910 | *actvar is the actual variable to handle |
---|
911 | *deg is the degree of the monomials to compute |
---|
912 | *monomdeg is the actual degree of the monomial in consideration |
---|
913 | */ |
---|
914 | static void makemonoms(int vars,int actvar,int deg,int monomdeg, const ring r) |
---|
915 | { |
---|
916 | poly p; |
---|
917 | int i=0; |
---|
918 | |
---|
919 | if ((idpowerpoint == 0) && (actvar ==1)) |
---|
920 | { |
---|
921 | idpower[idpowerpoint] = p_One(r); |
---|
922 | monomdeg = 0; |
---|
923 | } |
---|
924 | while (i<=deg) |
---|
925 | { |
---|
926 | if (deg == monomdeg) |
---|
927 | { |
---|
928 | p_Setm(idpower[idpowerpoint],r); |
---|
929 | idpowerpoint++; |
---|
930 | return; |
---|
931 | } |
---|
932 | if (actvar == vars) |
---|
933 | { |
---|
934 | p_SetExp(idpower[idpowerpoint],actvar,deg-monomdeg,r); |
---|
935 | p_Setm(idpower[idpowerpoint],r); |
---|
936 | p_Test(idpower[idpowerpoint],r); |
---|
937 | idpowerpoint++; |
---|
938 | return; |
---|
939 | } |
---|
940 | else |
---|
941 | { |
---|
942 | p = p_Copy(idpower[idpowerpoint],r); |
---|
943 | makemonoms(vars,actvar+1,deg,monomdeg,r); |
---|
944 | idpower[idpowerpoint] = p; |
---|
945 | } |
---|
946 | monomdeg++; |
---|
947 | p_SetExp(idpower[idpowerpoint],actvar,p_GetExp(idpower[idpowerpoint],actvar,r)+1,r); |
---|
948 | p_Setm(idpower[idpowerpoint],r); |
---|
949 | p_Test(idpower[idpowerpoint],r); |
---|
950 | i++; |
---|
951 | } |
---|
952 | } |
---|
953 | |
---|
954 | /*2 |
---|
955 | *returns the deg-th power of the maximal ideal of 0 |
---|
956 | */ |
---|
957 | ideal id_MaxIdeal(int deg, const ring r) |
---|
958 | { |
---|
959 | if (deg < 0) |
---|
960 | { |
---|
961 | WarnS("maxideal: power must be non-negative"); |
---|
962 | } |
---|
963 | if (deg < 1) |
---|
964 | { |
---|
965 | ideal I=idInit(1,1); |
---|
966 | I->m[0]=p_One(r); |
---|
967 | return I; |
---|
968 | } |
---|
969 | if (deg == 1) |
---|
970 | { |
---|
971 | return id_MaxIdeal(r); |
---|
972 | } |
---|
973 | |
---|
974 | int vars = rVar(r); |
---|
975 | int i = binom(vars+deg-1,deg); |
---|
976 | if (i<=0) return idInit(1,1); |
---|
977 | ideal id=idInit(i,1); |
---|
978 | idpower = id->m; |
---|
979 | idpowerpoint = 0; |
---|
980 | makemonoms(vars,1,deg,0,r); |
---|
981 | idpower = NULL; |
---|
982 | idpowerpoint = 0; |
---|
983 | return id; |
---|
984 | } |
---|
985 | |
---|
986 | /*2 |
---|
987 | *computes recursively all generators of a certain degree |
---|
988 | *of the ideal "givenideal" |
---|
989 | *elms is the number elements in the given ideal |
---|
990 | *actelm is the actual element to handle |
---|
991 | *deg is the degree of the power to compute |
---|
992 | *gendeg is the actual degree of the generator in consideration |
---|
993 | */ |
---|
994 | static void makepotence(int elms,int actelm,int deg,int gendeg, const ring r) |
---|
995 | { |
---|
996 | poly p; |
---|
997 | int i=0; |
---|
998 | |
---|
999 | if ((idpowerpoint == 0) && (actelm ==1)) |
---|
1000 | { |
---|
1001 | idpower[idpowerpoint] = p_One(r); |
---|
1002 | gendeg = 0; |
---|
1003 | } |
---|
1004 | while (i<=deg) |
---|
1005 | { |
---|
1006 | if (deg == gendeg) |
---|
1007 | { |
---|
1008 | idpowerpoint++; |
---|
1009 | return; |
---|
1010 | } |
---|
1011 | if (actelm == elms) |
---|
1012 | { |
---|
1013 | p=p_Power(p_Copy(givenideal[actelm-1],r),deg-gendeg,r); |
---|
1014 | idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p,r); |
---|
1015 | idpowerpoint++; |
---|
1016 | return; |
---|
1017 | } |
---|
1018 | else |
---|
1019 | { |
---|
1020 | p = p_Copy(idpower[idpowerpoint],r); |
---|
1021 | makepotence(elms,actelm+1,deg,gendeg,r); |
---|
1022 | idpower[idpowerpoint] = p; |
---|
1023 | } |
---|
1024 | gendeg++; |
---|
1025 | idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p_Copy(givenideal[actelm-1],r),r); |
---|
1026 | i++; |
---|
1027 | } |
---|
1028 | } |
---|
1029 | |
---|
1030 | /*2 |
---|
1031 | *returns the deg-th power of the ideal gid |
---|
1032 | */ |
---|
1033 | //ideal idPower(ideal gid,int deg) |
---|
1034 | //{ |
---|
1035 | // int i; |
---|
1036 | // ideal id; |
---|
1037 | // |
---|
1038 | // if (deg < 1) deg = 1; |
---|
1039 | // i = binom(IDELEMS(gid)+deg-1,deg); |
---|
1040 | // id=idInit(i,1); |
---|
1041 | // idpower = id->m; |
---|
1042 | // givenideal = gid->m; |
---|
1043 | // idpowerpoint = 0; |
---|
1044 | // makepotence(IDELEMS(gid),1,deg,0); |
---|
1045 | // idpower = NULL; |
---|
1046 | // givenideal = NULL; |
---|
1047 | // idpowerpoint = 0; |
---|
1048 | // return id; |
---|
1049 | //} |
---|
1050 | static void id_NextPotence(ideal given, ideal result, |
---|
1051 | int begin, int end, int deg, int restdeg, poly ap, const ring r) |
---|
1052 | { |
---|
1053 | poly p; |
---|
1054 | int i; |
---|
1055 | |
---|
1056 | p = p_Power(p_Copy(given->m[begin],r),restdeg,r); |
---|
1057 | i = result->nrows; |
---|
1058 | result->m[i] = p_Mult_q(p_Copy(ap,r),p,r); |
---|
1059 | //PrintS("."); |
---|
1060 | (result->nrows)++; |
---|
1061 | if (result->nrows >= IDELEMS(result)) |
---|
1062 | { |
---|
1063 | pEnlargeSet(&(result->m),IDELEMS(result),16); |
---|
1064 | IDELEMS(result) += 16; |
---|
1065 | } |
---|
1066 | if (begin == end) return; |
---|
1067 | for (i=restdeg-1;i>0;i--) |
---|
1068 | { |
---|
1069 | p = p_Power(p_Copy(given->m[begin],r),i,r); |
---|
1070 | p = p_Mult_q(p_Copy(ap,r),p,r); |
---|
1071 | id_NextPotence(given, result, begin+1, end, deg, restdeg-i, p,r); |
---|
1072 | p_Delete(&p,r); |
---|
1073 | } |
---|
1074 | id_NextPotence(given, result, begin+1, end, deg, restdeg, ap,r); |
---|
1075 | } |
---|
1076 | |
---|
1077 | ideal id_Power(ideal given,int exp, const ring r) |
---|
1078 | { |
---|
1079 | ideal result,temp; |
---|
1080 | poly p1; |
---|
1081 | int i; |
---|
1082 | |
---|
1083 | if (idIs0(given)) return idInit(1,1); |
---|
1084 | temp = id_Copy(given,r); |
---|
1085 | idSkipZeroes(temp); |
---|
1086 | i = binom(IDELEMS(temp)+exp-1,exp); |
---|
1087 | result = idInit(i,1); |
---|
1088 | result->nrows = 0; |
---|
1089 | //Print("ideal contains %d elements\n",i); |
---|
1090 | p1=p_One(r); |
---|
1091 | id_NextPotence(temp,result,0,IDELEMS(temp)-1,exp,exp,p1,r); |
---|
1092 | p_Delete(&p1,r); |
---|
1093 | id_Delete(&temp,r); |
---|
1094 | result->nrows = 1; |
---|
1095 | id_DelEquals(result,r); |
---|
1096 | idSkipZeroes(result); |
---|
1097 | return result; |
---|
1098 | } |
---|
1099 | |
---|
1100 | /*2 |
---|
1101 | *skips all zeroes and double elements, searches also for units |
---|
1102 | */ |
---|
1103 | void id_Compactify(ideal id, const ring r) |
---|
1104 | { |
---|
1105 | int i,j; |
---|
1106 | BOOLEAN b=FALSE; |
---|
1107 | |
---|
1108 | i = IDELEMS(id)-1; |
---|
1109 | while ((! b) && (i>=0)) |
---|
1110 | { |
---|
1111 | b=p_IsUnit(id->m[i],r); |
---|
1112 | i--; |
---|
1113 | } |
---|
1114 | if (b) |
---|
1115 | { |
---|
1116 | for(i=IDELEMS(id)-1;i>=0;i--) p_Delete(&id->m[i],r); |
---|
1117 | id->m[0]=p_One(r); |
---|
1118 | } |
---|
1119 | else |
---|
1120 | { |
---|
1121 | id_DelMultiples(id,r); |
---|
1122 | } |
---|
1123 | idSkipZeroes(id); |
---|
1124 | } |
---|
1125 | |
---|
1126 | /*2 |
---|
1127 | * returns the ideals of initial terms |
---|
1128 | */ |
---|
1129 | ideal id_Head(ideal h,const ring r) |
---|
1130 | { |
---|
1131 | ideal m = idInit(IDELEMS(h),h->rank); |
---|
1132 | int i; |
---|
1133 | |
---|
1134 | for (i=IDELEMS(h)-1;i>=0; i--) |
---|
1135 | { |
---|
1136 | if (h->m[i]!=NULL) m->m[i]=p_Head(h->m[i],r); |
---|
1137 | } |
---|
1138 | return m; |
---|
1139 | } |
---|
1140 | |
---|
1141 | ideal id_Homogen(ideal h, int varnum,const ring r) |
---|
1142 | { |
---|
1143 | ideal m = idInit(IDELEMS(h),h->rank); |
---|
1144 | int i; |
---|
1145 | |
---|
1146 | for (i=IDELEMS(h)-1;i>=0; i--) |
---|
1147 | { |
---|
1148 | m->m[i]=p_Homogen(h->m[i],varnum,r); |
---|
1149 | } |
---|
1150 | return m; |
---|
1151 | } |
---|
1152 | |
---|
1153 | /*------------------type conversions----------------*/ |
---|
1154 | ideal id_Vec2Ideal(poly vec, const ring R) |
---|
1155 | { |
---|
1156 | ideal result=idInit(1,1); |
---|
1157 | omFree((ADDRESS)result->m); |
---|
1158 | result->m=NULL; // remove later |
---|
1159 | p_Vec2Polys(vec, &(result->m), &(IDELEMS(result)),R); |
---|
1160 | return result; |
---|
1161 | } |
---|
1162 | |
---|
1163 | |
---|
1164 | // converts mat to module, destroys mat |
---|
1165 | ideal id_Matrix2Module(matrix mat, const ring R) |
---|
1166 | { |
---|
1167 | int mc=MATCOLS(mat); |
---|
1168 | int mr=MATROWS(mat); |
---|
1169 | ideal result = idInit(si_max(mc,1),si_max(mr,1)); |
---|
1170 | int i,j, l; |
---|
1171 | poly h; |
---|
1172 | poly p; |
---|
1173 | sBucket_pt bucket = sBucketCreate(R); |
---|
1174 | |
---|
1175 | for(j=0;j<mc /*MATCOLS(mat)*/;j++) /* j is also index in result->m */ |
---|
1176 | { |
---|
1177 | for (i=1;i<=mr /*MATROWS(mat)*/;i++) |
---|
1178 | { |
---|
1179 | h = MATELEM(mat,i,j+1); |
---|
1180 | if (h!=NULL) |
---|
1181 | { |
---|
1182 | l=pLength(h); |
---|
1183 | MATELEM(mat,i,j+1)=NULL; |
---|
1184 | p_SetCompP(h,i, R); |
---|
1185 | sBucket_Merge_p(bucket, h, l); |
---|
1186 | } |
---|
1187 | } |
---|
1188 | sBucketClearMerge(bucket, &(result->m[j]), &l); |
---|
1189 | } |
---|
1190 | sBucketDestroy(&bucket); |
---|
1191 | |
---|
1192 | // obachman: need to clean this up |
---|
1193 | id_Delete((ideal*) &mat,R); |
---|
1194 | return result; |
---|
1195 | } |
---|
1196 | |
---|
1197 | /*2 |
---|
1198 | * converts a module into a matrix, destroyes the input |
---|
1199 | */ |
---|
1200 | matrix id_Module2Matrix(ideal mod, const ring R) |
---|
1201 | { |
---|
1202 | matrix result = mpNew(mod->rank,IDELEMS(mod)); |
---|
1203 | int i,cp; |
---|
1204 | poly p,h; |
---|
1205 | |
---|
1206 | for(i=0;i<IDELEMS(mod);i++) |
---|
1207 | { |
---|
1208 | p=pReverse(mod->m[i]); |
---|
1209 | mod->m[i]=NULL; |
---|
1210 | while (p!=NULL) |
---|
1211 | { |
---|
1212 | h=p; |
---|
1213 | pIter(p); |
---|
1214 | pNext(h)=NULL; |
---|
1215 | // cp = si_max(1,pGetComp(h)); // if used for ideals too |
---|
1216 | cp = p_GetComp(h,R); |
---|
1217 | p_SetComp(h,0,R); |
---|
1218 | p_SetmComp(h,R); |
---|
1219 | #ifdef TEST |
---|
1220 | if (cp>mod->rank) |
---|
1221 | { |
---|
1222 | Print("## inv. rank %ld -> %d\n",mod->rank,cp); |
---|
1223 | int k,l,o=mod->rank; |
---|
1224 | mod->rank=cp; |
---|
1225 | matrix d=mpNew(mod->rank,IDELEMS(mod)); |
---|
1226 | for (l=1; l<=o; l++) |
---|
1227 | { |
---|
1228 | for (k=1; k<=IDELEMS(mod); k++) |
---|
1229 | { |
---|
1230 | MATELEM(d,l,k)=MATELEM(result,l,k); |
---|
1231 | MATELEM(result,l,k)=NULL; |
---|
1232 | } |
---|
1233 | } |
---|
1234 | id_Delete((ideal *)&result,R); |
---|
1235 | result=d; |
---|
1236 | } |
---|
1237 | #endif |
---|
1238 | MATELEM(result,cp,i+1) = p_Add_q(MATELEM(result,cp,i+1),h,R); |
---|
1239 | } |
---|
1240 | } |
---|
1241 | // obachman 10/99: added the following line, otherwise memory leack! |
---|
1242 | id_Delete(&mod,R); |
---|
1243 | return result; |
---|
1244 | } |
---|
1245 | |
---|
1246 | matrix id_Module2formatedMatrix(ideal mod,int rows, int cols, const ring R) |
---|
1247 | { |
---|
1248 | matrix result = mpNew(rows,cols); |
---|
1249 | int i,cp,r=id_RankFreeModule(mod,R),c=IDELEMS(mod); |
---|
1250 | poly p,h; |
---|
1251 | |
---|
1252 | if (r>rows) r = rows; |
---|
1253 | if (c>cols) c = cols; |
---|
1254 | for(i=0;i<c;i++) |
---|
1255 | { |
---|
1256 | p=pReverse(mod->m[i]); |
---|
1257 | mod->m[i]=NULL; |
---|
1258 | while (p!=NULL) |
---|
1259 | { |
---|
1260 | h=p; |
---|
1261 | pIter(p); |
---|
1262 | pNext(h)=NULL; |
---|
1263 | cp = p_GetComp(h,R); |
---|
1264 | if (cp<=r) |
---|
1265 | { |
---|
1266 | p_SetComp(h,0,R); |
---|
1267 | p_SetmComp(h,R); |
---|
1268 | MATELEM(result,cp,i+1) = p_Add_q(MATELEM(result,cp,i+1),h,R); |
---|
1269 | } |
---|
1270 | else |
---|
1271 | p_Delete(&h,R); |
---|
1272 | } |
---|
1273 | } |
---|
1274 | id_Delete(&mod,R); |
---|
1275 | return result; |
---|
1276 | } |
---|
1277 | |
---|
1278 | /*2 |
---|
1279 | * substitute the n-th variable by the monomial e in id |
---|
1280 | * destroy id |
---|
1281 | */ |
---|
1282 | ideal id_Subst(ideal id, int n, poly e, const ring r) |
---|
1283 | { |
---|
1284 | int k=MATROWS((matrix)id)*MATCOLS((matrix)id); |
---|
1285 | ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id)); |
---|
1286 | |
---|
1287 | res->rank = id->rank; |
---|
1288 | for(k--;k>=0;k--) |
---|
1289 | { |
---|
1290 | res->m[k]=p_Subst(id->m[k],n,e,r); |
---|
1291 | id->m[k]=NULL; |
---|
1292 | } |
---|
1293 | id_Delete(&id,r); |
---|
1294 | return res; |
---|
1295 | } |
---|
1296 | |
---|
1297 | BOOLEAN id_HomModule(ideal m, ideal Q, intvec **w, const ring R) |
---|
1298 | { |
---|
1299 | if (w!=NULL) *w=NULL; |
---|
1300 | if ((Q!=NULL) && (!id_HomIdeal(Q,NULL,R))) return FALSE; |
---|
1301 | if (idIs0(m)) |
---|
1302 | { |
---|
1303 | if (w!=NULL) (*w)=new intvec(m->rank); |
---|
1304 | return TRUE; |
---|
1305 | } |
---|
1306 | |
---|
1307 | long cmax=1,order=0,ord,* diff,diffmin=32000; |
---|
1308 | int *iscom; |
---|
1309 | int i,j; |
---|
1310 | poly p=NULL; |
---|
1311 | pFDegProc d; |
---|
1312 | if (R->pLexOrder && (R->order[0]==ringorder_lp)) |
---|
1313 | d=p_Totaldegree; |
---|
1314 | else |
---|
1315 | d=R->pFDeg; |
---|
1316 | int length=IDELEMS(m); |
---|
1317 | poly* P=m->m; |
---|
1318 | poly* F=(poly*)omAlloc(length*sizeof(poly)); |
---|
1319 | for (i=length-1;i>=0;i--) |
---|
1320 | { |
---|
1321 | p=F[i]=P[i]; |
---|
1322 | cmax=si_max(cmax,(long)p_MaxComp(p,R)); |
---|
1323 | } |
---|
1324 | cmax++; |
---|
1325 | diff = (long *)omAlloc0(cmax*sizeof(long)); |
---|
1326 | if (w!=NULL) *w=new intvec(cmax-1); |
---|
1327 | iscom = (int *)omAlloc0(cmax*sizeof(int)); |
---|
1328 | i=0; |
---|
1329 | while (i<=length) |
---|
1330 | { |
---|
1331 | if (i<length) |
---|
1332 | { |
---|
1333 | p=F[i]; |
---|
1334 | while ((p!=NULL) && (iscom[p_GetComp(p,R)]==0)) pIter(p); |
---|
1335 | } |
---|
1336 | if ((p==NULL) && (i<length)) |
---|
1337 | { |
---|
1338 | i++; |
---|
1339 | } |
---|
1340 | else |
---|
1341 | { |
---|
1342 | if (p==NULL) /* && (i==length) */ |
---|
1343 | { |
---|
1344 | i=0; |
---|
1345 | while ((i<length) && (F[i]==NULL)) i++; |
---|
1346 | if (i>=length) break; |
---|
1347 | p = F[i]; |
---|
1348 | } |
---|
1349 | //if (pLexOrder && (currRing->order[0]==ringorder_lp)) |
---|
1350 | // order=pTotaldegree(p); |
---|
1351 | //else |
---|
1352 | // order = p->order; |
---|
1353 | // order = pFDeg(p,currRing); |
---|
1354 | order = d(p,R) +diff[p_GetComp(p,R)]; |
---|
1355 | //order += diff[pGetComp(p)]; |
---|
1356 | p = F[i]; |
---|
1357 | //Print("Actual p=F[%d]: ",i);pWrite(p); |
---|
1358 | F[i] = NULL; |
---|
1359 | i=0; |
---|
1360 | } |
---|
1361 | while (p!=NULL) |
---|
1362 | { |
---|
1363 | if (R->pLexOrder && (R->order[0]==ringorder_lp)) |
---|
1364 | ord=p_Totaldegree(p,R); |
---|
1365 | else |
---|
1366 | // ord = p->order; |
---|
1367 | ord = R->pFDeg(p,R); |
---|
1368 | if (iscom[p_GetComp(p,R)]==0) |
---|
1369 | { |
---|
1370 | diff[p_GetComp(p,R)] = order-ord; |
---|
1371 | iscom[p_GetComp(p,R)] = 1; |
---|
1372 | /* |
---|
1373 | *PrintS("new diff: "); |
---|
1374 | *for (j=0;j<cmax;j++) Print("%d ",diff[j]); |
---|
1375 | *PrintLn(); |
---|
1376 | *PrintS("new iscom: "); |
---|
1377 | *for (j=0;j<cmax;j++) Print("%d ",iscom[j]); |
---|
1378 | *PrintLn(); |
---|
1379 | *Print("new set %d, order %d, ord %d, diff %d\n",pGetComp(p),order,ord,diff[pGetComp(p)]); |
---|
1380 | */ |
---|
1381 | } |
---|
1382 | else |
---|
1383 | { |
---|
1384 | /* |
---|
1385 | *PrintS("new diff: "); |
---|
1386 | *for (j=0;j<cmax;j++) Print("%d ",diff[j]); |
---|
1387 | *PrintLn(); |
---|
1388 | *Print("order %d, ord %d, diff %d\n",order,ord,diff[pGetComp(p)]); |
---|
1389 | */ |
---|
1390 | if (order != (ord+diff[p_GetComp(p,R)])) |
---|
1391 | { |
---|
1392 | omFreeSize((ADDRESS) iscom,cmax*sizeof(int)); |
---|
1393 | omFreeSize((ADDRESS) diff,cmax*sizeof(long)); |
---|
1394 | omFreeSize((ADDRESS) F,length*sizeof(poly)); |
---|
1395 | delete *w;*w=NULL; |
---|
1396 | return FALSE; |
---|
1397 | } |
---|
1398 | } |
---|
1399 | pIter(p); |
---|
1400 | } |
---|
1401 | } |
---|
1402 | omFreeSize((ADDRESS) iscom,cmax*sizeof(int)); |
---|
1403 | omFreeSize((ADDRESS) F,length*sizeof(poly)); |
---|
1404 | for (i=1;i<cmax;i++) (**w)[i-1]=(int)(diff[i]); |
---|
1405 | for (i=1;i<cmax;i++) |
---|
1406 | { |
---|
1407 | if (diff[i]<diffmin) diffmin=diff[i]; |
---|
1408 | } |
---|
1409 | if (w!=NULL) |
---|
1410 | { |
---|
1411 | for (i=1;i<cmax;i++) |
---|
1412 | { |
---|
1413 | (**w)[i-1]=(int)(diff[i]-diffmin); |
---|
1414 | } |
---|
1415 | } |
---|
1416 | omFreeSize((ADDRESS) diff,cmax*sizeof(long)); |
---|
1417 | return TRUE; |
---|
1418 | } |
---|
1419 | |
---|
1420 | // uses glabl vars via pSetModDeg |
---|
1421 | //BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w) |
---|
1422 | //{ |
---|
1423 | // if ((Q!=NULL) && (!idHomIdeal(Q,NULL))) { PrintS(" Q not hom\n"); return FALSE;} |
---|
1424 | // if (idIs0(m)) return TRUE; |
---|
1425 | // |
---|
1426 | // int cmax=-1; |
---|
1427 | // int i; |
---|
1428 | // poly p=NULL; |
---|
1429 | // int length=IDELEMS(m); |
---|
1430 | // poly* P=m->m; |
---|
1431 | // for (i=length-1;i>=0;i--) |
---|
1432 | // { |
---|
1433 | // p=P[i]; |
---|
1434 | // if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1); |
---|
1435 | // } |
---|
1436 | // if (w != NULL) |
---|
1437 | // if (w->length()+1 < cmax) |
---|
1438 | // { |
---|
1439 | // // Print("length: %d - %d \n", w->length(),cmax); |
---|
1440 | // return FALSE; |
---|
1441 | // } |
---|
1442 | // |
---|
1443 | // if(w!=NULL) |
---|
1444 | // pSetModDeg(w); |
---|
1445 | // |
---|
1446 | // for (i=length-1;i>=0;i--) |
---|
1447 | // { |
---|
1448 | // p=P[i]; |
---|
1449 | // poly q=p; |
---|
1450 | // if (p!=NULL) |
---|
1451 | // { |
---|
1452 | // int d=pFDeg(p,currRing); |
---|
1453 | // loop |
---|
1454 | // { |
---|
1455 | // pIter(p); |
---|
1456 | // if (p==NULL) break; |
---|
1457 | // if (d!=pFDeg(p,currRing)) |
---|
1458 | // { |
---|
1459 | // //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing)); |
---|
1460 | // if(w!=NULL) |
---|
1461 | // pSetModDeg(NULL); |
---|
1462 | // return FALSE; |
---|
1463 | // } |
---|
1464 | // } |
---|
1465 | // } |
---|
1466 | // } |
---|
1467 | // |
---|
1468 | // if(w!=NULL) |
---|
1469 | // pSetModDeg(NULL); |
---|
1470 | // |
---|
1471 | // return TRUE; |
---|
1472 | //} |
---|
1473 | |
---|
1474 | ideal id_Jet(ideal i,int d, const ring R) |
---|
1475 | { |
---|
1476 | ideal r=idInit((i->nrows)*(i->ncols),i->rank); |
---|
1477 | r->nrows = i-> nrows; |
---|
1478 | r->ncols = i-> ncols; |
---|
1479 | //r->rank = i-> rank; |
---|
1480 | int k; |
---|
1481 | for(k=(i->nrows)*(i->ncols)-1;k>=0; k--) |
---|
1482 | { |
---|
1483 | r->m[k]=pp_Jet(i->m[k],d,R); |
---|
1484 | } |
---|
1485 | return r; |
---|
1486 | } |
---|
1487 | |
---|
1488 | ideal id_JetW(ideal i,int d, intvec * iv, const ring R) |
---|
1489 | { |
---|
1490 | ideal r=idInit(IDELEMS(i),i->rank); |
---|
1491 | if (ecartWeights!=NULL) |
---|
1492 | { |
---|
1493 | WerrorS("cannot compute weighted jets now"); |
---|
1494 | } |
---|
1495 | else |
---|
1496 | { |
---|
1497 | short *w=iv2array(iv,R); |
---|
1498 | int k; |
---|
1499 | for(k=0; k<IDELEMS(i); k++) |
---|
1500 | { |
---|
1501 | r->m[k]=pp_JetW(i->m[k],d,w,R); |
---|
1502 | } |
---|
1503 | omFreeSize((ADDRESS)w,(rVar(R)+1)*sizeof(short)); |
---|
1504 | } |
---|
1505 | return r; |
---|
1506 | } |
---|
1507 | |
---|
1508 | /*3 |
---|
1509 | * searches for the next unit in the components of the module arg and |
---|
1510 | * returns the first one; |
---|
1511 | */ |
---|
1512 | static int id_ReadOutPivot(ideal arg,int* comp, const ring r) |
---|
1513 | { |
---|
1514 | if (idIs0(arg)) return -1; |
---|
1515 | int i=0,j, generator=-1; |
---|
1516 | int rk_arg=arg->rank; //idRankFreeModule(arg); |
---|
1517 | int * componentIsUsed =(int *)omAlloc((rk_arg+1)*sizeof(int)); |
---|
1518 | poly p; |
---|
1519 | |
---|
1520 | while ((generator<0) && (i<IDELEMS(arg))) |
---|
1521 | { |
---|
1522 | memset(componentIsUsed,0,(rk_arg+1)*sizeof(int)); |
---|
1523 | p = arg->m[i]; |
---|
1524 | while (p!=NULL) |
---|
1525 | { |
---|
1526 | j = p_GetComp(p,r); |
---|
1527 | if (componentIsUsed[j]==0) |
---|
1528 | { |
---|
1529 | #ifdef HAVE_RINGS |
---|
1530 | if (p_LmIsConstantComp(p,r) && |
---|
1531 | (!rField_is_Ring(r) || n_IsUnit(pGetCoeff(p),r->cf))) |
---|
1532 | { |
---|
1533 | #else |
---|
1534 | if (p_LmIsConstantComp(p,r)) |
---|
1535 | { |
---|
1536 | #endif |
---|
1537 | generator = i; |
---|
1538 | componentIsUsed[j] = 1; |
---|
1539 | } |
---|
1540 | else |
---|
1541 | { |
---|
1542 | componentIsUsed[j] = -1; |
---|
1543 | } |
---|
1544 | } |
---|
1545 | else if (componentIsUsed[j]>0) |
---|
1546 | { |
---|
1547 | (componentIsUsed[j])++; |
---|
1548 | } |
---|
1549 | pIter(p); |
---|
1550 | } |
---|
1551 | i++; |
---|
1552 | } |
---|
1553 | i = 0; |
---|
1554 | *comp = -1; |
---|
1555 | for (j=0;j<=rk_arg;j++) |
---|
1556 | { |
---|
1557 | if (componentIsUsed[j]>0) |
---|
1558 | { |
---|
1559 | if ((*comp==-1) || (componentIsUsed[j]<i)) |
---|
1560 | { |
---|
1561 | *comp = j; |
---|
1562 | i= componentIsUsed[j]; |
---|
1563 | } |
---|
1564 | } |
---|
1565 | } |
---|
1566 | omFree(componentIsUsed); |
---|
1567 | return generator; |
---|
1568 | } |
---|
1569 | |
---|
1570 | #if 0 |
---|
1571 | static void idDeleteComp(ideal arg,int red_comp) |
---|
1572 | { |
---|
1573 | int i,j; |
---|
1574 | poly p; |
---|
1575 | |
---|
1576 | for (i=IDELEMS(arg)-1;i>=0;i--) |
---|
1577 | { |
---|
1578 | p = arg->m[i]; |
---|
1579 | while (p!=NULL) |
---|
1580 | { |
---|
1581 | j = pGetComp(p); |
---|
1582 | if (j>red_comp) |
---|
1583 | { |
---|
1584 | pSetComp(p,j-1); |
---|
1585 | pSetm(p); |
---|
1586 | } |
---|
1587 | pIter(p); |
---|
1588 | } |
---|
1589 | } |
---|
1590 | (arg->rank)--; |
---|
1591 | } |
---|
1592 | #endif |
---|
1593 | |
---|
1594 | intvec * id_QHomWeight(ideal id, const ring r) |
---|
1595 | { |
---|
1596 | poly head, tail; |
---|
1597 | int k; |
---|
1598 | int in=IDELEMS(id)-1, ready=0, all=0, |
---|
1599 | coldim=rVar(r), rowmax=2*coldim; |
---|
1600 | if (in<0) return NULL; |
---|
1601 | intvec *imat=new intvec(rowmax+1,coldim,0); |
---|
1602 | |
---|
1603 | do |
---|
1604 | { |
---|
1605 | head = id->m[in--]; |
---|
1606 | if (head!=NULL) |
---|
1607 | { |
---|
1608 | tail = pNext(head); |
---|
1609 | while (tail!=NULL) |
---|
1610 | { |
---|
1611 | all++; |
---|
1612 | for (k=1;k<=coldim;k++) |
---|
1613 | IMATELEM(*imat,all,k) = p_GetExpDiff(head,tail,k,r); |
---|
1614 | if (all==rowmax) |
---|
1615 | { |
---|
1616 | ivTriangIntern(imat, ready, all); |
---|
1617 | if (ready==coldim) |
---|
1618 | { |
---|
1619 | delete imat; |
---|
1620 | return NULL; |
---|
1621 | } |
---|
1622 | } |
---|
1623 | pIter(tail); |
---|
1624 | } |
---|
1625 | } |
---|
1626 | } while (in>=0); |
---|
1627 | if (all>ready) |
---|
1628 | { |
---|
1629 | ivTriangIntern(imat, ready, all); |
---|
1630 | if (ready==coldim) |
---|
1631 | { |
---|
1632 | delete imat; |
---|
1633 | return NULL; |
---|
1634 | } |
---|
1635 | } |
---|
1636 | intvec *result = ivSolveKern(imat, ready); |
---|
1637 | delete imat; |
---|
1638 | return result; |
---|
1639 | } |
---|
1640 | |
---|
1641 | BOOLEAN id_IsZeroDim(ideal I, const ring r) |
---|
1642 | { |
---|
1643 | BOOLEAN *UsedAxis=(BOOLEAN *)omAlloc0(rVar(r)*sizeof(BOOLEAN)); |
---|
1644 | int i,n; |
---|
1645 | poly po; |
---|
1646 | BOOLEAN res=TRUE; |
---|
1647 | for(i=IDELEMS(I)-1;i>=0;i--) |
---|
1648 | { |
---|
1649 | po=I->m[i]; |
---|
1650 | if ((po!=NULL) &&((n=p_IsPurePower(po,r))!=0)) UsedAxis[n-1]=TRUE; |
---|
1651 | } |
---|
1652 | for(i=rVar(r)-1;i>=0;i--) |
---|
1653 | { |
---|
1654 | if(UsedAxis[i]==FALSE) {res=FALSE; break;} // not zero-dim. |
---|
1655 | } |
---|
1656 | omFreeSize(UsedAxis,rVar(r)*sizeof(BOOLEAN)); |
---|
1657 | return res; |
---|
1658 | } |
---|
1659 | |
---|
1660 | void id_Normalize(ideal I,const ring r) |
---|
1661 | { |
---|
1662 | if (rField_has_simple_inverse(r)) return; /* Z/p, GF(p,n), R, long R/C */ |
---|
1663 | int i; |
---|
1664 | for(i=IDELEMS(I)-1;i>=0;i--) |
---|
1665 | { |
---|
1666 | p_Normalize(I->m[i],r); |
---|
1667 | } |
---|
1668 | } |
---|
1669 | |
---|
1670 | // #include <kernel/clapsing.h> |
---|
1671 | |
---|
1672 | #ifdef HAVE_FACTORY |
---|
1673 | #if 0 |
---|
1674 | poly id_GCD(poly f, poly g, const ring r) |
---|
1675 | { |
---|
1676 | ring save_r=r; |
---|
1677 | rChangeCurrRing(r); |
---|
1678 | ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g; |
---|
1679 | intvec *w = NULL; |
---|
1680 | |
---|
1681 | ideal S=idSyzygies(I,testHomog,&w); |
---|
1682 | |
---|
1683 | if (w!=NULL) delete w; |
---|
1684 | poly gg=pTakeOutComp(&(S->m[0]),2); |
---|
1685 | idDelete(&S); |
---|
1686 | poly gcd_p=singclap_pdivide(f,gg); |
---|
1687 | pDelete(&gg); |
---|
1688 | rChangeCurrRing(save_r); |
---|
1689 | return gcd_p; |
---|
1690 | } |
---|
1691 | #endif |
---|
1692 | #endif |
---|
1693 | |
---|
1694 | /*2 |
---|
1695 | * xx,q: arrays of length 0..rl-1 |
---|
1696 | * xx[i]: SB mod q[i] |
---|
1697 | * assume: char=0 |
---|
1698 | * assume: q[i]!=0 |
---|
1699 | * destroys xx |
---|
1700 | */ |
---|
1701 | #ifdef HAVE_FACTORY |
---|
1702 | ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring R) |
---|
1703 | { |
---|
1704 | int cnt=IDELEMS(xx[0])*xx[0]->nrows; |
---|
1705 | ideal result=idInit(cnt,xx[0]->rank); |
---|
1706 | result->nrows=xx[0]->nrows; // for lifting matrices |
---|
1707 | result->ncols=xx[0]->ncols; // for lifting matrices |
---|
1708 | int i,j; |
---|
1709 | poly r,h,hh,res_p; |
---|
1710 | number *x=(number *)omAlloc(rl*sizeof(number)); |
---|
1711 | for(i=cnt-1;i>=0;i--) |
---|
1712 | { |
---|
1713 | res_p=NULL; |
---|
1714 | loop |
---|
1715 | { |
---|
1716 | r=NULL; |
---|
1717 | for(j=rl-1;j>=0;j--) |
---|
1718 | { |
---|
1719 | h=xx[j]->m[i]; |
---|
1720 | if ((h!=NULL) |
---|
1721 | &&((r==NULL)||(p_LmCmp(r,h,R)==-1))) |
---|
1722 | r=h; |
---|
1723 | } |
---|
1724 | if (r==NULL) break; |
---|
1725 | h=p_Head(r, R); |
---|
1726 | for(j=rl-1;j>=0;j--) |
---|
1727 | { |
---|
1728 | hh=xx[j]->m[i]; |
---|
1729 | if ((hh!=NULL) && (p_LmCmp(r,hh, R)==0)) |
---|
1730 | { |
---|
1731 | x[j]=p_GetCoeff(hh, R); |
---|
1732 | hh=p_LmFreeAndNext(hh, R); |
---|
1733 | xx[j]->m[i]=hh; |
---|
1734 | } |
---|
1735 | else |
---|
1736 | x[j]=nlInit(0, R->cf); // is R->cf really n_Q??? |
---|
1737 | } |
---|
1738 | |
---|
1739 | number n=nChineseRemainder(x,q,rl, R->cf); |
---|
1740 | |
---|
1741 | for(j=rl-1;j>=0;j--) |
---|
1742 | { |
---|
1743 | x[j]=NULL; // nlInit(0...) takes no memory |
---|
1744 | } |
---|
1745 | if (n_IsZero(n, R->cf)) p_Delete(&h, R); |
---|
1746 | else |
---|
1747 | { |
---|
1748 | p_SetCoeff(h,n, R); |
---|
1749 | //Print("new mon:");pWrite(h); |
---|
1750 | res_p=p_Add_q(res_p, h, R); |
---|
1751 | } |
---|
1752 | } |
---|
1753 | result->m[i]=res_p; |
---|
1754 | } |
---|
1755 | omFree(x); |
---|
1756 | for(i=rl-1;i>=0;i--) id_Delete(&(xx[i]), R); |
---|
1757 | omFree(xx); |
---|
1758 | return result; |
---|
1759 | } |
---|
1760 | #endif |
---|
1761 | |
---|
1762 | /*2 |
---|
1763 | * transpose a module |
---|
1764 | */ |
---|
1765 | ideal id_Transp(ideal a, const ring rRing) |
---|
1766 | { |
---|
1767 | int r = a->rank, c = IDELEMS(a); |
---|
1768 | ideal b = idInit(r,c); |
---|
1769 | |
---|
1770 | for (int i=c; i>0; i--) |
---|
1771 | { |
---|
1772 | poly p=a->m[i-1]; |
---|
1773 | while(p!=NULL) |
---|
1774 | { |
---|
1775 | poly h=p_Head(p, rRing); |
---|
1776 | int co=p_GetComp(h, rRing)-1; |
---|
1777 | p_SetComp(h, i, rRing); |
---|
1778 | p_Setm(h, rRing); |
---|
1779 | b->m[co] = p_Add_q(b->m[co], h, rRing); |
---|
1780 | pIter(p); |
---|
1781 | } |
---|
1782 | } |
---|
1783 | return b; |
---|
1784 | } |
---|
1785 | |
---|
1786 | |
---|
1787 | |
---|
1788 | /*2 |
---|
1789 | * The following is needed to compute the image of certain map used in |
---|
1790 | * the computation of cohomologies via BGG |
---|
1791 | * let M = { w_1, ..., w_k }, k = size(M) == ncols(M), n = nvars(currRing). |
---|
1792 | * assuming that nrows(M) <= m*n; the procedure computes: |
---|
1793 | * transpose(M) * transpose( var(1) I_m | ... | var(n) I_m ) :== transpose(module{f_1, ... f_k}), |
---|
1794 | * where f_i = \sum_{j=1}^{m} (w_i, v_j) gen(j), (w_i, v_j) is a `scalar` multiplication. |
---|
1795 | * that is, if w_i = (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m) then |
---|
1796 | |
---|
1797 | (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m) |
---|
1798 | * var_1 ... var_1 | var_2 ... var_2 | ... | var_n ... var(n) |
---|
1799 | * gen_1 ... gen_m | gen_1 ... gen_m | ... | gen_1 ... gen_m |
---|
1800 | + => |
---|
1801 | f_i = |
---|
1802 | |
---|
1803 | a^1_1 * var(1) * gen(1) + ... + a^1_m * var(1) * gen(m) + |
---|
1804 | a^2_1 * var(2) * gen(1) + ... + a^2_m * var(2) * gen(m) + |
---|
1805 | ... |
---|
1806 | a^n_1 * var(n) * gen(1) + ... + a^n_m * var(n) * gen(m); |
---|
1807 | |
---|
1808 | NOTE: for every f_i we run only ONCE along w_i saving partial sums into a temporary array of polys of size m |
---|
1809 | */ |
---|
1810 | ideal id_TensorModuleMult(const int m, const ideal M, const ring rRing) |
---|
1811 | { |
---|
1812 | // #ifdef DEBU |
---|
1813 | // WarnS("tensorModuleMult!!!!"); |
---|
1814 | |
---|
1815 | assume(m > 0); |
---|
1816 | assume(M != NULL); |
---|
1817 | |
---|
1818 | const int n = rRing->N; |
---|
1819 | |
---|
1820 | assume(M->rank <= m * n); |
---|
1821 | |
---|
1822 | const int k = IDELEMS(M); |
---|
1823 | |
---|
1824 | ideal idTemp = idInit(k,m); // = {f_1, ..., f_k } |
---|
1825 | |
---|
1826 | for( int i = 0; i < k; i++ ) // for every w \in M |
---|
1827 | { |
---|
1828 | poly pTempSum = NULL; |
---|
1829 | |
---|
1830 | poly w = M->m[i]; |
---|
1831 | |
---|
1832 | while(w != NULL) // for each term of w... |
---|
1833 | { |
---|
1834 | poly h = p_Head(w, rRing); |
---|
1835 | |
---|
1836 | const int gen = p_GetComp(h, rRing); // 1 ... |
---|
1837 | |
---|
1838 | assume(gen > 0); |
---|
1839 | assume(gen <= n*m); |
---|
1840 | |
---|
1841 | // TODO: write a formula with %, / instead of while! |
---|
1842 | /* |
---|
1843 | int c = gen; |
---|
1844 | int v = 1; |
---|
1845 | while(c > m) |
---|
1846 | { |
---|
1847 | c -= m; |
---|
1848 | v++; |
---|
1849 | } |
---|
1850 | */ |
---|
1851 | |
---|
1852 | int cc = gen % m; |
---|
1853 | if( cc == 0) cc = m; |
---|
1854 | int vv = 1 + (gen - cc) / m; |
---|
1855 | |
---|
1856 | // assume( cc == c ); |
---|
1857 | // assume( vv == v ); |
---|
1858 | |
---|
1859 | // 1<= c <= m |
---|
1860 | assume( cc > 0 ); |
---|
1861 | assume( cc <= m ); |
---|
1862 | |
---|
1863 | assume( vv > 0 ); |
---|
1864 | assume( vv <= n ); |
---|
1865 | |
---|
1866 | assume( (cc + (vv-1)*m) == gen ); |
---|
1867 | |
---|
1868 | p_IncrExp(h, vv, rRing); // h *= var(j) && // p_AddExp(h, vv, 1, rRing); |
---|
1869 | p_SetComp(h, cc, rRing); |
---|
1870 | |
---|
1871 | p_Setm(h, rRing); // addjust degree after the previous steps! |
---|
1872 | |
---|
1873 | pTempSum = p_Add_q(pTempSum, h, rRing); // it is slow since h will be usually put to the back of pTempSum!!! |
---|
1874 | |
---|
1875 | pIter(w); |
---|
1876 | } |
---|
1877 | |
---|
1878 | idTemp->m[i] = pTempSum; |
---|
1879 | } |
---|
1880 | |
---|
1881 | // simplify idTemp??? |
---|
1882 | |
---|
1883 | ideal idResult = id_Transp(idTemp, rRing); |
---|
1884 | |
---|
1885 | id_Delete(&idTemp, rRing); |
---|
1886 | |
---|
1887 | return(idResult); |
---|
1888 | } |
---|