source: git/libpolys/polys/simpleideals.cc @ 2f5547

spielwiese
Last change on this file since 2f5547 was 2f5547, checked in by Hans Schoenemann <hannes@…>, 13 years ago
id_FreeModule
  • Property mode set to 100644
File size: 50.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT - all basic methods to manipulate ideals
7*/
8
9
10/* includes */
11#include "config.h"
12#include <misc/auxiliary.h>
13#include <misc/options.h>
14#include <omalloc/omalloc.h>
15#include <polys/monomials/p_polys.h>
16#include <misc/intvec.h>
17#include <polys/simpleideals.h>
18
19static poly * idpower;
20/*collects the monomials in makemonoms, must be allocated befor*/
21static int idpowerpoint;
22/*index of the actual monomial in idpower*/
23static poly * givenideal;
24/*the ideal from which a power is computed*/
25
26/*2
27* initialise an ideal
28*/
29ideal idInit(int idsize, int rank)
30{
31  /*- initialise an ideal -*/
32  ideal hh = (ideal )omAllocBin(sip_sideal_bin);
33  hh->nrows = 1;
34  hh->rank = rank;
35  IDELEMS(hh) = idsize;
36  if (idsize>0)
37  {
38    hh->m = (poly *)omAlloc0(idsize*sizeof(poly));
39  }
40  else
41    hh->m=NULL;
42  return hh;
43}
44
45#ifdef PDEBUG
46// this is only for outputting an ideal within the debugger
47void idShow(const ideal id, const ring lmRing, const ring tailRing, const int debugPrint)
48{
49  assume( debugPrint >= 0 );
50
51  if( id == NULL )
52    PrintS("(NULL)");
53  else
54  {
55    Print("Module of rank %ld,real rank %ld and %d generators.\n",
56          id->rank,id_RankFreeModule(id, lmRing, tailRing),IDELEMS(id));
57
58    int j = (id->ncols*id->nrows) - 1;
59    while ((j > 0) && (id->m[j]==NULL)) j--;
60    for (int i = 0; i <= j; i++)
61    {
62      Print("generator %d: ",i); p_DebugPrint(id->m[i], lmRing, tailRing, debugPrint);
63    }
64  }
65}
66#endif
67
68/* index of generator with leading term in ground ring (if any);
69   otherwise -1 */
70int id_PosConstant(ideal id, const ring r)
71{
72  int k;
73  for (k = IDELEMS(id)-1; k>=0; k--)
74  {
75    if (p_LmIsConstantComp(id->m[k], r) == TRUE)
76      return k;
77  }
78  return -1;
79}
80
81/*2
82* initialise the maximal ideal (at 0)
83*/
84ideal id_MaxIdeal (const ring r)
85{
86  int l;
87  ideal hh=NULL;
88
89  hh=idInit(rVar(r),1);
90  for (l=0; l<rVar(r); l++)
91  {
92    hh->m[l] = p_One(r);
93    p_SetExp(hh->m[l],l+1,1,r);
94    p_Setm(hh->m[l],r);
95  }
96  return hh;
97}
98
99/*2
100* deletes an ideal/matrix
101*/
102void id_Delete (ideal * h, ring r)
103{
104  int j,elems;
105  if (*h == NULL)
106    return;
107  elems=j=(*h)->nrows*(*h)->ncols;
108  if (j>0)
109  {
110    do
111    {
112      p_Delete(&((*h)->m[--j]), r);
113    }
114    while (j>0);
115    omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems);
116  }
117  omFreeBin((ADDRESS)*h, sip_sideal_bin);
118  *h=NULL;
119}
120
121
122/*2
123* Shallowdeletes an ideal/matrix
124*/
125void id_ShallowDelete (ideal *h, ring r)
126{
127  int j,elems;
128  if (*h == NULL)
129    return;
130  elems=j=(*h)->nrows*(*h)->ncols;
131  if (j>0)
132  {
133    do
134    {
135      p_ShallowDelete(&((*h)->m[--j]), r);
136    }
137    while (j>0);
138    omFreeSize((ADDRESS)((*h)->m),sizeof(poly)*elems);
139  }
140  omFreeBin((ADDRESS)*h, sip_sideal_bin);
141  *h=NULL;
142}
143
144/*2
145*gives an ideal the minimal possible size
146*/
147void idSkipZeroes (ideal ide)
148{
149  int k;
150  int j = -1;
151  BOOLEAN change=FALSE;
152  for (k=0; k<IDELEMS(ide); k++)
153  {
154    if (ide->m[k] != NULL)
155    {
156      j++;
157      if (change)
158      {
159        ide->m[j] = ide->m[k];
160      }
161    }
162    else
163    {
164      change=TRUE;
165    }
166  }
167  if (change)
168  {
169    if (j == -1)
170      j = 0;
171    else
172    {
173      for (k=j+1; k<IDELEMS(ide); k++)
174        ide->m[k] = NULL;
175    }
176    pEnlargeSet(&(ide->m),IDELEMS(ide),j+1-IDELEMS(ide));
177    IDELEMS(ide) = j+1;
178  }
179}
180
181/*2
182* copies the first k (>= 1) entries of the given ideal
183* and returns these as a new ideal
184* (Note that the copied polynomials may be zero.)
185*/
186ideal id_CopyFirstK (const ideal ide, const int k,const ring r)
187{
188  ideal newI = idInit(k, 0);
189  for (int i = 0; i < k; i++)
190    newI->m[i] = p_Copy(ide->m[i],r);
191  return newI;
192}
193
194/*2
195* ideal id = (id[i])
196* result is leadcoeff(id[i]) = 1
197*/
198void id_Norm(ideal id, const ring r)
199{
200  for (int i=IDELEMS(id)-1; i>=0; i--)
201  {
202    if (id->m[i] != NULL)
203    {
204      p_Norm(id->m[i],r);
205    }
206  }
207}
208
209/*2
210* ideal id = (id[i]), c any unit
211* if id[i] = c*id[j] then id[j] is deleted for j > i
212*/
213void id_DelMultiples(ideal id, const ring r)
214{
215  int i, j;
216  int k = IDELEMS(id)-1;
217  for (i=k; i>=0; i--)
218  {
219    if (id->m[i]!=NULL)
220    {
221      for (j=k; j>i; j--)
222      {
223        if (id->m[j]!=NULL)
224        {
225#ifdef HAVE_RINGS
226          if (rField_is_Ring(r))
227          {
228            /* if id[j] = c*id[i] then delete id[j].
229               In the below cases of a ground field, we
230               check whether id[i] = c*id[j] and, if so,
231               delete id[j] for historical reasons (so
232               that previous output does not change) */
233            if (p_ComparePolys(id->m[j], id->m[i],r)) p_Delete(&id->m[j],r);
234          }
235          else
236          {
237            if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r);
238          }
239#else
240          if (p_ComparePolys(id->m[i], id->m[j],r)) p_Delete(&id->m[j],r);
241#endif   
242        }
243      }
244    }
245  }
246}
247
248/*2
249* ideal id = (id[i])
250* if id[i] = id[j] then id[j] is deleted for j > i
251*/
252void id_DelEquals(ideal id, const ring r)
253{
254  int i, j;
255  int k = IDELEMS(id)-1;
256  for (i=k; i>=0; i--)
257  {
258    if (id->m[i]!=NULL)
259    {
260      for (j=k; j>i; j--)
261      {
262        if ((id->m[j]!=NULL)
263        && (p_EqualPolys(id->m[i], id->m[j],r)))
264        {
265          p_Delete(&id->m[j],r);
266        }
267      }
268    }
269  }
270}
271
272//
273// Delete id[j], if Lm(j) == Lm(i) and both LC(j), LC(i) are units and j > i
274//
275void id_DelLmEquals(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_LmEqual(id->m[i], id->m[j],r)
287#ifdef HAVE_RINGS
288        && n_IsUnit(pGetCoeff(id->m[i]),r->cf) && n_IsUnit(pGetCoeff(id->m[j]),r->cf)
289#endif
290        )
291        {
292          p_Delete(&id->m[j],r);
293        }
294      }
295    }
296  }
297}
298
299//
300// delete id[j], if LT(j) == coeff*mon*LT(i) and vice versa, i.e.,
301// delete id[i], if LT(i) == coeff*mon*LT(j)
302//
303void id_DelDiv(ideal id, const ring r)
304{
305  int i, j;
306  int k = IDELEMS(id)-1;
307  for (i=k; i>=0; i--)
308  {
309    if (id->m[i] != NULL)
310    {
311      for (j=k; j>i; j--)
312      {
313        if (id->m[j]!=NULL)
314        {
315#ifdef HAVE_RINGS
316          if (rField_is_Ring(r))
317          {
318            if (p_DivisibleByRingCase(id->m[i], id->m[j],r))
319            {
320              p_Delete(&id->m[j],r);
321            }
322            else if (p_DivisibleByRingCase(id->m[j], id->m[i],r))
323            {
324              p_Delete(&id->m[i],r);
325              break;
326            }
327          }
328          else
329          {
330#endif
331          /* the case of a ground field: */
332          if (p_DivisibleBy(id->m[i], id->m[j],r))
333          {
334            p_Delete(&id->m[j],r);
335          }
336          else if (p_DivisibleBy(id->m[j], id->m[i],r))
337          {
338            p_Delete(&id->m[i],r);
339            break;
340          }
341#ifdef HAVE_RINGS
342          }
343#endif   
344        }
345      }
346    }
347  }
348}
349
350/*2
351*test if the ideal has only constant polynomials
352*/
353BOOLEAN id_IsConstant(ideal id, const ring r)
354{
355  int k;
356  for (k = IDELEMS(id)-1; k>=0; k--)
357  {
358    if (!p_IsConstantPoly(id->m[k],r))
359      return FALSE;
360  }
361  return TRUE;
362}
363
364/*2
365* copy an ideal
366*/
367#ifdef PDEBUG
368ideal id_DBCopy(ideal h1,const char *f,int l, const ring r)
369{
370  int i;
371  ideal h2;
372
373  id_DBTest(h1,PDEBUG,f,l,r);
374//#ifdef TEST
375  if (h1 == NULL)
376  {
377    h2=idDBInit(1,1,f,l);
378  }
379  else
380//#endif
381  {
382    h2=idDBInit(IDELEMS(h1),h1->rank,f,l);
383    for (i=IDELEMS(h1)-1; i>=0; i--)
384      h2->m[i] = p_Copy(h1->m[i],r);
385  }
386  return h2;
387}
388#else
389ideal id_Copy(ideal h1, const ring r)
390{
391  int i;
392  ideal h2;
393
394//#ifdef TEST
395  if (h1 == NULL)
396  {
397    h2=idInit(1,1);
398  }
399  else
400//#endif
401  {
402    h2=idInit(IDELEMS(h1),h1->rank);
403    for (i=IDELEMS(h1)-1; i>=0; i--)
404      h2->m[i] = p_Copy(h1->m[i],r);
405  }
406  return h2;
407}
408#endif
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
436* for idSort: compare a and b revlex inclusive module comp.
437*/
438static int p_Comp_RevLex(poly a, poly b,BOOLEAN nolex, const ring R)
439{
440  if (b==NULL) return 1;
441  if (a==NULL) return -1;
442
443  if (nolex)
444  {
445    int r=p_LmCmp(a,b,R);
446    if (r!=0) return r;
447    number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf);
448    r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */
449    n_Delete(&h, R->cf);
450    return r;
451  }
452  int l=rVar(R);
453  while ((l>0) && (p_GetExp(a,l,R)==p_GetExp(b,l,R))) l--;
454  if (l==0)
455  {
456    if (p_GetComp(a,R)==p_GetComp(b,R))
457    {
458      number h=n_Sub(pGetCoeff(a),pGetCoeff(b),R->cf);
459      int r = -1+n_IsZero(h,R->cf)+2*n_GreaterZero(h,R->cf); /* -1: <, 0:==, 1: > */
460      n_Delete(&h,R->cf);
461      return r;
462    }
463    if (p_GetComp(a,R)>p_GetComp(b,R)) return 1;
464  }
465  else if (p_GetExp(a,l,R)>p_GetExp(b,l,R))
466    return 1;
467  return -1;
468}
469
470/*2
471*sorts the ideal w.r.t. the actual ringordering
472*uses lex-ordering when nolex = FALSE
473*/
474intvec *id_Sort(ideal id,BOOLEAN nolex, const ring r)
475{
476  poly p,q;
477  intvec * result = new intvec(IDELEMS(id));
478  int i, j, actpos=0, newpos, l;
479  int diff, olddiff, lastcomp, newcomp;
480  BOOLEAN notFound;
481
482  for (i=0;i<IDELEMS(id);i++)
483  {
484    if (id->m[i]!=NULL)
485    {
486      notFound = TRUE;
487      newpos = actpos / 2;
488      diff = (actpos+1) / 2;
489      diff = (diff+1) / 2;
490      lastcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r);
491      if (lastcomp<0)
492      {
493        newpos -= diff;
494      }
495      else if (lastcomp>0)
496      {
497        newpos += diff;
498      }
499      else
500      {
501        notFound = FALSE;
502      }
503      //while ((newpos>=0) && (newpos<actpos) && (notFound))
504      while (notFound && (newpos>=0) && (newpos<actpos))
505      {
506        newcomp = p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r);
507        olddiff = diff;
508        if (diff>1)
509        {
510          diff = (diff+1) / 2;
511          if ((newcomp==1)
512          && (actpos-newpos>1)
513          && (diff>1)
514          && (newpos+diff>=actpos))
515          {
516            diff = actpos-newpos-1;
517          }
518          else if ((newcomp==-1)
519          && (diff>1)
520          && (newpos<diff))
521          {
522            diff = newpos;
523          }
524        }
525        if (newcomp<0)
526        {
527          if ((olddiff==1) && (lastcomp>0))
528            notFound = FALSE;
529          else
530            newpos -= diff;
531        }
532        else if (newcomp>0)
533        {
534          if ((olddiff==1) && (lastcomp<0))
535          {
536            notFound = FALSE;
537            newpos++;
538          }
539          else
540          {
541            newpos += diff;
542          }
543        }
544        else
545        {
546          notFound = FALSE;
547        }
548        lastcomp = newcomp;
549        if (diff==0) notFound=FALSE; /*hs*/
550      }
551      if (newpos<0) newpos = 0;
552      if (newpos>actpos) newpos = actpos;
553      while ((newpos<actpos) && (p_Comp_RevLex(id->m[i],id->m[(*result)[newpos]],nolex,r)==0))
554        newpos++;
555      for (j=actpos;j>newpos;j--)
556      {
557        (*result)[j] = (*result)[j-1];
558      }
559      (*result)[newpos] = i;
560      actpos++;
561    }
562  }
563  for (j=0;j<actpos;j++) (*result)[j]++;
564  return result;
565}
566
567/*2
568* concat the lists h1 and h2 without zeros
569*/
570ideal id_SimpleAdd (ideal h1,ideal h2, const ring R)
571{
572  int i,j,r,l;
573  ideal result;
574
575  if (h1==NULL) return id_Copy(h2,R);
576  if (h2==NULL) return id_Copy(h1,R);
577  j = IDELEMS(h1)-1;
578  while ((j >= 0) && (h1->m[j] == NULL)) j--;
579  i = IDELEMS(h2)-1;
580  while ((i >= 0) && (h2->m[i] == NULL)) i--;
581  r = si_max(h1->rank,h2->rank);
582  if (i+j==(-2))
583    return idInit(1,r);
584  else
585    result=idInit(i+j+2,r);
586  for (l=j; l>=0; l--)
587  {
588    result->m[l] = p_Copy(h1->m[l],R);
589  }
590  r = i+j+1;
591  for (l=i; l>=0; l--, r--)
592  {
593    result->m[r] = p_Copy(h2->m[l],R);
594  }
595  return result;
596}
597
598/*2
599* insert h2 into h1 (if h2 is not the zero polynomial)
600* return TRUE iff h2 was indeed inserted
601*/
602BOOLEAN idInsertPoly (ideal h1, poly h2)
603{
604  if (h2==NULL) return FALSE;
605  int j = IDELEMS(h1)-1;
606  while ((j >= 0) && (h1->m[j] == NULL)) j--;
607  j++;
608  if (j==IDELEMS(h1))
609  {
610    pEnlargeSet(&(h1->m),IDELEMS(h1),16);
611    IDELEMS(h1)+=16;
612  }
613  h1->m[j]=h2;
614  return TRUE;
615}
616
617/*2
618* insert h2 into h1 depending on the two boolean parameters:
619* - if zeroOk is true, then h2 will also be inserted when it is zero
620* - if duplicateOk is true, then h2 will also be inserted when it is
621*   already present in h1
622* return TRUE iff h2 was indeed inserted
623*/
624BOOLEAN id_InsertPolyWithTests (ideal h1, const int validEntries,
625  const poly h2, const bool zeroOk, const bool duplicateOk, const ring r)
626{
627  if ((!zeroOk) && (h2 == NULL)) return FALSE;
628  if (!duplicateOk)
629  {
630    bool h2FoundInH1 = false;
631    int i = 0;
632    while ((i < validEntries) && (!h2FoundInH1))
633    {
634      h2FoundInH1 = p_EqualPolys(h1->m[i], h2,r);
635      i++;
636    }
637    if (h2FoundInH1) return FALSE;
638  }
639  if (validEntries == IDELEMS(h1))
640  {
641    pEnlargeSet(&(h1->m), IDELEMS(h1), 16);
642    IDELEMS(h1) += 16;
643  }
644  h1->m[validEntries] = h2;
645  return TRUE;
646}
647
648/*2
649* h1 + h2
650*/
651ideal id_Add (ideal h1,ideal h2, const ring r)
652{
653  ideal result = id_SimpleAdd(h1,h2,r);
654  id_Compactify(result,r);
655  return result;
656}
657
658/*2
659* h1 * h2
660*/
661ideal  id_Mult (ideal h1,ideal  h2, const ring r)
662{
663  int i,j,k;
664  ideal  hh;
665
666  j = IDELEMS(h1);
667  while ((j > 0) && (h1->m[j-1] == NULL)) j--;
668  i = IDELEMS(h2);
669  while ((i > 0) && (h2->m[i-1] == NULL)) i--;
670  j = j * i;
671  if (j == 0)
672    hh = idInit(1,1);
673  else
674    hh=idInit(j,1);
675  if (h1->rank<h2->rank)
676    hh->rank = h2->rank;
677  else
678    hh->rank = h1->rank;
679  if (j==0) return hh;
680  k = 0;
681  for (i=0; i<IDELEMS(h1); i++)
682  {
683    if (h1->m[i] != NULL)
684    {
685      for (j=0; j<IDELEMS(h2); j++)
686      {
687        if (h2->m[j] != NULL)
688        {
689          hh->m[k] = pp_Mult_qq(h1->m[i],h2->m[j],r);
690          k++;
691        }
692      }
693    }
694  }
695  {
696    id_Compactify(hh,r);
697    return hh;
698  }
699}
700
701/*2
702*returns true if h is the zero ideal
703*/
704BOOLEAN idIs0 (ideal h)
705{
706  int i;
707
708  if (h == NULL) return TRUE;
709  i = IDELEMS(h)-1;
710  while ((i >= 0) && (h->m[i] == NULL))
711  {
712    i--;
713  }
714  if (i < 0)
715    return TRUE;
716  else
717    return FALSE;
718}
719
720/*2
721* return the maximal component number found in any polynomial in s
722*/
723long id_RankFreeModule (ideal s, ring lmRing, ring tailRing)
724{
725  if (s!=NULL)
726  {
727    int  j=0;
728
729    if (rRing_has_Comp(tailRing) && rRing_has_Comp(lmRing))
730    {
731      int  l=IDELEMS(s);
732      poly *p=s->m;
733      int  k;
734      for (; l != 0; l--)
735      {
736        if (*p!=NULL)
737        {
738          pp_Test(*p, lmRing, tailRing);
739          k = p_MaxComp(*p, lmRing, tailRing);
740          if (k>j) j = k;
741        }
742        p++;
743      }
744    }
745    return j;
746  }
747  return -1;
748}
749
750BOOLEAN idIsModule(ideal id, ring r)
751{
752  if (id != NULL && rRing_has_Comp(r))
753  {
754    int j, l = IDELEMS(id);
755    for (j=0; j<l; j++)
756    {
757      if (id->m[j] != NULL && p_GetComp(id->m[j], r) > 0) return TRUE;
758    }
759  }
760  return FALSE;
761}
762
763
764/*2
765*returns true if id is homogenous with respect to the aktual weights
766*/
767BOOLEAN id_HomIdeal (ideal id, ideal Q, const ring r)
768{
769  int i;
770  BOOLEAN     b;
771  if ((id == NULL) || (IDELEMS(id) == 0)) return TRUE;
772  i = 0;
773  b = TRUE;
774  while ((i < IDELEMS(id)) && b)
775  {
776    b = p_IsHomogeneous(id->m[i],r);
777    i++;
778  }
779  if ((b) && (Q!=NULL) && (IDELEMS(Q)>0))
780  {
781    i=0;
782    while ((i < IDELEMS(Q)) && b)
783    {
784      b = p_IsHomogeneous(Q->m[i],r);
785      i++;
786    }
787  }
788  return b;
789}
790
791/*2
792*the minimal index of used variables - 1
793*/
794int p_LowVar (poly p, const ring r)
795{
796  int k,l,lex;
797
798  if (p == NULL) return -1;
799
800  k = 32000;/*a very large dummy value*/
801  while (p != NULL)
802  {
803    l = 1;
804    lex = p_GetExp(p,l,r);
805    while ((l < rVar(r)) && (lex == 0))
806    {
807      l++;
808      lex = p_GetExp(p,l,r);
809    }
810    l--;
811    if (l < k) k = l;
812    pIter(p);
813  }
814  return k;
815}
816
817/*2
818*initialized a field with r numbers between beg and end for the
819*procedure idNextChoise
820*/
821void idInitChoise (int r,int beg,int end,BOOLEAN  *endch,int * choise)
822{
823  /*returns the first choise of r numbers between beg and end*/
824  int i;
825  for (i=0; i<r; i++)
826  {
827    choise[i] = 0;
828  }
829  if (r <= end-beg+1)
830    for (i=0; i<r; i++)
831    {
832      choise[i] = beg+i;
833    }
834  if (r > end-beg+1)
835    *endch = TRUE;
836  else
837    *endch = FALSE;
838}
839
840/*2
841*returns the next choise of r numbers between beg and end
842*/
843void idGetNextChoise (int r,int end,BOOLEAN  *endch,int * choise)
844{
845  int i = r-1,j;
846  while ((i >= 0) && (choise[i] == end))
847  {
848    i--;
849    end--;
850  }
851  if (i == -1)
852    *endch = TRUE;
853  else
854  {
855    choise[i]++;
856    for (j=i+1; j<r; j++)
857    {
858      choise[j] = choise[i]+j-i;
859    }
860    *endch = FALSE;
861  }
862}
863
864/*2
865*takes the field choise of d numbers between beg and end, cancels the t-th
866*entree and searches for the ordinal number of that d-1 dimensional field
867* w.r.t. the algorithm of construction
868*/
869int idGetNumberOfChoise(int t, int d, int begin, int end, int * choise)
870{
871  int * localchoise,i,result=0;
872  BOOLEAN b=FALSE;
873
874  if (d<=1) return 1;
875  localchoise=(int*)omAlloc((d-1)*sizeof(int));
876  idInitChoise(d-1,begin,end,&b,localchoise);
877  while (!b)
878  {
879    result++;
880    i = 0;
881    while ((i<t) && (localchoise[i]==choise[i])) i++;
882    if (i>=t)
883    {
884      i = t+1;
885      while ((i<d) && (localchoise[i-1]==choise[i])) i++;
886      if (i>=d)
887      {
888        omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int));
889        return result;
890      }
891    }
892    idGetNextChoise(d-1,end,&b,localchoise);
893  }
894  omFreeSize((ADDRESS)localchoise,(d-1)*sizeof(int));
895  return 0;
896}
897
898/*2
899*computes the binomial coefficient
900*/
901int binom (int n,int r)
902{
903  int i,result;
904
905  if (r==0) return 1;
906  if (n-r<r) return binom(n,n-r);
907  result = n-r+1;
908  for (i=2;i<=r;i++)
909  {
910    result *= n-r+i;
911    if (result<0)
912    {
913      WarnS("overflow in binomials");
914      return 0;
915    }
916    result /= i;
917  }
918  return result;
919}
920
921/*2
922*the free module of rank i
923*/
924ideal id_FreeModule (int i, const ring r)
925{
926  int j;
927  ideal h;
928
929  h=idInit(i,i);
930  for (j=0; j<i; j++)
931  {
932    h->m[j] = p_One(r);
933    p_SetComp(h->m[j],j+1,r);
934    p_SetmComp(h->m[j],r);
935  }
936  return h;
937}
938
939/*2
940*computes recursively all monomials of a certain degree
941*in every step the actvar-th entry in the exponential
942*vector is incremented and the other variables are
943*computed by recursive calls of makemonoms
944*if the last variable is reached, the difference to the
945*degree is computed directly
946*vars is the number variables
947*actvar is the actual variable to handle
948*deg is the degree of the monomials to compute
949*monomdeg is the actual degree of the monomial in consideration
950*/
951static void makemonoms(int vars,int actvar,int deg,int monomdeg, const ring r)
952{
953  poly p;
954  int i=0;
955
956  if ((idpowerpoint == 0) && (actvar ==1))
957  {
958    idpower[idpowerpoint] = p_One(r);
959    monomdeg = 0;
960  }
961  while (i<=deg)
962  {
963    if (deg == monomdeg)
964    {
965      p_Setm(idpower[idpowerpoint],r);
966      idpowerpoint++;
967      return;
968    }
969    if (actvar == vars)
970    {
971      p_SetExp(idpower[idpowerpoint],actvar,deg-monomdeg,r);
972      p_Setm(idpower[idpowerpoint],r);
973      p_Test(idpower[idpowerpoint],r);
974      idpowerpoint++;
975      return;
976    }
977    else
978    {
979      p = p_Copy(idpower[idpowerpoint],r);
980      makemonoms(vars,actvar+1,deg,monomdeg,r);
981      idpower[idpowerpoint] = p;
982    }
983    monomdeg++;
984    p_SetExp(idpower[idpowerpoint],actvar,p_GetExp(idpower[idpowerpoint],actvar,r)+1,r);
985    p_Setm(idpower[idpowerpoint],r);
986    p_Test(idpower[idpowerpoint],r);
987    i++;
988  }
989}
990
991/*2
992*returns the deg-th power of the maximal ideal of 0
993*/
994ideal id_MaxIdeal(int deg, const ring r)
995{
996  if (deg < 0)
997  {
998    WarnS("maxideal: power must be non-negative");
999  }
1000  if (deg < 1)
1001  {
1002    ideal I=idInit(1,1);
1003    I->m[0]=p_One(r);
1004    return I;
1005  }
1006  if (deg == 1)
1007  {
1008    return id_MaxIdeal(r);
1009  }
1010
1011  int vars = rVar(r);
1012  int i = binom(vars+deg-1,deg);
1013  if (i<=0) return idInit(1,1);
1014  ideal id=idInit(i,1);
1015  idpower = id->m;
1016  idpowerpoint = 0;
1017  makemonoms(vars,1,deg,0,r);
1018  idpower = NULL;
1019  idpowerpoint = 0;
1020  return id;
1021}
1022
1023/*2
1024*computes recursively all generators of a certain degree
1025*of the ideal "givenideal"
1026*elms is the number elements in the given ideal
1027*actelm is the actual element to handle
1028*deg is the degree of the power to compute
1029*gendeg is the actual degree of the generator in consideration
1030*/
1031static void makepotence(int elms,int actelm,int deg,int gendeg, const ring r)
1032{
1033  poly p;
1034  int i=0;
1035
1036  if ((idpowerpoint == 0) && (actelm ==1))
1037  {
1038    idpower[idpowerpoint] = p_One(r);
1039    gendeg = 0;
1040  }
1041  while (i<=deg)
1042  {
1043    if (deg == gendeg)
1044    {
1045      idpowerpoint++;
1046      return;
1047    }
1048    if (actelm == elms)
1049    {
1050      p=p_Power(p_Copy(givenideal[actelm-1],r),deg-gendeg,r);
1051      idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p,r);
1052      idpowerpoint++;
1053      return;
1054    }
1055    else
1056    {
1057      p = p_Copy(idpower[idpowerpoint],r);
1058      makepotence(elms,actelm+1,deg,gendeg,r);
1059      idpower[idpowerpoint] = p;
1060    }
1061    gendeg++;
1062    idpower[idpowerpoint]=p_Mult_q(idpower[idpowerpoint],p_Copy(givenideal[actelm-1],r),r);
1063    i++;
1064  }
1065}
1066
1067/*2
1068*returns the deg-th power of the ideal gid
1069*/
1070//ideal idPower(ideal gid,int deg)
1071//{
1072//  int i;
1073//  ideal id;
1074//
1075//  if (deg < 1) deg = 1;
1076//  i = binom(IDELEMS(gid)+deg-1,deg);
1077//  id=idInit(i,1);
1078//  idpower = id->m;
1079//  givenideal = gid->m;
1080//  idpowerpoint = 0;
1081//  makepotence(IDELEMS(gid),1,deg,0);
1082//  idpower = NULL;
1083//  givenideal = NULL;
1084//  idpowerpoint = 0;
1085//  return id;
1086//}
1087static void idNextPotence(ideal given, ideal result,
1088  int begin, int end, int deg, int restdeg, poly ap)
1089{
1090  poly p;
1091  int i;
1092
1093  p = pPower(pCopy(given->m[begin]),restdeg);
1094  i = result->nrows;
1095  result->m[i] = pMult(pCopy(ap),p);
1096//PrintS(".");
1097  (result->nrows)++;
1098  if (result->nrows >= IDELEMS(result))
1099  {
1100    pEnlargeSet(&(result->m),IDELEMS(result),16);
1101    IDELEMS(result) += 16;
1102  }
1103  if (begin == end) return;
1104  for (i=restdeg-1;i>0;i--)
1105  {
1106    p = pPower(pCopy(given->m[begin]),i);
1107    p = pMult(pCopy(ap),p);
1108    idNextPotence(given, result, begin+1, end, deg, restdeg-i, p);
1109    pDelete(&p);
1110  }
1111  idNextPotence(given, result, begin+1, end, deg, restdeg, ap);
1112}
1113
1114ideal id_Power(ideal given,int exp, const ring r)
1115{
1116  ideal result,temp;
1117  poly p1;
1118  int i;
1119
1120  if (idIs0(given)) return idInit(1,1);
1121  temp = id_Copy(given,r);
1122  idSkipZeroes(temp);
1123  i = binom(IDELEMS(temp)+exp-1,exp);
1124  result = idInit(i,1);
1125  result->nrows = 0;
1126//Print("ideal contains %d elements\n",i);
1127  p1=p_One(r);
1128  idNextPotence(temp,result,0,IDELEMS(temp)-1,exp,exp,p1);
1129  p_Delete(&p1,r);
1130  id_Delete(&temp,r);
1131  result->nrows = 1;
1132  id_DelEquals(result,r);
1133  idSkipZeroes(result);
1134  return result;
1135}
1136
1137/*2
1138* compute the which-th ar-minor of the matrix a
1139*/
1140poly idMinor(matrix a, int ar, unsigned long which, ideal R)
1141{
1142  int     i,j,k,size;
1143  unsigned long curr;
1144  int *rowchoise,*colchoise;
1145  BOOLEAN rowch,colch;
1146  ideal result;
1147  matrix tmp;
1148  poly p,q;
1149
1150  i = binom(a->rows(),ar);
1151  j = binom(a->cols(),ar);
1152
1153  rowchoise=(int *)omAlloc(ar*sizeof(int));
1154  colchoise=(int *)omAlloc(ar*sizeof(int));
1155  if ((i>512) || (j>512) || (i*j >512)) size=512;
1156  else size=i*j;
1157  result=idInit(size,1);
1158  tmp=mpNew(ar,ar);
1159  k = 0; /* the index in result*/
1160  curr = 0; /* index of current minor */
1161  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
1162  while (!rowch)
1163  {
1164    idInitChoise(ar,1,a->cols(),&colch,colchoise);
1165    while (!colch)
1166    {
1167      if (curr == which)
1168      {
1169        for (i=1; i<=ar; i++)
1170        {
1171          for (j=1; j<=ar; j++)
1172          {
1173            MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1174          }
1175        }
1176        p = mpDetBareiss(tmp);
1177        if (p!=NULL)
1178        {
1179          if (R!=NULL)
1180          {
1181            q = p;
1182            p = kNF(R,currQuotient,q);
1183            pDelete(&q);
1184          }
1185          /*delete the matrix tmp*/
1186          for (i=1; i<=ar; i++)
1187          {
1188            for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1189          }
1190          idDelete((ideal*)&tmp);
1191          omFreeSize((ADDRESS)rowchoise,ar*sizeof(int));
1192          omFreeSize((ADDRESS)colchoise,ar*sizeof(int));
1193          return (p);
1194        }
1195      }
1196      curr++;
1197      idGetNextChoise(ar,a->cols(),&colch,colchoise);
1198    }
1199    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
1200  }
1201  return (poly) 1;
1202}
1203
1204#ifdef WITH_OLD_MINOR
1205/*2
1206* compute all ar-minors of the matrix a
1207*/
1208ideal idMinors(matrix a, int ar, ideal R)
1209{
1210  int     i,j,k,size;
1211  int *rowchoise,*colchoise;
1212  BOOLEAN rowch,colch;
1213  ideal result;
1214  matrix tmp;
1215  poly p,q;
1216
1217  i = binom(a->rows(),ar);
1218  j = binom(a->cols(),ar);
1219
1220  rowchoise=(int *)omAlloc(ar*sizeof(int));
1221  colchoise=(int *)omAlloc(ar*sizeof(int));
1222  if ((i>512) || (j>512) || (i*j >512)) size=512;
1223  else size=i*j;
1224  result=idInit(size,1);
1225  tmp=mpNew(ar,ar);
1226  k = 0; /* the index in result*/
1227  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
1228  while (!rowch)
1229  {
1230    idInitChoise(ar,1,a->cols(),&colch,colchoise);
1231    while (!colch)
1232    {
1233      for (i=1; i<=ar; i++)
1234      {
1235        for (j=1; j<=ar; j++)
1236        {
1237          MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1238        }
1239      }
1240      p = mpDetBareiss(tmp);
1241      if (p!=NULL)
1242      {
1243        if (R!=NULL)
1244        {
1245          q = p;
1246          p = kNF(R,currQuotient,q);
1247          pDelete(&q);
1248        }
1249        if (p!=NULL)
1250        {
1251          if (k>=size)
1252          {
1253            pEnlargeSet(&result->m,size,32);
1254            size += 32;
1255          }
1256          result->m[k] = p;
1257          k++;
1258        }
1259      }
1260      idGetNextChoise(ar,a->cols(),&colch,colchoise);
1261    }
1262    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
1263  }
1264  /*delete the matrix tmp*/
1265  for (i=1; i<=ar; i++)
1266  {
1267    for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1268  }
1269  idDelete((ideal*)&tmp);
1270  if (k==0)
1271  {
1272    k=1;
1273    result->m[0]=NULL;
1274  }
1275  omFreeSize((ADDRESS)rowchoise,ar*sizeof(int));
1276  omFreeSize((ADDRESS)colchoise,ar*sizeof(int));
1277  pEnlargeSet(&result->m,size,k-size);
1278  IDELEMS(result) = k;
1279  return (result);
1280}
1281#else
1282/*2
1283* compute all ar-minors of the matrix a
1284* the caller of mpRecMin
1285* the elements of the result are not in R (if R!=NULL)
1286*/
1287ideal idMinors(matrix a, int ar, ideal R)
1288{
1289  int elems=0;
1290  int r=a->nrows,c=a->ncols;
1291  int i;
1292  matrix b;
1293  ideal result,h;
1294  ring origR;
1295  ring tmpR;
1296  long bound;
1297
1298  if((ar<=0) || (ar>r) || (ar>c))
1299  {
1300    Werror("%d-th minor, matrix is %dx%d",ar,r,c);
1301    return NULL;
1302  }
1303  h = idMatrix2Module(mpCopy(a));
1304  bound = smExpBound(h,c,r,ar);
1305  idDelete(&h);
1306  tmpR=smRingChange(&origR,bound);
1307  b = mpNew(r,c);
1308  for (i=r*c-1;i>=0;i--)
1309  {
1310    if (a->m[i])
1311      b->m[i] = prCopyR(a->m[i],origR);
1312  }
1313  if (R!=NULL)
1314  {
1315    R = idrCopyR(R,origR);
1316    //if (ar>1) // otherwise done in mpMinorToResult
1317    //{
1318    //  matrix bb=(matrix)kNF(R,currQuotient,(ideal)b);
1319    //  bb->rank=b->rank; bb->nrows=b->nrows; bb->ncols=b->ncols;
1320    //  idDelete((ideal*)&b); b=bb;
1321    //}
1322  }
1323  result=idInit(32,1);
1324  if(ar>1) mpRecMin(ar-1,result,elems,b,r,c,NULL,R);
1325  else mpMinorToResult(result,elems,b,r,c,R);
1326  idDelete((ideal *)&b);
1327  if (R!=NULL) idDelete(&R);
1328  idSkipZeroes(result);
1329  rChangeCurrRing(origR);
1330  result = idrMoveR(result,tmpR);
1331  smKillModifiedRing(tmpR);
1332  idTest(result);
1333  return result;
1334}
1335#endif
1336
1337/*2
1338*skips all zeroes and double elements, searches also for units
1339*/
1340void id_Compactify(ideal id, const ring r)
1341{
1342  int i,j;
1343  BOOLEAN b=FALSE;
1344
1345  i = IDELEMS(id)-1;
1346  while ((! b) && (i>=0))
1347  {
1348    b=p_IsUnit(id->m[i],r);
1349    i--;
1350  }
1351  if (b)
1352  {
1353    for(i=IDELEMS(id)-1;i>=0;i--) p_Delete(&id->m[i],r);
1354    id->m[0]=p_One(r);
1355  }
1356  else
1357  {
1358    id_DelMultiples(id,r);
1359  }
1360  idSkipZeroes(id);
1361}
1362
1363/*2
1364*returns TRUE if id1 is a submodule of id2
1365*/
1366BOOLEAN idIsSubModule(ideal id1,ideal id2)
1367{
1368  int i;
1369  poly p;
1370
1371  if (idIs0(id1)) return TRUE;
1372  for (i=0;i<IDELEMS(id1);i++)
1373  {
1374    if (id1->m[i] != NULL)
1375    {
1376      p = kNF(id2,currQuotient,id1->m[i]);
1377      if (p != NULL)
1378      {
1379        pDelete(&p);
1380        return FALSE;
1381      }
1382    }
1383  }
1384  return TRUE;
1385}
1386
1387/*2
1388* returns the ideals of initial terms
1389*/
1390ideal idHead(ideal h)
1391{
1392  ideal m = idInit(IDELEMS(h),h->rank);
1393  int i;
1394
1395  for (i=IDELEMS(h)-1;i>=0; i--)
1396  {
1397    if (h->m[i]!=NULL) m->m[i]=pHead(h->m[i]);
1398  }
1399  return m;
1400}
1401
1402ideal idHomogen(ideal h, int varnum)
1403{
1404  ideal m = idInit(IDELEMS(h),h->rank);
1405  int i;
1406
1407  for (i=IDELEMS(h)-1;i>=0; i--)
1408  {
1409    m->m[i]=pHomogen(h->m[i],varnum);
1410  }
1411  return m;
1412}
1413
1414/*------------------type conversions----------------*/
1415ideal idVec2Ideal(poly vec)
1416{
1417   ideal result=idInit(1,1);
1418   omFree((ADDRESS)result->m);
1419   result->m=NULL; // remove later
1420   pVec2Polys(vec, &(result->m), &(IDELEMS(result)));
1421   return result;
1422}
1423
1424#define NEW_STUFF
1425#ifndef NEW_STUFF
1426// converts mat to module, destroys mat
1427ideal idMatrix2Module(matrix mat)
1428{
1429  int mc=MATCOLS(mat);
1430  int mr=MATROWS(mat);
1431  ideal result = idInit(si_max(mc,1),si_max(mr,1));
1432  int i,j;
1433  poly h;
1434
1435  for(j=0;j<mc /*MATCOLS(mat)*/;j++) /* j is also index in result->m */
1436  {
1437    for (i=1;i<=mr /*MATROWS(mat)*/;i++)
1438    {
1439      h = MATELEM(mat,i,j+1);
1440      if (h!=NULL)
1441      {
1442        MATELEM(mat,i,j+1)=NULL;
1443        pSetCompP(h,i);
1444        result->m[j] = pAdd(result->m[j],h);
1445      }
1446    }
1447  }
1448  // obachman: need to clean this up
1449  idDelete((ideal*) &mat);
1450  return result;
1451}
1452#else
1453
1454#include "sbuckets.h"
1455
1456// converts mat to module, destroys mat
1457ideal idMatrix2Module(matrix mat)
1458{
1459  int mc=MATCOLS(mat);
1460  int mr=MATROWS(mat);
1461  ideal result = idInit(si_max(mc,1),si_max(mr,1));
1462  int i,j, l;
1463  poly h;
1464  poly p;
1465  sBucket_pt bucket = sBucketCreate(currRing);
1466
1467  for(j=0;j<mc /*MATCOLS(mat)*/;j++) /* j is also index in result->m */
1468  {
1469    for (i=1;i<=mr /*MATROWS(mat)*/;i++)
1470    {
1471      h = MATELEM(mat,i,j+1);
1472      if (h!=NULL)
1473      {
1474        l=pLength(h);
1475        MATELEM(mat,i,j+1)=NULL;
1476        p_SetCompP(h,i, currRing);
1477        sBucket_Merge_p(bucket, h, l);
1478      }
1479    }
1480    sBucketClearMerge(bucket, &(result->m[j]), &l);
1481  }
1482  sBucketDestroy(&bucket);
1483
1484  // obachman: need to clean this up
1485  idDelete((ideal*) &mat);
1486  return result;
1487}
1488#endif
1489
1490/*2
1491* converts a module into a matrix, destroyes the input
1492*/
1493matrix idModule2Matrix(ideal mod)
1494{
1495  matrix result = mpNew(mod->rank,IDELEMS(mod));
1496  int i,cp;
1497  poly p,h;
1498
1499  for(i=0;i<IDELEMS(mod);i++)
1500  {
1501    p=pReverse(mod->m[i]);
1502    mod->m[i]=NULL;
1503    while (p!=NULL)
1504    {
1505      h=p;
1506      pIter(p);
1507      pNext(h)=NULL;
1508//      cp = si_max(1,pGetComp(h));     // if used for ideals too
1509      cp = pGetComp(h);
1510      pSetComp(h,0);
1511      pSetmComp(h);
1512#ifdef TEST
1513      if (cp>mod->rank)
1514      {
1515        Print("## inv. rank %ld -> %d\n",mod->rank,cp);
1516        int k,l,o=mod->rank;
1517        mod->rank=cp;
1518        matrix d=mpNew(mod->rank,IDELEMS(mod));
1519        for (l=1; l<=o; l++)
1520        {
1521          for (k=1; k<=IDELEMS(mod); k++)
1522          {
1523            MATELEM(d,l,k)=MATELEM(result,l,k);
1524            MATELEM(result,l,k)=NULL;
1525          }
1526        }
1527        idDelete((ideal *)&result);
1528        result=d;
1529      }
1530#endif
1531      MATELEM(result,cp,i+1) = pAdd(MATELEM(result,cp,i+1),h);
1532    }
1533  }
1534  // obachman 10/99: added the following line, otherwise memory leack!
1535  idDelete(&mod);
1536  return result;
1537}
1538
1539matrix idModule2formatedMatrix(ideal mod,int rows, int cols)
1540{
1541  matrix result = mpNew(rows,cols);
1542  int i,cp,r=idRankFreeModule(mod),c=IDELEMS(mod);
1543  poly p,h;
1544
1545  if (r>rows) r = rows;
1546  if (c>cols) c = cols;
1547  for(i=0;i<c;i++)
1548  {
1549    p=pReverse(mod->m[i]);
1550    mod->m[i]=NULL;
1551    while (p!=NULL)
1552    {
1553      h=p;
1554      pIter(p);
1555      pNext(h)=NULL;
1556      cp = pGetComp(h);
1557      if (cp<=r)
1558      {
1559        pSetComp(h,0);
1560        pSetmComp(h);
1561        MATELEM(result,cp,i+1) = pAdd(MATELEM(result,cp,i+1),h);
1562      }
1563      else
1564        pDelete(&h);
1565    }
1566  }
1567  idDelete(&mod);
1568  return result;
1569}
1570
1571/*2
1572* substitute the n-th variable by the monomial e in id
1573* destroy id
1574*/
1575ideal  idSubst(ideal id, int n, poly e)
1576{
1577  int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
1578  ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
1579
1580  res->rank = id->rank;
1581  for(k--;k>=0;k--)
1582  {
1583    res->m[k]=pSubst(id->m[k],n,e);
1584    id->m[k]=NULL;
1585  }
1586  idDelete(&id);
1587  return res;
1588}
1589
1590BOOLEAN idHomModule(ideal m, ideal Q, intvec **w)
1591{
1592  if (w!=NULL) *w=NULL;
1593  if ((Q!=NULL) && (!idHomIdeal(Q,NULL))) return FALSE;
1594  if (idIs0(m))
1595  {
1596    if (w!=NULL) (*w)=new intvec(m->rank);
1597    return TRUE;
1598  }
1599
1600  long cmax=1,order=0,ord,* diff,diffmin=32000;
1601  int *iscom;
1602  int i,j;
1603  poly p=NULL;
1604  pFDegProc d;
1605  if (pLexOrder && (currRing->order[0]==ringorder_lp))
1606     d=p_Totaldegree;
1607  else
1608     d=pFDeg;
1609  int length=IDELEMS(m);
1610  polyset P=m->m;
1611  polyset F=(polyset)omAlloc(length*sizeof(poly));
1612  for (i=length-1;i>=0;i--)
1613  {
1614    p=F[i]=P[i];
1615    cmax=si_max(cmax,(long)pMaxComp(p));
1616  }
1617  cmax++;
1618  diff = (long *)omAlloc0(cmax*sizeof(long));
1619  if (w!=NULL) *w=new intvec(cmax-1);
1620  iscom = (int *)omAlloc0(cmax*sizeof(int));
1621  i=0;
1622  while (i<=length)
1623  {
1624    if (i<length)
1625    {
1626      p=F[i];
1627      while ((p!=NULL) && (iscom[pGetComp(p)]==0)) pIter(p);
1628    }
1629    if ((p==NULL) && (i<length))
1630    {
1631      i++;
1632    }
1633    else
1634    {
1635      if (p==NULL) /* && (i==length) */
1636      {
1637        i=0;
1638        while ((i<length) && (F[i]==NULL)) i++;
1639        if (i>=length) break;
1640        p = F[i];
1641      }
1642      //if (pLexOrder && (currRing->order[0]==ringorder_lp))
1643      //  order=pTotaldegree(p);
1644      //else
1645      //  order = p->order;
1646      //  order = pFDeg(p,currRing);
1647      order = d(p,currRing) +diff[pGetComp(p)];
1648      //order += diff[pGetComp(p)];
1649      p = F[i];
1650//Print("Actual p=F[%d]: ",i);pWrite(p);
1651      F[i] = NULL;
1652      i=0;
1653    }
1654    while (p!=NULL)
1655    {
1656      if (pLexOrder && (currRing->order[0]==ringorder_lp))
1657        ord=pTotaldegree(p);
1658      else
1659      //  ord = p->order;
1660        ord = pFDeg(p,currRing);
1661      if (iscom[pGetComp(p)]==0)
1662      {
1663        diff[pGetComp(p)] = order-ord;
1664        iscom[pGetComp(p)] = 1;
1665/*
1666*PrintS("new diff: ");
1667*for (j=0;j<cmax;j++) Print("%d ",diff[j]);
1668*PrintLn();
1669*PrintS("new iscom: ");
1670*for (j=0;j<cmax;j++) Print("%d ",iscom[j]);
1671*PrintLn();
1672*Print("new set %d, order %d, ord %d, diff %d\n",pGetComp(p),order,ord,diff[pGetComp(p)]);
1673*/
1674      }
1675      else
1676      {
1677/*
1678*PrintS("new diff: ");
1679*for (j=0;j<cmax;j++) Print("%d ",diff[j]);
1680*PrintLn();
1681*Print("order %d, ord %d, diff %d\n",order,ord,diff[pGetComp(p)]);
1682*/
1683        if (order != (ord+diff[pGetComp(p)]))
1684        {
1685          omFreeSize((ADDRESS) iscom,cmax*sizeof(int));
1686          omFreeSize((ADDRESS) diff,cmax*sizeof(long));
1687          omFreeSize((ADDRESS) F,length*sizeof(poly));
1688          delete *w;*w=NULL;
1689          return FALSE;
1690        }
1691      }
1692      pIter(p);
1693    }
1694  }
1695  omFreeSize((ADDRESS) iscom,cmax*sizeof(int));
1696  omFreeSize((ADDRESS) F,length*sizeof(poly));
1697  for (i=1;i<cmax;i++) (**w)[i-1]=(int)(diff[i]);
1698  for (i=1;i<cmax;i++)
1699  {
1700    if (diff[i]<diffmin) diffmin=diff[i];
1701  }
1702  if (w!=NULL)
1703  {
1704    for (i=1;i<cmax;i++)
1705    {
1706      (**w)[i-1]=(int)(diff[i]-diffmin);
1707    }
1708  }
1709  omFreeSize((ADDRESS) diff,cmax*sizeof(long));
1710  return TRUE;
1711}
1712
1713BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
1714{
1715  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
1716  if (idIs0(m)) return TRUE;
1717
1718  int cmax=-1;
1719  int i;
1720  poly p=NULL;
1721  int length=IDELEMS(m);
1722  polyset P=m->m;
1723  for (i=length-1;i>=0;i--)
1724  {
1725    p=P[i];
1726    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
1727  }
1728  if (w != NULL)
1729  if (w->length()+1 < cmax)
1730  {
1731    // Print("length: %d - %d \n", w->length(),cmax);
1732    return FALSE;
1733  }
1734
1735  if(w!=NULL)
1736    pSetModDeg(w);
1737
1738  for (i=length-1;i>=0;i--)
1739  {
1740    p=P[i];
1741    poly q=p;
1742    if (p!=NULL)
1743    {
1744      int d=pFDeg(p,currRing);
1745      loop
1746      {
1747        pIter(p);
1748        if (p==NULL) break;
1749        if (d!=pFDeg(p,currRing))
1750        {
1751          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
1752          if(w!=NULL)
1753            pSetModDeg(NULL);
1754          return FALSE;
1755        }
1756      }
1757    }
1758  }
1759
1760  if(w!=NULL)
1761    pSetModDeg(NULL);
1762
1763  return TRUE;
1764}
1765
1766ideal idJet(ideal i,int d)
1767{
1768  ideal r=idInit((i->nrows)*(i->ncols),i->rank);
1769  r->nrows = i-> nrows;
1770  r->ncols = i-> ncols;
1771  //r->rank = i-> rank;
1772  int k;
1773  for(k=(i->nrows)*(i->ncols)-1;k>=0; k--)
1774  {
1775    r->m[k]=ppJet(i->m[k],d);
1776  }
1777  return r;
1778}
1779
1780ideal idJetW(ideal i,int d, intvec * iv)
1781{
1782  ideal r=idInit(IDELEMS(i),i->rank);
1783  if (ecartWeights!=NULL)
1784  {
1785    WerrorS("cannot compute weighted jets now");
1786  }
1787  else
1788  {
1789    short *w=iv2array(iv);
1790    int k;
1791    for(k=0; k<IDELEMS(i); k++)
1792    {
1793      r->m[k]=ppJetW(i->m[k],d,w);
1794    }
1795    omFreeSize((ADDRESS)w,(rVar(r)+1)*sizeof(short));
1796  }
1797  return r;
1798}
1799
1800int idMinDegW(ideal M,intvec *w)
1801{
1802  int d=-1;
1803  for(int i=0;i<IDELEMS(M);i++)
1804  {
1805    int d0=pMinDeg(M->m[i],w);
1806    if(-1<d0&&(d0<d||d==-1))
1807      d=d0;
1808  }
1809  return d;
1810}
1811
1812ideal idSeries(int n,ideal M,matrix U,intvec *w)
1813{
1814  for(int i=IDELEMS(M)-1;i>=0;i--)
1815  {
1816    if(U==NULL)
1817      M->m[i]=pSeries(n,M->m[i],NULL,w);
1818    else
1819    {
1820      M->m[i]=pSeries(n,M->m[i],MATELEM(U,i+1,i+1),w);
1821      MATELEM(U,i+1,i+1)=NULL;
1822    }
1823  }
1824  if(U!=NULL)
1825    idDelete((ideal*)&U);
1826  return M;
1827}
1828
1829matrix idDiff(matrix i, int k)
1830{
1831  int e=MATCOLS(i)*MATROWS(i);
1832  matrix r=mpNew(MATROWS(i),MATCOLS(i));
1833  r->rank=i->rank;
1834  int j;
1835  for(j=0; j<e; j++)
1836  {
1837    r->m[j]=pDiff(i->m[j],k);
1838  }
1839  return r;
1840}
1841
1842matrix idDiffOp(ideal I, ideal J,BOOLEAN multiply)
1843{
1844  matrix r=mpNew(IDELEMS(I),IDELEMS(J));
1845  int i,j;
1846  for(i=0; i<IDELEMS(I); i++)
1847  {
1848    for(j=0; j<IDELEMS(J); j++)
1849    {
1850      MATELEM(r,i+1,j+1)=pDiffOp(I->m[i],J->m[j],multiply);
1851    }
1852  }
1853  return r;
1854}
1855
1856int idElem(const ideal F)
1857{
1858  int i=0,j=IDELEMS(F)-1;
1859
1860  while(j>=0)
1861  {
1862    if ((F->m)[j]!=NULL) i++;
1863    j--;
1864  }
1865  return i;
1866}
1867
1868/*
1869*computes module-weights for liftings of homogeneous modules
1870*/
1871intvec * idMWLift(ideal mod,intvec * weights)
1872{
1873  if (idIs0(mod)) return new intvec(2);
1874  int i=IDELEMS(mod);
1875  while ((i>0) && (mod->m[i-1]==NULL)) i--;
1876  intvec *result = new intvec(i+1);
1877  while (i>0)
1878  {
1879    (*result)[i]=pFDeg(mod->m[i],currRing)+(*weights)[pGetComp(mod->m[i])];
1880  }
1881  return result;
1882}
1883
1884/*2
1885*sorts the kbase for idCoef* in a special way (lexicographically
1886*with x_max,...,x_1)
1887*/
1888ideal idCreateSpecialKbase(ideal kBase,intvec ** convert)
1889{
1890  int i;
1891  ideal result;
1892
1893  if (idIs0(kBase)) return NULL;
1894  result = idInit(IDELEMS(kBase),kBase->rank);
1895  *convert = idSort(kBase,FALSE);
1896  for (i=0;i<(*convert)->length();i++)
1897  {
1898    result->m[i] = pCopy(kBase->m[(**convert)[i]-1]);
1899  }
1900  return result;
1901}
1902
1903/*2
1904*returns the index of a given monom in the list of the special kbase
1905*/
1906int idIndexOfKBase(poly monom, ideal kbase)
1907{
1908  int j=IDELEMS(kbase);
1909
1910  while ((j>0) && (kbase->m[j-1]==NULL)) j--;
1911  if (j==0) return -1;
1912  int i=rVar(r);
1913  while (i>0)
1914  {
1915    loop
1916    {
1917      if (pGetExp(monom,i)>pGetExp(kbase->m[j-1],i)) return -1;
1918      if (pGetExp(monom,i)==pGetExp(kbase->m[j-1],i)) break;
1919      j--;
1920      if (j==0) return -1;
1921    }
1922    if (i==1)
1923    {
1924      while(j>0)
1925      {
1926        if (pGetComp(monom)==pGetComp(kbase->m[j-1])) return j-1;
1927        if (pGetComp(monom)>pGetComp(kbase->m[j-1])) return -1;
1928        j--;
1929      }
1930    }
1931    i--;
1932  }
1933  return -1;
1934}
1935
1936/*2
1937*decomposes the monom in a part of coefficients described by the
1938*complement of how and a monom in variables occuring in how, the
1939*index of which in kbase is returned as integer pos (-1 if it don't
1940*exists)
1941*/
1942poly idDecompose(poly monom, poly how, ideal kbase, int * pos)
1943{
1944  int i;
1945  poly coeff=p_One(r), base=p_One(r);
1946
1947  for (i=1;i<=rVar(r);i++)
1948  {
1949    if (pGetExp(how,i)>0)
1950    {
1951      pSetExp(base,i,pGetExp(monom,i));
1952    }
1953    else
1954    {
1955      pSetExp(coeff,i,pGetExp(monom,i));
1956    }
1957  }
1958  pSetComp(base,pGetComp(monom));
1959  pSetm(base);
1960  pSetCoeff(coeff,nCopy(pGetCoeff(monom)));
1961  pSetm(coeff);
1962  *pos = idIndexOfKBase(base,kbase);
1963  if (*pos<0)
1964    pDelete(&coeff);
1965  pDelete(&base);
1966  return coeff;
1967}
1968
1969/*2
1970*returns a matrix A of coefficients with kbase*A=arg
1971*if all monomials in variables of how occur in kbase
1972*the other are deleted
1973*/
1974matrix idCoeffOfKBase(ideal arg, ideal kbase, poly how)
1975{
1976  matrix result;
1977  ideal tempKbase;
1978  poly p,q;
1979  intvec * convert;
1980  int i=IDELEMS(kbase),j=IDELEMS(arg),k,pos;
1981#if 0
1982  while ((i>0) && (kbase->m[i-1]==NULL)) i--;
1983  if (idIs0(arg))
1984    return mpNew(i,1);
1985  while ((j>0) && (arg->m[j-1]==NULL)) j--;
1986  result = mpNew(i,j);
1987#else
1988  result = mpNew(i, j);
1989  while ((j>0) && (arg->m[j-1]==NULL)) j--;
1990#endif
1991
1992  tempKbase = idCreateSpecialKbase(kbase,&convert);
1993  for (k=0;k<j;k++)
1994  {
1995    p = arg->m[k];
1996    while (p!=NULL)
1997    {
1998      q = idDecompose(p,how,tempKbase,&pos);
1999      if (pos>=0)
2000      {
2001        MATELEM(result,(*convert)[pos],k+1) =
2002            pAdd(MATELEM(result,(*convert)[pos],k+1),q);
2003      }
2004      else
2005        pDelete(&q);
2006      pIter(p);
2007    }
2008  }
2009  idDelete(&tempKbase);
2010  return result;
2011}
2012
2013/*3
2014* searches for the next unit in the components of the module arg and
2015* returns the first one;
2016*/
2017static int id_ReadOutPivot(ideal arg,int* comp, const ring r)
2018{
2019  if (idIs0(arg)) return -1;
2020  int i=0,j, generator=-1;
2021  int rk_arg=arg->rank; //idRankFreeModule(arg);
2022  int * componentIsUsed =(int *)omAlloc((rk_arg+1)*sizeof(int));
2023  poly p;
2024
2025  while ((generator<0) && (i<IDELEMS(arg)))
2026  {
2027    memset(componentIsUsed,0,(rk_arg+1)*sizeof(int));
2028    p = arg->m[i];
2029    while (p!=NULL)
2030    {
2031      j = p_GetComp(p,r);
2032      if (componentIsUsed[j]==0)
2033      {
2034#ifdef HAVE_RINGS
2035        if (p_LmIsConstantComp(p,r) &&
2036            (!rField_is_Ring(r) || n_IsUnit(pGetCoeff(p),r->cf)))
2037        {
2038#else
2039        if (p_LmIsConstantComp(p,r))
2040        {
2041#endif
2042          generator = i;
2043          componentIsUsed[j] = 1;
2044        }
2045        else
2046        {
2047          componentIsUsed[j] = -1;
2048        }
2049      }
2050      else if (componentIsUsed[j]>0)
2051      {
2052        (componentIsUsed[j])++;
2053      }
2054      pIter(p);
2055    }
2056    i++;
2057  }
2058  i = 0;
2059  *comp = -1;
2060  for (j=0;j<=rk_arg;j++)
2061  {
2062    if (componentIsUsed[j]>0)
2063    {
2064      if ((*comp==-1) || (componentIsUsed[j]<i))
2065      {
2066        *comp = j;
2067        i= componentIsUsed[j];
2068      }
2069    }
2070  }
2071  omFree(componentIsUsed);
2072  return generator;
2073}
2074
2075#if 0
2076static void idDeleteComp(ideal arg,int red_comp)
2077{
2078  int i,j;
2079  poly p;
2080
2081  for (i=IDELEMS(arg)-1;i>=0;i--)
2082  {
2083    p = arg->m[i];
2084    while (p!=NULL)
2085    {
2086      j = pGetComp(p);
2087      if (j>red_comp)
2088      {
2089        pSetComp(p,j-1);
2090        pSetm(p);
2091      }
2092      pIter(p);
2093    }
2094  }
2095  (arg->rank)--;
2096}
2097#endif
2098
2099static void idDeleteComps(ideal arg,int* red_comp,int del)
2100// red_comp is an array [0..args->rank]
2101{
2102  int i,j;
2103  poly p;
2104
2105  for (i=IDELEMS(arg)-1;i>=0;i--)
2106  {
2107    p = arg->m[i];
2108    while (p!=NULL)
2109    {
2110      j = pGetComp(p);
2111      if (red_comp[j]!=j)
2112      {
2113        pSetComp(p,red_comp[j]);
2114        pSetmComp(p);
2115      }
2116      pIter(p);
2117    }
2118  }
2119  (arg->rank) -= del;
2120}
2121
2122/*2
2123* returns the presentation of an isomorphic, minimally
2124* embedded  module (arg represents the quotient!)
2125*/
2126ideal idMinEmbedding(ideal arg,BOOLEAN inPlace, intvec **w, const ring r)
2127{
2128  if (idIs0(arg)) return idInit(1,arg->rank);
2129  int i,next_gen,next_comp;
2130  ideal res=arg;
2131  if (!inPlace) res = id_Copy(arg,r);
2132  res->rank=si_max(res->rank,id_RankFreeModule(res,r));
2133  int *red_comp=(int*)omAlloc((res->rank+1)*sizeof(int));
2134  for (i=res->rank;i>=0;i--) red_comp[i]=i;
2135
2136  int del=0;
2137  loop
2138  {
2139    next_gen = idReadOutPivot(res,&next_comp);
2140    if (next_gen<0) break;
2141    del++;
2142    syGaussForOne(res,next_gen,next_comp,0,IDELEMS(res));
2143    for(i=next_comp+1;i<=arg->rank;i++) red_comp[i]--;
2144    if ((w !=NULL)&&(*w!=NULL))
2145    {
2146      for(i=next_comp;i<(*w)->length();i++) (**w)[i-1]=(**w)[i];
2147    }
2148  }
2149
2150  idDeleteComps(res,red_comp,del);
2151  idSkipZeroes(res);
2152  omFree(red_comp);
2153
2154  if ((w !=NULL)&&(*w!=NULL) &&(del>0))
2155  {
2156    intvec *wtmp=new intvec((*w)->length()-del);
2157    for(i=0;i<res->rank;i++) (*wtmp)[i]=(**w)[i];
2158    delete *w;
2159    *w=wtmp;
2160  }
2161  return res;
2162}
2163
2164intvec * idQHomWeight(ideal id)
2165{
2166  poly head, tail;
2167  int k;
2168  int in=IDELEMS(id)-1, ready=0, all=0,
2169      coldim=rVar(r), rowmax=2*coldim;
2170  if (in<0) return NULL;
2171  intvec *imat=new intvec(rowmax+1,coldim,0);
2172
2173  do
2174  {
2175    head = id->m[in--];
2176    if (head!=NULL)
2177    {
2178      tail = pNext(head);
2179      while (tail!=NULL)
2180      {
2181        all++;
2182        for (k=1;k<=coldim;k++)
2183          IMATELEM(*imat,all,k) = pGetExpDiff(head,tail,k);
2184        if (all==rowmax)
2185        {
2186          ivTriangIntern(imat, ready, all);
2187          if (ready==coldim)
2188          {
2189            delete imat;
2190            return NULL;
2191          }
2192        }
2193        pIter(tail);
2194      }
2195    }
2196  } while (in>=0);
2197  if (all>ready)
2198  {
2199    ivTriangIntern(imat, ready, all);
2200    if (ready==coldim)
2201    {
2202      delete imat;
2203      return NULL;
2204    }
2205  }
2206  intvec *result = ivSolveKern(imat, ready);
2207  delete imat;
2208  return result;
2209}
2210
2211BOOLEAN idIsZeroDim(ideal I)
2212{
2213  BOOLEAN *UsedAxis=(BOOLEAN *)omAlloc0(rVar(r)*sizeof(BOOLEAN));
2214  int i,n;
2215  poly po;
2216  BOOLEAN res=TRUE;
2217  for(i=IDELEMS(I)-1;i>=0;i--)
2218  {
2219    po=I->m[i];
2220    if ((po!=NULL) &&((n=pIsPurePower(po))!=0)) UsedAxis[n-1]=TRUE;
2221  }
2222  for(i=rVar(r)-1;i>=0;i--)
2223  {
2224    if(UsedAxis[i]==FALSE) {res=FALSE; break;} // not zero-dim.
2225  }
2226  omFreeSize(UsedAxis,rVar(r)*sizeof(BOOLEAN));
2227  return res;
2228}
2229
2230void id_Normalize(ideal I,const ring r)
2231{
2232  if (rField_has_simple_inverse(r)) return; /* Z/p, GF(p,n), R, long R/C */
2233  int i;
2234  for(i=IDELEMS(I)-1;i>=0;i--)
2235  {
2236    p_Normalize(I->m[i],r);
2237  }
2238}
2239
2240// #include <kernel/clapsing.h>
2241
2242#ifdef HAVE_FACTORY
2243poly id_GCD(poly f, poly g, const ring r)
2244{
2245  ring save_r=r;
2246  rChangeCurrRing(r);
2247  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2248  intvec *w = NULL;
2249  ideal S=idSyzygies(I,testHomog,&w);
2250  if (w!=NULL) delete w;
2251  poly gg=pTakeOutComp(&(S->m[0]),2);
2252  idDelete(&S);
2253  poly gcd_p=singclap_pdivide(f,gg);
2254  pDelete(&gg);
2255  rChangeCurrRing(save_r);
2256  return gcd_p;
2257}
2258#endif
2259
2260/*2
2261* xx,q: arrays of length 0..rl-1
2262* xx[i]: SB mod q[i]
2263* assume: char=0
2264* assume: q[i]!=0
2265* destroys xx
2266*/
2267#ifdef HAVE_FACTORY
2268ideal idChineseRemainder(ideal *xx, number *q, int rl, const ring r)
2269{
2270  int cnt=IDELEMS(xx[0])*xx[0]->nrows;
2271  ideal result=idInit(cnt,xx[0]->rank);
2272  result->nrows=xx[0]->nrows; // for lifting matrices
2273  result->ncols=xx[0]->ncols; // for lifting matrices
2274  int i,j;
2275  poly r,h,hh,res_p;
2276  number *x=(number *)omAlloc(rl*sizeof(number));
2277  for(i=cnt-1;i>=0;i--)
2278  {
2279    res_p=NULL;
2280    loop
2281    {
2282      r=NULL;
2283      for(j=rl-1;j>=0;j--)
2284      {
2285        h=xx[j]->m[i];
2286        if ((h!=NULL)
2287        &&((r==NULL)||(pLmCmp(r,h)==-1)))
2288          r=h;
2289      }
2290      if (r==NULL) break;
2291      h=pHead(r);
2292      for(j=rl-1;j>=0;j--)
2293      {
2294        hh=xx[j]->m[i];
2295        if ((hh!=NULL) && (pLmCmp(r,hh)==0))
2296        {
2297          x[j]=pGetCoeff(hh);
2298          hh=pLmFreeAndNext(hh);
2299          xx[j]->m[i]=hh;
2300        }
2301        else
2302          x[j]=nlInit(0, r);
2303      }
2304      number n=nlChineseRemainder(x,q,rl);
2305      for(j=rl-1;j>=0;j--)
2306      {
2307        x[j]=NULL; // nlInit(0...) takes no memory
2308      }
2309      if (nlIsZero(n)) pDelete(&h);
2310      else
2311      {
2312        pSetCoeff(h,n);
2313        //Print("new mon:");pWrite(h);
2314        res_p=pAdd(res_p,h);
2315      }
2316    }
2317    result->m[i]=res_p;
2318  }
2319  omFree(x);
2320  for(i=rl-1;i>=0;i--) idDelete(&(xx[i]));
2321  omFree(xx);
2322  return result;
2323}
2324#endif
2325/* currently unsed:
2326ideal idChineseRemainder(ideal *xx, intvec *iv)
2327{
2328  int rl=iv->length();
2329  number *q=(number *)omAlloc(rl*sizeof(number));
2330  int i;
2331  for(i=0; i<rl; i++)
2332  {
2333    q[i]=nInit((*iv)[i]);
2334  }
2335  return idChineseRemainder(xx,q,rl);
2336}
2337*/
2338/*
2339 * lift ideal with coeffs over Z (mod N) to Q via Farey
2340 */
2341ideal id_Farey(ideal x, number N, const ring r)
2342{
2343  int cnt=IDELEMS(x)*x->nrows;
2344  ideal result=idInit(cnt,x->rank);
2345  result->nrows=x->nrows; // for lifting matrices
2346  result->ncols=x->ncols; // for lifting matrices
2347
2348  int i;
2349  for(i=cnt-1;i>=0;i--)
2350  {
2351    poly h=p_Copy(x->m[i],r);
2352    result->m[i]=h;
2353    while(h!=NULL)
2354    {
2355      number c=pGetCoeff(h);
2356      pSetCoeff0(h,nlFarey(c,N));
2357      n_Delete(&c,r->cf);
2358      pIter(h);
2359    }
2360    while((result->m[i]!=NULL)&&(n_IsZero(pGetCoeff(result->m[i]),r->cf)))
2361    {
2362      p_LmDelete(&(result->m[i]),r);
2363    }
2364    h=result->m[i];
2365    while((h!=NULL) && (pNext(h)!=NULL))
2366    {
2367      if(n_IsZero(pGetCoeff(pNext(h)),r->cf))
2368      {
2369        p_LmDelete(&pNext(h),r);
2370      }
2371      else pIter(h);
2372    }
2373  }
2374  return result;
2375}
2376
2377/*2
2378* transpose a module
2379*/
2380ideal id_Transp(ideal a, const ring rRing)
2381{
2382  int r = a->rank, c = IDELEMS(a);
2383  ideal b =  idInit(r,c);
2384
2385  for (int i=c; i>0; i--)
2386  {
2387    poly p=a->m[i-1];
2388    while(p!=NULL)
2389    {
2390      poly h=p_Head(p, rRing);
2391      int co=p_GetComp(h, rRing)-1;
2392      p_SetComp(h, i, rRing);
2393      p_Setm(h, rRing);
2394      b->m[co] = p_Add_q(b->m[co], h, rRing);
2395      pIter(p);
2396    }
2397  }
2398  return b;
2399}
2400
2401
2402
2403/*2
2404* The following is needed to compute the image of certain map used in
2405* the computation of cohomologies via BGG
2406* let M = { w_1, ..., w_k }, k = size(M) == ncols(M), n = nvars(currRing).
2407* assuming that nrows(M) <= m*n; the procedure computes:
2408* transpose(M) * transpose( var(1) I_m | ... | var(n) I_m ) :== transpose(module{f_1, ... f_k}),
2409* where f_i = \sum_{j=1}^{m} (w_i, v_j) gen(j),  (w_i, v_j) is a `scalar` multiplication.
2410* that is, if w_i = (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m) then
2411
2412  (a^1_1, ... a^1_m) | (a^2_1, ..., a^2_m) | ... | (a^n_1, ..., a^n_m)
2413*  var_1  ... var_1  |  var_2  ...  var_2  | ... |  var_n  ...  var(n)
2414*  gen_1  ... gen_m  |  gen_1  ...  gen_m  | ... |  gen_1  ...  gen_m
2415+ =>
2416  f_i =
2417
2418   a^1_1 * var(1) * gen(1) + ... + a^1_m * var(1) * gen(m) +
2419   a^2_1 * var(2) * gen(1) + ... + a^2_m * var(2) * gen(m) +
2420                             ...
2421   a^n_1 * var(n) * gen(1) + ... + a^n_m * var(n) * gen(m);
2422
2423   NOTE: for every f_i we run only ONCE along w_i saving partial sums into a temporary array of polys of size m
2424*/
2425ideal id_TensorModuleMult(const int m, const ideal M, const ring rRing)
2426{
2427// #ifdef DEBU
2428//  WarnS("tensorModuleMult!!!!");
2429
2430  assume(m > 0);
2431  assume(M != NULL);
2432
2433  const int n = rRing->N;
2434
2435  assume(M->rank <= m * n);
2436
2437  const int k = IDELEMS(M);
2438
2439  ideal idTemp =  idInit(k,m); // = {f_1, ..., f_k }
2440
2441  for( int i = 0; i < k; i++ ) // for every w \in M
2442  {
2443    poly pTempSum = NULL;
2444
2445    poly w = M->m[i];
2446
2447    while(w != NULL) // for each term of w...
2448    {
2449      poly h = p_Head(w, rRing);
2450
2451      const int  gen = p_GetComp(h, rRing); // 1 ...
2452
2453      assume(gen > 0);
2454      assume(gen <= n*m);
2455
2456      // TODO: write a formula with %, / instead of while!
2457      /*
2458      int c = gen;
2459      int v = 1;
2460      while(c > m)
2461      {
2462        c -= m;
2463        v++;
2464      }
2465      */
2466
2467      int cc = gen % m;
2468      if( cc == 0) cc = m;
2469      int vv = 1 + (gen - cc) / m;
2470
2471//      assume( cc == c );
2472//      assume( vv == v );
2473
2474      //  1<= c <= m
2475      assume( cc > 0 );
2476      assume( cc <= m );
2477
2478      assume( vv > 0 );
2479      assume( vv <= n );
2480
2481      assume( (cc + (vv-1)*m) == gen );
2482
2483      p_IncrExp(h, vv, rRing); // h *= var(j) && //      p_AddExp(h, vv, 1, rRing);
2484      p_SetComp(h, cc, rRing);
2485
2486      p_Setm(h, rRing);         // addjust degree after the previous steps!
2487
2488      pTempSum = p_Add_q(pTempSum, h, rRing); // it is slow since h will be usually put to the back of pTempSum!!!
2489
2490      pIter(w);
2491    }
2492
2493    idTemp->m[i] = pTempSum;
2494  }
2495
2496  // simplify idTemp???
2497
2498  ideal idResult = id_Transp(idTemp, rRing);
2499
2500  id_Delete(&idTemp, rRing);
2501
2502  return(idResult);
2503}
Note: See TracBrowser for help on using the repository browser.