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