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