source: git/Singular/ideals.cc @ 82a8acc

spielwiese
Last change on this file since 82a8acc was 82a8acc, checked in by Hans Schönemann <hannes@…>, 24 years ago
*obachman/hannes: bigendian fixes: wrong ring in idrMoveR_NoSort call git-svn-id: file:///usr/local/Singular/svn/trunk@3899 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 64.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ideals.cc,v 1.77 1999-11-23 17:09:20 hannes Exp $ */
5/*
6* ABSTRACT - all basic methods to manipulate ideals
7*/
8
9/* includes */
10#include "mod2.h"
11#include "tok.h"
12#include "mmemory.h"
13#include "febase.h"
14#include "numbers.h"
15#include "polys.h"
16#include "ipid.h"
17#include "ring.h"
18#include "kstd1.h"
19#include "matpol.h"
20#include "weight.h"
21#include "intvec.h"
22#include "syz.h"
23#include "ideals.h"
24#include "lists.h"
25#include "prCopy.h"
26
27
28/* #define WITH_OLD_MINOR */
29#define pCopy_noCheck(p) pCopy(p)
30
31static poly * idpower;
32/*collects the monomials in makemonoms, must be allocated befor*/
33static int idpowerpoint;
34/*index of the actual monomial in idpower*/
35static poly * givenideal;
36/*the ideal from which a power is computed*/
37
38/*0 implementation*/
39
40/*2
41* initialise an ideal
42*/
43#ifdef PDEBUG
44ideal idDBInit(int idsize, int rank, char *f, int l)
45#else
46ideal idInit(int idsize, int rank)
47#endif
48{
49  /*- initialise an ideal -*/
50#if defined(MDEBUG) && defined(PDEBUG)
51  ideal hh = (ideal )mmDBAllocBlock(sizeof(sip_sideal),f,l);
52#else
53  ideal hh = (ideal )AllocSizeOf(sip_sideal);
54#endif
55  hh->nrows = 1;
56  hh->rank = rank;
57  IDELEMS(hh) = idsize;
58  if (idsize>0)
59  {
60#if defined(MDEBUG) && defined(PDEBUG)
61    hh->m = (poly *)mmDBAllocBlock0(idsize*sizeof(poly),f,l);
62#else
63    hh->m = (poly *)Alloc0(idsize*sizeof(poly));
64#endif
65  }
66  else
67    hh->m=NULL;
68  return hh;
69}
70
71#ifndef __OPTIMIZE__
72// this is mainly for outputting an ideal within the debugger
73void idPrint(ideal id)
74{
75  Print("Module of rank %d,real rank %d and %d generators.\n",
76         id->rank,idRankFreeModule(id),IDELEMS(id));
77  for (int i=0;i<IDELEMS(id);i++)
78  {
79    if (id->m[i]!=NULL)
80    {
81      Print("generator %d: ",i);pWrite(id->m[i]);
82    }
83  }
84}
85#endif
86
87/*2
88* initialise the maximal ideal (at 0)
89*/
90ideal idMaxIdeal (void)
91{
92  int l;
93  ideal hh=NULL;
94
95  hh=idInit(pVariables,1);
96  for (l=0; l<pVariables; l++)
97  {
98    hh->m[l] = pOne();
99    pSetExp(hh->m[l],l+1,1);
100    pSetm(hh->m[l]);
101  }
102  return hh;
103}
104
105/*2
106* deletes an ideal/matrix
107*/
108#ifdef PDEBUG
109void idDBDelete (ideal* h, char *f, int l)
110#else
111void idDelete (ideal * h)
112#endif
113{
114  int j,elems;
115  if (*h == NULL)
116    return;
117  elems=j=(*h)->nrows*(*h)->ncols;
118  if (j>0)
119  {
120    do
121    {
122      #if defined(MDEBUG) && defined(PDEBUG)
123      pDBDelete(&((*h)->m[--j]),mm_specHeap, f,l);
124      #else
125      pDelete(&((*h)->m[--j]));
126      #endif
127    }
128    while (j>0);
129    #if defined(MDEBUG) && defined(PDEBUG)
130    mmDBFreeBlock((ADDRESS)((*h)->m),sizeof(poly)*elems,f,l);
131    #else
132    Free((ADDRESS)((*h)->m),sizeof(poly)*elems);
133    #endif
134  }
135  #if defined(MDEBUG) && defined(PDEBUG)
136  mmDBFreeBlock((ADDRESS)(*h),sizeof(sip_sideal),f,l);
137  #else
138  FreeSizeOf((ADDRESS)*h,sip_sideal);
139  #endif
140  *h=NULL;
141}
142
143/*2
144*gives an ideal the minimal possible size
145*/
146void idSkipZeroes (ideal ide)
147{
148  int k;
149  int j = -1;
150  BOOLEAN change=FALSE;
151  for (k=0; k<IDELEMS(ide); k++)
152  {
153    if (ide->m[k] != NULL)
154    {
155      j++;
156      if (change)
157      {
158        ide->m[j] = ide->m[k];
159      }
160    }
161    else
162    {
163      change=TRUE;
164    }
165  }
166  if (change)
167  {
168    if (j == -1)
169      j = 0;
170    else
171    {
172      for (k=j+1; k<IDELEMS(ide); k++)
173        ide->m[k] = NULL;
174    }
175    pEnlargeSet(&(ide->m),IDELEMS(ide),j+1-IDELEMS(ide));
176    IDELEMS(ide) = j+1;
177  }
178}
179
180/*2
181* ideal id = (id[i])
182* result is leadcoeff(id[i]) = 1
183*/
184void idNorm(ideal id)
185{
186  for (int i=0; i<IDELEMS(id); i++)
187  {
188    if (id->m[i] != NULL)
189    {
190      pNorm(id->m[i]);
191    }
192  }
193}
194
195/*2
196* ideal id = (id[i]), c any number
197* if id[i] = c*id[j] then id[j] is deleted for j > i
198*/
199void idDelMultiples(ideal id)
200{
201  int i, j, t;
202  int k = IDELEMS(id), l = k;
203  for (i=k-2; i>=0; i--)
204  {
205    if (id->m[i]!=NULL)
206    {
207      for (j=l-1; j>i; j--)
208      {
209        if ((id->m[j]!=NULL)
210        && (pComparePolys(id->m[i], id->m[j])))
211        {
212          pDelete(&id->m[j]);
213          l--;
214          for(t=j; t<l; t++)
215          {
216            id->m[t] = id->m[t+1];
217          }
218        }
219      }
220    }
221  }
222  if (l != k)
223  {
224    pEnlargeSet(&id->m, k, l-k);
225    IDELEMS(id) = l;
226  }
227}
228
229/*2
230* ideal id = (id[i])
231* if id[i] = id[j] then id[j] is deleted for j > i
232*/
233void idDelEquals(ideal id)
234{
235  int i, j, t;
236  int k = IDELEMS(id), l = k;
237  for (i=k-2; i>=0; i--)
238  {
239    for (j=l-1; j>i; j--)
240    {
241      if (pEqualPolys(id->m[i], id->m[j]))
242      {
243        pDelete(&id->m[j]);
244        l--;
245        for(t=j; t<l; t++)
246        {
247          id->m[t] = id->m[t+1];
248        }
249      }
250    }
251  }
252  if (l != k)
253  {
254    pEnlargeSet(&id->m, k, l-k);
255    IDELEMS(id) = l;
256  }
257}
258
259//
260// Delete id[j], if Lm(j) == Lm(i) and j > i
261//
262void idDelLmEquals(ideal id)
263{
264  int i, j, t;
265  int k = IDELEMS(id), l = k;
266  for (i=k-2; i>=0; i--)
267  {
268    for (j=l-1; j>i; j--)
269    {
270      if (pLmEqual(id->m[i], id->m[j]))
271      {
272        pDelete(&id->m[j]);
273        l--;
274        for(t=j; t<l; t++)
275        {
276          id->m[t] = id->m[t+1];
277        }
278      }
279    }
280  }
281  if (l != k)
282  {
283    pEnlargeSet(&id->m, k, l-k);
284    IDELEMS(id) = l;
285  }
286}
287
288void idDelDiv(ideal id)
289{
290  int i, j, t;
291  int k = IDELEMS(id), l = k;
292  for (i=k-2; i>=0; i--)
293  {
294    for (j=l-1; j>i; j--)
295    {
296
297      if (((id->m[j] != NULL) && pDivisibleBy(id->m[i], id->m[j])) ||
298          (id->m[i] == NULL && id->m[j] == NULL))
299      {
300        pDelete(&id->m[j]);
301        l--;
302        for(t=j; t<l; t++)
303        {
304          id->m[t] = id->m[t+1];
305        }
306      }
307    }
308  }
309  if (l != k)
310  {
311    pEnlargeSet(&id->m, k, l-k);
312    IDELEMS(id) = l;
313  }
314}
315
316
317/*2
318* copy an ideal
319*/
320#ifdef PDEBUG
321ideal idDBCopy(ideal h1,char *f,int l)
322#else
323ideal idCopy (ideal h1)
324#endif
325{
326  int i;
327  ideal h2;
328
329#ifdef PDEBUG
330  idDBTest(h1,f,l);
331#endif
332//#ifdef TEST
333  if (h1 == NULL)
334  {
335#ifdef PDEBUG
336    h2=idDBInit(1,1,f,l);
337#else
338    h2=idInit(1,1);
339#endif
340  }
341  else
342//#endif
343  {
344#ifdef PDEBUG
345    h2=idDBInit(IDELEMS(h1),h1->rank,f,l);
346#else
347    h2=idInit(IDELEMS(h1),h1->rank);
348#endif
349    for (i=IDELEMS(h1)-1; i>=0; i--)
350#if defined(PDEBUG) && defined(MDEBUG)
351      h2->m[i] = pDBCopy(h1->m[i],f,l);
352#else
353      h2->m[i] = pCopy(h1->m[i]);
354#endif
355  }
356  return h2;
357}
358
359#ifdef PDEBUG
360void idDBTest(ideal h1,char *f,int l)
361{
362  int i;
363
364  if (h1 != NULL)
365  {
366    #ifdef MDEBUG
367    mmDBTestBlock(h1,sizeof(*h1),f,l);
368    /* to be able to test matrices: */
369    mmDBTestBlock(h1->m,h1->ncols*h1->nrows*sizeof(poly),f,l);
370    #endif
371    /* to be able to test matrices: */
372    for (i=(h1->ncols*h1->nrows)-1; i>=0; i--)
373      pDBTest(h1->m[i],f,l);
374    int new_rk=idRankFreeModule(h1);
375    if(new_rk > h1->rank)
376    {
377      Print("wrong rank %d (should be %d) in %s:%d\n",
378      h1->rank, new_rk, f,l);
379      h1->rank=new_rk;
380    }
381  }
382}
383#endif
384
385/*3
386* for idSort: compare a and b revlex inclusive module comp.
387*/
388static int pComp_RevLex(poly a, poly b)
389{
390 int l=pVariables;
391 while ((l>0) && (pGetExp(a,l)==pGetExp(b,l))) l--;
392 if (l==0)
393 {
394   if (pGetComp(a)==pGetComp(b)) return 0;
395   if (pGetComp(a)>pGetComp(b)) return 1;
396 }
397 else if (pGetExp(a,l)>pGetExp(b,l))
398   return 1;
399 return -1;
400}
401
402/*2
403*sorts the ideal w.r.t. the actual ringordering
404*uses lex-ordering when nolex = FALSE
405*/
406intvec *idSort(ideal id,BOOLEAN nolex)
407{
408  poly p,q;
409  intvec * result = NewIntvec1(IDELEMS(id));
410  int i, j, actpos=0, newpos, l;
411  int diff, olddiff, lastcomp, newcomp;
412  BOOLEAN notFound;
413
414  pCompProc oldComp=pComp0;
415
416  if (!nolex) pComp0=pComp_RevLex;
417
418  for (i=0;i<IDELEMS(id);i++)
419  {
420    if (id->m[i]!=NULL)
421    {
422      notFound = TRUE;
423      newpos = actpos / 2;
424      diff = (actpos+1) / 2;
425      diff = (diff+1) / 2;
426      lastcomp = pComp0(id->m[i],id->m[(*result)[newpos]]);
427      if (lastcomp<0)
428      {
429        newpos -= diff;
430      }
431      else if (lastcomp>0)
432      {
433        newpos += diff;
434      }
435      else
436      {
437        notFound = FALSE;
438      }
439      while ((newpos>=0) && (newpos<actpos) && (notFound))
440      {
441        newcomp = pComp0(id->m[i],id->m[(*result)[newpos]]);
442        olddiff = diff;
443        if (diff>1)
444        {
445          diff = (diff+1) / 2;
446          if ((newcomp==1)
447          && (actpos-newpos>1)
448          && (diff>1)
449          && (newpos+diff>=actpos))
450          {
451            diff = actpos-newpos-1;
452          }
453          else if ((newcomp==-1)
454          && (diff>1)
455          && (newpos<diff))
456          {
457            diff = newpos;
458          }
459        }
460        if (newcomp<0)
461        {
462          if ((olddiff==1) && (lastcomp>0))
463            notFound = FALSE;
464          else
465            newpos -= diff;
466        }
467        else if (newcomp>0)
468        {
469          if ((olddiff==1) && (lastcomp<0))
470          {
471            notFound = FALSE;
472            newpos++;
473          }
474          else
475          {
476            newpos += diff;
477          }
478        }
479        else
480        {
481          notFound = FALSE;
482        }
483        lastcomp = newcomp;
484        if (diff==0) notFound=FALSE; /*hs*/
485      }
486      if (newpos<0) newpos = 0;
487      if (newpos>actpos) newpos = actpos;
488      while ((newpos<actpos) && (pComp0(id->m[i],id->m[(*result)[newpos]])==0))
489        newpos++;
490      for (j=actpos;j>newpos;j--)
491      {
492        (*result)[j] = (*result)[j-1];
493      }
494      (*result)[newpos] = i;
495      actpos++;
496    }
497  }
498  for (j=0;j<actpos;j++) (*result)[j]++;
499  pComp0=oldComp;
500  return result;
501}
502
503/*2
504* concat the lists h1 and h2 without zeros
505*/
506ideal idSimpleAdd (ideal h1,ideal h2)
507{
508  int i,j,r,l;
509  ideal result;
510
511  if (h1==NULL) return idCopy(h2);
512  if (h2==NULL) return idCopy(h1);
513  j = IDELEMS(h1)-1;
514  while ((j >= 0) && (h1->m[j] == NULL)) j--;
515  i = IDELEMS(h2)-1;
516  while ((i >= 0) && (h2->m[i] == NULL)) i--;
517  r = max(h1->rank,h2->rank);
518  if (i+j==(-2))
519    return idInit(1,r);
520  else
521    result=idInit(i+j+2,r);
522  for (l=j; l>=0; l--)
523  {
524    result->m[l] = pCopy(h1->m[l]);
525  }
526  r = i+j+1;
527  for (l=i; l>=0; l--, r--)
528  {
529    result->m[r] = pCopy(h2->m[l]);
530  }
531  return result;
532}
533
534/*2
535* h1 + h2
536*/
537ideal idAdd (ideal h1,ideal h2)
538{
539  ideal result = idSimpleAdd(h1,h2);
540  ideal tmp = idCompactify(result);
541
542  idDelete(&result);
543  return tmp;
544}
545
546/*2
547* h1 * h2
548*/
549ideal  idMult (ideal h1,ideal  h2)
550{
551  int i,j,k;
552  ideal  hh;
553
554  j = IDELEMS(h1);
555  while ((j > 0) && (h1->m[j-1] == NULL)) j--;
556  i = IDELEMS(h2);
557  while ((i > 0) && (h2->m[i-1] == NULL)) i--;
558  j = j * i;
559  if (j == 0)
560    hh = idInit(1,1);
561  else
562    hh=idInit(j,1);
563  if (h1->rank<h2->rank)
564    hh->rank = h2->rank;
565  else
566    hh->rank = h1->rank;
567  if (j==0) return hh;
568  k = 0;
569  for (i=0; i<IDELEMS(h1); i++)
570  {
571    if (h1->m[i] != NULL)
572    {
573      for (j=0; j<IDELEMS(h2); j++)
574      {
575        if (h2->m[j] != NULL)
576        {
577          hh->m[k] = pMult(pCopy(h1->m[i]),pCopy(h2->m[j]));
578          k++;
579        }
580      }
581    }
582  }
583  {
584    ideal tmp = idCompactify(hh);
585    idDelete(&hh);
586    return tmp;
587    //return hh;
588  }
589}
590
591/*2
592*returns true if h is the zero ideal
593*/
594BOOLEAN idIs0 (ideal h)
595{
596  int i;
597
598  if (h == NULL) return TRUE;
599  i = IDELEMS(h);
600  while ((i > 0) && (h->m[i-1] == NULL))
601  {
602    i--;
603  }
604  if (i == 0)
605    return TRUE;
606  else
607    return FALSE;
608}
609
610/*2
611* return the maximal component number found in any polynomial in s
612*/
613int idRankFreeModule (ideal s)
614{
615  if (s!=NULL)
616  {
617    int  j=0;
618    int  l=IDELEMS(s);
619    poly *p=s->m;
620    int  k;
621
622    for (; l != 0; l--)
623    {
624      if (*p!=NULL)
625      {
626        k = pMaxComp(*p);
627        if (k>j) j = k;
628      }
629      p++;
630    }
631    return j;
632  }
633  return -1;
634}
635
636/*2
637*returns true if id is homogenous with respect to the aktual weights
638*/
639BOOLEAN idHomIdeal (ideal id, ideal Q)
640{
641  int i;
642  BOOLEAN     b;
643  if ((id == NULL) || (IDELEMS(id) == 0)) return TRUE;
644  i = 0;
645  b = TRUE;
646  while ((i < IDELEMS(id)) && b)
647  {
648    b = pIsHomogeneous(id->m[i]);
649    i++;
650  }
651  if ((b) && (Q!=NULL) && (IDELEMS(Q)>0))
652  {
653    i=0;
654    while ((i < IDELEMS(Q)) && b)
655    {
656      b = pIsHomogeneous(Q->m[i]);
657      i++;
658    }
659  }
660  return b;
661}
662
663/*2
664*returns a minimized set of generators of h1
665*/
666ideal idMinBase (ideal h1)
667{
668  ideal h2, h3,h4,e;
669  int j,k;
670  int i,l,ll;
671  intvec * wth;
672  BOOLEAN homog;
673
674  homog = idHomModule(h1,currQuotient,&wth);
675  if ((currRing->OrdSgn == 1) && (!homog))
676  {
677    Warn("minbase applies only to the local or homogeneous case");
678    e=idCopy(h1);
679    return e;
680  }
681  if ((currRing->OrdSgn == 1) && (homog))
682  {
683    lists re=min_std(h1,currQuotient,(tHomog)homog,&wth,NULL,0,3);
684    h2 = (ideal)re->m[1].data;
685    re->m[1].data = NULL;
686    re->m[1].rtyp = NONE;
687    re->Clean();
688    return h2;
689  }
690  e=idInit(1,h1->rank);
691  if (idIs0(h1))
692  {
693    return e;
694  }
695  pEnlargeSet(&(e->m),IDELEMS(e),15);
696  IDELEMS(e) = 16;
697  h2 = kStd(h1,currQuotient,isNotHomog,NULL);
698  h3 = idMaxIdeal();
699  h4=idMult(h2,h3);
700  idDelete(&h3);
701  h3=kStd(h4,currQuotient,isNotHomog,NULL);
702  k = IDELEMS(h3);
703  while ((k > 0) && (h3->m[k-1] == NULL)) k--;
704  j = -1;
705  l = IDELEMS(h2);
706  while ((l > 0) && (h2->m[l-1] == NULL)) l--;
707  for (i=l-1; i>=0; i--)
708  {
709    if (h2->m[i] != NULL)
710    {
711      ll = 0;
712      while ((ll < k) && ((h3->m[ll] == NULL)
713      || !pDivisibleBy(h3->m[ll],h2->m[i])))
714        ll++;
715      if (ll >= k)
716      {
717        j++;
718        if (j > IDELEMS(e)-1)
719        {
720          pEnlargeSet(&(e->m),IDELEMS(e),16);
721          IDELEMS(e) += 16;
722        }
723        e->m[j] = pCopy(h2->m[i]);
724      }
725    }
726  }
727  idDelete(&h2);
728  idDelete(&h3);
729  idDelete(&h4);
730  if (currQuotient!=NULL)
731  {
732    h3=idInit(1,e->rank);
733    h2=kNF(h3,currQuotient,e);
734    idDelete(&h3);
735    idDelete(&e);
736    e=h2;
737  }
738  idSkipZeroes(e);
739  return e;
740}
741
742/*2
743*the minimal index of used variables - 1
744*/
745int pLowVar (poly p)
746{
747  int k,l,lex;
748
749  if (p == NULL) return -1;
750
751  k = 32000;/*a very large dummy value*/
752  while (p != NULL)
753  {
754    l = 1;
755    lex = pGetExp(p,l);
756    while ((l < pVariables) && (lex == 0))
757    {
758      l++;
759      lex = pGetExp(p,l);
760    }
761    l--;
762    if (l < k) k = l;
763    pIter(p);
764  }
765  return k;
766}
767
768/*3
769*multiplies p with t (!cas) or  (t-1)
770*the index of t is:1, so we have to shift all variables
771*p is NOT in the actual ring, it has no t
772*/
773static poly pMultWithT (poly p,BOOLEAN cas)
774{
775  /*qp is the working pointer in p*/
776  /*result is the result, qresult is the working pointer*/
777  /*pp is p in the actual ring(shifted), qpp the working pointer*/
778  poly result,qp,pp;
779  poly qresult=NULL;
780  poly qpp=NULL;
781  int  i,j,lex;
782  number n;
783
784  pp = NULL;
785  result = NULL;
786  qp = p;
787  while (qp != NULL)
788  {
789    i = 0;
790    if (result == NULL)
791    {/*first monomial*/
792      result = pInit();
793      qresult = result;
794    }
795    else
796    {
797      qresult->next = pInit();
798      pIter(qresult);
799    }
800    for (j=pVariables-1; j>0; j--)
801    {
802      lex = pGetExp(qp,j);
803      pSetExp(qresult,j+1,lex);/*copy all variables*/
804    }
805    lex = pGetComp(qp);
806    pSetComp(qresult,lex);
807    n=nCopy(pGetCoeff(qp));
808    pSetCoeff0(qresult,n);
809    qresult->next = NULL;
810    pSetm(qresult);
811    /*qresult is now qp brought into the actual ring*/
812    if (cas)
813    { /*case: mult with t-1*/
814      pSetExp(qresult,1,0);
815      pSetm(qresult);
816      if (pp == NULL)
817      { /*first monomial*/
818        pp = pCopy(qresult);
819        qpp = pp;
820      }
821      else
822      {
823        qpp->next = pCopy(qresult);
824        pIter(qpp);
825      }
826      pGetCoeff(qpp)=nNeg(pGetCoeff(qpp));
827      /*now qpp contains -1*qp*/
828    }
829    pSetExp(qresult,1,1);/*this is mult. by t*/
830    pSetm(qresult);
831    pIter(qp);
832  }
833  /*
834  *now p is processed:
835  *result contains t*p
836  * if cas: pp contains -1*p (in the new ring)
837  */
838  if (cas)  qresult->next = pp;
839  /*  else      qresult->next = NULL;*/
840  return result;
841}
842
843/*3
844*deletes the place of t in p (t: variable with index 1)
845*p is NOT in the actual ring: it has pVariables+1 variables
846*/
847static poly pDivByT (poly * p,int size)
848{
849
850  poly result=NULL,
851       resultp=NULL , /** working pointer in result **/
852       pp;
853  int  i,j;
854
855  while (*p != NULL)
856  {
857    i = 0;
858    if (result == NULL)
859    {/*the first monomial*/
860      result = pInit();
861      resultp = result;
862      resultp->next = NULL;
863    }
864    else
865    {
866      resultp->next = pInit();
867      pIter(resultp);
868      resultp->next = NULL;
869    }
870    for (j=1; j<=pVariables; j++)
871    {
872      pSetExp(resultp,j,pGetExp(*p,j+1));
873    }
874    pSetComp(resultp,pGetComp(*p));
875    pSetCoeff0(resultp,pGetCoeff(*p));
876    pSetm(resultp);
877    pp = (*p)->next;
878    Free((ADDRESS)*p,size);
879    *p = pp;
880  }
881  return result;
882}
883
884/*2
885*dehomogenized the generators of the ideal id1 with the leading
886*monomial of p replaced by n
887*/
888ideal idDehomogen (ideal id1,poly p,number n)
889{
890  int i;
891  ideal result;
892
893  if (idIs0(id1))
894  {
895    return idInit(1,id1->rank);
896  }
897  result=idInit(IDELEMS(id1),id1->rank);
898  for (i=0; i<IDELEMS(id1); i++)
899  {
900    result->m[i] = pDehomogen(id1->m[i],p,n);
901  }
902  return result;
903}
904
905/*2
906* verschiebt die Indizees der Modulerzeugenden um i
907*/
908void pShift (poly * p,int i)
909{
910  poly qp1 = *p,qp2 = *p;/*working pointers*/
911  int     j = pMaxComp(*p),k = pMinComp(*p);
912
913  if (j+i < 0) return ;
914  while (qp1 != NULL)
915  {
916    if ((pGetComp(qp1)+i > 0) || ((j == -i) && (j == k)))
917    {
918      pSetComp(qp1,pGetComp(qp1)+i);
919      pSetmComp(qp1);
920      qp2 = qp1;
921      pIter(qp1);
922    }
923    else
924    {
925      if (qp2 == *p)
926      {
927        pIter(*p);
928        qp2->next = NULL;
929        pDelete(&qp2);
930        qp2 = *p;
931        qp1 = *p;
932      }
933      else
934      {
935        qp2->next = qp1->next;
936        qp1->next = NULL;
937        pDelete(&qp1);
938        qp1 = qp2->next;
939      }
940    }
941  }
942}
943
944/*2
945*initialized a field with r numbers between beg and end for the
946*procedure idNextChoise
947*/
948void idInitChoise (int r,int beg,int end,BOOLEAN  *endch,int * choise)
949{
950  /*returns the first choise of r numbers between beg and end*/
951  int i;
952  for (i=0; i<r; i++)
953  {
954    choise[i] = 0;
955  }
956  if (r <= end-beg+1)
957    for (i=0; i<r; i++)
958    {
959      choise[i] = beg+i;
960    }
961  if (r > end-beg+1)
962    *endch = TRUE;
963  else
964    *endch = FALSE;
965}
966
967/*2
968*returns the next choise of r numbers between beg and end
969*/
970void idGetNextChoise (int r,int end,BOOLEAN  *endch,int * choise)
971{
972  int i = r-1,j;
973  while ((i >= 0) && (choise[i] == end))
974  {
975    i--;
976    end--;
977  }
978  if (i == -1)
979    *endch = TRUE;
980  else
981  {
982    choise[i]++;
983    for (j=i+1; j<r; j++)
984    {
985      choise[j] = choise[i]+j-i;
986    }
987    *endch = FALSE;
988  }
989}
990
991/*2
992*takes the field choise of d numbers between beg and end, cancels the t-th
993*entree and searches for the ordinal number of that d-1 dimensional field
994* w.r.t. the algorithm of construction
995*/
996int idGetNumberOfChoise(int t, int d, int begin, int end, int * choise)
997{
998  int * localchoise,i,result=0;
999  BOOLEAN b=FALSE;
1000
1001  if (d<=1) return 1;
1002  localchoise=(int*)Alloc((d-1)*sizeof(int));
1003  idInitChoise(d-1,begin,end,&b,localchoise);
1004  while (!b)
1005  {
1006    result++;
1007    i = 0;
1008    while ((i<t) && (localchoise[i]==choise[i])) i++;
1009    if (i>=t)
1010    {
1011      i = t+1;
1012      while ((i<d) && (localchoise[i-1]==choise[i])) i++;
1013      if (i>=d)
1014      {
1015        Free((ADDRESS)localchoise,(d-1)*sizeof(int));
1016        return result;
1017      }
1018    }
1019    idGetNextChoise(d-1,end,&b,localchoise);
1020  }
1021  Free((ADDRESS)localchoise,(d-1)*sizeof(int));
1022  return 0;
1023}
1024
1025/*2
1026*computes the binomial coefficient
1027*/
1028int binom (int n,int r)
1029{
1030  int i,result;
1031
1032  if (r==0) return 1;
1033  if (n-r<r) return binom(n,n-r);
1034  result = n-r+1;
1035  for (i=2;i<=r;i++)
1036  {
1037    result *= n-r+i;
1038    result /= i;
1039  }
1040  return result;
1041}
1042
1043/*2
1044*the free module of rank i
1045*/
1046ideal idFreeModule (int i)
1047{
1048  int j;
1049  ideal h;
1050
1051  h=idInit(i,i);
1052  for (j=0; j<i; j++)
1053  {
1054    h->m[j] = pOne();
1055    pSetComp(h->m[j],j+1);
1056    pSetmComp(h->m[j]);
1057  }
1058  return h;
1059}
1060
1061/*2
1062* h3 := h1 intersect h2
1063*/
1064ideal idSect (ideal h1,ideal h2)
1065{
1066  ideal first=h2,second=h1,temp,temp1,result;
1067  int i,j,k,flength,slength,length,rank=min(h1->rank,h2->rank);
1068  intvec *w;
1069  poly p,q;
1070
1071  if ((idIs0(h1)) && (idIs0(h2)))  return idInit(1,rank);
1072  if (IDELEMS(h1)<IDELEMS(h2))
1073  {
1074    first = h1;
1075    second = h2;
1076  }
1077  flength = idRankFreeModule(first);
1078  slength = idRankFreeModule(second);
1079  length  = max(flength,slength);
1080  if (length==0)
1081  {
1082    length = 1;
1083  }
1084  j = IDELEMS(first);
1085  temp = idInit(j /*IDELEMS(first)*/,length+j);
1086
1087  ring orig_ring=currRing;
1088  ring syz_ring=rCurrRingAssure_SyzComp();
1089  rSetSyzComp(length);
1090
1091  while ((j>0) && (first->m[j-1]==NULL)) j--;
1092  k = 0;
1093  for (i=0;i<j;i++)
1094  {
1095    if (first->m[i]!=NULL)
1096    {
1097      if (syz_ring==orig_ring)
1098        temp->m[k] = pCopy(first->m[i]);
1099      else
1100        temp->m[k] = prCopyR(first->m[i], orig_ring);
1101      q = pOne();
1102      pSetComp(q,i+1+length);
1103      pSetmComp(q);
1104      if (flength==0) pShift(&(temp->m[k]),1);
1105      p = temp->m[k];
1106      while (pNext(p)!=NULL) pIter(p);
1107      pNext(p) = q;
1108      k++;
1109    }
1110  }
1111  pEnlargeSet(&(temp->m),IDELEMS(temp),j+IDELEMS(second)-IDELEMS(temp));
1112  IDELEMS(temp) = j+IDELEMS(second);
1113  for (i=0;i<IDELEMS(second);i++)
1114  {
1115    if (second->m[i]!=NULL)
1116    {
1117      if (syz_ring==orig_ring)
1118        temp->m[k] = pCopy(second->m[i]);
1119      else
1120        temp->m[k] = prCopyR(second->m[i], orig_ring);
1121      if (slength==0) pShift(&(temp->m[k]),1);
1122      k++;
1123    }
1124  }
1125  temp1 = kStd(temp,currQuotient,testHomog,&w,NULL,length);
1126  if (w!=NULL) delete w;
1127  idDelete(&temp);
1128
1129  if(syz_ring!=orig_ring)
1130    rChangeCurrRing(orig_ring,TRUE);
1131
1132  result = idInit(IDELEMS(temp1),rank);
1133  j = 0;
1134  for (i=0;i<IDELEMS(temp1);i++)
1135  {
1136    if ((temp1->m[i]!=NULL)
1137    && (pRingGetComp(syz_ring,temp1->m[i])>length))
1138    {
1139      if(syz_ring==orig_ring)
1140        p = pCopy(temp1->m[i]);
1141      else
1142        p = prCopyR(temp1->m[i], syz_ring);
1143      while (p!=NULL)
1144      {
1145        q = pNext(p);
1146        pNext(p) = NULL;
1147        k = pGetComp(p)-1-length;
1148        pSetComp(p,0);
1149        pSetmComp(p);
1150        result->m[j] = pAdd(result->m[j],pMult(pCopy(first->m[k]),p));
1151        p = q;
1152      }
1153      j++;
1154    }
1155  }
1156  if(syz_ring!=orig_ring)
1157  {
1158    rChangeCurrRing(syz_ring,FALSE);
1159    idDelete(&temp1);
1160    rChangeCurrRing(orig_ring,TRUE);
1161    rKill(syz_ring);
1162  }
1163  else
1164  {
1165    idDelete(&temp1);
1166  }
1167
1168  idSkipZeroes(result);
1169  return result;
1170}
1171
1172/*2
1173* ideal/module intersection for a list of objects
1174* given as 'resolvente'
1175*/
1176ideal idMultSect(resolvente arg, int length)
1177{
1178  int i,j=0,k=0,syzComp,l,maxrk=-1,realrki;
1179  ideal bigmat,tempstd,result;
1180  poly p;
1181  int isIdeal=0;
1182  intvec * w=NULL;
1183
1184  /* find 0-ideals and max rank -----------------------------------*/
1185  for (i=0;i<length;i++)
1186  {
1187    if (!idIs0(arg[i]))
1188    {
1189      realrki=idRankFreeModule(arg[i]);
1190      k++;
1191      j += IDELEMS(arg[i]);
1192      if (realrki>maxrk) maxrk = realrki;
1193    }
1194    else
1195    {
1196      if (arg[i]!=NULL)
1197      {
1198        return idInit(1,arg[i]->rank);
1199      }
1200    }
1201  }
1202  if (maxrk == 0)
1203  {
1204    isIdeal = 1;
1205    maxrk = 1;
1206  }
1207  /* init -----------------------------------------------------------*/
1208  j += maxrk;
1209  syzComp = k*maxrk;
1210
1211  ring orig_ring=currRing;
1212  ring syz_ring=rCurrRingAssure_SyzComp();
1213  rSetSyzComp(syzComp);
1214
1215  bigmat = idInit(j,(k+1)*maxrk);
1216  /* create unit matrices ------------------------------------------*/
1217  for (i=0;i<maxrk;i++)
1218  {
1219    for (j=0;j<=k;j++)
1220    {
1221      p = pOne();
1222      pSetComp(p,i+1+j*maxrk);
1223      pSetmComp(p);
1224      bigmat->m[i] = pAdd(bigmat->m[i],p);
1225    }
1226  }
1227  /* enter given ideals ------------------------------------------*/
1228  i = maxrk;
1229  k = 0;
1230  for (j=0;j<length;j++)
1231  {
1232    if (arg[j]!=NULL)
1233    {
1234      for (l=0;l<IDELEMS(arg[j]);l++)
1235      {
1236        if (arg[j]->m[l]!=NULL)
1237        {
1238          if (syz_ring==orig_ring)
1239            bigmat->m[i] = pCopy(arg[j]->m[l]);
1240          else
1241            bigmat->m[i] = prCopyR(arg[j]->m[l], orig_ring);
1242          pShift(&(bigmat->m[i]),k*maxrk+isIdeal);
1243          i++;
1244        }
1245      }
1246      k++;
1247    }
1248  }
1249  /* std computation --------------------------------------------*/
1250  tempstd = kStd(bigmat,currQuotient,testHomog,&w,NULL,syzComp);
1251  if (w!=NULL) delete w;
1252  idDelete(&bigmat);
1253
1254  if(syz_ring!=orig_ring)
1255    rChangeCurrRing(orig_ring,TRUE);
1256
1257  /* interprete result ----------------------------------------*/
1258  result = idInit(8,maxrk);
1259  k = 0;
1260  for (j=0;j<IDELEMS(tempstd);j++)
1261  {
1262    if ((tempstd->m[j]!=NULL) && (pRingGetComp(syz_ring,tempstd->m[j])>syzComp))
1263    {
1264      if (k>=IDELEMS(result))
1265      {
1266        pEnlargeSet(&(result->m),IDELEMS(result),8);
1267        IDELEMS(result) += 8;
1268      }
1269      if (syz_ring==orig_ring)
1270        p = pCopy(tempstd->m[j]);
1271      else
1272        p = prCopyR(tempstd->m[j], syz_ring);
1273      pShift(&p,-syzComp-isIdeal);
1274      result->m[k] = p;
1275      k++;
1276    }
1277  }
1278  /* clean up ----------------------------------------------------*/
1279  if(syz_ring!=orig_ring)
1280    rChangeCurrRing(syz_ring,FALSE);
1281  idDelete(&tempstd);
1282  if(syz_ring!=orig_ring)
1283  {
1284    rChangeCurrRing(orig_ring,TRUE);
1285    rKill(syz_ring);
1286  }
1287  idSkipZeroes(result);
1288  return result;
1289}
1290
1291/*2
1292*computes syzygies of h1,
1293*if quot != NULL it computes in the quotient ring modulo "quot"
1294*works always in a ring with ringorder_s
1295*/
1296static ideal idPrepare (ideal  h1, tHomog h, int syzcomp, intvec **w)
1297{
1298  ideal   h2, h3;
1299  int     i;
1300  int     j,jj=0,k;
1301  poly    p,q;
1302
1303  if (idIs0(h1)) return NULL;
1304  k = idRankFreeModule(h1);
1305  h2=idCopy(h1);
1306  i = IDELEMS(h2)-1;
1307  if (k == 0)
1308  {
1309    for (j=0; j<=i; j++) pShift(&(h2->m[j]),1);
1310    k = 1;
1311  }
1312  if (syzcomp<k)
1313  {
1314    Warn("syzcomp too low, should be %d instead of %d",k,syzcomp);
1315    syzcomp = k;
1316    rSetSyzComp(k);
1317  }
1318  h2->rank = syzcomp+i+1;
1319  for (j=0; j<=i; j++)
1320  {
1321    p = h2->m[j];
1322    q = pOne();
1323    pSetComp(q,syzcomp+1+j);
1324    pSetmComp(q);
1325    if (p!=NULL)
1326    {
1327      while (pNext(p)) pIter(p);
1328      p->next = q;
1329    }
1330    else
1331      h2->m[j]=q;
1332  }
1333
1334#ifdef PDEBUG
1335  for(j=0;j<IDELEMS(h2);j++) pTest(h2->m[j]);
1336#endif
1337  h3=kStd(h2,currQuotient,h,w,NULL,syzcomp);
1338  idDelete(&h2);
1339  return h3;
1340}
1341
1342/*2
1343* compute the syzygies of h1 in R/quot,
1344* weights of components are in w
1345* if setRegularity, return the regularity in deg
1346* do not change h1,  w
1347*/
1348ideal idSyzygies (ideal  h1, tHomog h,intvec **w, BOOLEAN setSyzComp,
1349                  BOOLEAN setRegularity, int *deg)
1350{
1351  ideal s_h1;
1352  poly  p;
1353  int   i, j, k, length=0,reg;
1354  BOOLEAN isMonomial=TRUE;
1355
1356#ifdef PDEBUG
1357  int ii;
1358  for(ii=0;ii<IDELEMS(h1);ii++) pTest(h1->m[ii]);
1359#endif
1360  if (idIs0(h1))
1361    return idFreeModule(IDELEMS(h1));
1362  k=max(1,idRankFreeModule(h1));
1363
1364  assume(currRing != NULL);
1365  ring orig_ring=currRing;
1366  ring syz_ring=rCurrRingAssure_SyzComp();
1367
1368  if (setSyzComp)
1369    rSetSyzComp(k);
1370
1371  if (orig_ring != syz_ring)
1372  {
1373    s_h1=idrCopyR_NoSort(h1,orig_ring);
1374  }
1375  else
1376  {
1377    s_h1 = h1;
1378  }
1379
1380  ideal s_h3=idPrepare(s_h1,h,k,w);
1381
1382  if (s_h3==NULL)
1383  {
1384    return idFreeModule(IDELEMS(h1));
1385  }
1386
1387  if (orig_ring != syz_ring)
1388  {
1389    idDelete(&s_h1);
1390    for (j=0; j<IDELEMS(s_h3); j++)
1391    {
1392      if (s_h3->m[j] != NULL) 
1393      {
1394        if (pMinComp(s_h3->m[j],syz_ring) > k)
1395          pShift(&s_h3->m[j], -k);
1396        else
1397          pDelete(&s_h3->m[j]);
1398      }
1399    }
1400    idSkipZeroes(s_h3);
1401    s_h3->rank -= k;
1402    rChangeCurrRing(orig_ring, TRUE);
1403    s_h3 = idrMoveR_NoSort(s_h3, syz_ring);
1404    rKill(syz_ring);
1405    idTest(s_h3);
1406    return s_h3;
1407  }
1408 
1409  ideal e = idInit(IDELEMS(s_h3), s_h3->rank);
1410
1411  for (j=0; j<IDELEMS(s_h3); j++)
1412  {
1413    if (s_h3->m[j] != NULL)
1414    {
1415      if (pMinComp(s_h3->m[j],syz_ring) <= k)
1416      {
1417        e->m[j] = s_h3->m[j];
1418        isMonomial=isMonomial && (pNext(s_h3->m[j])==NULL);
1419        pDelete(&pNext(s_h3->m[j]));
1420        s_h3->m[j] = NULL;
1421      }
1422    }
1423  }
1424 
1425  idSkipZeroes(s_h3);
1426  idSkipZeroes(e);
1427 
1428  if (deg != NULL 
1429  && (!isMonomial)
1430  && (!TEST_OPT_NOTREGULARITY)
1431  && (setRegularity)
1432  && (h==isHomog))
1433  {
1434    ring dp_C_ring = rCurrRingAssure_dp_C();
1435    if (dp_C_ring != syz_ring)
1436      e = idrMoveR_NoSort(e, syz_ring);
1437    resolvente res = sySchreyerResolvente(e,-1,&length,TRUE, TRUE);
1438    intvec * dummy = syBetti(res,length,&reg, *w);
1439    *deg = reg+2;
1440    delete dummy;
1441    for (j=0;j<length;j++)
1442    {
1443      if (res[j]!=NULL) idDelete(&(res[j]));
1444    }
1445    Free((ADDRESS)res,length*sizeof(ideal));
1446    idDelete(&e);
1447    if (dp_C_ring != syz_ring)
1448    {
1449      rChangeCurrRing(syz_ring, TRUE);
1450      rKill(dp_C_ring);
1451    }
1452  }
1453  else
1454  {
1455    idDelete(&e);
1456  }
1457  idTest(s_h3);
1458  return s_h3;
1459}
1460
1461/*
1462*computes a standard basis for h1 and stores the transformation matrix
1463* in ma
1464*/
1465ideal idLiftStd (ideal  h1, matrix* ma, tHomog h)
1466{
1467  int   i, j, k, t, inputIsIdeal=idRankFreeModule(h1);
1468  poly  p=NULL, q, qq;
1469  intvec *w=NULL;
1470
1471  idDelete((ideal*)ma);
1472  *ma=mpNew(1,0);
1473  if (idIs0(h1))
1474    return idInit(1,h1->rank);
1475  k=max(1,idRankFreeModule(h1));
1476
1477  ring orig_ring=currRing;
1478  ring syz_ring=rCurrRingAssure_SyzComp();
1479  rSetSyzComp(k);
1480
1481  ideal s_h1=h1;
1482
1483  if (orig_ring != syz_ring)
1484    s_h1 = idrCopyR_NoSort(h1,orig_ring);
1485  else
1486    s_h1 = h1;
1487
1488  ideal s_h3=idPrepare(s_h1,h,k,&w);
1489  ideal s_h2 = idInit(IDELEMS(s_h3), s_h3->rank);
1490
1491  if (w!=NULL) delete w;
1492  i = 0;
1493  for (j=0; j<IDELEMS(s_h3); j++)
1494  {
1495    if ((s_h3->m[j] != NULL) && (pMinComp(s_h3->m[j],syz_ring) <= k))
1496    {
1497      i++;
1498      q = s_h3->m[j];
1499      while (pNext(q) != NULL)
1500      {
1501        if (pGetComp(pNext(q)) > k)
1502        {
1503          s_h2->m[j] = pNext(q);
1504          pNext(q) = NULL;
1505        }
1506        else
1507        {
1508          pIter(q);
1509        }
1510      }
1511      if (!inputIsIdeal) pShift(&(s_h3->m[j]), -1);
1512    }
1513    else
1514    {
1515      pDelete(&(s_h3->m[j]));
1516    }
1517  }
1518
1519  idSkipZeroes(s_h3);
1520  j = IDELEMS(s_h1);
1521
1522  if (syz_ring!=orig_ring)
1523  {
1524    idDelete(&s_h1);
1525    rChangeCurrRing(orig_ring,TRUE);
1526  }
1527 
1528  idDelete((ideal*)ma);
1529  *ma = mpNew(j,i);
1530
1531  i = 1;
1532  for (j=0; j<IDELEMS(s_h2); j++)
1533  {
1534    if (s_h2->m[j] != NULL)
1535    {
1536      q = prMoveR( s_h2->m[j], syz_ring);
1537      s_h2->m[j] = NULL;
1538
1539      while (q != NULL)
1540      {
1541        p = q;
1542        pIter(q);
1543        pNext(p) = NULL;
1544        t=pGetComp(p);
1545        pSetComp(p,0);
1546        pSetmComp(p);
1547        MATELEM(*ma,t-k,i) = pAdd(MATELEM(*ma,t-k,i),p);
1548      }
1549      i++;
1550    }
1551  }
1552  idDelete(&s_h2);
1553
1554  for (i=0; i<IDELEMS(s_h3); i++)
1555  {
1556    s_h3->m[i] = prMoveR_NoSort(s_h3->m[i], syz_ring);
1557  }
1558
1559  if (syz_ring!=orig_ring) rKill(syz_ring);
1560  return s_h3;
1561}
1562
1563/*2
1564*computes a representation of the generators of submod with respect to those
1565* of mod
1566*/
1567ideal idLiftNonStB (ideal  mod, ideal submod,BOOLEAN goodShape)
1568{
1569  int   lsmod =idRankFreeModule(submod), i, j, k;
1570
1571  if (idIs0(mod))
1572    return idInit(1,mod->rank);
1573
1574  k=idRankFreeModule(mod);
1575  if  ((k!=0) && (lsmod==0)) lsmod=1;
1576  k=max(k,1);
1577
1578  ring orig_ring=currRing;
1579  ring syz_ring=rCurrRingAssure_SyzComp();
1580  rSetSyzComp(k);
1581
1582  ideal s_mod, s_temp;
1583  if (orig_ring != syz_ring)
1584  {
1585    s_mod = idrCopyR_NoSort(mod,orig_ring);
1586    s_temp = idrCopyR_NoSort(submod,orig_ring);
1587  }
1588  else
1589  {
1590    s_mod = mod;
1591    s_temp = idCopy(submod);
1592  }
1593
1594  ideal s_h3=idPrepare(s_mod,(tHomog)FALSE,k,NULL);
1595
1596  if (!goodShape)
1597  {
1598    for (j=0;j<IDELEMS(s_h3);j++)
1599    {
1600      if ((s_h3->m[j] != NULL) && (pMinComp(s_h3->m[j]) > k))
1601        pDelete(&(s_h3->m[j]));
1602    }
1603  }
1604  idSkipZeroes(s_h3);
1605  if (lsmod==0)
1606  {
1607    for (j=IDELEMS(s_temp);j>0;j--)
1608    {
1609      if (s_temp->m[j-1]!=NULL)
1610        pShift(&(s_temp->m[j-1]),1);
1611    }
1612  }
1613  ideal s_result = kNF(s_h3,currQuotient,s_temp,k);
1614  s_result->rank = s_h3->rank;
1615  idDelete(&s_h3);
1616  idDelete(&s_temp);
1617
1618  for (j=0;j<IDELEMS(s_result);j++)
1619  {
1620    if (s_result->m[j]!=NULL)
1621    {
1622      if (pGetComp(s_result->m[j])<=k)
1623      {
1624        WerrorS("2nd module lies not in the first");
1625        idDelete(&s_result);
1626        s_result=idInit(1,1);
1627        break;
1628      }
1629      else
1630      {
1631        pShift(&(s_result->m[j]),-k);
1632        pNeg(s_result->m[j]);
1633      }
1634    }
1635  }
1636
1637  if(syz_ring!=orig_ring)
1638  {
1639    idDelete(&s_mod);
1640    rChangeCurrRing(orig_ring,TRUE);
1641    s_result = idrMoveR_NoSort(s_result, syz_ring);
1642    rKill(syz_ring);
1643  }
1644  return s_result;
1645}
1646
1647/*2
1648*computes a representation of the generators of submod with respect to those
1649* of mod which is given as standardbasis,
1650* uses currQuotient as the quotient ideal (if not NULL)
1651*/
1652ideal  idLift (ideal  mod,ideal submod)
1653{
1654  int   j,k;
1655  poly  p,q;
1656  BOOLEAN reported=FALSE;
1657
1658  if (idIs0(mod)) return idInit(1,mod->rank);
1659
1660  k = idRankFreeModule(mod);
1661
1662
1663  ring orig_ring=currRing;
1664  ring syz_ring=rCurrRingAssure_SyzComp();
1665  rSetSyzComp(max(k,1));
1666
1667  ideal s_result=idInit(IDELEMS(submod),submod->rank);
1668  ideal s_temp;
1669 
1670  if (syz_ring != orig_ring)
1671  {
1672    s_temp = idrCopyR_NoSort(mod,orig_ring);
1673  }
1674  else
1675  {
1676    s_temp = idCopy(mod);
1677  }
1678 
1679  if (k == 0)
1680  {
1681    for (j=0; j<IDELEMS(s_temp); j++)
1682    {
1683      if (s_temp->m[j]!=NULL) pSetCompP(s_temp->m[j],1);
1684    }
1685    k = 1;
1686  }
1687  for (j=0; j<IDELEMS(s_temp); j++)
1688  {
1689    if (s_temp->m[j]!=NULL)
1690    {
1691      p = s_temp->m[j];
1692      q = pOne();
1693      pGetCoeff(q)=nNeg(pGetCoeff(q));   //set q to -1
1694      pSetComp(q,k+1+j);
1695      pSetmComp(q);
1696      while (pNext(p)) pIter(p);
1697      pNext(p) = q;
1698    }
1699  }
1700
1701  for (j=0; j<IDELEMS(submod); j++)
1702  {
1703    if (submod->m[j]!=NULL)
1704    {
1705      if (syz_ring==orig_ring)
1706        p = pCopy(submod->m[j]);
1707      else
1708        p=prCopyR(submod->m[j], orig_ring);
1709      if (pGetComp(p)==0) pSetCompP(p,1);
1710      q = kNF(s_temp,currQuotient,p,k);
1711      pDelete(&p);
1712      if (q!=NULL)
1713      {
1714        if (pMinComp(q)<=k)
1715        {
1716          if (!reported)
1717          {
1718            WarnS("first module not a standardbasis\n"
1719            "// ** or second not a proper submodule");
1720            reported=TRUE;
1721          }
1722          pDelete(&q);
1723        }
1724        else
1725        {
1726          pShift(&q,-k);
1727          s_result->m[j] = q;
1728        }
1729      }
1730    }
1731  }
1732  idDelete(&s_temp);
1733
1734  if(syz_ring!=orig_ring)
1735  {
1736    rChangeCurrRing(orig_ring,TRUE);
1737    s_result = idrMoveR_NoSort(s_result, syz_ring);
1738    rKill(syz_ring);
1739  }
1740
1741  return s_result;
1742}
1743
1744
1745/*2
1746*computes the quotient of h1,h2
1747*/
1748static ideal idInitializeQuot (ideal  h1, ideal h2, BOOLEAN h1IsStb,
1749                               BOOLEAN *addOnlyOne, int *kkmax)
1750{
1751  ideal temph1;
1752  poly     p,q = NULL;
1753  int i,l,ll,k,kkk,kmax;
1754  int j = 0;
1755  int k1 = idRankFreeModule(h1);
1756  int k2 = idRankFreeModule(h2);
1757  tHomog   hom=isNotHomog;
1758
1759  k=max(k1,k2);
1760  if (k==0)
1761    k = 1;
1762  if ((k2==0) && (k>1)) *addOnlyOne = FALSE;
1763
1764  intvec * weights;
1765  hom = (tHomog)idHomModule(h1,currQuotient,&weights);
1766  if (addOnlyOne && (!h1IsStb))
1767    temph1 = kStd(h1,currQuotient,hom,&weights,NULL);
1768  else
1769    temph1 = idCopy(h1);
1770  if (weights!=NULL) delete weights;
1771  idTest(temph1);
1772/*--- making a single vector from h2 ---------------------*/
1773  for (i=0; i<IDELEMS(h2); i++)
1774  {
1775    if (h2->m[i] != NULL)
1776    {
1777      p = pCopy(h2->m[i]);
1778      if (k2 == 0)
1779        pShift(&p,j*k+1);
1780      else
1781        pShift(&p,j*k);
1782      q = pAdd(q,p);
1783      j++;
1784    }
1785  }
1786  *kkmax = kmax = j*k+1;
1787/*--- adding a monomial for the result (syzygy) ----------*/
1788  p = q;
1789  while (pNext(p)!=NULL) pIter(p);
1790  pNext(p) = pOne();
1791  pIter(p);
1792  pSetComp(p,kmax);
1793  pSetmComp(p);
1794/*--- constructing the big matrix ------------------------*/
1795  ideal h4 = idInit(16,kmax);
1796  h4->m[0] = q;
1797  if (k2 == 0)
1798  {
1799    if (k > IDELEMS(h4))
1800    {
1801      pEnlargeSet(&(h4->m),IDELEMS(h4),k-IDELEMS(h4));
1802      IDELEMS(h4) = k;
1803    }
1804    for (i=1; i<k; i++)
1805    {
1806      p = pCopy_noCheck(h4->m[i-1]);
1807      pShift(&p,1);
1808      h4->m[i] = p;
1809    }
1810  }
1811
1812  kkk = IDELEMS(h4);
1813  i = IDELEMS(temph1);
1814  while ((i>0) && (temph1->m[i-1]==NULL)) i--;
1815  for (l=0; l<i; l++)
1816  {
1817    if(temph1->m[l]!=NULL)
1818    {
1819      for (ll=0; ll<j; ll++)
1820      {
1821        p = pCopy(temph1->m[l]);
1822        if (k1 == 0)
1823          pShift(&p,ll*k+1);
1824        else
1825          pShift(&p,ll*k);
1826        if (kkk >= IDELEMS(h4))
1827        {
1828          pEnlargeSet(&(h4->m),IDELEMS(h4),16);
1829          IDELEMS(h4) += 16;
1830        }
1831        h4->m[kkk] = p;
1832        kkk++;
1833      }
1834    }
1835  }
1836/*--- if h2 goes in as single vector - the h1-part is just SB ---*/
1837  if (*addOnlyOne)
1838  {
1839    p = h4->m[0];
1840    for (i=0;i<IDELEMS(h4)-1;i++)
1841    {
1842      h4->m[i] = h4->m[i+1];
1843    }
1844    h4->m[IDELEMS(h4)-1] = p;
1845    idSkipZeroes(h4);
1846    test |= Sy_bit(OPT_SB_1);
1847  }
1848  idDelete(&temph1);
1849  idTest(h4);
1850  return h4;
1851}
1852/*2
1853*computes the quotient of h1,h2
1854*/
1855ideal idQuot (ideal  h1, ideal h2, BOOLEAN h1IsStb, BOOLEAN resultIsIdeal)
1856{
1857  // first check for special case h1:(0)
1858  if (idIs0(h2))
1859  {
1860    ideal res;
1861    if (resultIsIdeal)
1862    {
1863      res = idInit(1,1);
1864      res->m[0] = pOne();
1865    }
1866    else
1867      res = idFreeModule(h1->rank);
1868    return res;
1869  }
1870  BITSET old_test=test;
1871  poly     p,q = NULL;
1872  int i,l,ll,k,kkk,kmax;
1873  BOOLEAN  addOnlyOne=TRUE;
1874  tHomog   hom=isNotHomog;
1875  intvec * weights1;
1876
1877  ideal s_h4 = idInitializeQuot (h1,h2,h1IsStb,&addOnlyOne,&kmax);
1878  hom = (tHomog)idHomModule(s_h4,currQuotient,&weights1);
1879  ring orig_ring=currRing;
1880  ring syz_ring=rCurrRingAssure_SyzComp();
1881  rSetSyzComp(kmax-1);
1882  if (orig_ring!=syz_ring)
1883    s_h4 = idrMoveR_NoSort(s_h4,orig_ring);
1884
1885  ideal s_h3;
1886  if (addOnlyOne)
1887  {
1888    s_h3 = kStd(s_h4,currQuotient,hom,&weights1,NULL,kmax-1,IDELEMS(s_h4)-1);
1889  }
1890  else
1891  {
1892    s_h3 = kStd(s_h4,currQuotient,hom,&weights1,NULL,kmax-1);
1893  }
1894  idTest(s_h3);
1895  if (weights1!=NULL) delete weights1;
1896  idDelete(&s_h4);
1897
1898
1899  for (i=0;i<IDELEMS(s_h3);i++)
1900  {
1901    if ((s_h3->m[i]!=NULL) && (pGetComp(s_h3->m[i])>=kmax))
1902    {
1903      if (resultIsIdeal)
1904        pShift(&s_h3->m[i],-kmax);
1905      else
1906        pShift(&s_h3->m[i],-kmax+1);
1907    }
1908    else
1909      pDelete(&s_h3->m[i]);
1910  }
1911  if (resultIsIdeal)
1912    s_h3->rank = 1;
1913  else
1914    s_h3->rank = h1->rank;
1915  if(syz_ring!=orig_ring)
1916  {
1917//    pDelete(&q);
1918    rChangeCurrRing(orig_ring,TRUE);
1919    s_h3 = idrMoveR_NoSort(s_h3, syz_ring);
1920    rKill(syz_ring);
1921  }
1922  idSkipZeroes(s_h3);
1923  test = old_test;
1924  idTest(s_h3);
1925  return s_h3;
1926}
1927
1928/*2
1929*computes recursively all monomials of a certain degree
1930*in every step the actvar-th entry in the exponential
1931*vector is incremented and the other variables are
1932*computed by recursive calls of makemonoms
1933*if the last variable is reached, the difference to the
1934*degree is computed directly
1935*vars is the number variables
1936*actvar is the actual variable to handle
1937*deg is the degree of the monomials to compute
1938*monomdeg is the actual degree of the monomial in consideration
1939*/
1940static void makemonoms(int vars,int actvar,int deg,int monomdeg)
1941{
1942  poly p;
1943  int i=0;
1944
1945  if ((idpowerpoint == 0) && (actvar ==1))
1946  {
1947    idpower[idpowerpoint] = pOne();
1948    monomdeg = 0;
1949  }
1950  while (i<=deg)
1951  {
1952    if (deg == monomdeg)
1953    {
1954      pSetm(idpower[idpowerpoint]);
1955      idpowerpoint++;
1956      return;
1957    }
1958    if (actvar == vars)
1959    {
1960      pSetExp(idpower[idpowerpoint],actvar,deg-monomdeg);
1961      pSetm(idpower[idpowerpoint]);
1962      pTest(idpower[idpowerpoint]);
1963      idpowerpoint++;
1964      return;
1965    }
1966    else
1967    {
1968      p = pCopy(idpower[idpowerpoint]);
1969      makemonoms(vars,actvar+1,deg,monomdeg);
1970      idpower[idpowerpoint] = p;
1971    }
1972    monomdeg++;
1973    pSetExp(idpower[idpowerpoint],actvar,pGetExp(idpower[idpowerpoint],actvar)+1);
1974    pSetm(idpower[idpowerpoint]);
1975    pTest(idpower[idpowerpoint]);
1976    i++;
1977  }
1978}
1979
1980/*2
1981*returns the deg-th power of the maximal ideal of 0
1982*/
1983ideal idMaxIdeal(int deg)
1984{
1985  if (deg < 0)
1986  {
1987    WarnS("maxideal: power must be non-negative");
1988  }
1989  if (deg < 1)
1990  {
1991    ideal I=idInit(1,1);
1992    I->m[0]=pOne();
1993    return I;
1994  }
1995  if (deg == 1)
1996  {
1997    return idMaxIdeal();
1998  }
1999
2000  int vars = currRing->N;
2001  int i = binom(vars+deg-1,deg);
2002  ideal id=idInit(i,1);
2003  idpower = id->m;
2004  idpowerpoint = 0;
2005  makemonoms(vars,1,deg,0);
2006  idpower = NULL;
2007  idpowerpoint = 0;
2008  return id;
2009}
2010
2011/*2
2012*computes recursively all generators of a certain degree
2013*of the ideal "givenideal"
2014*elms is the number elements in the given ideal
2015*actelm is the actual element to handle
2016*deg is the degree of the power to compute
2017*gendeg is the actual degree of the generator in consideration
2018*/
2019static void makepotence(int elms,int actelm,int deg,int gendeg)
2020{
2021  poly p;
2022  int i=0;
2023
2024  if ((idpowerpoint == 0) && (actelm ==1))
2025  {
2026    idpower[idpowerpoint] = pOne();
2027    gendeg = 0;
2028  }
2029  while (i<=deg)
2030  {
2031    if (deg == gendeg)
2032    {
2033      idpowerpoint++;
2034      return;
2035    }
2036    if (actelm == elms)
2037    {
2038      p=pPower(pCopy(givenideal[actelm-1]),deg-gendeg);
2039      idpower[idpowerpoint]=pMult(idpower[idpowerpoint],p);
2040      idpowerpoint++;
2041      return;
2042    }
2043    else
2044    {
2045      p = pCopy(idpower[idpowerpoint]);
2046      makepotence(elms,actelm+1,deg,gendeg);
2047      idpower[idpowerpoint] = p;
2048    }
2049    gendeg++;
2050    idpower[idpowerpoint]=pMult(idpower[idpowerpoint],pCopy(givenideal[actelm-1]));
2051    i++;
2052  }
2053}
2054
2055/*2
2056*returns the deg-th power of the ideal gid
2057*/
2058//ideal idPower(ideal gid,int deg)
2059//{
2060//  int i;
2061//  ideal id;
2062//
2063//  if (deg < 1) deg = 1;
2064//  i = binom(IDELEMS(gid)+deg-1,deg);
2065//  id=idInit(i,1);
2066//  idpower = id->m;
2067//  givenideal = gid->m;
2068//  idpowerpoint = 0;
2069//  makepotence(IDELEMS(gid),1,deg,0);
2070//  idpower = NULL;
2071//  givenideal = NULL;
2072//  idpowerpoint = 0;
2073//  return id;
2074//}
2075static void idNextPotence(ideal given, ideal result,
2076  int begin, int end, int deg, int restdeg, poly ap)
2077{
2078  poly p;
2079  int i;
2080
2081  p = pPower(pCopy(given->m[begin]),restdeg);
2082  i = result->nrows;
2083  result->m[i] = pMult(pCopy(ap),p);
2084//PrintS(".");
2085  (result->nrows)++;
2086  if (result->nrows >= IDELEMS(result))
2087  {
2088    pEnlargeSet(&(result->m),IDELEMS(result),16);
2089    IDELEMS(result) += 16;
2090  }
2091  if (begin == end) return;
2092  for (i=restdeg-1;i>0;i--)
2093  {
2094    p = pPower(pCopy(given->m[begin]),i);
2095    p = pMult(pCopy(ap),p);
2096    idNextPotence(given, result, begin+1, end, deg, restdeg-i, p);
2097    pDelete(&p);
2098  }
2099  idNextPotence(given, result, begin+1, end, deg, restdeg, ap);
2100}
2101
2102ideal idPower(ideal given,int exp)
2103{
2104  ideal result,temp;
2105  poly p1;
2106  int i;
2107
2108  if (idIs0(given)) return idInit(1,1);
2109  temp = idCopy(given);
2110  idSkipZeroes(temp);
2111  i = binom(IDELEMS(temp)+exp-1,exp);
2112  result = idInit(i,1);
2113  result->nrows = 0;
2114//Print("ideal contains %d elements\n",i);
2115  p1=pOne();
2116  idNextPotence(temp,result,0,IDELEMS(temp)-1,exp,exp,p1);
2117  pDelete(&p1);
2118  idDelete(&temp);
2119  result->nrows = 1;
2120  idSkipZeroes(result);
2121  idDelEquals(result);
2122  return result;
2123}
2124
2125/*2
2126* eliminate delVar (product of vars) in h1
2127*/
2128ideal idElimination (ideal h1,poly delVar,intvec *hilb)
2129{
2130  int    i,j=0,k,l;
2131  ideal  h,hh, h3;
2132  int    *ord,*block0,*block1;
2133  int    ordersize=2;
2134  int    **wv;
2135  tHomog hom;
2136  intvec * w;
2137  sip_sring tmpR;
2138  ring origR = currRing;
2139
2140  if (delVar==NULL)
2141  {
2142    return idCopy(h1);
2143  }
2144  if (currQuotient!=NULL)
2145  {
2146    WerrorS("cannot eliminate in a qring");
2147    return idCopy(h1);
2148  }
2149  if (idIs0(h1)) return idInit(1,h1->rank);
2150  hom=(tHomog)idHomModule(h1,NULL,&w); //sets w to weight vector or NULL
2151  h3=idInit(16,h1->rank);
2152  for (k=0;; k++)
2153  {
2154    if (currRing->order[k]!=0) ordersize++;
2155    else break;
2156  }
2157  ord=(int*)Alloc0(ordersize*sizeof(int));
2158  block0=(int*)Alloc(ordersize*sizeof(int));
2159  block1=(int*)Alloc(ordersize*sizeof(int));
2160  for (k=0;; k++)
2161  {
2162    if (currRing->order[k]!=0)
2163    {
2164      block0[k+1] = currRing->block0[k];
2165      block1[k+1] = currRing->block1[k];
2166      ord[k+1] = currRing->order[k];
2167    }
2168    else
2169      break;
2170  }
2171  block0[0] = 1;
2172  block1[0] = pVariables;
2173  wv=(int**) Alloc0(ordersize*sizeof(int**));
2174  memcpy4(wv+1,currRing->wvhdl,(ordersize-1)*sizeof(int**));
2175  wv[0]=(int*)AllocL((pVariables+1)*sizeof(int));
2176  memset(wv[0],0,(pVariables+1)*sizeof(int));
2177  for (j=0;j<pVariables;j++)
2178    if (pGetExp(delVar,j+1)!=0) wv[0][j]=1;
2179  ord[0] = ringorder_a;
2180
2181  // fill in tmp ring to get back the data later on
2182  tmpR  = *origR;
2183  tmpR.order = ord;
2184  tmpR.block0 = block0;
2185  tmpR.block1 = block1;
2186  tmpR.wvhdl = wv;
2187  rComplete(&tmpR, 1);
2188
2189  // change into the new ring
2190  //pChangeRing(pVariables,currRing->OrdSgn,ord,block0,block1,wv);
2191  rChangeCurrRing(&tmpR, TRUE);
2192  currRing = &tmpR;
2193  h = idInit(IDELEMS(h1),1);
2194  // fetch data from the old ring
2195  for (k=0;k<IDELEMS(h1);k++) h->m[k] = prCopyR( h1->m[k], origR);
2196  // compute kStd
2197  hh = kStd(h,NULL,hom,&w,hilb);
2198  idDelete(&h);
2199
2200  // go back to the original ring
2201  rChangeCurrRing(origR,TRUE);
2202  i = IDELEMS(hh)-1;
2203  while ((i >= 0) && (hh->m[i] == NULL)) i--;
2204  j = -1;
2205  // fetch data from temp ring
2206  for (k=0; k<=i; k++)
2207  {
2208    l=pVariables;
2209    while ((l>0) && (pRingGetExp(&tmpR, hh->m[k],l)*pGetExp(delVar,l)==0)) l--;
2210    if (l==0)
2211    {
2212      j++;
2213      if (j >= IDELEMS(h3))
2214      {
2215        pEnlargeSet(&(h3->m),IDELEMS(h3),16);
2216        IDELEMS(h3) += 16;
2217      }
2218      h3->m[j] = prCopyR( hh->m[k], &tmpR);
2219    }
2220  }
2221  rChangeCurrRing(&tmpR, FALSE);
2222  idDelete(&hh);
2223  rChangeCurrRing(origR, TRUE);
2224  idSkipZeroes(h3);
2225  FreeL((ADDRESS)wv[0]);
2226  Free((ADDRESS)wv,ordersize*sizeof(int**));
2227  Free((ADDRESS)ord,ordersize*sizeof(int));
2228  Free((ADDRESS)block0,ordersize*sizeof(int));
2229  Free((ADDRESS)block1,ordersize*sizeof(int));
2230  rUnComplete(&tmpR);
2231  if (w!=NULL)
2232    delete w;
2233  return h3;
2234}
2235
2236//void idEnterSet (poly p,ideal r, int * next)
2237//{
2238//
2239//  if ((*next) == IDELEMS(r)-1)
2240//  {
2241//    pEnlargeSet(&(r->m),IDELEMS(r),16);
2242//    IDELEMS(r)+=16;
2243//  }
2244//  int at;
2245//  int i;
2246//  if (*next==0) at=0;
2247//  else
2248//  {
2249//    int an = 0;
2250//    int en= *next-1;
2251//    int c;
2252//    if (pComp0(r->m[(*next)-1],p)!= 1)
2253//      at=*next;
2254//    else
2255//    {
2256//      loop
2257//      {
2258//        if (an >= en-1)
2259//        {
2260//          if (pComp0(r->m[an],p) == 1)
2261//          {
2262//            at=an; break;
2263//          }
2264//          else
2265//          {
2266//            at=en; break;
2267//          }
2268//        }
2269//        i=(an+en) / 2;
2270//        if (pComp0(r->m[i],p) == 1) en=i;
2271//        else                       an=i;
2272//      }
2273//    }
2274//  }
2275//  if (pComp(r->m[at],p)==0)
2276//  {
2277//    pDelete(&p);
2278//  }
2279//  else
2280//  {
2281//    (*next)++;
2282//    for (i=(*next); i>=at+1; i--)
2283//    {
2284//      r->m[i] = r->m[i-1];
2285//    }
2286//    /*- save result -*/
2287//    r->m[at] = p;
2288//  }
2289//}
2290
2291#ifdef WITH_OLD_MINOR
2292/*2
2293* compute all ar-minors of the matrix a
2294*/
2295ideal idMinors(matrix a, int ar)
2296{
2297  int     i,j,k,size;
2298  int *rowchoise,*colchoise;
2299  BOOLEAN rowch,colch;
2300  ideal result;
2301  matrix tmp;
2302  poly p;
2303
2304  i = binom(a->rows(),ar);
2305  j = binom(a->cols(),ar);
2306
2307  rowchoise=(int *)Alloc(ar*sizeof(int));
2308  colchoise=(int *)Alloc(ar*sizeof(int));
2309  if ((i>512) || (j>512) || (i*j >512)) size=512;
2310  else size=i*j;
2311  result=idInit(size,1);
2312  tmp=mpNew(ar,ar);
2313  k = 0; /* the index in result*/
2314  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
2315  while (!rowch)
2316  {
2317    idInitChoise(ar,1,a->cols(),&colch,colchoise);
2318    while (!colch)
2319    {
2320      for (i=1; i<=ar; i++)
2321      {
2322        for (j=1; j<=ar; j++)
2323        {
2324          MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
2325        }
2326      }
2327      p = mpDetBareiss(tmp);
2328      if (p!=NULL)
2329      {
2330        if (k>=size)
2331        {
2332          pEnlargeSet(&result->m,size,32);
2333          size += 32;
2334        }
2335        result->m[k] = p;
2336        k++;
2337      }
2338      idGetNextChoise(ar,a->cols(),&colch,colchoise);
2339    }
2340    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
2341  }
2342  /*delete the matrix tmp*/
2343  for (i=1; i<=ar; i++)
2344  {
2345    for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
2346  }
2347  idDelete((ideal*)&tmp);
2348  if (k==0)
2349  {
2350    k=1;
2351    result->m[0]=NULL;
2352  }
2353  Free((ADDRESS)rowchoise,ar*sizeof(int));
2354  Free((ADDRESS)colchoise,ar*sizeof(int));
2355  pEnlargeSet(&result->m,size,k-size);
2356  IDELEMS(result) = k;
2357  return (result);
2358}
2359#else
2360/*2
2361* compute all ar-minors of the matrix a
2362* the caller of mpRecMin
2363*/
2364ideal idMinors(matrix a, int ar)
2365{
2366  ideal result;
2367  int elems=0;
2368
2369  if((ar<=0) || (ar>min(a->ncols,a->nrows)))
2370  {
2371    Werror("%d-th minor, matrix is %dx%d",ar,a->ncols,a->nrows);
2372    return NULL;
2373  }
2374  a = mpCopy(a);
2375  result=idInit(32,1);
2376  if(ar>1) mpRecMin(ar-1,result,elems,a,a->nrows,a->ncols,NULL);
2377  else mpMinorToResult(result,elems,a,a->nrows,a->ncols);
2378  idDelete((ideal *)&a);
2379  idSkipZeroes(result);
2380  idTest(result);
2381  return result;
2382}
2383#endif
2384
2385/*2
2386*returns TRUE if p is a unit element in the current ring
2387*/
2388BOOLEAN pIsUnit(poly p)
2389{
2390  int i;
2391
2392  if (p == NULL) return FALSE;
2393  i = 1;
2394  while (i<=pVariables && pGetExp(p,i) == 0) i++;
2395  if (i > pVariables && (pGetComp(p) == 0))
2396  {
2397    if (currRing->OrdSgn == 1 && pNext(p) !=NULL) return FALSE;
2398    return TRUE;
2399  }
2400  return FALSE;
2401}
2402
2403/*2
2404*skips all zeroes and double elements, searches also for units
2405*/
2406ideal idCompactify(ideal id)
2407{
2408  int i,j;
2409  BOOLEAN b=FALSE;
2410
2411  i = IDELEMS(id)-1;
2412  while ((! b) && (i>=0))
2413  {
2414    b=pIsUnit(id->m[i]);
2415    i--;
2416  }
2417  if (b)
2418  {
2419    ideal result=idInit(1,id->rank);
2420    result->m[0]=pOne();
2421    return result;
2422  }
2423  else
2424  {
2425    ideal result=idCopy(id);
2426    for (i=1;i<IDELEMS(result);i++)
2427    {
2428      if (result->m[i]!=NULL)
2429      {
2430        for (j=0;j<i;j++)
2431        {
2432          if ((result->m[j]!=NULL)
2433          && (pComparePolys(result->m[i],result->m[j])))
2434          {
2435            pDelete(&(result->m[j]));
2436          }
2437        }
2438      }
2439    }
2440    idSkipZeroes(result);
2441    return result;
2442  }
2443}
2444
2445/*2
2446*returns TRUE if id1 is a submodule of id2
2447*/
2448BOOLEAN idIsSubModule(ideal id1,ideal id2)
2449{
2450  int i;
2451  poly p;
2452
2453  if (idIs0(id1)) return TRUE;
2454  for (i=0;i<IDELEMS(id1);i++)
2455  {
2456    if (id1->m[i] != NULL)
2457    {
2458      p = kNF(id2,currQuotient,id1->m[i]);
2459      if (p != NULL)
2460      {
2461        pDelete(&p);
2462        return FALSE;
2463      }
2464    }
2465  }
2466  return TRUE;
2467}
2468
2469/*2
2470* returns the ideals of initial terms
2471*/
2472ideal idHead(ideal h)
2473{
2474  ideal m = idInit(IDELEMS(h),h->rank);
2475  int i;
2476
2477  for (i=IDELEMS(h)-1;i>=0; i--)
2478  {
2479    if (h->m[i]!=NULL) m->m[i]=pHead(h->m[i]);
2480  }
2481  return m;
2482}
2483
2484ideal idHomogen(ideal h, int varnum)
2485{
2486  ideal m = idInit(IDELEMS(h),h->rank);
2487  int i;
2488
2489  for (i=IDELEMS(h)-1;i>=0; i--)
2490  {
2491    m->m[i]=pHomogen(h->m[i],varnum);
2492  }
2493  return m;
2494}
2495
2496/*------------------type conversions----------------*/
2497ideal idVec2Ideal(poly vec)
2498{
2499   ideal result=idInit(1,1);
2500   FreeSizeOf((ADDRESS)result->m,poly);
2501   result->m=NULL; // remove later
2502   pVec2Polys(vec, &(result->m), &(IDELEMS(result)));
2503   return result;
2504}
2505
2506// converts mat to module, destroys mat
2507ideal idMatrix2Module(matrix mat)
2508{
2509  ideal result = idInit(MATCOLS(mat),MATROWS(mat));
2510  int i,j;
2511  poly h;
2512#ifdef DRING
2513  poly p;
2514#endif
2515
2516  for(j=0;j<MATCOLS(mat);j++) /* j is also index in result->m */
2517  {
2518    for (i=1;i<=MATROWS(mat);i++)
2519    {
2520      h = MATELEM(mat,i,j+1);
2521      if (h!=NULL)
2522      {
2523        MATELEM(mat,i,j+1)=NULL;
2524        pSetCompP(h,i);
2525#ifdef DRING
2526        pdSetDFlagP(h,0);
2527#endif
2528        result->m[j] = pAdd(result->m[j],h);
2529      }
2530    }
2531  }
2532  // obachman: need to clean this up
2533  idDelete((ideal*) &mat);
2534  return result;
2535}
2536
2537/*2
2538* converts a module into a matrix, destroyes the input
2539*/
2540matrix idModule2Matrix(ideal mod)
2541{
2542  matrix result = mpNew(mod->rank,IDELEMS(mod));
2543  int i,cp;
2544  poly p,h;
2545
2546  for(i=0;i<IDELEMS(mod);i++)
2547  {
2548    p=mod->m[i];
2549    mod->m[i]=NULL;
2550    while (p!=NULL)
2551    {
2552      h=p;
2553      pIter(p);
2554      pNext(h)=NULL;
2555//      cp = max(1,pGetComp(h));     // if used for ideals too
2556      cp = pGetComp(h);
2557      pSetComp(h,0);
2558      pSetmComp(h);
2559#ifdef TEST
2560      if (cp>mod->rank)
2561      {
2562        Print("## inv. rank %d -> %d\n",mod->rank,cp);
2563        int k,l,o=mod->rank;
2564        mod->rank=cp;
2565        matrix d=mpNew(mod->rank,IDELEMS(mod));
2566        for (l=1; l<=o; l++)
2567        {
2568          for (k=1; k<=IDELEMS(mod); k++)
2569          {
2570            MATELEM(d,l,k)=MATELEM(result,l,k);
2571            MATELEM(result,l,k)=NULL;
2572          }
2573        }
2574        idDelete((ideal *)&result);
2575        result=d;
2576      }
2577#endif
2578      MATELEM(result,cp,i+1) = pAdd(MATELEM(result,cp,i+1),h);
2579    }
2580  }
2581// obachman 10/99: added the following line, otherwise memory lack!
2582  idDelete(&mod);
2583  return result;
2584}
2585
2586matrix idModule2formatedMatrix(ideal mod,int rows, int cols)
2587{
2588  matrix result = mpNew(rows,cols);
2589  int i,cp,r=idRankFreeModule(mod),c=IDELEMS(mod);
2590  poly p,h;
2591
2592  if (r>rows) r = rows;
2593  if (c>cols) c = cols;
2594  for(i=0;i<c;i++)
2595  {
2596    p=mod->m[i];
2597    mod->m[i]=NULL;
2598    while (p!=NULL)
2599    {
2600      h=p;
2601      pIter(p);
2602      pNext(h)=NULL;
2603      cp = pGetComp(h);
2604      if (cp<=r)
2605      {
2606        pSetComp(h,0);
2607        pSetmComp(h);
2608        MATELEM(result,cp,i+1) = pAdd(MATELEM(result,cp,i+1),h);
2609      }
2610      else
2611        pDelete(&h);
2612    }
2613  }
2614  idDelete(&mod);
2615  return result;
2616}
2617
2618/*2
2619* substitute the n-th variable by the monomial e in id
2620* destroy id
2621*/
2622ideal  idSubst(ideal id, int n, poly e)
2623{
2624  int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
2625  ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
2626
2627  res->rank = id->rank;
2628  for(k--;k>=0;k--)
2629  {
2630    res->m[k]=pSubst(id->m[k],n,e);
2631    id->m[k]=NULL;
2632  }
2633  idDelete(&id);
2634  return res;
2635}
2636
2637BOOLEAN idHomModule(ideal m, ideal Q, intvec **w)
2638{
2639  if (w!=NULL) *w=NULL;
2640  if ((Q!=NULL) && (!idHomIdeal(Q,NULL))) return FALSE;
2641  if (idIs0(m)) return TRUE;
2642
2643  int i,j,cmax=2,order=0,ord,* diff,* iscom,diffmin=32000;
2644  poly p=NULL;
2645  int length=IDELEMS(m);
2646  polyset P=m->m;
2647  polyset F=(polyset)Alloc(length*sizeof(poly));
2648  for (i=length-1;i>=0;i--)
2649  {
2650    p=F[i]=P[i];
2651    cmax=max(cmax,pMaxComp(p)+1);
2652  }
2653  diff = (int *)Alloc0(cmax*sizeof(int));
2654  if (w!=NULL) *w=NewIntvec1(cmax-1);
2655  iscom = (int *)Alloc0(cmax*sizeof(int));
2656  i=0;
2657  while (i<=length)
2658  {
2659    if (i<length)
2660    {
2661      p=F[i];
2662      while ((p!=NULL) && (!iscom[pGetComp(p)])) pIter(p);
2663    }
2664    if ((p==NULL) && (i<length))
2665    {
2666      i++;
2667    }
2668    else
2669    {
2670      if (p==NULL)
2671      {
2672        i=0;
2673        while ((i<length) && (F[i]==NULL)) i++;
2674        if (i>=length) break;
2675        p = F[i];
2676      }
2677      if (pLexOrder)
2678        order=pTotaldegree(p);
2679      else
2680      //  order = p->order;
2681        order = pFDeg(p);
2682      order += diff[pGetComp(p)];
2683      p = F[i];
2684//Print("Actual p=F[%d]: ",i);pWrite(p);
2685      F[i] = NULL;
2686      i=0;
2687    }
2688    while (p!=NULL)
2689    {
2690      //if (pLexOrder)
2691      //  ord=pTotaldegree(p);
2692      //else
2693      //  ord = p->order;
2694      ord = pFDeg(p);
2695      if (!iscom[pGetComp(p)])
2696      {
2697        diff[pGetComp(p)] = order-ord;
2698        iscom[pGetComp(p)] = 1;
2699/*
2700*PrintS("new diff: ");
2701*for (j=0;j<cmax;j++) Print("%d ",diff[j]);
2702*PrintLn();
2703*PrintS("new iscom: ");
2704*for (j=0;j<cmax;j++) Print("%d ",iscom[j]);
2705*PrintLn();
2706*Print("new set %d, order %d, ord %d, diff %d\n",pGetComp(p),order,ord,diff[pGetComp(p)]);
2707*/
2708      }
2709      else
2710      {
2711/*
2712*PrintS("new diff: ");
2713*for (j=0;j<cmax;j++) Print("%d ",diff[j]);
2714*PrintLn();
2715*Print("order %d, ord %d, diff %d\n",order,ord,diff[pGetComp(p)]);
2716*/
2717        if (order != ord+diff[pGetComp(p)])
2718        {
2719          Free((ADDRESS) iscom,cmax*sizeof(int));
2720          Free((ADDRESS) diff,cmax*sizeof(int));
2721          Free((ADDRESS) F,length*sizeof(poly));
2722          delete *w;*w=NULL;
2723          return FALSE;
2724        }
2725      }
2726      pIter(p);
2727    }
2728  }
2729  Free((ADDRESS) iscom,cmax*sizeof(int));
2730  Free((ADDRESS) F,length*sizeof(poly));
2731  for (i=1;i<cmax;i++) (**w)[i-1]=diff[i];
2732  for (i=1;i<cmax;i++)
2733  {
2734    if (diff[i]<diffmin) diffmin=diff[i];
2735  }
2736  if (w!=NULL)
2737  {
2738    for (i=1;i<cmax;i++)
2739    {
2740      (**w)[i-1]=diff[i]-diffmin;
2741    }
2742  }
2743  Free((ADDRESS) diff,cmax*sizeof(int));
2744  return TRUE;
2745}
2746
2747ideal idJet(ideal i,int d)
2748{
2749  ideal r=idInit((i->nrows)*(i->ncols),i->rank);
2750  r->nrows = i-> nrows;
2751  r->ncols = i-> ncols;
2752  //r->rank = i-> rank;
2753  int k;
2754  for(k=(i->nrows)*(i->ncols)-1;k>=0; k--)
2755  {
2756    r->m[k]=pJet(i->m[k],d);
2757  }
2758  return r;
2759}
2760
2761ideal idJetW(ideal i,int d, intvec * iv)
2762{
2763  ideal r=idInit(IDELEMS(i),i->rank);
2764  if (ecartWeights!=NULL)
2765  {
2766    WerrorS("cannot compute weighted jets now");
2767  }
2768  else
2769  {
2770    short *w=iv2array(iv);
2771    int k;
2772    for(k=0; k<IDELEMS(i); k++)
2773    {
2774      r->m[k]=pJetW(i->m[k],d,w);
2775    }
2776    Free((ADDRESS)w,(pVariables+1)*sizeof(short));
2777  }
2778  return r;
2779}
2780
2781matrix idDiff(matrix i, int k)
2782{
2783  int e=MATCOLS(i)*MATROWS(i);
2784  matrix r=mpNew(MATROWS(i),MATCOLS(i));
2785  int j;
2786  for(j=0; j<e; j++)
2787  {
2788    r->m[j]=pDiff(i->m[j],k);
2789  }
2790  return r;
2791}
2792
2793matrix idDiffOp(ideal I, ideal J,BOOLEAN multiply)
2794{
2795  matrix r=mpNew(IDELEMS(I),IDELEMS(J));
2796  int i,j;
2797  for(i=0; i<IDELEMS(I); i++)
2798  {
2799    for(j=0; j<IDELEMS(J); j++)
2800    {
2801      MATELEM(r,i+1,j+1)=pDiffOp(I->m[i],J->m[j],multiply);
2802    }
2803  }
2804  return r;
2805}
2806
2807/*3
2808*handles for some ideal operations the ring/syzcomp managment
2809*returns all syzygies (componentwise-)shifted by -syzcomp
2810*or -syzcomp-1 (in case of ideals as input)
2811static ideal idHandleIdealOp(ideal arg,int syzcomp,int isIdeal=FALSE)
2812{
2813  ring orig_ring=currRing;
2814  ring syz_ring=rCurrRingAssure_SyzComp();
2815  rSetSyzComp(length);
2816
2817  ideal s_temp;
2818  if (orig_ring!=syz_ring)
2819    s_temp=idrMoveR_NoSort(arg,orig_ring);
2820  else
2821    s_temp=arg;
2822
2823  ideal s_temp1 = kStd(s_temp,currQuotient,testHomog,&w,NULL,length);
2824  if (w!=NULL) delete w;
2825
2826  if (syz_ring!=orig_ring)
2827  {
2828    idDelete(&s_temp);
2829    rChangeCurrRing(orig_ring,TRUE);
2830  }
2831
2832  idDelete(&temp);
2833  ideal temp1=idRingCopy(s_temp1,syz_ring);
2834
2835  if (syz_ring!=orig_ring)
2836  {
2837    rChangeCurrRing(syz_ring,FALSE);
2838    idDelete(&s_temp1);
2839    rChangeCurrRing(orig_ring,TRUE);
2840    rKill(syz_ring);
2841  }
2842
2843  for (i=0;i<IDELEMS(temp1);i++)
2844  {
2845    if ((temp1->m[i]!=NULL)
2846    && (pGetComp(temp1->m[i])<=length))
2847    {
2848      pDelete(&(temp1->m[i]));
2849    }
2850    else
2851    {
2852      pShift(&(temp1->m[i]),-length);
2853    }
2854  }
2855  temp1->rank = rk;
2856  idSkipZeroes(temp1);
2857
2858  return temp1;
2859}
2860*/
2861/*2
2862* represents (h1+h2)/h2=h1/(h1 intersect h2)
2863*/
2864ideal idModulo (ideal h2,ideal h1)
2865{
2866  int i,j,k,rk,flength=0,slength,length;
2867  intvec * w;
2868  poly p,q;
2869
2870  if (idIs0(h2))
2871    return idFreeModule(max(1,h2->ncols));
2872  if (!idIs0(h1))
2873    flength = idRankFreeModule(h1);
2874  slength = idRankFreeModule(h2);
2875  length  = max(flength,slength);
2876  if (length==0)
2877  {
2878    length = 1;
2879  }
2880  ideal temp = idInit(IDELEMS(h2),length+IDELEMS(h2));
2881  for (i=0;i<IDELEMS(h2);i++)
2882  {
2883    temp->m[i] = pCopy(h2->m[i]);
2884    q = pOne();
2885    pSetComp(q,i+1+length);
2886    pSetmComp(q);
2887    if(temp->m[i]!=NULL)
2888    {
2889      if (slength==0) pShift(&(temp->m[i]),1);
2890      p = temp->m[i];
2891      while (pNext(p)!=NULL) pIter(p);
2892      pNext(p) = q;
2893    }
2894    else
2895      temp->m[i]=q;
2896  }
2897  rk = k = IDELEMS(h2);
2898  if (!idIs0(h1))
2899  {
2900    pEnlargeSet(&(temp->m),IDELEMS(temp),IDELEMS(h1));
2901    IDELEMS(temp) += IDELEMS(h1);
2902    for (i=0;i<IDELEMS(h1);i++)
2903    {
2904      if (h1->m[i]!=NULL)
2905      {
2906        temp->m[k] = pCopy(h1->m[i]);
2907        if (flength==0) pShift(&(temp->m[k]),1);
2908        k++;
2909      }
2910    }
2911  }
2912
2913  ring orig_ring=currRing;
2914  ring syz_ring=rCurrRingAssure_SyzComp();
2915  rSetSyzComp(length);
2916  ideal s_temp;
2917 
2918  if (syz_ring != orig_ring)
2919  {
2920    s_temp = idrMoveR_NoSort(temp, orig_ring);
2921  }
2922  else
2923  {
2924    s_temp = temp;
2925  }
2926 
2927  idTest(s_temp);
2928  ideal s_temp1 = kStd(s_temp,currQuotient,testHomog,&w,NULL,length);
2929  if (w!=NULL) delete w;
2930
2931  for (i=0;i<IDELEMS(s_temp1);i++)
2932  {
2933    if ((s_temp1->m[i]!=NULL)
2934    && (pGetComp(s_temp1->m[i])<=length))
2935    {
2936      pDelete(&(s_temp1->m[i]));
2937    }
2938    else
2939    {
2940      pShift(&(s_temp1->m[i]),-length);
2941    }
2942  }
2943  s_temp1->rank = rk;
2944  idSkipZeroes(s_temp1);
2945
2946  if (syz_ring!=orig_ring)
2947  {
2948    rChangeCurrRing(orig_ring,TRUE);
2949    s_temp1 = idrMoveR_NoSort(s_temp1, syz_ring);
2950    rKill(syz_ring);
2951  }
2952  else
2953  {
2954    idDelete(&temp);
2955  }
2956  idTest(s_temp1);
2957  return s_temp1;
2958}
2959
2960int idElem(ideal F)
2961{
2962  int i=0,j=0;
2963
2964  while(j<IDELEMS(F))
2965  {
2966   if ((F->m)[j]!=NULL) i++;
2967   j++;
2968  }
2969  return i;
2970}
2971
2972/*
2973*computes module-weights for liftings of homogeneous modules
2974*/
2975intvec * idMWLift(ideal mod,intvec * weights)
2976{
2977  if (idIs0(mod)) return NewIntvec1(2);
2978  int i=IDELEMS(mod);
2979  while ((i>0) && (mod->m[i-1]==NULL)) i--;
2980  intvec *result = NewIntvec1(i+1);
2981  while (i>0)
2982  {
2983    (*result)[i]=pFDeg(mod->m[i])+(*weights)[pGetComp(mod->m[i])];
2984  }
2985  return result;
2986}
2987
2988/*2
2989*sorts the kbase for idCoef* in a special way (lexicographically
2990*with x_max,...,x_1)
2991*/
2992ideal idCreateSpecialKbase(ideal kBase,intvec ** convert)
2993{
2994  int i;
2995  ideal result;
2996
2997  if (idIs0(kBase)) return NULL;
2998  result = idInit(IDELEMS(kBase),kBase->rank);
2999  *convert = idSort(kBase,FALSE);
3000  for (i=0;i<(*convert)->length();i++)
3001  {
3002    result->m[i] = pCopy(kBase->m[(**convert)[i]-1]);
3003  }
3004  return result;
3005}
3006
3007/*2
3008*returns the index of a given monom in the list of the special kbase
3009*/
3010int idIndexOfKBase(poly monom, ideal kbase)
3011{
3012  int j=IDELEMS(kbase);
3013
3014  while ((j>0) && (kbase->m[j-1]==NULL)) j--;
3015  if (j==0) return -1;
3016  int i=pVariables;
3017  while (i>0)
3018  {
3019    loop
3020    {
3021      if (pGetExp(monom,i)>pGetExp(kbase->m[j-1],i)) return -1;
3022      if (pGetExp(monom,i)==pGetExp(kbase->m[j-1],i)) break;
3023      j--;
3024      if (j==0) return -1;
3025    }
3026    if (i==1)
3027    {
3028      while(j>0)
3029      {
3030        if (pGetComp(monom)==pGetComp(kbase->m[j-1])) return j-1;
3031        if (pGetComp(monom)>pGetComp(kbase->m[j-1])) return -1;
3032        j--;
3033      }
3034    }
3035    i--;
3036  }
3037  return -1;
3038}
3039
3040/*2
3041*decomposes the monom in a part of coefficients described by the
3042*complement of how and a monom in variables occuring in how, the
3043*index of which in kbase is returned as integer pos (-1 if it don't
3044*exists)
3045*/
3046poly idDecompose(poly monom, poly how, ideal kbase, int * pos)
3047{
3048  int i;
3049  poly coeff=pOne(), base=pOne();
3050
3051  for (i=1;i<=pVariables;i++)
3052  {
3053    if (pGetExp(how,i)>0)
3054    {
3055      pSetExp(base,i,pGetExp(monom,i));
3056    }
3057    else
3058    {
3059      pSetExp(coeff,i,pGetExp(monom,i));
3060    }
3061  }
3062  pSetComp(base,pGetComp(monom));
3063  pSetm(base);
3064  pSetCoeff(coeff,nCopy(pGetCoeff(monom)));
3065  pSetm(coeff);
3066  *pos = idIndexOfKBase(base,kbase);
3067  if (*pos<0)
3068    pDelete(&coeff);
3069  pDelete(&base);
3070  return coeff;
3071}
3072
3073/*2
3074*returns a matrix A of coefficients with kbase*A=arg
3075*if all monomials in variables of how occur in kbase
3076*the other are deleted
3077*/
3078matrix idCoeffOfKBase(ideal arg, ideal kbase, poly how)
3079{
3080  matrix result;
3081  ideal tempKbase;
3082  poly p,q;
3083  intvec * convert;
3084  int i=IDELEMS(kbase),j=IDELEMS(arg),k,pos;
3085#if 0
3086  while ((i>0) && (kbase->m[i-1]==NULL)) i--;
3087  if (idIs0(arg))
3088    return mpNew(i,1);
3089  while ((j>0) && (arg->m[j-1]==NULL)) j--;
3090  result = mpNew(i,j);
3091#else
3092  result = mpNew(i, j);
3093  while ((j>0) && (arg->m[j-1]==NULL)) j--;
3094#endif
3095
3096  tempKbase = idCreateSpecialKbase(kbase,&convert);
3097  for (k=0;k<j;k++)
3098  {
3099    p = arg->m[k];
3100    while (p!=NULL)
3101    {
3102      q = idDecompose(p,how,tempKbase,&pos);
3103      if (pos>=0)
3104      {
3105        MATELEM(result,(*convert)[pos],k+1) =
3106            pAdd(MATELEM(result,(*convert)[pos],k+1),q);
3107      }
3108      else
3109        pDelete(&q);
3110      pIter(p);
3111    }
3112  }
3113  idDelete(&tempKbase);
3114  return result;
3115}
3116
3117intvec * idQHomWeights(ideal id)
3118{
3119  intvec * imat=NewIntvec3(2*pVariables,pVariables,0);
3120  poly actHead=NULL,wPoint=NULL;
3121  int actIndex,i=-1,j=1,k;
3122  BOOLEAN notReady=TRUE;
3123
3124  while (notReady)
3125  {
3126    if (wPoint==NULL)
3127    {
3128      i++;
3129      while ((i<IDELEMS(id))
3130      && ((id->m[i]==NULL) || (pNext(id->m[i])==NULL)))
3131        i++;
3132      if (i<IDELEMS(id))
3133      {
3134        actHead = id->m[i];
3135        wPoint = pNext(actHead);
3136      }
3137    }
3138    while ((wPoint!=NULL) && (j<=2*pVariables))
3139    {
3140      for (k=1;k<=pVariables;k++)
3141        IMATELEM(*imat,j,k) += pGetExp(actHead,k)-pGetExp(wPoint,k);
3142      pIter(wPoint);
3143      j++;
3144    }
3145    if ((i>=IDELEMS(id)) || (j>2*pVariables))
3146    {
3147      ivTriangMat(imat,1,1);
3148      j = ivFirstEmptyRow(imat);
3149      if ((i>=IDELEMS(id)) || (j>pVariables)) notReady=FALSE;
3150    }
3151  }
3152  intvec *result=NULL;
3153  if (j<=pVariables)
3154  {
3155    result=ivSolveIntMat(imat);
3156  }
3157  //else
3158  //{
3159  //  WerrorS("not homogeneous");
3160  //}
3161  delete imat;
3162  return result;
3163}
3164
3165/*3
3166* searches for units in the components of the module arg and
3167* returns the first one
3168*/
3169static int idReadOutUnits(ideal arg,int* comp)
3170{
3171  if (idIs0(arg)) return -1;
3172  int i=0,j,rk_arg=idRankFreeModule(arg),generator=-1;
3173  intvec * componentIsUsed =new intvec(rk_arg+1);
3174  poly p,q;
3175
3176  while ((i<IDELEMS(arg)) && (generator<0))
3177  {
3178    for (j=rk_arg;j>=0;j--) 
3179      (*componentIsUsed)[j]=0;
3180    p = arg->m[i];
3181    while (p!=NULL)
3182    { 
3183      j = pGetComp(p);
3184      if ((*componentIsUsed)[j]==0)
3185      {
3186        if (pIsConstantComp(p))
3187        {
3188          generator = i;
3189          (*componentIsUsed)[j] = 1;
3190        }
3191        else
3192        {
3193          (*componentIsUsed)[j] = -1;
3194        }
3195      }
3196      else if ((*componentIsUsed)[j]>0)
3197      {
3198        ((*componentIsUsed)[j])++;
3199      }
3200      pIter(p);
3201    }
3202    i++;
3203  }
3204  i = 0;
3205  *comp = -1;
3206  for (j=0;j<=rk_arg;j++)
3207  {
3208    if ((*componentIsUsed)[j]>0)
3209    {
3210      if ((*comp==-1) || ((*componentIsUsed)[j]<i))
3211      {
3212        *comp = j;
3213        i= (*componentIsUsed)[j];
3214      }
3215    }
3216  }
3217  return generator;
3218}
3219
3220static void idDeleteComp(ideal arg,int red_comp)
3221{
3222  int i,j;
3223  poly p;
3224
3225  for (i=IDELEMS(arg)-1;i>=0;i--)
3226  {
3227    p = arg->m[i];
3228    while (p!=NULL)
3229    {
3230      j = pGetComp(p);
3231      if (j>red_comp)
3232      {
3233        pSetComp(p,j-1);
3234        pSetm(p);
3235      }
3236      pIter(p);
3237    }
3238  }
3239  (arg->rank)--;
3240}
3241
3242/*2
3243* returns the presentation of an isomorphic, minimally
3244* embedded  module
3245*/
3246ideal idMinEmbedding(ideal arg,BOOLEAN inPlace)
3247{
3248  if (idIs0(arg)) return idInit(1,arg->rank);
3249  int next_gen,next_comp;
3250  ideal res=arg;
3251
3252  if (!inPlace) res = idCopy(arg);
3253  loop
3254  {
3255    next_gen = idReadOutUnits(res,&next_comp);
3256    if (next_gen<0) break;
3257    syGaussForOne(res,next_gen,next_comp,0,IDELEMS(res));
3258    idDeleteComp(res,next_comp);
3259  }
3260  idSkipZeroes(res);
3261  return res;
3262}
3263
3264/*2
3265* transpose a module
3266*/
3267ideal idTransp(ideal a)
3268{
3269  int r = a->rank, c = IDELEMS(a);
3270  ideal b =  idInit(r,c);
3271
3272  for (int i=c; i>0; i--)
3273  {
3274    poly p=a->m[i-1];
3275    while(p!=NULL)
3276    {
3277      poly h=pHead(p);
3278      int co=pGetComp(h)-1;
3279      pSetComp(h,i);
3280      pSetmComp(h);
3281      b->m[co]=pAdd(b->m[co],h);
3282      pIter(p);
3283    }
3284  }
3285  return b;
3286}
3287
Note: See TracBrowser for help on using the repository browser.