source: git/kernel/ideals.cc @ 4d0cbc7

spielwiese
Last change on this file since 4d0cbc7 was a5d181c, checked in by Hans Schoenemann <hannes@…>, 13 years ago
fix to tr. 379 (weight.cc::iv2array) fix to tr. 380 (ideals.cc::idLift)
  • Property mode set to 100644
File size: 59.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ideals.cc 14320 2011-07-04 14:48:27Z hannes $ */
5/*
6* ABSTRACT - all basic methods to manipulate ideals
7*/
8
9/* includes */
10#include "mod2.h"
11
12#include <omalloc/omalloc.h>
13#include <misc/auxiliary.h>
14
15
16#ifndef NDEBUG
17# define MYTEST 0
18#else /* ifndef NDEBUG */
19# define MYTEST 0
20#endif /* ifndef NDEBUG */
21
22#include <omalloc/omalloc.h>
23
24#include <misc/options.h>
25#include <misc/intvec.h>
26
27#include <coeffs/coeffs.h>
28#include <coeffs/numbers.h>
29
30#include <kernel/polys.h>
31#include <polys/monomials/ring.h>
32#include <polys/matpol.h>
33#include <polys/weight.h>
34#include <polys/sparsmat.h>
35#include <polys/prCopy.h>
36#include <polys/nc/nc.h>
37
38
39#include <kernel/ideals.h>
40
41#include <kernel/febase.h>
42#include <kernel/kstd1.h>
43#include <kernel/syz.h>
44
45#include <kernel/longrat.h>
46
47
48/* #define WITH_OLD_MINOR */
49#define pCopy_noCheck(p) pCopy(p)
50
51static poly * idpower;
52/*collects the monomials in makemonoms, must be allocated befor*/
53static int idpowerpoint;
54/*index of the actual monomial in idpower*/
55static poly * givenideal;
56/*the ideal from which a power is computed*/
57
58/*0 implementation*/
59
60/*2
61*returns a minimized set of generators of h1
62*/
63ideal idMinBase (ideal h1)
64{
65  ideal h2, h3,h4,e;
66  int j,k;
67  int i,l,ll;
68  intvec * wth;
69  BOOLEAN homog;
70
71  homog = idHomModule(h1,currQuotient,&wth);
72  if (rHasGlobalOrdering(currRing))
73  {
74    if(!homog)
75    {
76      WarnS("minbase applies only to the local or homogeneous case");
77      e=idCopy(h1);
78      return e;
79    }
80    else
81    {
82      ideal re=kMin_std(h1,currQuotient,(tHomog)homog,&wth,h2,NULL,0,3);
83      idDelete(&re);
84      return h2;
85    }
86  }
87  e=idInit(1,h1->rank);
88  if (idIs0(h1))
89  {
90    return e;
91  }
92  pEnlargeSet(&(e->m),IDELEMS(e),15);
93  IDELEMS(e) = 16;
94  h2 = kStd(h1,currQuotient,isNotHomog,NULL);
95  h3 = idMaxIdeal(1);
96  h4=idMult(h2,h3);
97  idDelete(&h3);
98  h3=kStd(h4,currQuotient,isNotHomog,NULL);
99  k = IDELEMS(h3);
100  while ((k > 0) && (h3->m[k-1] == NULL)) k--;
101  j = -1;
102  l = IDELEMS(h2);
103  while ((l > 0) && (h2->m[l-1] == NULL)) l--;
104  for (i=l-1; i>=0; i--)
105  {
106    if (h2->m[i] != NULL)
107    {
108      ll = 0;
109      while ((ll < k) && ((h3->m[ll] == NULL)
110      || !pDivisibleBy(h3->m[ll],h2->m[i])))
111        ll++;
112      if (ll >= k)
113      {
114        j++;
115        if (j > IDELEMS(e)-1)
116        {
117          pEnlargeSet(&(e->m),IDELEMS(e),16);
118          IDELEMS(e) += 16;
119        }
120        e->m[j] = pCopy(h2->m[i]);
121      }
122    }
123  }
124  idDelete(&h2);
125  idDelete(&h3);
126  idDelete(&h4);
127  if (currQuotient!=NULL)
128  {
129    h3=idInit(1,e->rank);
130    h2=kNF(h3,currQuotient,e);
131    idDelete(&h3);
132    idDelete(&e);
133    e=h2;
134  }
135  idSkipZeroes(e);
136  return e;
137}
138
139
140/*3
141*multiplies p with t (!cas) or  (t-1)
142*the index of t is:1, so we have to shift all variables
143*p is NOT in the actual ring, it has no t
144*/
145static poly pMultWithT (poly p,BOOLEAN cas)
146{
147  /*qp is the working pointer in p*/
148  /*result is the result, qresult is the working pointer*/
149  /*pp is p in the actual ring(shifted), qpp the working pointer*/
150  poly result,qp,pp;
151  poly qresult=NULL;
152  poly qpp=NULL;
153  int  i,j,lex;
154  number n;
155
156  pp = NULL;
157  result = NULL;
158  qp = p;
159  while (qp != NULL)
160  {
161    i = 0;
162    if (result == NULL)
163    {/*first monomial*/
164      result = pInit();
165      qresult = result;
166    }
167    else
168    {
169      qresult->next = pInit();
170      pIter(qresult);
171    }
172    for (j=(currRing->N)-1; j>0; j--)
173    {
174      lex = pGetExp(qp,j);
175      pSetExp(qresult,j+1,lex);/*copy all variables*/
176    }
177    lex = pGetComp(qp);
178    pSetComp(qresult,lex);
179    n=nCopy(pGetCoeff(qp));
180    pSetCoeff0(qresult,n);
181    qresult->next = NULL;
182    pSetm(qresult);
183    /*qresult is now qp brought into the actual ring*/
184    if (cas)
185    { /*case: mult with t-1*/
186      pSetExp(qresult,1,0);
187      pSetm(qresult);
188      if (pp == NULL)
189      { /*first monomial*/
190        pp = pCopy(qresult);
191        qpp = pp;
192      }
193      else
194      {
195        qpp->next = pCopy(qresult);
196        pIter(qpp);
197      }
198      pGetCoeff(qpp)=nNeg(pGetCoeff(qpp));
199      /*now qpp contains -1*qp*/
200    }
201    pSetExp(qresult,1,1);/*this is mult. by t*/
202    pSetm(qresult);
203    pIter(qp);
204  }
205  /*
206  *now p is processed:
207  *result contains t*p
208  * if cas: pp contains -1*p (in the new ring)
209  */
210  if (cas)  qresult->next = pp;
211  /*  else      qresult->next = NULL;*/
212  return result;
213}
214
215/*2
216*initialized a field with r numbers between beg and end for the
217*procedure idNextChoise
218*/
219ideal idSectWithElim (ideal h1,ideal h2)
220// does not destroy h1,h2
221{
222  if (TEST_OPT_PROT) PrintS("intersect by elimination method\n");
223  assume(!idIs0(h1));
224  assume(!idIs0(h2));
225  assume(IDELEMS(h1)<=IDELEMS(h2));
226  assume(id_RankFreeModule(h1,currRing)==0);
227  assume(id_RankFreeModule(h2,currRing)==0);
228  // add a new variable:
229  int j;
230  ring origRing=currRing;
231  ring r=rCopy0(origRing);
232  r->N++;
233  r->block0[0]=1;
234  r->block1[0]= r->N;
235  omFree(r->order);
236  r->order=(int*)omAlloc0(3*sizeof(int*));
237  r->order[0]=ringorder_dp;
238  r->order[1]=ringorder_C;
239  char **names=(char**)omAlloc0(rVar(r) * sizeof(char_ptr));
240  for (j=0;j<r->N-1;j++) names[j]=r->names[j];
241  names[r->N-1]=omStrDup("@");
242  omFree(r->names);
243  r->names=names;
244  rComplete(r,TRUE);
245  // fetch h1, h2
246  ideal h;
247  h1=idrCopyR(h1,origRing,r);
248  h2=idrCopyR(h2,origRing,r);
249  // switch to temp. ring r
250  rChangeCurrRing(r);
251  // create 1-t, t
252  poly omt=p_One(currRing);
253  p_SetExp(omt,r->N,1,currRing);
254  poly t=p_Copy(omt,currRing);
255  p_Setm(omt,currRing);
256  omt=p_Neg(omt,currRing);
257  omt=p_Add_q(omt,pOne(),currRing);
258  // compute (1-t)*h1
259  h1=(ideal)mp_MultP((matrix)h1,omt,currRing);
260  // compute t*h2
261  h2=(ideal)mp_MultP((matrix)h2,pCopy(t),currRing);
262  // (1-t)h1 + t*h2
263  h=idInit(IDELEMS(h1)+IDELEMS(h2),1);
264  int l;
265  for (l=IDELEMS(h1)-1; l>=0; l--)
266  {
267    h->m[l] = h1->m[l];  h1->m[l]=NULL;
268  }
269  j=IDELEMS(h1);
270  for (l=IDELEMS(h2)-1; l>=0; l--)
271  {
272    h->m[l+j] = h2->m[l];  h2->m[l]=NULL;
273  }
274  idDelete(&h1);
275  idDelete(&h2);
276  // eliminate t:
277
278  ideal res=idElimination(h,t);
279  // cleanup
280  idDelete(&h);
281  if (res!=NULL) res=idrMoveR(res,r,origRing);
282  rChangeCurrRing(origRing);
283  rDelete(r);
284  return res;
285}
286/*2
287* h3 := h1 intersect h2
288*/
289ideal idSect (ideal h1,ideal h2)
290{
291  int i,j,k,length;
292  int flength = id_RankFreeModule(h1,currRing);
293  int slength = id_RankFreeModule(h2,currRing);
294  int rank=si_min(flength,slength);
295  if ((idIs0(h1)) || (idIs0(h2)))  return idInit(1,rank);
296
297  ideal first,second,temp,temp1,result;
298  poly p,q;
299
300  if (IDELEMS(h1)<IDELEMS(h2))
301  {
302    first = h1;
303    second = h2;
304  }
305  else
306  {
307    first = h2;
308    second = h1;
309    int t=flength; flength=slength; slength=t;
310  }
311  length  = si_max(flength,slength);
312  if (length==0)
313  {
314    if ((currQuotient==NULL)
315    && (currRing->OrdSgn==1)
316    && (!rIsPluralRing(currRing))
317    && ((TEST_V_INTERSECT_ELIM) || (!TEST_V_INTERSECT_SYZ)))
318      return idSectWithElim(first,second);
319    else length = 1;
320  }
321  if (TEST_OPT_PROT) PrintS("intersect by syzygy methods\n");
322  j = IDELEMS(first);
323
324  ring orig_ring=currRing;
325  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE); rChangeCurrRing(syz_ring);
326  rSetSyzComp(length, syz_ring);
327
328  while ((j>0) && (first->m[j-1]==NULL)) j--;
329  temp = idInit(j /*IDELEMS(first)*/+IDELEMS(second),length+j);
330  k = 0;
331  for (i=0;i<j;i++)
332  {
333    if (first->m[i]!=NULL)
334    {
335      if (syz_ring==orig_ring)
336        temp->m[k] = pCopy(first->m[i]);
337      else
338        temp->m[k] = prCopyR(first->m[i], orig_ring, syz_ring);
339      q = pOne();
340      pSetComp(q,i+1+length);
341      pSetmComp(q);
342      if (flength==0) p_Shift(&(temp->m[k]),1,currRing);
343      p = temp->m[k];
344      while (pNext(p)!=NULL) pIter(p);
345      pNext(p) = q;
346      k++;
347    }
348  }
349  for (i=0;i<IDELEMS(second);i++)
350  {
351    if (second->m[i]!=NULL)
352    {
353      if (syz_ring==orig_ring)
354        temp->m[k] = pCopy(second->m[i]);
355      else
356        temp->m[k] = prCopyR(second->m[i], orig_ring,currRing);
357      if (slength==0) p_Shift(&(temp->m[k]),1,currRing);
358      k++;
359    }
360  }
361  intvec *w=NULL;
362  temp1 = kStd(temp,currQuotient,testHomog,&w,NULL,length);
363  if (w!=NULL) delete w;
364  idDelete(&temp);
365  if(syz_ring!=orig_ring)
366    rChangeCurrRing(orig_ring);
367
368  result = idInit(IDELEMS(temp1),rank);
369  j = 0;
370  for (i=0;i<IDELEMS(temp1);i++)
371  {
372    if ((temp1->m[i]!=NULL)
373    && (p_GetComp(temp1->m[i],syz_ring)>length))
374    {
375      if(syz_ring==orig_ring)
376      {
377        p = temp1->m[i];
378      }
379      else
380      {
381        p = prMoveR(temp1->m[i], syz_ring,orig_ring);
382      }
383      temp1->m[i]=NULL;
384      while (p!=NULL)
385      {
386        q = pNext(p);
387        pNext(p) = NULL;
388        k = pGetComp(p)-1-length;
389        pSetComp(p,0);
390        pSetmComp(p);
391        /* Warning! multiply only from the left! it's very important for Plural */
392        result->m[j] = pAdd(result->m[j],pMult(p,pCopy(first->m[k])));
393        p = q;
394      }
395      j++;
396    }
397  }
398  if(syz_ring!=orig_ring)
399  {
400    rChangeCurrRing(syz_ring);
401    idDelete(&temp1);
402    rChangeCurrRing(orig_ring);
403    rDelete(syz_ring);
404  }
405  else
406  {
407    idDelete(&temp1);
408  }
409
410  idSkipZeroes(result);
411  if (TEST_OPT_RETURN_SB)
412  {
413     w=NULL;
414     temp1=kStd(result,currQuotient,testHomog,&w);
415     if (w!=NULL) delete w;
416     idDelete(&result);
417     idSkipZeroes(temp1);
418     return temp1;
419  }
420  else //temp1=kInterRed(result,currQuotient);
421    return result;
422}
423
424/*2
425* ideal/module intersection for a list of objects
426* given as 'resolvente'
427*/
428ideal idMultSect(resolvente arg, int length)
429{
430  int i,j=0,k=0,syzComp,l,maxrk=-1,realrki;
431  ideal bigmat,tempstd,result;
432  poly p;
433  int isIdeal=0;
434  intvec * w=NULL;
435
436  /* find 0-ideals and max rank -----------------------------------*/
437  for (i=0;i<length;i++)
438  {
439    if (!idIs0(arg[i]))
440    {
441      realrki=id_RankFreeModule(arg[i],currRing);
442      k++;
443      j += IDELEMS(arg[i]);
444      if (realrki>maxrk) maxrk = realrki;
445    }
446    else
447    {
448      if (arg[i]!=NULL)
449      {
450        return idInit(1,arg[i]->rank);
451      }
452    }
453  }
454  if (maxrk == 0)
455  {
456    isIdeal = 1;
457    maxrk = 1;
458  }
459  /* init -----------------------------------------------------------*/
460  j += maxrk;
461  syzComp = k*maxrk;
462
463  ring orig_ring=currRing;
464  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE); rChangeCurrRing(syz_ring);
465  rSetSyzComp(syzComp, syz_ring);
466
467  bigmat = idInit(j,(k+1)*maxrk);
468  /* create unit matrices ------------------------------------------*/
469  for (i=0;i<maxrk;i++)
470  {
471    for (j=0;j<=k;j++)
472    {
473      p = pOne();
474      pSetComp(p,i+1+j*maxrk);
475      pSetmComp(p);
476      bigmat->m[i] = pAdd(bigmat->m[i],p);
477    }
478  }
479  /* enter given ideals ------------------------------------------*/
480  i = maxrk;
481  k = 0;
482  for (j=0;j<length;j++)
483  {
484    if (arg[j]!=NULL)
485    {
486      for (l=0;l<IDELEMS(arg[j]);l++)
487      {
488        if (arg[j]->m[l]!=NULL)
489        {
490          if (syz_ring==orig_ring)
491            bigmat->m[i] = pCopy(arg[j]->m[l]);
492          else
493            bigmat->m[i] = prCopyR(arg[j]->m[l], orig_ring,currRing);
494          p_Shift(&(bigmat->m[i]),k*maxrk+isIdeal,currRing);
495          i++;
496        }
497      }
498      k++;
499    }
500  }
501  /* std computation --------------------------------------------*/
502  tempstd = kStd(bigmat,currQuotient,testHomog,&w,NULL,syzComp);
503  if (w!=NULL) delete w;
504  idDelete(&bigmat);
505
506  if(syz_ring!=orig_ring)
507    rChangeCurrRing(orig_ring);
508
509  /* interprete result ----------------------------------------*/
510  result = idInit(IDELEMS(tempstd),maxrk);
511  k = 0;
512  for (j=0;j<IDELEMS(tempstd);j++)
513  {
514    if ((tempstd->m[j]!=NULL) && (p_GetComp(tempstd->m[j],syz_ring)>syzComp))
515    {
516      if (syz_ring==orig_ring)
517        p = pCopy(tempstd->m[j]);
518      else
519        p = prCopyR(tempstd->m[j], syz_ring,currRing);
520      p_Shift(&p,-syzComp-isIdeal,currRing);
521      result->m[k] = p;
522      k++;
523    }
524  }
525  /* clean up ----------------------------------------------------*/
526  if(syz_ring!=orig_ring)
527    rChangeCurrRing(syz_ring);
528  idDelete(&tempstd);
529  if(syz_ring!=orig_ring)
530  {
531    rChangeCurrRing(orig_ring);
532    rDelete(syz_ring);
533  }
534  idSkipZeroes(result);
535  return result;
536}
537
538/*2
539*computes syzygies of h1,
540*if quot != NULL it computes in the quotient ring modulo "quot"
541*works always in a ring with ringorder_s
542*/
543static ideal idPrepare (ideal  h1, tHomog hom, int syzcomp, intvec **w)
544{
545  ideal   h2, h3;
546  int     i;
547  int     j,jj=0,k;
548  poly    p,q;
549
550  if (idIs0(h1)) return NULL;
551  k = id_RankFreeModule(h1,currRing);
552  h2=idCopy(h1);
553  i = IDELEMS(h2)-1;
554  if (k == 0)
555  {
556    for (j=0; j<=i; j++) p_Shift(&(h2->m[j]),1,currRing);
557    k = 1;
558  }
559  if (syzcomp<k)
560  {
561    Warn("syzcomp too low, should be %d instead of %d",k,syzcomp);
562    syzcomp = k;
563    rSetSyzComp(k,currRing);
564  }
565  h2->rank = syzcomp+i+1;
566
567  //if (hom==testHomog)
568  //{
569  //  if(idHomIdeal(h1,currQuotient))
570  //  {
571  //    hom=TRUE;
572  //  }
573  //}
574
575#if MYTEST
576#ifdef RDEBUG
577  Print("Prepare::h2: ");
578  idPrint(h2);
579
580  for(j=0;j<IDELEMS(h2);j++) pTest(h2->m[j]);
581
582#endif
583#endif
584
585  for (j=0; j<=i; j++)
586  {
587    p = h2->m[j];
588    q = pOne();
589    pSetComp(q,syzcomp+1+j);
590    pSetmComp(q);
591    if (p!=NULL)
592    {
593      while (pNext(p)) pIter(p);
594      p->next = q;
595    }
596    else
597      h2->m[j]=q;
598  }
599
600#ifdef PDEBUG
601  for(j=0;j<IDELEMS(h2);j++) pTest(h2->m[j]);
602
603#if MYTEST
604#ifdef RDEBUG
605  Print("Prepare::Input: ");
606  idPrint(h2);
607
608  Print("Prepare::currQuotient: ");
609  idPrint(currQuotient);
610#endif
611#endif
612
613#endif
614
615  idTest(h2);
616
617  h3 = kStd(h2,currQuotient,hom,w,NULL,syzcomp);
618
619#if MYTEST
620#ifdef RDEBUG
621  Print("Prepare::Output: ");
622  idPrint(h3);
623  for(j=0;j<IDELEMS(h2);j++) pTest(h3->m[j]);
624#endif
625#endif
626
627
628  idDelete(&h2);
629  return h3;
630}
631
632/*2
633* compute the syzygies of h1 in R/quot,
634* weights of components are in w
635* if setRegularity, return the regularity in deg
636* do not change h1,  w
637*/
638ideal idSyzygies (ideal  h1, tHomog h,intvec **w, BOOLEAN setSyzComp,
639                  BOOLEAN setRegularity, int *deg)
640{
641  ideal s_h1;
642  poly  p;
643  int   j, k, length=0,reg;
644  BOOLEAN isMonomial=TRUE;
645  int ii, idElemens_h1;
646
647  assume(h1 != NULL);
648
649  idElemens_h1=IDELEMS(h1);
650#ifdef PDEBUG
651  for(ii=0;ii<idElemens_h1 /*IDELEMS(h1)*/;ii++) pTest(h1->m[ii]);
652#endif
653  if (idIs0(h1))
654  {
655    ideal result=idFreeModule(idElemens_h1/*IDELEMS(h1)*/);
656    int curr_syz_limit=rGetCurrSyzLimit(currRing);
657    if (curr_syz_limit>0)
658    for (ii=0;ii<idElemens_h1/*IDELEMS(h1)*/;ii++)
659    {
660      if (h1->m[ii]!=NULL)
661        p_Shift(&h1->m[ii],curr_syz_limit,currRing);
662    }
663    return result;
664  }
665  int slength=(int)id_RankFreeModule(h1,currRing);
666  k=si_max(1,slength /*id_RankFreeModule(h1)*/);
667
668  assume(currRing != NULL);
669  ring orig_ring=currRing;
670  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE); rChangeCurrRing(syz_ring);
671
672  if (setSyzComp)
673    rSetSyzComp(k,syz_ring);
674
675  if (orig_ring != syz_ring)
676  {
677    s_h1=idrCopyR_NoSort(h1,orig_ring,syz_ring);
678  }
679  else
680  {
681    s_h1 = h1;
682  }
683
684  idTest(s_h1);
685
686  ideal s_h3=idPrepare(s_h1,h,k,w); // main (syz) GB computation
687
688  if (s_h3==NULL)
689  {
690    return idFreeModule( idElemens_h1 /*IDELEMS(h1)*/);
691  }
692
693  if (orig_ring != syz_ring)
694  {
695    idDelete(&s_h1);
696    for (j=0; j<IDELEMS(s_h3); j++)
697    {
698      if (s_h3->m[j] != NULL)
699      {
700        if (p_MinComp(s_h3->m[j],syz_ring) > k)
701          p_Shift(&s_h3->m[j], -k,syz_ring);
702        else
703          p_Delete(&s_h3->m[j],syz_ring);
704      }
705    }
706    idSkipZeroes(s_h3);
707    s_h3->rank -= k;
708    rChangeCurrRing(orig_ring);
709    s_h3 = idrMoveR_NoSort(s_h3, syz_ring, orig_ring);
710    rDelete(syz_ring);
711    #ifdef HAVE_PLURAL
712    if (rIsPluralRing(orig_ring))
713    {
714      id_DelMultiples(s_h3,orig_ring);
715      idSkipZeroes(s_h3);
716    }
717    #endif
718    idTest(s_h3);
719    return s_h3;
720  }
721
722  ideal e = idInit(IDELEMS(s_h3), s_h3->rank);
723
724  for (j=IDELEMS(s_h3)-1; j>=0; j--)
725  {
726    if (s_h3->m[j] != NULL)
727    {
728      if (p_MinComp(s_h3->m[j],syz_ring) <= k)
729      {
730        e->m[j] = s_h3->m[j];
731        isMonomial=isMonomial && (pNext(s_h3->m[j])==NULL);
732        p_Delete(&pNext(s_h3->m[j]),syz_ring);
733        s_h3->m[j] = NULL;
734      }
735    }
736  }
737
738  idSkipZeroes(s_h3);
739  idSkipZeroes(e);
740
741  if ((deg != NULL)
742  && (!isMonomial)
743  && (!TEST_OPT_NOTREGULARITY)
744  && (setRegularity)
745  && (h==isHomog)
746  && (!rIsPluralRing(currRing))
747  )
748  {
749    ring dp_C_ring = rAssure_dp_C(syz_ring); // will do rChangeCurrRing later
750    if (dp_C_ring != syz_ring)
751    {
752      rChangeCurrRing(dp_C_ring);
753      e = idrMoveR_NoSort(e, syz_ring, dp_C_ring);
754    }
755    resolvente res = sySchreyerResolvente(e,-1,&length,TRUE, TRUE);
756    intvec * dummy = syBetti(res,length,&reg, *w);
757    *deg = reg+2;
758    delete dummy;
759    for (j=0;j<length;j++)
760    {
761      if (res[j]!=NULL) idDelete(&(res[j]));
762    }
763    omFreeSize((ADDRESS)res,length*sizeof(ideal));
764    idDelete(&e);
765    if (dp_C_ring != syz_ring)
766    {
767      rChangeCurrRing(syz_ring);
768      rDelete(dp_C_ring);
769    }
770  }
771  else
772  {
773    idDelete(&e);
774  }
775  idTest(s_h3);
776  if (currQuotient != NULL)
777  {
778    ideal ts_h3=kStd(s_h3,currQuotient,h,w);
779    idDelete(&s_h3);
780    s_h3 = ts_h3;
781  }
782  return s_h3;
783}
784
785/*2
786*/
787ideal idXXX (ideal  h1, int k)
788{
789  ideal s_h1;
790  int j;
791  intvec *w=NULL;
792
793  assume(currRing != NULL);
794  ring orig_ring=currRing;
795  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE); rChangeCurrRing(syz_ring);
796
797  rSetSyzComp(k,syz_ring);
798
799  if (orig_ring != syz_ring)
800  {
801    s_h1=idrCopyR_NoSort(h1,orig_ring, syz_ring);
802  }
803  else
804  {
805    s_h1 = h1;
806  }
807
808  ideal s_h3=kStd(s_h1,NULL,testHomog,&w,NULL,k);
809
810  if (s_h3==NULL)
811  {
812    return idFreeModule(IDELEMS(h1));
813  }
814
815  if (orig_ring != syz_ring)
816  {
817    idDelete(&s_h1);
818    idSkipZeroes(s_h3);
819    rChangeCurrRing(orig_ring);
820    s_h3 = idrMoveR_NoSort(s_h3, syz_ring, orig_ring);
821    rDelete(syz_ring);
822    idTest(s_h3);
823    return s_h3;
824  }
825
826  idSkipZeroes(s_h3);
827  idTest(s_h3);
828  return s_h3;
829}
830
831/*
832*computes a standard basis for h1 and stores the transformation matrix
833* in ma
834*/
835ideal idLiftStd (ideal  h1, matrix* ma, tHomog hi, ideal * syz)
836{
837  int   i, j, k, t, inputIsIdeal=id_RankFreeModule(h1,currRing);
838  poly  p=NULL, q, qq;
839  intvec *w=NULL;
840
841  idDelete((ideal*)ma);
842  BOOLEAN lift3=FALSE;
843  if (syz!=NULL) { lift3=TRUE; idDelete(syz); }
844  if (idIs0(h1))
845  {
846    *ma=mpNew(1,0);
847    if (lift3)
848    {
849      *syz=idFreeModule(IDELEMS(h1));
850      int curr_syz_limit=rGetCurrSyzLimit(currRing);
851      if (curr_syz_limit>0)
852      for (int ii=0;ii<IDELEMS(h1);ii++)
853      {
854        if (h1->m[ii]!=NULL)
855          p_Shift(&h1->m[ii],curr_syz_limit,currRing);
856      }
857    }
858    return idInit(1,h1->rank);
859  }
860
861  BITSET save_verbose=verbose;
862
863  k=si_max(1,(int)id_RankFreeModule(h1,currRing));
864
865  if ((k==1) && (!lift3)) verbose |=Sy_bit(V_IDLIFT);
866
867  ring orig_ring = currRing;
868  ring syz_ring = rAssure_SyzComp(orig_ring,TRUE);  rChangeCurrRing(syz_ring);
869  rSetSyzComp(k,syz_ring);
870
871  ideal s_h1=h1;
872
873  if (orig_ring != syz_ring)
874    s_h1 = idrCopyR_NoSort(h1,orig_ring,syz_ring);
875  else
876    s_h1 = h1;
877
878  ideal s_h3=idPrepare(s_h1,hi,k,&w); // main (syz) GB computation
879
880  ideal s_h2 = idInit(IDELEMS(s_h3), s_h3->rank);
881
882  if (lift3) (*syz)=idInit(IDELEMS(s_h3),IDELEMS(h1));
883
884  if (w!=NULL) delete w;
885  i = 0;
886
887  // now sort the result, SB : leave in s_h3
888  //                      T:  put in s_h2
889  //                      syz: put in *syz
890  for (j=0; j<IDELEMS(s_h3); j++)
891  {
892    if (s_h3->m[j] != NULL)
893    {
894      //if (p_MinComp(s_h3->m[j],syz_ring) <= k)
895      if (pGetComp(s_h3->m[j]) <= k) // syz_ring == currRing
896      {
897        i++;
898        q = s_h3->m[j];
899        while (pNext(q) != NULL)
900        {
901          if (pGetComp(pNext(q)) > k)
902          {
903            s_h2->m[j] = pNext(q);
904            pNext(q) = NULL;
905          }
906          else
907          {
908            pIter(q);
909          }
910        }
911        if (!inputIsIdeal) p_Shift(&(s_h3->m[j]), -1,currRing);
912      }
913      else
914      {
915        // we a syzygy here:
916        if (lift3)
917        {
918          p_Shift(&s_h3->m[j], -k,currRing);
919          (*syz)->m[j]=s_h3->m[j];
920          s_h3->m[j]=NULL;
921        }
922        else
923          p_Delete(&(s_h3->m[j]),currRing);
924      }
925    }
926  }
927  idSkipZeroes(s_h3);
928  //extern char * iiStringMatrix(matrix im, int dim,char ch);
929  //PrintS("SB: ----------------------------------------\n");
930  //PrintS(iiStringMatrix((matrix)s_h3,k,'\n'));
931  //PrintLn();
932  //PrintS("T: ----------------------------------------\n");
933  //PrintS(iiStringMatrix((matrix)s_h2,h1->rank,'\n'));
934  //PrintLn();
935
936  if (lift3) idSkipZeroes(*syz);
937
938  j = IDELEMS(s_h1);
939
940
941  if (syz_ring!=orig_ring)
942  {
943    idDelete(&s_h1);
944    rChangeCurrRing(orig_ring);
945  }
946
947  *ma = mpNew(j,i);
948
949  i = 1;
950  for (j=0; j<IDELEMS(s_h2); j++)
951  {
952    if (s_h2->m[j] != NULL)
953    {
954      q = prMoveR( s_h2->m[j], syz_ring,orig_ring);
955      s_h2->m[j] = NULL;
956
957      while (q != NULL)
958      {
959        p = q;
960        pIter(q);
961        pNext(p) = NULL;
962        t=pGetComp(p);
963        pSetComp(p,0);
964        pSetmComp(p);
965        MATELEM(*ma,t-k,i) = pAdd(MATELEM(*ma,t-k,i),p);
966      }
967      i++;
968    }
969  }
970  idDelete(&s_h2);
971
972  for (i=0; i<IDELEMS(s_h3); i++)
973  {
974    s_h3->m[i] = prMoveR_NoSort(s_h3->m[i], syz_ring,orig_ring);
975  }
976  if (lift3)
977  {
978    for (i=0; i<IDELEMS(*syz); i++)
979    {
980      (*syz)->m[i] = prMoveR_NoSort((*syz)->m[i], syz_ring,orig_ring);
981    }
982  }
983
984  if (syz_ring!=orig_ring) rDelete(syz_ring);
985  verbose = save_verbose;
986  return s_h3;
987}
988
989static void idPrepareStd(ideal s_temp, int k)
990{
991  int j,rk=id_RankFreeModule(s_temp,currRing);
992  poly p,q;
993
994  if (rk == 0)
995  {
996    for (j=0; j<IDELEMS(s_temp); j++)
997    {
998      if (s_temp->m[j]!=NULL) pSetCompP(s_temp->m[j],1);
999    }
1000    k = si_max(k,1);
1001  }
1002  for (j=0; j<IDELEMS(s_temp); j++)
1003  {
1004    if (s_temp->m[j]!=NULL)
1005    {
1006      p = s_temp->m[j];
1007      q = pOne();
1008      //pGetCoeff(q)=nNeg(pGetCoeff(q));   //set q to -1
1009      pSetComp(q,k+1+j);
1010      pSetmComp(q);
1011      while (pNext(p)) pIter(p);
1012      pNext(p) = q;
1013    }
1014  }
1015}
1016
1017/*2
1018*computes a representation of the generators of submod with respect to those
1019* of mod
1020*/
1021
1022ideal idLift(ideal mod, ideal submod,ideal *rest, BOOLEAN goodShape,
1023             BOOLEAN isSB, BOOLEAN divide, matrix *unit)
1024{
1025  int lsmod =id_RankFreeModule(submod,currRing), i, j, k;
1026  int comps_to_add=0;
1027  poly p;
1028
1029  if (idIs0(submod))
1030  {
1031    if (unit!=NULL)
1032    {
1033      *unit=mpNew(1,1);
1034      MATELEM(*unit,1,1)=pOne();
1035    }
1036    if (rest!=NULL)
1037    {
1038      *rest=idInit(1,mod->rank);
1039    }
1040    return idInit(1,mod->rank);
1041  }
1042  if (idIs0(mod)) /* and not idIs0(submod) */
1043  {
1044    WerrorS("2nd module does not lie in the first");
1045    return NULL;
1046  }
1047  if (unit!=NULL)
1048  {
1049    comps_to_add = IDELEMS(submod);
1050    while ((comps_to_add>0) && (submod->m[comps_to_add-1]==NULL))
1051      comps_to_add--;
1052  }
1053  k=si_max(id_RankFreeModule(mod,currRing),id_RankFreeModule(submod,currRing));
1054  if  ((k!=0) && (lsmod==0)) lsmod=1;
1055  k=si_max(k,(int)mod->rank);
1056  if (k<submod->rank) { WarnS("rk(submod) > rk(mod) ?");k=submod->rank; }
1057
1058  ring orig_ring=currRing;
1059  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE);  rChangeCurrRing(syz_ring);
1060  rSetSyzComp(k,syz_ring);
1061
1062  ideal s_mod, s_temp;
1063  if (orig_ring != syz_ring)
1064  {
1065    s_mod = idrCopyR_NoSort(mod,orig_ring,syz_ring);
1066    s_temp = idrCopyR_NoSort(submod,orig_ring,syz_ring);
1067  }
1068  else
1069  {
1070    s_mod = mod;
1071    s_temp = idCopy(submod);
1072  }
1073  ideal s_h3;
1074  if (isSB)
1075  {
1076    s_h3 = idCopy(s_mod);
1077    idPrepareStd(s_h3, k+comps_to_add);
1078  }
1079  else
1080  {
1081    s_h3 = idPrepare(s_mod,(tHomog)FALSE,k+comps_to_add,NULL);
1082  }
1083  if (!goodShape)
1084  {
1085    for (j=0;j<IDELEMS(s_h3);j++)
1086    {
1087      if ((s_h3->m[j] != NULL) && (pMinComp(s_h3->m[j]) > k))
1088        p_Delete(&(s_h3->m[j]),currRing);
1089    }
1090  }
1091  idSkipZeroes(s_h3);
1092  if (lsmod==0)
1093  {
1094    for (j=IDELEMS(s_temp);j>0;j--)
1095    {
1096      if (s_temp->m[j-1]!=NULL)
1097        p_Shift(&(s_temp->m[j-1]),1,currRing);
1098    }
1099  }
1100  if (unit!=NULL)
1101  {
1102    for(j = 0;j<comps_to_add;j++)
1103    {
1104      p = s_temp->m[j];
1105      if (p!=NULL)
1106      {
1107        while (pNext(p)!=NULL) pIter(p);
1108        pNext(p) = pOne();
1109        pIter(p);
1110        pSetComp(p,1+j+k);
1111        pSetmComp(p);
1112        p = pNeg(p);
1113      }
1114    }
1115  }
1116  ideal s_result = kNF(s_h3,currQuotient,s_temp,k);
1117  s_result->rank = s_h3->rank;
1118  ideal s_rest = idInit(IDELEMS(s_result),k);
1119  idDelete(&s_h3);
1120  idDelete(&s_temp);
1121
1122  for (j=0;j<IDELEMS(s_result);j++)
1123  {
1124    if (s_result->m[j]!=NULL)
1125    {
1126      if (pGetComp(s_result->m[j])<=k)
1127      {
1128        if (!divide)
1129        {
1130          if (isSB)
1131          {
1132            WarnS("first module not a standardbasis\n"
1133              "// ** or second not a proper submodule");
1134          }
1135          else
1136            WerrorS("2nd module does not lie in the first");
1137          idDelete(&s_result);
1138          idDelete(&s_rest);
1139          s_result=idInit(IDELEMS(submod),submod->rank);
1140          break;
1141        }
1142        else
1143        {
1144          p = s_rest->m[j] = s_result->m[j];
1145          while ((pNext(p)!=NULL) && (pGetComp(pNext(p))<=k)) pIter(p);
1146          s_result->m[j] = pNext(p);
1147          pNext(p) = NULL;
1148        }
1149      }
1150      p_Shift(&(s_result->m[j]),-k,currRing);
1151      pNeg(s_result->m[j]);
1152    }
1153  }
1154  if ((lsmod==0) && (!idIs0(s_rest)))
1155  {
1156    for (j=IDELEMS(s_rest);j>0;j--)
1157    {
1158      if (s_rest->m[j-1]!=NULL)
1159      {
1160        p_Shift(&(s_rest->m[j-1]),-1,currRing);
1161        s_rest->m[j-1] = s_rest->m[j-1];
1162      }
1163    }
1164  }
1165  if(syz_ring!=orig_ring)
1166  {
1167    idDelete(&s_mod);
1168    rChangeCurrRing(orig_ring);
1169    s_result = idrMoveR_NoSort(s_result, syz_ring, orig_ring);
1170    s_rest = idrMoveR_NoSort(s_rest, syz_ring, orig_ring);
1171    rDelete(syz_ring);
1172  }
1173  if (rest!=NULL)
1174    *rest = s_rest;
1175  else
1176    idDelete(&s_rest);
1177//idPrint(s_result);
1178  if (unit!=NULL)
1179  {
1180    *unit=mpNew(comps_to_add,comps_to_add);
1181    int i;
1182    for(i=0;i<IDELEMS(s_result);i++)
1183    {
1184      poly p=s_result->m[i];
1185      poly q=NULL;
1186      while(p!=NULL)
1187      {
1188        if(pGetComp(p)<=comps_to_add)
1189        {
1190          pSetComp(p,0);
1191          if (q!=NULL)
1192          {
1193            pNext(q)=pNext(p);
1194          }
1195          else
1196          {
1197            pIter(s_result->m[i]);
1198          }
1199          pNext(p)=NULL;
1200          MATELEM(*unit,i+1,i+1)=pAdd(MATELEM(*unit,i+1,i+1),p);
1201          if(q!=NULL)   p=pNext(q);
1202          else          p=s_result->m[i];
1203        }
1204        else
1205        {
1206          q=p;
1207          pIter(p);
1208        }
1209      }
1210      p_Shift(&s_result->m[i],-comps_to_add,currRing);
1211    }
1212  }
1213  return s_result;
1214}
1215
1216/*2
1217*computes division of P by Q with remainder up to (w-weighted) degree n
1218*P, Q, and w are not changed
1219*/
1220void idLiftW(ideal P,ideal Q,int n,matrix &T, ideal &R,short *w)
1221{
1222  long N=0;
1223  int i;
1224  for(i=IDELEMS(Q)-1;i>=0;i--)
1225    if(w==NULL)
1226      N=si_max(N,pDeg(Q->m[i]));
1227    else
1228      N=si_max(N,pDegW(Q->m[i],w));
1229  N+=n;
1230
1231  T=mpNew(IDELEMS(Q),IDELEMS(P));
1232  R=idInit(IDELEMS(P),P->rank);
1233
1234  for(i=IDELEMS(P)-1;i>=0;i--)
1235  {
1236    poly p;
1237    if(w==NULL)
1238      p=ppJet(P->m[i],N);
1239    else
1240      p=ppJetW(P->m[i],N,w);
1241
1242    int j=IDELEMS(Q)-1;
1243    while(p!=NULL)
1244    {
1245      if(pDivisibleBy(Q->m[j],p))
1246      {
1247        poly p0=p_DivideM(pHead(p),pHead(Q->m[j]),currRing);
1248        if(w==NULL)
1249          p=pJet(pSub(p,ppMult_mm(Q->m[j],p0)),N);
1250        else
1251          p=pJetW(pSub(p,ppMult_mm(Q->m[j],p0)),N,w);
1252        pNormalize(p);
1253        if((w==NULL)&&(pDeg(p0)>n)||(w!=NULL)&&(pDegW(p0,w)>n))
1254          p_Delete(&p0,currRing);
1255        else
1256          MATELEM(T,j+1,i+1)=pAdd(MATELEM(T,j+1,i+1),p0);
1257        j=IDELEMS(Q)-1;
1258      }
1259      else
1260      {
1261        if(j==0)
1262        {
1263          poly p0=p;
1264          pIter(p);
1265          pNext(p0)=NULL;
1266          if(((w==NULL)&&(pDeg(p0)>n))
1267          ||((w!=NULL)&&(pDegW(p0,w)>n)))
1268            p_Delete(&p0,currRing);
1269          else
1270            R->m[i]=pAdd(R->m[i],p0);
1271          j=IDELEMS(Q)-1;
1272        }
1273        else
1274          j--;
1275      }
1276    }
1277  }
1278}
1279
1280/*2
1281*computes the quotient of h1,h2 : internal routine for idQuot
1282*BEWARE: the returned ideals may contain incorrectly ordered polys !
1283*
1284*/
1285static ideal idInitializeQuot (ideal  h1, ideal h2, BOOLEAN h1IsStb,
1286                               BOOLEAN *addOnlyOne, int *kkmax)
1287{
1288  ideal temph1;
1289  poly     p,q = NULL;
1290  int i,l,ll,k,kkk,kmax;
1291  int j = 0;
1292  int k1 = id_RankFreeModule(h1,currRing);
1293  int k2 = id_RankFreeModule(h2,currRing);
1294  tHomog   hom=isNotHomog;
1295
1296  k=si_max(k1,k2);
1297  if (k==0)
1298    k = 1;
1299  if ((k2==0) && (k>1)) *addOnlyOne = FALSE;
1300
1301  intvec * weights;
1302  hom = (tHomog)idHomModule(h1,currQuotient,&weights);
1303  if (/**addOnlyOne &&*/ (!h1IsStb))
1304    temph1 = kStd(h1,currQuotient,hom,&weights,NULL);
1305  else
1306    temph1 = idCopy(h1);
1307  if (weights!=NULL) delete weights;
1308  idTest(temph1);
1309/*--- making a single vector from h2 ---------------------*/
1310  for (i=0; i<IDELEMS(h2); i++)
1311  {
1312    if (h2->m[i] != NULL)
1313    {
1314      p = pCopy(h2->m[i]);
1315      if (k2 == 0)
1316        p_Shift(&p,j*k+1,currRing);
1317      else
1318        p_Shift(&p,j*k,currRing);
1319      q = pAdd(q,p);
1320      j++;
1321    }
1322  }
1323  *kkmax = kmax = j*k+1;
1324/*--- adding a monomial for the result (syzygy) ----------*/
1325  p = q;
1326  while (pNext(p)!=NULL) pIter(p);
1327  pNext(p) = pOne();
1328  pIter(p);
1329  pSetComp(p,kmax);
1330  pSetmComp(p);
1331/*--- constructing the big matrix ------------------------*/
1332  ideal h4 = idInit(16,kmax+k-1);
1333  h4->m[0] = q;
1334  if (k2 == 0)
1335  {
1336    if (k > IDELEMS(h4))
1337    {
1338      pEnlargeSet(&(h4->m),IDELEMS(h4),k-IDELEMS(h4));
1339      IDELEMS(h4) = k;
1340    }
1341    for (i=1; i<k; i++)
1342    {
1343      if (h4->m[i-1]!=NULL)
1344      {
1345        p = pCopy_noCheck(h4->m[i-1]);
1346        p_Shift(&p,1,currRing);
1347        h4->m[i] = p;
1348      }
1349    }
1350  }
1351  idSkipZeroes(h4);
1352  kkk = IDELEMS(h4);
1353  i = IDELEMS(temph1);
1354  for (l=0; l<i; l++)
1355  {
1356    if(temph1->m[l]!=NULL)
1357    {
1358      for (ll=0; ll<j; ll++)
1359      {
1360        p = pCopy(temph1->m[l]);
1361        if (k1 == 0)
1362          p_Shift(&p,ll*k+1,currRing);
1363        else
1364          p_Shift(&p,ll*k,currRing);
1365        if (kkk >= IDELEMS(h4))
1366        {
1367          pEnlargeSet(&(h4->m),IDELEMS(h4),16);
1368          IDELEMS(h4) += 16;
1369        }
1370        h4->m[kkk] = p;
1371        kkk++;
1372      }
1373    }
1374  }
1375/*--- if h2 goes in as single vector - the h1-part is just SB ---*/
1376  if (*addOnlyOne)
1377  {
1378    idSkipZeroes(h4);
1379    p = h4->m[0];
1380    for (i=0;i<IDELEMS(h4)-1;i++)
1381    {
1382      h4->m[i] = h4->m[i+1];
1383    }
1384    h4->m[IDELEMS(h4)-1] = p;
1385    test |= Sy_bit(OPT_SB_1);
1386  }
1387  idDelete(&temph1);
1388  return h4;
1389}
1390/*2
1391*computes the quotient of h1,h2
1392*/
1393ideal idQuot (ideal  h1, ideal h2, BOOLEAN h1IsStb, BOOLEAN resultIsIdeal)
1394{
1395  // first check for special case h1:(0)
1396  if (idIs0(h2))
1397  {
1398    ideal res;
1399    if (resultIsIdeal)
1400    {
1401      res = idInit(1,1);
1402      res->m[0] = pOne();
1403    }
1404    else
1405      res = idFreeModule(h1->rank);
1406    return res;
1407  }
1408  BITSET old_test=test;
1409  int i,l,ll,k,kkk,kmax;
1410  BOOLEAN  addOnlyOne=TRUE;
1411  tHomog   hom=isNotHomog;
1412  intvec * weights1;
1413
1414  ideal s_h4 = idInitializeQuot (h1,h2,h1IsStb,&addOnlyOne,&kmax);
1415
1416  hom = (tHomog)idHomModule(s_h4,currQuotient,&weights1);
1417
1418  ring orig_ring=currRing;
1419  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE);  rChangeCurrRing(syz_ring);
1420  rSetSyzComp(kmax-1,syz_ring);
1421  if (orig_ring!=syz_ring)
1422  //  s_h4 = idrMoveR_NoSort(s_h4,orig_ring, syz_ring);
1423    s_h4 = idrMoveR(s_h4,orig_ring, syz_ring);
1424  idTest(s_h4);
1425  #if 0
1426  void ipPrint_MA0(matrix m, const char *name);
1427  matrix m=idModule2Matrix(idCopy(s_h4));
1428  PrintS("start:\n");
1429  ipPrint_MA0(m,"Q");
1430  idDelete((ideal *)&m);
1431  PrintS("last elem:");wrp(s_h4->m[IDELEMS(s_h4)-1]);PrintLn();
1432  #endif
1433  ideal s_h3;
1434  if (addOnlyOne)
1435  {
1436    s_h3 = kStd(s_h4,currQuotient,hom,&weights1,NULL,0/*kmax-1*/,IDELEMS(s_h4)-1);
1437  }
1438  else
1439  {
1440    s_h3 = kStd(s_h4,currQuotient,hom,&weights1,NULL,kmax-1);
1441  }
1442  test = old_test;
1443  #if 0
1444  // only together with the above debug stuff
1445  idSkipZeroes(s_h3);
1446  m=idModule2Matrix(idCopy(s_h3));
1447  Print("result, kmax=%d:\n",kmax);
1448  ipPrint_MA0(m,"S");
1449  idDelete((ideal *)&m);
1450  #endif
1451  idTest(s_h3);
1452  if (weights1!=NULL) delete weights1;
1453  idDelete(&s_h4);
1454
1455  for (i=0;i<IDELEMS(s_h3);i++)
1456  {
1457    if ((s_h3->m[i]!=NULL) && (pGetComp(s_h3->m[i])>=kmax))
1458    {
1459      if (resultIsIdeal)
1460        p_Shift(&s_h3->m[i],-kmax,currRing);
1461      else
1462        p_Shift(&s_h3->m[i],-kmax+1,currRing);
1463    }
1464    else
1465      p_Delete(&s_h3->m[i],currRing);
1466  }
1467  if (resultIsIdeal)
1468    s_h3->rank = 1;
1469  else
1470    s_h3->rank = h1->rank;
1471  if(syz_ring!=orig_ring)
1472  {
1473    rChangeCurrRing(orig_ring);
1474    s_h3 = idrMoveR_NoSort(s_h3, syz_ring, orig_ring);
1475    rDelete(syz_ring);
1476  }
1477  idSkipZeroes(s_h3);
1478  idTest(s_h3);
1479  return s_h3;
1480}
1481
1482/*2
1483* eliminate delVar (product of vars) in h1
1484*/
1485ideal idElimination (ideal h1,poly delVar,intvec *hilb)
1486{
1487  int    i,j=0,k,l;
1488  ideal  h,hh, h3;
1489  int    *ord,*block0,*block1;
1490  int    ordersize=2;
1491  int    **wv;
1492  tHomog hom;
1493  intvec * w;
1494  ring tmpR;
1495  ring origR = currRing;
1496
1497  if (delVar==NULL)
1498  {
1499    return idCopy(h1);
1500  }
1501  if ((currQuotient!=NULL) && rIsPluralRing(origR))
1502  {
1503    WerrorS("cannot eliminate in a qring");
1504    return NULL;
1505  }
1506  if (idIs0(h1)) return idInit(1,h1->rank);
1507#ifdef HAVE_PLURAL
1508  if (rIsPluralRing(origR))
1509    /* in the NC case, we have to check the admissibility of */
1510    /* the subalgebra to be intersected with */
1511  {
1512    if ((ncRingType(origR) != nc_skew) && (ncRingType(origR) != nc_exterior)) /* in (quasi)-commutative algebras every subalgebra is admissible */
1513    {
1514      if (nc_CheckSubalgebra(delVar,origR))
1515      {
1516        WerrorS("no elimination is possible: subalgebra is not admissible");
1517        return NULL;
1518      }
1519    }
1520  }
1521#endif
1522  hom=(tHomog)idHomModule(h1,NULL,&w); //sets w to weight vector or NULL
1523  h3=idInit(16,h1->rank);
1524  for (k=0;; k++)
1525  {
1526    if (origR->order[k]!=0) ordersize++;
1527    else break;
1528  }
1529#if 0
1530  if (rIsPluralRing(origR)) // we have too keep the odering: it may be needed
1531                            // for G-algebra
1532  {
1533    for (k=0;k<ordersize-1; k++)
1534    {
1535      block0[k+1] = origR->block0[k];
1536      block1[k+1] = origR->block1[k];
1537      ord[k+1] = origR->order[k];
1538      if (origR->wvhdl[k]!=NULL) wv[k+1] = (int*) omMemDup(origR->wvhdl[k]);
1539    }
1540  }
1541  else
1542  {
1543    block0[1] = 1;
1544    block1[1] = (currRing->N);
1545    if (origR->OrdSgn==1) ord[1] = ringorder_wp;
1546    else                  ord[1] = ringorder_ws;
1547    wv[1]=(int*)omAlloc0((currRing->N)*sizeof(int));
1548    double wNsqr = (double)2.0 / (double)(currRing->N);
1549    wFunctional = wFunctionalBuch;
1550    int  *x= (int * )omAlloc(2 * ((currRing->N) + 1) * sizeof(int));
1551    int sl=IDELEMS(h1) - 1;
1552    wCall(h1->m, sl, x, wNsqr);
1553    for (sl = (currRing->N); sl!=0; sl--)
1554      wv[1][sl-1] = x[sl + (currRing->N) + 1];
1555    omFreeSize((ADDRESS)x, 2 * ((currRing->N) + 1) * sizeof(int));
1556
1557    ord[2]=ringorder_C;
1558    ord[3]=0;
1559  }
1560#else
1561#endif
1562  if ((hom==TRUE) && (origR->OrdSgn==1) && (!rIsPluralRing(origR)))
1563  {
1564    #if 1
1565    // we change to an ordering:
1566    // aa(1,1,1,...,0,0,0),wp(...),C
1567    // this seems to be better than version 2 below,
1568    // according to Tst/../elimiate_[3568].tat (- 17 %)
1569    ord=(int*)omAlloc0(4*sizeof(int));
1570    block0=(int*)omAlloc0(4*sizeof(int));
1571    block1=(int*)omAlloc0(4*sizeof(int));
1572    wv=(int**) omAlloc0(4*sizeof(int**));
1573    block0[0] = block0[1] = 1;
1574    block1[0] = block1[1] = rVar(origR);
1575    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1576    // use this special ordering: like ringorder_a, except that pFDeg, pWeights
1577    // ignore it
1578    ord[0] = ringorder_aa;
1579    for (j=0;j<rVar(origR);j++)
1580      if (pGetExp(delVar,j+1)!=0) wv[0][j]=1;
1581    BOOLEAN wp=FALSE;
1582    for (j=0;j<rVar(origR);j++)
1583      if (pWeight(j+1,origR)!=1) { wp=TRUE;break; }
1584    if (wp)
1585    {
1586      wv[1]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1587      for (j=0;j<rVar(origR);j++)
1588        wv[1][j]=pWeight(j+1,origR);
1589      ord[1] = ringorder_wp;
1590    }
1591    else
1592      ord[1] = ringorder_dp;
1593    #else
1594    // we change to an ordering:
1595    // a(w1,...wn),wp(1,...0.....),C
1596    ord=(int*)omAlloc0(4*sizeof(int));
1597    block0=(int*)omAlloc0(4*sizeof(int));
1598    block1=(int*)omAlloc0(4*sizeof(int));
1599    wv=(int**) omAlloc0(4*sizeof(int**));
1600    block0[0] = block0[1] = 1;
1601    block1[0] = block1[1] = rVar(origR);
1602    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1603    wv[1]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1604    ord[0] = ringorder_a;
1605    for (j=0;j<rVar(origR);j++)
1606      wv[0][j]=pWeight(j+1,origR);
1607    ord[1] = ringorder_wp;
1608    for (j=0;j<rVar(origR);j++)
1609      if (pGetExp(delVar,j+1)!=0) wv[1][j]=1;
1610    #endif
1611    ord[2] = ringorder_C;
1612    ord[3] = 0;
1613  }
1614  else
1615  {
1616    // we change to an ordering:
1617    // aa(....),orig_ordering
1618    ord=(int*)omAlloc0(ordersize*sizeof(int));
1619    block0=(int*)omAlloc0(ordersize*sizeof(int));
1620    block1=(int*)omAlloc0(ordersize*sizeof(int));
1621    wv=(int**) omAlloc0(ordersize*sizeof(int**));
1622    for (k=0;k<ordersize-1; k++)
1623    {
1624      block0[k+1] = origR->block0[k];
1625      block1[k+1] = origR->block1[k];
1626      ord[k+1] = origR->order[k];
1627      if (origR->wvhdl[k]!=NULL) wv[k+1] = (int*) omMemDup(origR->wvhdl[k]);
1628    }
1629    block0[0] = 1;
1630    block1[0] = rVar(origR);
1631    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1632    for (j=0;j<rVar(origR);j++)
1633      if (pGetExp(delVar,j+1)!=0) wv[0][j]=1;
1634    // use this special ordering: like ringorder_a, except that pFDeg, pWeights
1635    // ignore it
1636    ord[0] = ringorder_aa;
1637  }
1638  // fill in tmp ring to get back the data later on
1639  tmpR  = rCopy0(origR,FALSE,FALSE); // qring==NULL
1640  //rUnComplete(tmpR);
1641  tmpR->p_Procs=NULL;
1642  tmpR->order = ord;
1643  tmpR->block0 = block0;
1644  tmpR->block1 = block1;
1645  tmpR->wvhdl = wv;
1646  rComplete(tmpR, 1);
1647
1648#ifdef HAVE_PLURAL
1649  /* update nc structure on tmpR */
1650  if (rIsPluralRing(origR))
1651  {
1652    if ( nc_rComplete(origR, tmpR, false) ) // no quotient ideal!
1653    {
1654      Werror("no elimination is possible: ordering condition is violated");
1655      // cleanup
1656      rDelete(tmpR);
1657      if (w!=NULL)
1658        delete w;
1659      return NULL;
1660    }
1661  }
1662#endif
1663  // change into the new ring
1664  //pChangeRing((currRing->N),currRing->OrdSgn,ord,block0,block1,wv);
1665  rChangeCurrRing(tmpR);
1666
1667  //h = idInit(IDELEMS(h1),h1->rank);
1668  // fetch data from the old ring
1669  //for (k=0;k<IDELEMS(h1);k++) h->m[k] = prCopyR( h1->m[k], origR);
1670  h=idrCopyR(h1,origR,currRing);
1671  if (origR->qideal!=NULL)
1672  {
1673    WarnS("eliminate in q-ring: experimental");
1674    ideal q=idrCopyR(origR->qideal,origR,currRing);
1675    ideal s=idSimpleAdd(h,q);
1676    idDelete(&h);
1677    idDelete(&q);
1678    h=s;
1679  }
1680  // compute kStd
1681#if 1
1682  //rWrite(tmpR);PrintLn();
1683  BITSET save=test;
1684  //test |=1;
1685  //Print("h: %d gen, rk=%d\n",IDELEMS(h),h->rank);
1686  //extern char * showOption();
1687  //Print("%s\n",showOption());
1688  hh = kStd(h,NULL,hom,&w,hilb);
1689  test=save;
1690  idDelete(&h);
1691#else
1692  extern ideal kGroebner(ideal F, ideal Q);
1693  hh=kGroebner(h,NULL);
1694#endif
1695  // go back to the original ring
1696  rChangeCurrRing(origR);
1697  i = IDELEMS(hh)-1;
1698  while ((i >= 0) && (hh->m[i] == NULL)) i--;
1699  j = -1;
1700  // fetch data from temp ring
1701  for (k=0; k<=i; k++)
1702  {
1703    l=(currRing->N);
1704    while ((l>0) && (p_GetExp( hh->m[k],l,tmpR)*pGetExp(delVar,l)==0)) l--;
1705    if (l==0)
1706    {
1707      j++;
1708      if (j >= IDELEMS(h3))
1709      {
1710        pEnlargeSet(&(h3->m),IDELEMS(h3),16);
1711        IDELEMS(h3) += 16;
1712      }
1713      h3->m[j] = prMoveR( hh->m[k], tmpR,origR);
1714      hh->m[k] = NULL;
1715    }
1716  }
1717  id_Delete(&hh, tmpR);
1718  idSkipZeroes(h3);
1719  rDelete(tmpR);
1720  if (w!=NULL)
1721    delete w;
1722  return h3;
1723}
1724
1725/*2
1726* compute the which-th ar-minor of the matrix a
1727*/
1728poly idMinor(matrix a, int ar, unsigned long which, ideal R)
1729{
1730  int     i,j,k,size;
1731  unsigned long curr;
1732  int *rowchoise,*colchoise;
1733  BOOLEAN rowch,colch;
1734  ideal result;
1735  matrix tmp;
1736  poly p,q;
1737
1738  i = binom(a->rows(),ar);
1739  j = binom(a->cols(),ar);
1740
1741  rowchoise=(int *)omAlloc(ar*sizeof(int));
1742  colchoise=(int *)omAlloc(ar*sizeof(int));
1743  if ((i>512) || (j>512) || (i*j >512)) size=512;
1744  else size=i*j;
1745  result=idInit(size,1);
1746  tmp=mpNew(ar,ar);
1747  k = 0; /* the index in result*/
1748  curr = 0; /* index of current minor */
1749  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
1750  while (!rowch)
1751  {
1752    idInitChoise(ar,1,a->cols(),&colch,colchoise);
1753    while (!colch)
1754    {
1755      if (curr == which)
1756      {
1757        for (i=1; i<=ar; i++)
1758        {
1759          for (j=1; j<=ar; j++)
1760          {
1761            MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1762          }
1763        }
1764        p = mp_DetBareiss(tmp,currRing);
1765        if (p!=NULL)
1766        {
1767          if (R!=NULL)
1768          {
1769            q = p;
1770            p = kNF(R,currQuotient,q);
1771            p_Delete(&q,currRing);
1772          }
1773          /*delete the matrix tmp*/
1774          for (i=1; i<=ar; i++)
1775          {
1776            for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1777          }
1778          idDelete((ideal*)&tmp);
1779          omFreeSize((ADDRESS)rowchoise,ar*sizeof(int));
1780          omFreeSize((ADDRESS)colchoise,ar*sizeof(int));
1781          return (p);
1782        }
1783      }
1784      curr++;
1785      idGetNextChoise(ar,a->cols(),&colch,colchoise);
1786    }
1787    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
1788  }
1789  return (poly) 1;
1790}
1791
1792#ifdef WITH_OLD_MINOR
1793/*2
1794* compute all ar-minors of the matrix a
1795*/
1796ideal idMinors(matrix a, int ar, ideal R)
1797{
1798  int     i,j,k,size;
1799  int *rowchoise,*colchoise;
1800  BOOLEAN rowch,colch;
1801  ideal result;
1802  matrix tmp;
1803  poly p,q;
1804
1805  i = binom(a->rows(),ar);
1806  j = binom(a->cols(),ar);
1807
1808  rowchoise=(int *)omAlloc(ar*sizeof(int));
1809  colchoise=(int *)omAlloc(ar*sizeof(int));
1810  if ((i>512) || (j>512) || (i*j >512)) size=512;
1811  else size=i*j;
1812  result=idInit(size,1);
1813  tmp=mpNew(ar,ar);
1814  k = 0; /* the index in result*/
1815  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
1816  while (!rowch)
1817  {
1818    idInitChoise(ar,1,a->cols(),&colch,colchoise);
1819    while (!colch)
1820    {
1821      for (i=1; i<=ar; i++)
1822      {
1823        for (j=1; j<=ar; j++)
1824        {
1825          MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1826        }
1827      }
1828      p = mp_DetBareiss(tmp,vcurrRing);
1829      if (p!=NULL)
1830      {
1831        if (R!=NULL)
1832        {
1833          q = p;
1834          p = kNF(R,currQuotient,q);
1835          p_Delete(&q,currRing);
1836        }
1837        if (p!=NULL)
1838        {
1839          if (k>=size)
1840          {
1841            pEnlargeSet(&result->m,size,32);
1842            size += 32;
1843          }
1844          result->m[k] = p;
1845          k++;
1846        }
1847      }
1848      idGetNextChoise(ar,a->cols(),&colch,colchoise);
1849    }
1850    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
1851  }
1852  /*delete the matrix tmp*/
1853  for (i=1; i<=ar; i++)
1854  {
1855    for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1856  }
1857  idDelete((ideal*)&tmp);
1858  if (k==0)
1859  {
1860    k=1;
1861    result->m[0]=NULL;
1862  }
1863  omFreeSize((ADDRESS)rowchoise,ar*sizeof(int));
1864  omFreeSize((ADDRESS)colchoise,ar*sizeof(int));
1865  pEnlargeSet(&result->m,size,k-size);
1866  IDELEMS(result) = k;
1867  return (result);
1868}
1869#else
1870/*2
1871* compute all ar-minors of the matrix a
1872* the caller of mpRecMin
1873* the elements of the result are not in R (if R!=NULL)
1874*/
1875ideal idMinors(matrix a, int ar, ideal R)
1876{
1877  int elems=0;
1878  int r=a->nrows,c=a->ncols;
1879  int i;
1880  matrix b;
1881  ideal result,h;
1882  ring origR=currRing;
1883  ring tmpR;
1884  long bound;
1885
1886  if((ar<=0) || (ar>r) || (ar>c))
1887  {
1888    Werror("%d-th minor, matrix is %dx%d",ar,r,c);
1889    return NULL;
1890  }
1891  h = id_Matrix2Module(mp_Copy(a,origR),origR);
1892  bound = sm_ExpBound(h,c,r,ar,origR);
1893  idDelete(&h);
1894  tmpR=sm_RingChange(origR,bound);
1895  b = mpNew(r,c);
1896  for (i=r*c-1;i>=0;i--)
1897  {
1898    if (a->m[i])
1899      b->m[i] = prCopyR(a->m[i],origR,tmpR);
1900  }
1901  if (R!=NULL)
1902  {
1903    R = idrCopyR(R,origR,tmpR);
1904    //if (ar>1) // otherwise done in mpMinorToResult
1905    //{
1906    //  matrix bb=(matrix)kNF(R,currQuotient,(ideal)b);
1907    //  bb->rank=b->rank; bb->nrows=b->nrows; bb->ncols=b->ncols;
1908    //  idDelete((ideal*)&b); b=bb;
1909    //}
1910  }
1911  result=idInit(32,1);
1912  if(ar>1) mp_RecMin(ar-1,result,elems,b,r,c,NULL,R,tmpR);
1913  else mp_MinorToResult(result,elems,b,r,c,R,tmpR);
1914  idDelete((ideal *)&b);
1915  if (R!=NULL) idDelete(&R);
1916  idSkipZeroes(result);
1917  rChangeCurrRing(origR);
1918  result = idrMoveR(result,tmpR,origR);
1919  sm_KillModifiedRing(tmpR);
1920  idTest(result);
1921  return result;
1922}
1923#endif
1924
1925/*2
1926*returns TRUE if id1 is a submodule of id2
1927*/
1928BOOLEAN idIsSubModule(ideal id1,ideal id2)
1929{
1930  int i;
1931  poly p;
1932
1933  if (idIs0(id1)) return TRUE;
1934  for (i=0;i<IDELEMS(id1);i++)
1935  {
1936    if (id1->m[i] != NULL)
1937    {
1938      p = kNF(id2,currQuotient,id1->m[i]);
1939      if (p != NULL)
1940      {
1941        p_Delete(&p,currRing);
1942        return FALSE;
1943      }
1944    }
1945  }
1946  return TRUE;
1947}
1948
1949BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
1950{
1951  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
1952  if (idIs0(m)) return TRUE;
1953
1954  int cmax=-1;
1955  int i;
1956  poly p=NULL;
1957  int length=IDELEMS(m);
1958  polyset P=m->m;
1959  for (i=length-1;i>=0;i--)
1960  {
1961    p=P[i];
1962    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
1963  }
1964  if (w != NULL)
1965  if (w->length()+1 < cmax)
1966  {
1967    // Print("length: %d - %d \n", w->length(),cmax);
1968    return FALSE;
1969  }
1970
1971  if(w!=NULL)
1972    p_SetModDeg(w, currRing);
1973
1974  for (i=length-1;i>=0;i--)
1975  {
1976    p=P[i];
1977    poly q=p;
1978    if (p!=NULL)
1979    {
1980      int d=currRing->pFDeg(p,currRing);
1981      loop
1982      {
1983        pIter(p);
1984        if (p==NULL) break;
1985        if (d!=currRing->pFDeg(p,currRing))
1986        {
1987          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
1988          if(w!=NULL)
1989            p_SetModDeg(NULL, currRing);
1990          return FALSE;
1991        }
1992      }
1993    }
1994  }
1995
1996  if(w!=NULL)
1997    p_SetModDeg(NULL, currRing);
1998
1999  return TRUE;
2000}
2001
2002ideal idSeries(int n,ideal M,matrix U,intvec *w)
2003{
2004  for(int i=IDELEMS(M)-1;i>=0;i--)
2005  {
2006    if(U==NULL)
2007      M->m[i]=pSeries(n,M->m[i],NULL,w);
2008    else
2009    {
2010      M->m[i]=pSeries(n,M->m[i],MATELEM(U,i+1,i+1),w);
2011      MATELEM(U,i+1,i+1)=NULL;
2012    }
2013  }
2014  if(U!=NULL)
2015    idDelete((ideal*)&U);
2016  return M;
2017}
2018
2019matrix idDiff(matrix i, int k)
2020{
2021  int e=MATCOLS(i)*MATROWS(i);
2022  matrix r=mpNew(MATROWS(i),MATCOLS(i));
2023  r->rank=i->rank;
2024  int j;
2025  for(j=0; j<e; j++)
2026  {
2027    r->m[j]=pDiff(i->m[j],k);
2028  }
2029  return r;
2030}
2031
2032matrix idDiffOp(ideal I, ideal J,BOOLEAN multiply)
2033{
2034  matrix r=mpNew(IDELEMS(I),IDELEMS(J));
2035  int i,j;
2036  for(i=0; i<IDELEMS(I); i++)
2037  {
2038    for(j=0; j<IDELEMS(J); j++)
2039    {
2040      MATELEM(r,i+1,j+1)=pDiffOp(I->m[i],J->m[j],multiply);
2041    }
2042  }
2043  return r;
2044}
2045
2046/*3
2047*handles for some ideal operations the ring/syzcomp managment
2048*returns all syzygies (componentwise-)shifted by -syzcomp
2049*or -syzcomp-1 (in case of ideals as input)
2050static ideal idHandleIdealOp(ideal arg,int syzcomp,int isIdeal=FALSE)
2051{
2052  ring orig_ring=currRing;
2053  ring syz_ring=rAssure_SyzComp(orig_ring, TRUE); rChangeCurrRing(syz_ring);
2054  rSetSyzComp(length, syz_ring);
2055
2056  ideal s_temp;
2057  if (orig_ring!=syz_ring)
2058    s_temp=idrMoveR_NoSort(arg,orig_ring, syz_ring);
2059  else
2060    s_temp=arg;
2061
2062  ideal s_temp1 = kStd(s_temp,currQuotient,testHomog,&w,NULL,length);
2063  if (w!=NULL) delete w;
2064
2065  if (syz_ring!=orig_ring)
2066  {
2067    idDelete(&s_temp);
2068    rChangeCurrRing(orig_ring);
2069  }
2070
2071  idDelete(&temp);
2072  ideal temp1=idRingCopy(s_temp1,syz_ring);
2073
2074  if (syz_ring!=orig_ring)
2075  {
2076    rChangeCurrRing(syz_ring);
2077    idDelete(&s_temp1);
2078    rChangeCurrRing(orig_ring);
2079    rDelete(syz_ring);
2080  }
2081
2082  for (i=0;i<IDELEMS(temp1);i++)
2083  {
2084    if ((temp1->m[i]!=NULL)
2085    && (pGetComp(temp1->m[i])<=length))
2086    {
2087      pDelete(&(temp1->m[i]));
2088    }
2089    else
2090    {
2091      p_Shift(&(temp1->m[i]),-length,currRing);
2092    }
2093  }
2094  temp1->rank = rk;
2095  idSkipZeroes(temp1);
2096
2097  return temp1;
2098}
2099*/
2100/*2
2101* represents (h1+h2)/h2=h1/(h1 intersect h2)
2102*/
2103//ideal idModulo (ideal h2,ideal h1)
2104ideal idModulo (ideal h2,ideal h1, tHomog hom, intvec ** w)
2105{
2106  intvec *wtmp=NULL;
2107
2108  int i,j,k,rk,flength=0,slength,length;
2109  poly p,q;
2110
2111  if (idIs0(h2))
2112    return idFreeModule(si_max(1,h2->ncols));
2113  if (!idIs0(h1))
2114    flength = id_RankFreeModule(h1,currRing);
2115  slength = id_RankFreeModule(h2,currRing);
2116  length  = si_max(flength,slength);
2117  if (length==0)
2118  {
2119    length = 1;
2120  }
2121  ideal temp = idInit(IDELEMS(h2),length+IDELEMS(h2));
2122  if ((w!=NULL)&&((*w)!=NULL))
2123  {
2124    //Print("input weights:");(*w)->show(1);PrintLn();
2125    int d;
2126    int k;
2127    wtmp=new intvec(length+IDELEMS(h2));
2128    for (i=0;i<length;i++)
2129      ((*wtmp)[i])=(**w)[i];
2130    for (i=0;i<IDELEMS(h2);i++)
2131    {
2132      poly p=h2->m[i];
2133      if (p!=NULL)
2134      {
2135        d = pDeg(p);
2136        k= pGetComp(p);
2137        if (slength>0) k--;
2138        d +=((**w)[k]);
2139        ((*wtmp)[i+length]) = d;
2140      }
2141    }
2142    //Print("weights:");wtmp->show(1);PrintLn();
2143  }
2144  for (i=0;i<IDELEMS(h2);i++)
2145  {
2146    temp->m[i] = pCopy(h2->m[i]);
2147    q = pOne();
2148    pSetComp(q,i+1+length);
2149    pSetmComp(q);
2150    if(temp->m[i]!=NULL)
2151    {
2152      if (slength==0) p_Shift(&(temp->m[i]),1,currRing);
2153      p = temp->m[i];
2154      while (pNext(p)!=NULL) pIter(p);
2155      pNext(p) = q;
2156    }
2157    else
2158      temp->m[i]=q;
2159  }
2160  rk = k = IDELEMS(h2);
2161  if (!idIs0(h1))
2162  {
2163    pEnlargeSet(&(temp->m),IDELEMS(temp),IDELEMS(h1));
2164    IDELEMS(temp) += IDELEMS(h1);
2165    for (i=0;i<IDELEMS(h1);i++)
2166    {
2167      if (h1->m[i]!=NULL)
2168      {
2169        temp->m[k] = pCopy(h1->m[i]);
2170        if (flength==0) p_Shift(&(temp->m[k]),1,currRing);
2171        k++;
2172      }
2173    }
2174  }
2175
2176  ring orig_ring=currRing;
2177  ring syz_ring=rAssure_SyzComp(orig_ring, TRUE); rChangeCurrRing(syz_ring);
2178  rSetSyzComp(length, syz_ring);
2179  ideal s_temp;
2180
2181  if (syz_ring != orig_ring)
2182  {
2183    s_temp = idrMoveR_NoSort(temp, orig_ring, syz_ring);
2184  }
2185  else
2186  {
2187    s_temp = temp;
2188  }
2189
2190  idTest(s_temp);
2191  ideal s_temp1 = kStd(s_temp,currQuotient,hom,&wtmp,NULL,length);
2192
2193  //if (wtmp!=NULL)  Print("output weights:");wtmp->show(1);PrintLn();
2194  if ((w!=NULL) && (*w !=NULL) && (wtmp!=NULL))
2195  {
2196    delete *w;
2197    *w=new intvec(IDELEMS(h2));
2198    for (i=0;i<IDELEMS(h2);i++)
2199      ((**w)[i])=(*wtmp)[i+length];
2200  }
2201  if (wtmp!=NULL) delete wtmp;
2202
2203  for (i=0;i<IDELEMS(s_temp1);i++)
2204  {
2205    if ((s_temp1->m[i]!=NULL)
2206    && (pGetComp(s_temp1->m[i])<=length))
2207    {
2208      p_Delete(&(s_temp1->m[i]),currRing);
2209    }
2210    else
2211    {
2212      p_Shift(&(s_temp1->m[i]),-length,currRing);
2213    }
2214  }
2215  s_temp1->rank = rk;
2216  idSkipZeroes(s_temp1);
2217
2218  if (syz_ring!=orig_ring)
2219  {
2220    rChangeCurrRing(orig_ring);
2221    s_temp1 = idrMoveR_NoSort(s_temp1, syz_ring, orig_ring);
2222    rDelete(syz_ring);
2223    // Hmm ... here seems to be a memory leak
2224    // However, simply deleting it causes memory trouble
2225    // idDelete(&s_temp);
2226  }
2227  else
2228  {
2229    idDelete(&temp);
2230  }
2231  idTest(s_temp1);
2232  return s_temp1;
2233}
2234
2235/*
2236*computes module-weights for liftings of homogeneous modules
2237*/
2238intvec * idMWLift(ideal mod,intvec * weights)
2239{
2240  if (idIs0(mod)) return new intvec(2);
2241  int i=IDELEMS(mod);
2242  while ((i>0) && (mod->m[i-1]==NULL)) i--;
2243  intvec *result = new intvec(i+1);
2244  while (i>0)
2245  {
2246    (*result)[i]=currRing->pFDeg(mod->m[i],currRing)+(*weights)[pGetComp(mod->m[i])];
2247  }
2248  return result;
2249}
2250
2251/*2
2252*sorts the kbase for idCoef* in a special way (lexicographically
2253*with x_max,...,x_1)
2254*/
2255ideal idCreateSpecialKbase(ideal kBase,intvec ** convert)
2256{
2257  int i;
2258  ideal result;
2259
2260  if (idIs0(kBase)) return NULL;
2261  result = idInit(IDELEMS(kBase),kBase->rank);
2262  *convert = idSort(kBase,FALSE);
2263  for (i=0;i<(*convert)->length();i++)
2264  {
2265    result->m[i] = pCopy(kBase->m[(**convert)[i]-1]);
2266  }
2267  return result;
2268}
2269
2270/*2
2271*returns the index of a given monom in the list of the special kbase
2272*/
2273int idIndexOfKBase(poly monom, ideal kbase)
2274{
2275  int j=IDELEMS(kbase);
2276
2277  while ((j>0) && (kbase->m[j-1]==NULL)) j--;
2278  if (j==0) return -1;
2279  int i=(currRing->N);
2280  while (i>0)
2281  {
2282    loop
2283    {
2284      if (pGetExp(monom,i)>pGetExp(kbase->m[j-1],i)) return -1;
2285      if (pGetExp(monom,i)==pGetExp(kbase->m[j-1],i)) break;
2286      j--;
2287      if (j==0) return -1;
2288    }
2289    if (i==1)
2290    {
2291      while(j>0)
2292      {
2293        if (pGetComp(monom)==pGetComp(kbase->m[j-1])) return j-1;
2294        if (pGetComp(monom)>pGetComp(kbase->m[j-1])) return -1;
2295        j--;
2296      }
2297    }
2298    i--;
2299  }
2300  return -1;
2301}
2302
2303/*2
2304*decomposes the monom in a part of coefficients described by the
2305*complement of how and a monom in variables occuring in how, the
2306*index of which in kbase is returned as integer pos (-1 if it don't
2307*exists)
2308*/
2309poly idDecompose(poly monom, poly how, ideal kbase, int * pos)
2310{
2311  int i;
2312  poly coeff=pOne(), base=pOne();
2313
2314  for (i=1;i<=(currRing->N);i++)
2315  {
2316    if (pGetExp(how,i)>0)
2317    {
2318      pSetExp(base,i,pGetExp(monom,i));
2319    }
2320    else
2321    {
2322      pSetExp(coeff,i,pGetExp(monom,i));
2323    }
2324  }
2325  pSetComp(base,pGetComp(monom));
2326  pSetm(base);
2327  pSetCoeff(coeff,nCopy(pGetCoeff(monom)));
2328  pSetm(coeff);
2329  *pos = idIndexOfKBase(base,kbase);
2330  if (*pos<0)
2331    p_Delete(&coeff,currRing);
2332  p_Delete(&base,currRing);
2333  return coeff;
2334}
2335
2336/*2
2337*returns a matrix A of coefficients with kbase*A=arg
2338*if all monomials in variables of how occur in kbase
2339*the other are deleted
2340*/
2341matrix idCoeffOfKBase(ideal arg, ideal kbase, poly how)
2342{
2343  matrix result;
2344  ideal tempKbase;
2345  poly p,q;
2346  intvec * convert;
2347  int i=IDELEMS(kbase),j=IDELEMS(arg),k,pos;
2348#if 0
2349  while ((i>0) && (kbase->m[i-1]==NULL)) i--;
2350  if (idIs0(arg))
2351    return mpNew(i,1);
2352  while ((j>0) && (arg->m[j-1]==NULL)) j--;
2353  result = mpNew(i,j);
2354#else
2355  result = mpNew(i, j);
2356  while ((j>0) && (arg->m[j-1]==NULL)) j--;
2357#endif
2358
2359  tempKbase = idCreateSpecialKbase(kbase,&convert);
2360  for (k=0;k<j;k++)
2361  {
2362    p = arg->m[k];
2363    while (p!=NULL)
2364    {
2365      q = idDecompose(p,how,tempKbase,&pos);
2366      if (pos>=0)
2367      {
2368        MATELEM(result,(*convert)[pos],k+1) =
2369            pAdd(MATELEM(result,(*convert)[pos],k+1),q);
2370      }
2371      else
2372        p_Delete(&q,currRing);
2373      pIter(p);
2374    }
2375  }
2376  idDelete(&tempKbase);
2377  return result;
2378}
2379
2380static void idDeleteComps(ideal arg,int* red_comp,int del)
2381// red_comp is an array [0..args->rank]
2382{
2383  int i,j;
2384  poly p;
2385
2386  for (i=IDELEMS(arg)-1;i>=0;i--)
2387  {
2388    p = arg->m[i];
2389    while (p!=NULL)
2390    {
2391      j = pGetComp(p);
2392      if (red_comp[j]!=j)
2393      {
2394        pSetComp(p,red_comp[j]);
2395        pSetmComp(p);
2396      }
2397      pIter(p);
2398    }
2399  }
2400  (arg->rank) -= del;
2401}
2402
2403/*2
2404* returns the presentation of an isomorphic, minimally
2405* embedded  module (arg represents the quotient!)
2406*/
2407ideal idMinEmbedding(ideal arg,BOOLEAN inPlace, intvec **w)
2408{
2409  if (idIs0(arg)) return idInit(1,arg->rank);
2410  int i,next_gen,next_comp;
2411  ideal res=arg;
2412  if (!inPlace) res = idCopy(arg);
2413  res->rank=si_max(res->rank,id_RankFreeModule(res,currRing));
2414  int *red_comp=(int*)omAlloc((res->rank+1)*sizeof(int));
2415  for (i=res->rank;i>=0;i--) red_comp[i]=i;
2416
2417  int del=0;
2418  loop
2419  {
2420    next_gen = id_ReadOutPivot(res, &next_comp, currRing);
2421    if (next_gen<0) break;
2422    del++;
2423    syGaussForOne(res,next_gen,next_comp,0,IDELEMS(res));
2424    for(i=next_comp+1;i<=arg->rank;i++) red_comp[i]--;
2425    if ((w !=NULL)&&(*w!=NULL))
2426    {
2427      for(i=next_comp;i<(*w)->length();i++) (**w)[i-1]=(**w)[i];
2428    }
2429  }
2430
2431  idDeleteComps(res,red_comp,del);
2432  idSkipZeroes(res);
2433  omFree(red_comp);
2434
2435  if ((w !=NULL)&&(*w!=NULL) &&(del>0))
2436  {
2437    intvec *wtmp=new intvec((*w)->length()-del);
2438    for(i=0;i<res->rank;i++) (*wtmp)[i]=(**w)[i];
2439    delete *w;
2440    *w=wtmp;
2441  }
2442  return res;
2443}
2444
2445#include <polys/clapsing.h>
2446
2447#ifdef HAVE_FACTORY
2448#if 0
2449poly id_GCD(poly f, poly g, const ring r)
2450{
2451  ring save_r=currRing;
2452  rChangeCurrRing(r);
2453  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2454  intvec *w = NULL;
2455  ideal S=idSyzygies(I,testHomog,&w);
2456  if (w!=NULL) delete w;
2457  poly gg=pTakeOutComp(&(S->m[0]),2);
2458  idDelete(&S);
2459  poly gcd_p=singclap_pdivide(f,gg,r);
2460  p_Delete(&gg,r);
2461  rChangeCurrRing(save_r);
2462  return gcd_p;
2463}
2464#else
2465poly id_GCD(poly f, poly g, const ring r)
2466{
2467  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2468  intvec *w = NULL;
2469
2470  ring save_r = currRing; rChangeCurrRing(r); ideal S=idSyzygies(I,testHomog,&w); rChangeCurrRing(save_r);
2471
2472  if (w!=NULL) delete w;
2473  poly gg=p_TakeOutComp(&(S->m[0]), 2, r);
2474  id_Delete(&S, r);
2475  poly gcd_p=singclap_pdivide(f,gg, r);
2476  p_Delete(&gg, r);
2477
2478  return gcd_p;
2479}
2480#endif
2481#endif
2482
2483/*2
2484* xx,q: arrays of length 0..rl-1
2485* xx[i]: SB mod q[i]
2486* assume: char=0
2487* assume: q[i]!=0
2488* destroys xx
2489*/
2490#ifdef HAVE_FACTORY
2491ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring R)
2492{
2493  int cnt=IDELEMS(xx[0])*xx[0]->nrows;
2494  ideal result=idInit(cnt,xx[0]->rank);
2495  result->nrows=xx[0]->nrows; // for lifting matrices
2496  result->ncols=xx[0]->ncols; // for lifting matrices
2497  int i,j;
2498  poly r,h,hh,res_p;
2499  number *x=(number *)omAlloc(rl*sizeof(number));
2500  for(i=cnt-1;i>=0;i--)
2501  {
2502    res_p=NULL;
2503    loop
2504    {
2505      r=NULL;
2506      for(j=rl-1;j>=0;j--)
2507      {
2508        h=xx[j]->m[i];
2509        if ((h!=NULL)
2510        &&((r==NULL)||(p_LmCmp(r,h,R)==-1)))
2511          r=h;
2512      }
2513      if (r==NULL) break;
2514      h=p_Head(r,R);
2515      for(j=rl-1;j>=0;j--)
2516      {
2517        hh=xx[j]->m[i];
2518        if ((hh!=NULL) && (p_LmCmp(r,hh,R)==0))
2519        {
2520          x[j]=pGetCoeff(hh);
2521          hh=p_LmFreeAndNext(hh,R);
2522          xx[j]->m[i]=hh;
2523        }
2524        else
2525          x[j]=n_Init(0, R->cf);
2526      }
2527      number n=n_ChineseRemainder(x,q,rl,R->cf);
2528      for(j=rl-1;j>=0;j--)
2529      {
2530        x[j]=NULL; // nlInit(0...) takes no memory
2531      }
2532      if (n_IsZero(n,R->cf)) p_Delete(&h,R);
2533      else
2534      {
2535        p_SetCoeff(h,n,R);
2536        //Print("new mon:");pWrite(h);
2537        res_p=p_Add_q(res_p,h,R);
2538      }
2539    }
2540    result->m[i]=res_p;
2541  }
2542  omFree(x);
2543  for(i=rl-1;i>=0;i--) id_Delete(&(xx[i]),R);
2544  omFree(xx);
2545  return result;
2546}
2547#endif
2548
2549#if 0
2550/*2
2551* xx,q: arrays of length 0..rl-1
2552* xx[i]: SB mod q[i]
2553* assume: char=0
2554* assume: q[i]!=0
2555* destroys xx
2556*/
2557#ifdef HAVE_FACTORY
2558ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring R)
2559{
2560  int cnt=IDELEMS(xx[0])*xx[0]->nrows;
2561  ideal result=idInit(cnt,xx[0]->rank);
2562  result->nrows=xx[0]->nrows; // for lifting matrices
2563  result->ncols=xx[0]->ncols; // for lifting matrices
2564  int i,j;
2565  poly r,h,hh,res_p;
2566  number *x=(number *)omAlloc(rl*sizeof(number));
2567  for(i=cnt-1;i>=0;i--)
2568  {
2569    res_p=NULL;
2570    loop
2571    {
2572      r=NULL;
2573      for(j=rl-1;j>=0;j--)
2574      {
2575        h=xx[j]->m[i];
2576        if ((h!=NULL)
2577        &&((r==NULL)||(p_LmCmp(r,h,R)==-1)))
2578          r=h;
2579      }
2580      if (r==NULL) break;
2581      h=p_Head(r, R);
2582      for(j=rl-1;j>=0;j--)
2583      {
2584        hh=xx[j]->m[i];
2585        if ((hh!=NULL) && (p_LmCmp(r,hh, R)==0))
2586        {
2587          x[j]=p_GetCoeff(hh, R);
2588          hh=p_LmFreeAndNext(hh, R);
2589          xx[j]->m[i]=hh;
2590        }
2591        else
2592          x[j]=n_Init(0, R->cf); // is R->cf really n_Q???, yes!
2593      }
2594
2595      number n=nChineseRemainder(x,q,rl, R->cf);
2596
2597      for(j=rl-1;j>=0;j--)
2598      {
2599        x[j]=NULL; // nlInit(0...) takes no memory
2600      }
2601      if (n_IsZero(n, R->cf)) p_Delete(&h, R);
2602      else
2603      {
2604        p_SetCoeff(h,n, R);
2605        //Print("new mon:");pWrite(h);
2606        res_p=p_Add_q(res_p, h, R);
2607      }
2608    }
2609    result->m[i]=res_p;
2610  }
2611  omFree(x);
2612  for(i=rl-1;i>=0;i--) id_Delete(&(xx[i]), R);
2613  omFree(xx);
2614  return result;
2615}
2616#endif
2617#endif
2618/* currently unsed:
2619ideal idChineseRemainder(ideal *xx, intvec *iv)
2620{
2621  int rl=iv->length();
2622  number *q=(number *)omAlloc(rl*sizeof(number));
2623  int i;
2624  for(i=0; i<rl; i++)
2625  {
2626    q[i]=nInit((*iv)[i]);
2627  }
2628  return idChineseRemainder(xx,q,rl);
2629}
2630*/
2631/*
2632 * lift ideal with coeffs over Z (mod N) to Q via Farey
2633 */
2634ideal id_Farey(ideal x, number N, const ring r)
2635{
2636  int cnt=IDELEMS(x)*x->nrows;
2637  ideal result=idInit(cnt,x->rank);
2638  result->nrows=x->nrows; // for lifting matrices
2639  result->ncols=x->ncols; // for lifting matrices
2640
2641  int i;
2642  for(i=cnt-1;i>=0;i--)
2643  {
2644    poly h=p_Copy(x->m[i],r);
2645    result->m[i]=h;
2646    while(h!=NULL)
2647    {
2648      number c=pGetCoeff(h);
2649      pSetCoeff0(h,n_Farey(c,N,r->cf));
2650      n_Delete(&c,r->cf);
2651      pIter(h);
2652    }
2653    while((result->m[i]!=NULL)&&(n_IsZero(pGetCoeff(result->m[i]),r->cf)))
2654    {
2655      p_LmDelete(&(result->m[i]),r);
2656    }
2657    h=result->m[i];
2658    while((h!=NULL) && (pNext(h)!=NULL))
2659    {
2660      if(n_IsZero(pGetCoeff(pNext(h)),r->cf))
2661      {
2662        p_LmDelete(&pNext(h),r);
2663      }
2664      else pIter(h);
2665    }
2666  }
2667  return result;
2668}
2669
2670
2671
2672
2673// uses glabl vars via pSetModDeg
2674/*
2675BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
2676{
2677  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
2678  if (idIs0(m)) return TRUE;
2679
2680  int cmax=-1;
2681  int i;
2682  poly p=NULL;
2683  int length=IDELEMS(m);
2684  poly* P=m->m;
2685  for (i=length-1;i>=0;i--)
2686  {
2687    p=P[i];
2688    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
2689  }
2690  if (w != NULL)
2691  if (w->length()+1 < cmax)
2692  {
2693    // Print("length: %d - %d \n", w->length(),cmax);
2694    return FALSE;
2695  }
2696
2697  if(w!=NULL)
2698    p_SetModDeg(w, currRing);
2699
2700  for (i=length-1;i>=0;i--)
2701  {
2702    p=P[i];
2703    poly q=p;
2704    if (p!=NULL)
2705    {
2706      int d=p_FDeg(p,currRing);
2707      loop
2708      {
2709        pIter(p);
2710        if (p==NULL) break;
2711        if (d!=p_FDeg(p,currRing))
2712        {
2713          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
2714          if(w!=NULL)
2715            p_SetModDeg(NULL, currRing);
2716          return FALSE;
2717        }
2718      }
2719    }
2720  }
2721
2722  if(w!=NULL)
2723    p_SetModDeg(NULL, currRing);
2724
2725  return TRUE;
2726}
2727*/
2728
2729
2730
Note: See TracBrowser for help on using the repository browser.