source: git/libpolys/polys/simpleideals.cc @ 6ce030f

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