source: git/kernel/ideals.cc @ 750754

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