source: git/kernel/ideals.cc @ 81f40d7

fieker-DuValspielwiese
Last change on this file since 81f40d7 was 35edb5, checked in by Hans Schoenemann <hannes@…>, 9 years ago
optimized idLiftStd
  • 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}
931
932/*2
933*computes a representation of the generators of submod with respect to those
934* of mod
935*/
936
937ideal idLift(ideal mod, ideal submod,ideal *rest, BOOLEAN goodShape,
938             BOOLEAN isSB, BOOLEAN divide, matrix *unit)
939{
940  int lsmod =id_RankFreeModule(submod,currRing), j, k;
941  int comps_to_add=0;
942  poly p;
943
944  if (idIs0(submod))
945  {
946    if (unit!=NULL)
947    {
948      *unit=mpNew(1,1);
949      MATELEM(*unit,1,1)=pOne();
950    }
951    if (rest!=NULL)
952    {
953      *rest=idInit(1,mod->rank);
954    }
955    return idInit(1,mod->rank);
956  }
957  if (idIs0(mod)) /* and not idIs0(submod) */
958  {
959    WerrorS("2nd module does not lie in the first");
960    return NULL;
961  }
962  if (unit!=NULL)
963  {
964    comps_to_add = IDELEMS(submod);
965    while ((comps_to_add>0) && (submod->m[comps_to_add-1]==NULL))
966      comps_to_add--;
967  }
968  k=si_max(id_RankFreeModule(mod,currRing),id_RankFreeModule(submod,currRing));
969  if  ((k!=0) && (lsmod==0)) lsmod=1;
970  k=si_max(k,(int)mod->rank);
971  if (k<submod->rank) { WarnS("rk(submod) > rk(mod) ?");k=submod->rank; }
972
973  ring orig_ring=currRing;
974  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE);  rChangeCurrRing(syz_ring);
975  rSetSyzComp(k,syz_ring);
976
977  ideal s_mod, s_temp;
978  if (orig_ring != syz_ring)
979  {
980    s_mod = idrCopyR_NoSort(mod,orig_ring,syz_ring);
981    s_temp = idrCopyR_NoSort(submod,orig_ring,syz_ring);
982  }
983  else
984  {
985    s_mod = mod;
986    s_temp = idCopy(submod);
987  }
988  ideal s_h3;
989  if (isSB)
990  {
991    s_h3 = idCopy(s_mod);
992    idPrepareStd(s_h3, k+comps_to_add);
993  }
994  else
995  {
996    s_h3 = idPrepare(s_mod,(tHomog)FALSE,k+comps_to_add,NULL);
997  }
998  if (!goodShape)
999  {
1000    for (j=0;j<IDELEMS(s_h3);j++)
1001    {
1002      if ((s_h3->m[j] != NULL) && (pMinComp(s_h3->m[j]) > k))
1003        p_Delete(&(s_h3->m[j]),currRing);
1004    }
1005  }
1006  idSkipZeroes(s_h3);
1007  if (lsmod==0)
1008  {
1009    id_Shift(s_temp,1,currRing);
1010  }
1011  if (unit!=NULL)
1012  {
1013    for(j = 0;j<comps_to_add;j++)
1014    {
1015      p = s_temp->m[j];
1016      if (p!=NULL)
1017      {
1018        while (pNext(p)!=NULL) pIter(p);
1019        pNext(p) = pOne();
1020        pIter(p);
1021        pSetComp(p,1+j+k);
1022        pSetmComp(p);
1023        p = pNeg(p);
1024      }
1025    }
1026  }
1027  ideal s_result = kNF(s_h3,currRing->qideal,s_temp,k);
1028  s_result->rank = s_h3->rank;
1029  ideal s_rest = idInit(IDELEMS(s_result),k);
1030  idDelete(&s_h3);
1031  idDelete(&s_temp);
1032
1033  for (j=0;j<IDELEMS(s_result);j++)
1034  {
1035    if (s_result->m[j]!=NULL)
1036    {
1037      if (pGetComp(s_result->m[j])<=k)
1038      {
1039        if (!divide)
1040        {
1041          if (isSB)
1042          {
1043            WarnS("first module not a standardbasis\n"
1044              "// ** or second not a proper submodule");
1045          }
1046          else
1047            WerrorS("2nd module does not lie in the first");
1048          idDelete(&s_result);
1049          idDelete(&s_rest);
1050          s_result=idInit(IDELEMS(submod),submod->rank);
1051          break;
1052        }
1053        else
1054        {
1055          p = s_rest->m[j] = s_result->m[j];
1056          while ((pNext(p)!=NULL) && (pGetComp(pNext(p))<=k)) pIter(p);
1057          s_result->m[j] = pNext(p);
1058          pNext(p) = NULL;
1059        }
1060      }
1061      p_Shift(&(s_result->m[j]),-k,currRing);
1062      pNeg(s_result->m[j]);
1063    }
1064  }
1065  if ((lsmod==0) && (s_rest!=NULL))
1066  {
1067    for (j=IDELEMS(s_rest);j>0;j--)
1068    {
1069      if (s_rest->m[j-1]!=NULL)
1070      {
1071        p_Shift(&(s_rest->m[j-1]),-1,currRing);
1072        s_rest->m[j-1] = s_rest->m[j-1];
1073      }
1074    }
1075  }
1076  if(syz_ring!=orig_ring)
1077  {
1078    idDelete(&s_mod);
1079    rChangeCurrRing(orig_ring);
1080    s_result = idrMoveR_NoSort(s_result, syz_ring, orig_ring);
1081    s_rest = idrMoveR_NoSort(s_rest, syz_ring, orig_ring);
1082    rDelete(syz_ring);
1083  }
1084  if (rest!=NULL)
1085    *rest = s_rest;
1086  else
1087    idDelete(&s_rest);
1088//idPrint(s_result);
1089  if (unit!=NULL)
1090  {
1091    *unit=mpNew(comps_to_add,comps_to_add);
1092    int i;
1093    for(i=0;i<IDELEMS(s_result);i++)
1094    {
1095      poly p=s_result->m[i];
1096      poly q=NULL;
1097      while(p!=NULL)
1098      {
1099        if(pGetComp(p)<=comps_to_add)
1100        {
1101          pSetComp(p,0);
1102          if (q!=NULL)
1103          {
1104            pNext(q)=pNext(p);
1105          }
1106          else
1107          {
1108            pIter(s_result->m[i]);
1109          }
1110          pNext(p)=NULL;
1111          MATELEM(*unit,i+1,i+1)=pAdd(MATELEM(*unit,i+1,i+1),p);
1112          if(q!=NULL)   p=pNext(q);
1113          else          p=s_result->m[i];
1114        }
1115        else
1116        {
1117          q=p;
1118          pIter(p);
1119        }
1120      }
1121      p_Shift(&s_result->m[i],-comps_to_add,currRing);
1122    }
1123  }
1124  return s_result;
1125}
1126
1127/*2
1128*computes division of P by Q with remainder up to (w-weighted) degree n
1129*P, Q, and w are not changed
1130*/
1131void idLiftW(ideal P,ideal Q,int n,matrix &T, ideal &R,short *w)
1132{
1133  long N=0;
1134  int i;
1135  for(i=IDELEMS(Q)-1;i>=0;i--)
1136    if(w==NULL)
1137      N=si_max(N,p_Deg(Q->m[i],currRing));
1138    else
1139      N=si_max(N,p_DegW(Q->m[i],w,currRing));
1140  N+=n;
1141
1142  T=mpNew(IDELEMS(Q),IDELEMS(P));
1143  R=idInit(IDELEMS(P),P->rank);
1144
1145  for(i=IDELEMS(P)-1;i>=0;i--)
1146  {
1147    poly p;
1148    if(w==NULL)
1149      p=ppJet(P->m[i],N);
1150    else
1151      p=ppJetW(P->m[i],N,w);
1152
1153    int j=IDELEMS(Q)-1;
1154    while(p!=NULL)
1155    {
1156      if(pDivisibleBy(Q->m[j],p))
1157      {
1158        poly p0=p_DivideM(pHead(p),pHead(Q->m[j]),currRing);
1159        if(w==NULL)
1160          p=pJet(pSub(p,ppMult_mm(Q->m[j],p0)),N);
1161        else
1162          p=pJetW(pSub(p,ppMult_mm(Q->m[j],p0)),N,w);
1163        pNormalize(p);
1164        if(((w==NULL)&&(p_Deg(p0,currRing)>n))||((w!=NULL)&&(p_DegW(p0,w,currRing)>n)))
1165          p_Delete(&p0,currRing);
1166        else
1167          MATELEM(T,j+1,i+1)=pAdd(MATELEM(T,j+1,i+1),p0);
1168        j=IDELEMS(Q)-1;
1169      }
1170      else
1171      {
1172        if(j==0)
1173        {
1174          poly p0=p;
1175          pIter(p);
1176          pNext(p0)=NULL;
1177          if(((w==NULL)&&(p_Deg(p0,currRing)>n))
1178          ||((w!=NULL)&&(p_DegW(p0,w,currRing)>n)))
1179            p_Delete(&p0,currRing);
1180          else
1181            R->m[i]=pAdd(R->m[i],p0);
1182          j=IDELEMS(Q)-1;
1183        }
1184        else
1185          j--;
1186      }
1187    }
1188  }
1189}
1190
1191/*2
1192*computes the quotient of h1,h2 : internal routine for idQuot
1193*BEWARE: the returned ideals may contain incorrectly ordered polys !
1194*
1195*/
1196static ideal idInitializeQuot (ideal  h1, ideal h2, BOOLEAN h1IsStb, BOOLEAN *addOnlyOne, int *kkmax)
1197{
1198  idTest(h1);
1199  idTest(h2);
1200
1201  ideal temph1;
1202  poly     p,q = NULL;
1203  int i,l,ll,k,kkk,kmax;
1204  int j = 0;
1205  int k1 = id_RankFreeModule(h1,currRing);
1206  int k2 = id_RankFreeModule(h2,currRing);
1207  tHomog   hom=isNotHomog;
1208  k=si_max(k1,k2);
1209  if (k==0)
1210    k = 1;
1211  if ((k2==0) && (k>1)) *addOnlyOne = FALSE;
1212  intvec * weights;
1213  hom = (tHomog)idHomModule(h1,currRing->qideal,&weights);
1214  if /**addOnlyOne &&*/ (/*(*/ !h1IsStb /*)*/)
1215    temph1 = kStd(h1,currRing->qideal,hom,&weights,NULL);
1216  else
1217    temph1 = idCopy(h1);
1218  if (weights!=NULL) delete weights;
1219  idTest(temph1);
1220/*--- making a single vector from h2 ---------------------*/
1221  for (i=0; i<IDELEMS(h2); i++)
1222  {
1223    if (h2->m[i] != NULL)
1224    {
1225      p = pCopy(h2->m[i]);
1226      if (k2 == 0)
1227        p_Shift(&p,j*k+1,currRing);
1228      else
1229        p_Shift(&p,j*k,currRing);
1230      q = pAdd(q,p);
1231      j++;
1232    }
1233  }
1234  *kkmax = kmax = j*k+1;
1235/*--- adding a monomial for the result (syzygy) ----------*/
1236  p = q;
1237  while (pNext(p)!=NULL) pIter(p);
1238  pNext(p) = pOne();
1239  pIter(p);
1240  pSetComp(p,kmax);
1241  pSetmComp(p);
1242/*--- constructing the big matrix ------------------------*/
1243  ideal h4 = idInit(16,kmax+k-1);
1244  h4->m[0] = q;
1245  if (k2 == 0)
1246  {
1247    if (k > IDELEMS(h4))
1248    {
1249      pEnlargeSet(&(h4->m),IDELEMS(h4),k-IDELEMS(h4));
1250      IDELEMS(h4) = k;
1251    }
1252    for (i=1; i<k; i++)
1253    {
1254      if (h4->m[i-1]!=NULL)
1255      {
1256        p = p_Copy_noCheck(h4->m[i-1], currRing); p_Shift(&p,1,currRing);
1257        // pTest(p);
1258        h4->m[i] = p;
1259      }
1260    }
1261  }
1262  idSkipZeroes(h4);
1263  kkk = IDELEMS(h4);
1264  i = IDELEMS(temph1);
1265  for (l=0; l<i; l++)
1266  {
1267    if(temph1->m[l]!=NULL)
1268    {
1269      for (ll=0; ll<j; ll++)
1270      {
1271        p = pCopy(temph1->m[l]);
1272        if (k1 == 0)
1273          p_Shift(&p,ll*k+1,currRing);
1274        else
1275          p_Shift(&p,ll*k,currRing);
1276        if (kkk >= IDELEMS(h4))
1277        {
1278          pEnlargeSet(&(h4->m),IDELEMS(h4),16);
1279          IDELEMS(h4) += 16;
1280        }
1281        h4->m[kkk] = p;
1282        kkk++;
1283      }
1284    }
1285  }
1286/*--- if h2 goes in as single vector - the h1-part is just SB ---*/
1287  if (*addOnlyOne)
1288  {
1289    idSkipZeroes(h4);
1290    p = h4->m[0];
1291    for (i=0;i<IDELEMS(h4)-1;i++)
1292    {
1293      h4->m[i] = h4->m[i+1];
1294    }
1295    h4->m[IDELEMS(h4)-1] = p;
1296    #ifdef HAVE_RINGS
1297    if(!rField_is_Ring(currRing))
1298    #endif
1299    si_opt_1 |= Sy_bit(OPT_SB_1);
1300  }
1301  idDelete(&temph1);
1302  //idTest(h4);//see remark at the beginning
1303  return h4;
1304}
1305/*2
1306*computes the quotient of h1,h2
1307*/
1308ideal idQuot (ideal  h1, ideal h2, BOOLEAN h1IsStb, BOOLEAN resultIsIdeal)
1309{
1310  // first check for special case h1:(0)
1311  if (idIs0(h2))
1312  {
1313    ideal res;
1314    if (resultIsIdeal)
1315    {
1316      res = idInit(1,1);
1317      res->m[0] = pOne();
1318    }
1319    else
1320      res = idFreeModule(h1->rank);
1321    return res;
1322  }
1323  BITSET old_test1;
1324  SI_SAVE_OPT1(old_test1);
1325  int i, kmax;
1326  BOOLEAN  addOnlyOne=TRUE;
1327  tHomog   hom=isNotHomog;
1328  intvec * weights1;
1329
1330  ideal s_h4 = idInitializeQuot (h1,h2,h1IsStb,&addOnlyOne,&kmax);
1331
1332  hom = (tHomog)idHomModule(s_h4,currRing->qideal,&weights1);
1333
1334  ring orig_ring=currRing;
1335  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE);  rChangeCurrRing(syz_ring);
1336  rSetSyzComp(kmax-1,syz_ring);
1337  if (orig_ring!=syz_ring)
1338  //  s_h4 = idrMoveR_NoSort(s_h4,orig_ring, syz_ring);
1339    s_h4 = idrMoveR(s_h4,orig_ring, syz_ring);
1340  idTest(s_h4);
1341  #if 0
1342  void ipPrint_MA0(matrix m, const char *name);
1343  matrix m=idModule2Matrix(idCopy(s_h4));
1344  PrintS("start:\n");
1345  ipPrint_MA0(m,"Q");
1346  idDelete((ideal *)&m);
1347  PrintS("last elem:");wrp(s_h4->m[IDELEMS(s_h4)-1]);PrintLn();
1348  #endif
1349  ideal s_h3;
1350  if (addOnlyOne)
1351  {
1352    s_h3 = kStd(s_h4,currRing->qideal,hom,&weights1,NULL,0/*kmax-1*/,IDELEMS(s_h4)-1);
1353  }
1354  else
1355  {
1356    s_h3 = kStd(s_h4,currRing->qideal,hom,&weights1,NULL,kmax-1);
1357  }
1358  SI_RESTORE_OPT1(old_test1);
1359  #if 0
1360  // only together with the above debug stuff
1361  idSkipZeroes(s_h3);
1362  m=idModule2Matrix(idCopy(s_h3));
1363  Print("result, kmax=%d:\n",kmax);
1364  ipPrint_MA0(m,"S");
1365  idDelete((ideal *)&m);
1366  #endif
1367  idTest(s_h3);
1368  if (weights1!=NULL) delete weights1;
1369  idDelete(&s_h4);
1370
1371  for (i=0;i<IDELEMS(s_h3);i++)
1372  {
1373    if ((s_h3->m[i]!=NULL) && (pGetComp(s_h3->m[i])>=kmax))
1374    {
1375      if (resultIsIdeal)
1376        p_Shift(&s_h3->m[i],-kmax,currRing);
1377      else
1378        p_Shift(&s_h3->m[i],-kmax+1,currRing);
1379    }
1380    else
1381      p_Delete(&s_h3->m[i],currRing);
1382  }
1383  if (resultIsIdeal)
1384    s_h3->rank = 1;
1385  else
1386    s_h3->rank = h1->rank;
1387  if(syz_ring!=orig_ring)
1388  {
1389    rChangeCurrRing(orig_ring);
1390    s_h3 = idrMoveR_NoSort(s_h3, syz_ring, orig_ring);
1391    rDelete(syz_ring);
1392  }
1393  idSkipZeroes(s_h3);
1394  idTest(s_h3);
1395  return s_h3;
1396}
1397
1398/*2
1399* eliminate delVar (product of vars) in h1
1400*/
1401ideal idElimination (ideal h1,poly delVar,intvec *hilb)
1402{
1403  int    i,j=0,k,l;
1404  ideal  h,hh, h3;
1405  int    *ord,*block0,*block1;
1406  int    ordersize=2;
1407  int    **wv;
1408  tHomog hom;
1409  intvec * w;
1410  ring tmpR;
1411  ring origR = currRing;
1412
1413  if (delVar==NULL)
1414  {
1415    return idCopy(h1);
1416  }
1417  if ((currRing->qideal!=NULL) && rIsPluralRing(origR))
1418  {
1419    WerrorS("cannot eliminate in a qring");
1420    return NULL;
1421  }
1422  if (idIs0(h1)) return idInit(1,h1->rank);
1423#ifdef HAVE_PLURAL
1424  if (rIsPluralRing(origR))
1425    /* in the NC case, we have to check the admissibility of */
1426    /* the subalgebra to be intersected with */
1427  {
1428    if ((ncRingType(origR) != nc_skew) && (ncRingType(origR) != nc_exterior)) /* in (quasi)-commutative algebras every subalgebra is admissible */
1429    {
1430      if (nc_CheckSubalgebra(delVar,origR))
1431      {
1432        WerrorS("no elimination is possible: subalgebra is not admissible");
1433        return NULL;
1434      }
1435    }
1436  }
1437#endif
1438  hom=(tHomog)idHomModule(h1,NULL,&w); //sets w to weight vector or NULL
1439  h3=idInit(16,h1->rank);
1440  for (k=0;; k++)
1441  {
1442    if (origR->order[k]!=0) ordersize++;
1443    else break;
1444  }
1445#if 0
1446  if (rIsPluralRing(origR)) // we have too keep the odering: it may be needed
1447                            // for G-algebra
1448  {
1449    for (k=0;k<ordersize-1; k++)
1450    {
1451      block0[k+1] = origR->block0[k];
1452      block1[k+1] = origR->block1[k];
1453      ord[k+1] = origR->order[k];
1454      if (origR->wvhdl[k]!=NULL) wv[k+1] = (int*) omMemDup(origR->wvhdl[k]);
1455    }
1456  }
1457  else
1458  {
1459    block0[1] = 1;
1460    block1[1] = (currRing->N);
1461    if (origR->OrdSgn==1) ord[1] = ringorder_wp;
1462    else                  ord[1] = ringorder_ws;
1463    wv[1]=(int*)omAlloc0((currRing->N)*sizeof(int));
1464    double wNsqr = (double)2.0 / (double)(currRing->N);
1465    wFunctional = wFunctionalBuch;
1466    int  *x= (int * )omAlloc(2 * ((currRing->N) + 1) * sizeof(int));
1467    int sl=IDELEMS(h1) - 1;
1468    wCall(h1->m, sl, x, wNsqr);
1469    for (sl = (currRing->N); sl!=0; sl--)
1470      wv[1][sl-1] = x[sl + (currRing->N) + 1];
1471    omFreeSize((ADDRESS)x, 2 * ((currRing->N) + 1) * sizeof(int));
1472
1473    ord[2]=ringorder_C;
1474    ord[3]=0;
1475  }
1476#else
1477#endif
1478  if ((hom==TRUE) && (origR->OrdSgn==1) && (!rIsPluralRing(origR)))
1479  {
1480    #if 1
1481    // we change to an ordering:
1482    // aa(1,1,1,...,0,0,0),wp(...),C
1483    // this seems to be better than version 2 below,
1484    // according to Tst/../elimiate_[3568].tat (- 17 %)
1485    ord=(int*)omAlloc0(4*sizeof(int));
1486    block0=(int*)omAlloc0(4*sizeof(int));
1487    block1=(int*)omAlloc0(4*sizeof(int));
1488    wv=(int**) omAlloc0(4*sizeof(int**));
1489    block0[0] = block0[1] = 1;
1490    block1[0] = block1[1] = rVar(origR);
1491    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1492    // use this special ordering: like ringorder_a, except that pFDeg, pWeights
1493    // ignore it
1494    ord[0] = ringorder_aa;
1495    for (j=0;j<rVar(origR);j++)
1496      if (pGetExp(delVar,j+1)!=0) wv[0][j]=1;
1497    BOOLEAN wp=FALSE;
1498    for (j=0;j<rVar(origR);j++)
1499      if (pWeight(j+1,origR)!=1) { wp=TRUE;break; }
1500    if (wp)
1501    {
1502      wv[1]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1503      for (j=0;j<rVar(origR);j++)
1504        wv[1][j]=pWeight(j+1,origR);
1505      ord[1] = ringorder_wp;
1506    }
1507    else
1508      ord[1] = ringorder_dp;
1509    #else
1510    // we change to an ordering:
1511    // a(w1,...wn),wp(1,...0.....),C
1512    ord=(int*)omAlloc0(4*sizeof(int));
1513    block0=(int*)omAlloc0(4*sizeof(int));
1514    block1=(int*)omAlloc0(4*sizeof(int));
1515    wv=(int**) omAlloc0(4*sizeof(int**));
1516    block0[0] = block0[1] = 1;
1517    block1[0] = block1[1] = rVar(origR);
1518    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1519    wv[1]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1520    ord[0] = ringorder_a;
1521    for (j=0;j<rVar(origR);j++)
1522      wv[0][j]=pWeight(j+1,origR);
1523    ord[1] = ringorder_wp;
1524    for (j=0;j<rVar(origR);j++)
1525      if (pGetExp(delVar,j+1)!=0) wv[1][j]=1;
1526    #endif
1527    ord[2] = ringorder_C;
1528    ord[3] = 0;
1529  }
1530  else
1531  {
1532    // we change to an ordering:
1533    // aa(....),orig_ordering
1534    ord=(int*)omAlloc0(ordersize*sizeof(int));
1535    block0=(int*)omAlloc0(ordersize*sizeof(int));
1536    block1=(int*)omAlloc0(ordersize*sizeof(int));
1537    wv=(int**) omAlloc0(ordersize*sizeof(int**));
1538    for (k=0;k<ordersize-1; k++)
1539    {
1540      block0[k+1] = origR->block0[k];
1541      block1[k+1] = origR->block1[k];
1542      ord[k+1] = origR->order[k];
1543      if (origR->wvhdl[k]!=NULL) wv[k+1] = (int*) omMemDup(origR->wvhdl[k]);
1544    }
1545    block0[0] = 1;
1546    block1[0] = rVar(origR);
1547    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1548    for (j=0;j<rVar(origR);j++)
1549      if (pGetExp(delVar,j+1)!=0) wv[0][j]=1;
1550    // use this special ordering: like ringorder_a, except that pFDeg, pWeights
1551    // ignore it
1552    ord[0] = ringorder_aa;
1553  }
1554  // fill in tmp ring to get back the data later on
1555  tmpR  = rCopy0(origR,FALSE,FALSE); // qring==NULL
1556  //rUnComplete(tmpR);
1557  tmpR->p_Procs=NULL;
1558  tmpR->order = ord;
1559  tmpR->block0 = block0;
1560  tmpR->block1 = block1;
1561  tmpR->wvhdl = wv;
1562  rComplete(tmpR, 1);
1563
1564#ifdef HAVE_PLURAL
1565  /* update nc structure on tmpR */
1566  if (rIsPluralRing(origR))
1567  {
1568    if ( nc_rComplete(origR, tmpR, false) ) // no quotient ideal!
1569    {
1570      Werror("no elimination is possible: ordering condition is violated");
1571      // cleanup
1572      rDelete(tmpR);
1573      if (w!=NULL)
1574        delete w;
1575      return NULL;
1576    }
1577  }
1578#endif
1579  // change into the new ring
1580  //pChangeRing((currRing->N),currRing->OrdSgn,ord,block0,block1,wv);
1581  rChangeCurrRing(tmpR);
1582
1583  //h = idInit(IDELEMS(h1),h1->rank);
1584  // fetch data from the old ring
1585  //for (k=0;k<IDELEMS(h1);k++) h->m[k] = prCopyR( h1->m[k], origR);
1586  h=idrCopyR(h1,origR,currRing);
1587  if (origR->qideal!=NULL)
1588  {
1589    WarnS("eliminate in q-ring: experimental");
1590    ideal q=idrCopyR(origR->qideal,origR,currRing);
1591    ideal s=idSimpleAdd(h,q);
1592    idDelete(&h);
1593    idDelete(&q);
1594    h=s;
1595  }
1596  // compute kStd
1597#if 1
1598  //rWrite(tmpR);PrintLn();
1599  //BITSET save1;
1600  //SI_SAVE_OPT1(save1);
1601  //si_opt_1 |=1;
1602  //Print("h: %d gen, rk=%d\n",IDELEMS(h),h->rank);
1603  //extern char * showOption();
1604  //Print("%s\n",showOption());
1605  hh = kStd(h,NULL,hom,&w,hilb);
1606  //SI_RESTORE_OPT1(save1);
1607  idDelete(&h);
1608#else
1609  extern ideal kGroebner(ideal F, ideal Q);
1610  hh=kGroebner(h,NULL);
1611#endif
1612  // go back to the original ring
1613  rChangeCurrRing(origR);
1614  i = IDELEMS(hh)-1;
1615  while ((i >= 0) && (hh->m[i] == NULL)) i--;
1616  j = -1;
1617  // fetch data from temp ring
1618  for (k=0; k<=i; k++)
1619  {
1620    l=(currRing->N);
1621    while ((l>0) && (p_GetExp( hh->m[k],l,tmpR)*pGetExp(delVar,l)==0)) l--;
1622    if (l==0)
1623    {
1624      j++;
1625      if (j >= IDELEMS(h3))
1626      {
1627        pEnlargeSet(&(h3->m),IDELEMS(h3),16);
1628        IDELEMS(h3) += 16;
1629      }
1630      h3->m[j] = prMoveR( hh->m[k], tmpR,origR);
1631      hh->m[k] = NULL;
1632    }
1633  }
1634  id_Delete(&hh, tmpR);
1635  idSkipZeroes(h3);
1636  rDelete(tmpR);
1637  if (w!=NULL)
1638    delete w;
1639  return h3;
1640}
1641
1642#ifdef WITH_OLD_MINOR
1643/*2
1644* compute the which-th ar-minor of the matrix a
1645*/
1646poly idMinor(matrix a, int ar, unsigned long which, ideal R)
1647{
1648  int     i,j/*,k,size*/;
1649  unsigned long curr;
1650  int *rowchoise,*colchoise;
1651  BOOLEAN rowch,colch;
1652  // ideal result;
1653  matrix tmp;
1654  poly p,q;
1655
1656  i = binom(a->rows(),ar);
1657  j = binom(a->cols(),ar);
1658
1659  rowchoise=(int *)omAlloc(ar*sizeof(int));
1660  colchoise=(int *)omAlloc(ar*sizeof(int));
1661  // if ((i>512) || (j>512) || (i*j >512)) size=512;
1662  // else size=i*j;
1663  // result=idInit(size,1);
1664  tmp=mpNew(ar,ar);
1665  // k = 0; /* the index in result*/
1666  curr = 0; /* index of current minor */
1667  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
1668  while (!rowch)
1669  {
1670    idInitChoise(ar,1,a->cols(),&colch,colchoise);
1671    while (!colch)
1672    {
1673      if (curr == which)
1674      {
1675        for (i=1; i<=ar; i++)
1676        {
1677          for (j=1; j<=ar; j++)
1678          {
1679            MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1680          }
1681        }
1682        p = mp_DetBareiss(tmp,currRing);
1683        if (p!=NULL)
1684        {
1685          if (R!=NULL)
1686          {
1687            q = p;
1688            p = kNF(R,currRing->qideal,q);
1689            p_Delete(&q,currRing);
1690          }
1691          /*delete the matrix tmp*/
1692          for (i=1; i<=ar; i++)
1693          {
1694            for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1695          }
1696          idDelete((ideal*)&tmp);
1697          omFreeSize((ADDRESS)rowchoise,ar*sizeof(int));
1698          omFreeSize((ADDRESS)colchoise,ar*sizeof(int));
1699          return (p);
1700        }
1701      }
1702      curr++;
1703      idGetNextChoise(ar,a->cols(),&colch,colchoise);
1704    }
1705    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
1706  }
1707  return (poly) 1;
1708}
1709
1710/*2
1711* compute all ar-minors of the matrix a
1712*/
1713ideal idMinors(matrix a, int ar, ideal R)
1714{
1715  int     i,j,/*k,*/size;
1716  int *rowchoise,*colchoise;
1717  BOOLEAN rowch,colch;
1718  ideal result;
1719  matrix tmp;
1720  poly p,q;
1721
1722  i = binom(a->rows(),ar);
1723  j = binom(a->cols(),ar);
1724
1725  rowchoise=(int *)omAlloc(ar*sizeof(int));
1726  colchoise=(int *)omAlloc(ar*sizeof(int));
1727  if ((i>512) || (j>512) || (i*j >512)) size=512;
1728  else size=i*j;
1729  result=idInit(size,1);
1730  tmp=mpNew(ar,ar);
1731  // k = 0; /* the index in result*/
1732  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
1733  while (!rowch)
1734  {
1735    idInitChoise(ar,1,a->cols(),&colch,colchoise);
1736    while (!colch)
1737    {
1738      for (i=1; i<=ar; i++)
1739      {
1740        for (j=1; j<=ar; j++)
1741        {
1742          MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1743        }
1744      }
1745      p = mp_DetBareiss(tmp,vcurrRing);
1746      if (p!=NULL)
1747      {
1748        if (R!=NULL)
1749        {
1750          q = p;
1751          p = kNF(R,currRing->qideal,q);
1752          p_Delete(&q,currRing);
1753        }
1754        if (p!=NULL)
1755        {
1756          if (k>=size)
1757          {
1758            pEnlargeSet(&result->m,size,32);
1759            size += 32;
1760          }
1761          result->m[k] = p;
1762          k++;
1763        }
1764      }
1765      idGetNextChoise(ar,a->cols(),&colch,colchoise);
1766    }
1767    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
1768  }
1769  /*delete the matrix tmp*/
1770  for (i=1; i<=ar; i++)
1771  {
1772    for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1773  }
1774  idDelete((ideal*)&tmp);
1775  if (k==0)
1776  {
1777    k=1;
1778    result->m[0]=NULL;
1779  }
1780  omFreeSize((ADDRESS)rowchoise,ar*sizeof(int));
1781  omFreeSize((ADDRESS)colchoise,ar*sizeof(int));
1782  pEnlargeSet(&result->m,size,k-size);
1783  IDELEMS(result) = k;
1784  return (result);
1785}
1786#else
1787
1788
1789/// compute all ar-minors of the matrix a
1790/// the caller of mpRecMin
1791/// the elements of the result are not in R (if R!=NULL)
1792ideal idMinors(matrix a, int ar, ideal R)
1793{
1794
1795  const ring origR=currRing;
1796  id_Test((ideal)a, origR);
1797
1798  const int r = a->nrows;
1799  const int c = a->ncols;
1800
1801  if((ar<=0) || (ar>r) || (ar>c))
1802  {
1803    Werror("%d-th minor, matrix is %dx%d",ar,r,c);
1804    return NULL;
1805  }
1806
1807  ideal h = id_Matrix2Module(mp_Copy(a,origR),origR);
1808  long bound = sm_ExpBound(h,c,r,ar,origR);
1809  id_Delete(&h, origR);
1810
1811  ring tmpR = sm_RingChange(origR,bound);
1812
1813  matrix b = mpNew(r,c);
1814
1815  for (int i=r*c-1;i>=0;i--)
1816    if (a->m[i] != NULL)
1817      b->m[i] = prCopyR(a->m[i],origR,tmpR);
1818
1819  id_Test( (ideal)b, tmpR);
1820
1821  if (R!=NULL)
1822  {
1823    R = idrCopyR(R,origR,tmpR); // TODO: overwrites R? memory leak?
1824    //if (ar>1) // otherwise done in mpMinorToResult
1825    //{
1826    //  matrix bb=(matrix)kNF(R,currRing->qideal,(ideal)b);
1827    //  bb->rank=b->rank; bb->nrows=b->nrows; bb->ncols=b->ncols;
1828    //  idDelete((ideal*)&b); b=bb;
1829    //}
1830    id_Test( R, tmpR);
1831  }
1832
1833
1834  ideal result = idInit(32,1);
1835
1836  int elems = 0;
1837
1838  if(ar>1)
1839    mp_RecMin(ar-1,result,elems,b,r,c,NULL,R,tmpR);
1840  else
1841    mp_MinorToResult(result,elems,b,r,c,R,tmpR);
1842
1843  id_Test( (ideal)b, tmpR);
1844
1845  id_Delete((ideal *)&b, tmpR);
1846
1847  if (R!=NULL) idDelete(&R);
1848
1849  idSkipZeroes(result);
1850  rChangeCurrRing(origR);
1851  result = idrMoveR(result,tmpR,origR);
1852  sm_KillModifiedRing(tmpR);
1853  idTest(result);
1854  return result;
1855}
1856#endif
1857
1858/*2
1859*returns TRUE if id1 is a submodule of id2
1860*/
1861BOOLEAN idIsSubModule(ideal id1,ideal id2)
1862{
1863  int i;
1864  poly p;
1865
1866  if (idIs0(id1)) return TRUE;
1867  for (i=0;i<IDELEMS(id1);i++)
1868  {
1869    if (id1->m[i] != NULL)
1870    {
1871      p = kNF(id2,currRing->qideal,id1->m[i]);
1872      if (p != NULL)
1873      {
1874        p_Delete(&p,currRing);
1875        return FALSE;
1876      }
1877    }
1878  }
1879  return TRUE;
1880}
1881
1882BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
1883{
1884  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
1885  if (idIs0(m)) return TRUE;
1886
1887  int cmax=-1;
1888  int i;
1889  poly p=NULL;
1890  int length=IDELEMS(m);
1891  polyset P=m->m;
1892  for (i=length-1;i>=0;i--)
1893  {
1894    p=P[i];
1895    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
1896  }
1897  if (w != NULL)
1898  if (w->length()+1 < cmax)
1899  {
1900    // Print("length: %d - %d \n", w->length(),cmax);
1901    return FALSE;
1902  }
1903
1904  if(w!=NULL)
1905    p_SetModDeg(w, currRing);
1906
1907  for (i=length-1;i>=0;i--)
1908  {
1909    p=P[i];
1910    if (p!=NULL)
1911    {
1912      int d=currRing->pFDeg(p,currRing);
1913      loop
1914      {
1915        pIter(p);
1916        if (p==NULL) break;
1917        if (d!=currRing->pFDeg(p,currRing))
1918        {
1919          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
1920          if(w!=NULL)
1921            p_SetModDeg(NULL, currRing);
1922          return FALSE;
1923        }
1924      }
1925    }
1926  }
1927
1928  if(w!=NULL)
1929    p_SetModDeg(NULL, currRing);
1930
1931  return TRUE;
1932}
1933
1934ideal idSeries(int n,ideal M,matrix U,intvec *w)
1935{
1936  for(int i=IDELEMS(M)-1;i>=0;i--)
1937  {
1938    if(U==NULL)
1939      M->m[i]=pSeries(n,M->m[i],NULL,w);
1940    else
1941    {
1942      M->m[i]=pSeries(n,M->m[i],MATELEM(U,i+1,i+1),w);
1943      MATELEM(U,i+1,i+1)=NULL;
1944    }
1945  }
1946  if(U!=NULL)
1947    idDelete((ideal*)&U);
1948  return M;
1949}
1950
1951matrix idDiff(matrix i, int k)
1952{
1953  int e=MATCOLS(i)*MATROWS(i);
1954  matrix r=mpNew(MATROWS(i),MATCOLS(i));
1955  r->rank=i->rank;
1956  int j;
1957  for(j=0; j<e; j++)
1958  {
1959    r->m[j]=pDiff(i->m[j],k);
1960  }
1961  return r;
1962}
1963
1964matrix idDiffOp(ideal I, ideal J,BOOLEAN multiply)
1965{
1966  matrix r=mpNew(IDELEMS(I),IDELEMS(J));
1967  int i,j;
1968  for(i=0; i<IDELEMS(I); i++)
1969  {
1970    for(j=0; j<IDELEMS(J); j++)
1971    {
1972      MATELEM(r,i+1,j+1)=pDiffOp(I->m[i],J->m[j],multiply);
1973    }
1974  }
1975  return r;
1976}
1977
1978/*3
1979*handles for some ideal operations the ring/syzcomp managment
1980*returns all syzygies (componentwise-)shifted by -syzcomp
1981*or -syzcomp-1 (in case of ideals as input)
1982static ideal idHandleIdealOp(ideal arg,int syzcomp,int isIdeal=FALSE)
1983{
1984  ring orig_ring=currRing;
1985  ring syz_ring=rAssure_SyzComp(orig_ring, TRUE); rChangeCurrRing(syz_ring);
1986  rSetSyzComp(length, syz_ring);
1987
1988  ideal s_temp;
1989  if (orig_ring!=syz_ring)
1990    s_temp=idrMoveR_NoSort(arg,orig_ring, syz_ring);
1991  else
1992    s_temp=arg;
1993
1994  ideal s_temp1 = kStd(s_temp,currRing->qideal,testHomog,&w,NULL,length);
1995  if (w!=NULL) delete w;
1996
1997  if (syz_ring!=orig_ring)
1998  {
1999    idDelete(&s_temp);
2000    rChangeCurrRing(orig_ring);
2001  }
2002
2003  idDelete(&temp);
2004  ideal temp1=idRingCopy(s_temp1,syz_ring);
2005
2006  if (syz_ring!=orig_ring)
2007  {
2008    rChangeCurrRing(syz_ring);
2009    idDelete(&s_temp1);
2010    rChangeCurrRing(orig_ring);
2011    rDelete(syz_ring);
2012  }
2013
2014  for (i=0;i<IDELEMS(temp1);i++)
2015  {
2016    if ((temp1->m[i]!=NULL)
2017    && (pGetComp(temp1->m[i])<=length))
2018    {
2019      pDelete(&(temp1->m[i]));
2020    }
2021    else
2022    {
2023      p_Shift(&(temp1->m[i]),-length,currRing);
2024    }
2025  }
2026  temp1->rank = rk;
2027  idSkipZeroes(temp1);
2028
2029  return temp1;
2030}
2031*/
2032/*2
2033* represents (h1+h2)/h2=h1/(h1 intersect h2)
2034*/
2035//ideal idModulo (ideal h2,ideal h1)
2036ideal idModulo (ideal h2,ideal h1, tHomog hom, intvec ** w)
2037{
2038  intvec *wtmp=NULL;
2039
2040  int i,k,rk,flength=0,slength,length;
2041  poly p,q;
2042
2043  if (idIs0(h2))
2044    return idFreeModule(si_max(1,h2->ncols));
2045  if (!idIs0(h1))
2046    flength = id_RankFreeModule(h1,currRing);
2047  slength = id_RankFreeModule(h2,currRing);
2048  length  = si_max(flength,slength);
2049  if (length==0)
2050  {
2051    length = 1;
2052  }
2053  ideal temp = idInit(IDELEMS(h2),length+IDELEMS(h2));
2054  if ((w!=NULL)&&((*w)!=NULL))
2055  {
2056    //Print("input weights:");(*w)->show(1);PrintLn();
2057    int d;
2058    int k;
2059    wtmp=new intvec(length+IDELEMS(h2));
2060    for (i=0;i<length;i++)
2061      ((*wtmp)[i])=(**w)[i];
2062    for (i=0;i<IDELEMS(h2);i++)
2063    {
2064      poly p=h2->m[i];
2065      if (p!=NULL)
2066      {
2067        d = p_Deg(p,currRing);
2068        k= pGetComp(p);
2069        if (slength>0) k--;
2070        d +=((**w)[k]);
2071        ((*wtmp)[i+length]) = d;
2072      }
2073    }
2074    //Print("weights:");wtmp->show(1);PrintLn();
2075  }
2076  for (i=0;i<IDELEMS(h2);i++)
2077  {
2078    temp->m[i] = pCopy(h2->m[i]);
2079    q = pOne();
2080    pSetComp(q,i+1+length);
2081    pSetmComp(q);
2082    if(temp->m[i]!=NULL)
2083    {
2084      if (slength==0) p_Shift(&(temp->m[i]),1,currRing);
2085      p = temp->m[i];
2086      while (pNext(p)!=NULL) pIter(p);
2087      pNext(p) = q; // will be sorted later correctly
2088    }
2089    else
2090      temp->m[i]=q;
2091  }
2092  rk = k = IDELEMS(h2);
2093  if (!idIs0(h1))
2094  {
2095    pEnlargeSet(&(temp->m),IDELEMS(temp),IDELEMS(h1));
2096    IDELEMS(temp) += IDELEMS(h1);
2097    for (i=0;i<IDELEMS(h1);i++)
2098    {
2099      if (h1->m[i]!=NULL)
2100      {
2101        temp->m[k] = pCopy(h1->m[i]);
2102        if (flength==0) p_Shift(&(temp->m[k]),1,currRing);
2103        k++;
2104      }
2105    }
2106  }
2107
2108  ring orig_ring=currRing;
2109  ring syz_ring=rAssure_SyzComp(orig_ring, TRUE); rChangeCurrRing(syz_ring);
2110  // we can use OPT_RETURN_SB only, if syz_ring==orig_ring,
2111  // therefore we disable OPT_RETURN_SB for modulo:
2112  // (see tr. #701)
2113  //if (TEST_OPT_RETURN_SB)
2114  //  rSetSyzComp(IDELEMS(h2)+length, syz_ring);
2115  //else
2116    rSetSyzComp(length, syz_ring);
2117  ideal s_temp;
2118
2119  if (syz_ring != orig_ring)
2120  {
2121    s_temp = idrMoveR_NoSort(temp, orig_ring, syz_ring);
2122  }
2123  else
2124  {
2125    s_temp = temp;
2126  }
2127
2128  idTest(s_temp);
2129  ideal s_temp1 = kStd(s_temp,currRing->qideal,hom,&wtmp,NULL,length);
2130
2131  //if (wtmp!=NULL)  Print("output weights:");wtmp->show(1);PrintLn();
2132  if ((w!=NULL) && (*w !=NULL) && (wtmp!=NULL))
2133  {
2134    delete *w;
2135    *w=new intvec(IDELEMS(h2));
2136    for (i=0;i<IDELEMS(h2);i++)
2137      ((**w)[i])=(*wtmp)[i+length];
2138  }
2139  if (wtmp!=NULL) delete wtmp;
2140
2141  for (i=0;i<IDELEMS(s_temp1);i++)
2142  {
2143    if ((s_temp1->m[i]!=NULL)
2144    && (((int)pGetComp(s_temp1->m[i]))<=length))
2145    {
2146      p_Delete(&(s_temp1->m[i]),currRing);
2147    }
2148    else
2149    {
2150      p_Shift(&(s_temp1->m[i]),-length,currRing);
2151    }
2152  }
2153  s_temp1->rank = rk;
2154  idSkipZeroes(s_temp1);
2155
2156  if (syz_ring!=orig_ring)
2157  {
2158    rChangeCurrRing(orig_ring);
2159    s_temp1 = idrMoveR_NoSort(s_temp1, syz_ring, orig_ring);
2160    rDelete(syz_ring);
2161    // Hmm ... here seems to be a memory leak
2162    // However, simply deleting it causes memory trouble
2163    // idDelete(&s_temp);
2164  }
2165  else
2166  {
2167    idDelete(&temp);
2168  }
2169  idTest(s_temp1);
2170  return s_temp1;
2171}
2172
2173/*
2174*computes module-weights for liftings of homogeneous modules
2175*/
2176intvec * idMWLift(ideal mod,intvec * weights)
2177{
2178  if (idIs0(mod)) return new intvec(2);
2179  int i=IDELEMS(mod);
2180  while ((i>0) && (mod->m[i-1]==NULL)) i--;
2181  intvec *result = new intvec(i+1);
2182  while (i>0)
2183  {
2184    (*result)[i]=currRing->pFDeg(mod->m[i],currRing)+(*weights)[pGetComp(mod->m[i])];
2185  }
2186  return result;
2187}
2188
2189/*2
2190*sorts the kbase for idCoef* in a special way (lexicographically
2191*with x_max,...,x_1)
2192*/
2193ideal idCreateSpecialKbase(ideal kBase,intvec ** convert)
2194{
2195  int i;
2196  ideal result;
2197
2198  if (idIs0(kBase)) return NULL;
2199  result = idInit(IDELEMS(kBase),kBase->rank);
2200  *convert = idSort(kBase,FALSE);
2201  for (i=0;i<(*convert)->length();i++)
2202  {
2203    result->m[i] = pCopy(kBase->m[(**convert)[i]-1]);
2204  }
2205  return result;
2206}
2207
2208/*2
2209*returns the index of a given monom in the list of the special kbase
2210*/
2211int idIndexOfKBase(poly monom, ideal kbase)
2212{
2213  int j=IDELEMS(kbase);
2214
2215  while ((j>0) && (kbase->m[j-1]==NULL)) j--;
2216  if (j==0) return -1;
2217  int i=(currRing->N);
2218  while (i>0)
2219  {
2220    loop
2221    {
2222      if (pGetExp(monom,i)>pGetExp(kbase->m[j-1],i)) return -1;
2223      if (pGetExp(monom,i)==pGetExp(kbase->m[j-1],i)) break;
2224      j--;
2225      if (j==0) return -1;
2226    }
2227    if (i==1)
2228    {
2229      while(j>0)
2230      {
2231        if (pGetComp(monom)==pGetComp(kbase->m[j-1])) return j-1;
2232        if (pGetComp(monom)>pGetComp(kbase->m[j-1])) return -1;
2233        j--;
2234      }
2235    }
2236    i--;
2237  }
2238  return -1;
2239}
2240
2241/*2
2242*decomposes the monom in a part of coefficients described by the
2243*complement of how and a monom in variables occuring in how, the
2244*index of which in kbase is returned as integer pos (-1 if it don't
2245*exists)
2246*/
2247poly idDecompose(poly monom, poly how, ideal kbase, int * pos)
2248{
2249  int i;
2250  poly coeff=pOne(), base=pOne();
2251
2252  for (i=1;i<=(currRing->N);i++)
2253  {
2254    if (pGetExp(how,i)>0)
2255    {
2256      pSetExp(base,i,pGetExp(monom,i));
2257    }
2258    else
2259    {
2260      pSetExp(coeff,i,pGetExp(monom,i));
2261    }
2262  }
2263  pSetComp(base,pGetComp(monom));
2264  pSetm(base);
2265  pSetCoeff(coeff,nCopy(pGetCoeff(monom)));
2266  pSetm(coeff);
2267  *pos = idIndexOfKBase(base,kbase);
2268  if (*pos<0)
2269    p_Delete(&coeff,currRing);
2270  p_Delete(&base,currRing);
2271  return coeff;
2272}
2273
2274/*2
2275*returns a matrix A of coefficients with kbase*A=arg
2276*if all monomials in variables of how occur in kbase
2277*the other are deleted
2278*/
2279matrix idCoeffOfKBase(ideal arg, ideal kbase, poly how)
2280{
2281  matrix result;
2282  ideal tempKbase;
2283  poly p,q;
2284  intvec * convert;
2285  int i=IDELEMS(kbase),j=IDELEMS(arg),k,pos;
2286#if 0
2287  while ((i>0) && (kbase->m[i-1]==NULL)) i--;
2288  if (idIs0(arg))
2289    return mpNew(i,1);
2290  while ((j>0) && (arg->m[j-1]==NULL)) j--;
2291  result = mpNew(i,j);
2292#else
2293  result = mpNew(i, j);
2294  while ((j>0) && (arg->m[j-1]==NULL)) j--;
2295#endif
2296
2297  tempKbase = idCreateSpecialKbase(kbase,&convert);
2298  for (k=0;k<j;k++)
2299  {
2300    p = arg->m[k];
2301    while (p!=NULL)
2302    {
2303      q = idDecompose(p,how,tempKbase,&pos);
2304      if (pos>=0)
2305      {
2306        MATELEM(result,(*convert)[pos],k+1) =
2307            pAdd(MATELEM(result,(*convert)[pos],k+1),q);
2308      }
2309      else
2310        p_Delete(&q,currRing);
2311      pIter(p);
2312    }
2313  }
2314  idDelete(&tempKbase);
2315  return result;
2316}
2317
2318static void idDeleteComps(ideal arg,int* red_comp,int del)
2319// red_comp is an array [0..args->rank]
2320{
2321  int i,j;
2322  poly p;
2323
2324  for (i=IDELEMS(arg)-1;i>=0;i--)
2325  {
2326    p = arg->m[i];
2327    while (p!=NULL)
2328    {
2329      j = pGetComp(p);
2330      if (red_comp[j]!=j)
2331      {
2332        pSetComp(p,red_comp[j]);
2333        pSetmComp(p);
2334      }
2335      pIter(p);
2336    }
2337  }
2338  (arg->rank) -= del;
2339}
2340
2341/*2
2342* returns the presentation of an isomorphic, minimally
2343* embedded  module (arg represents the quotient!)
2344*/
2345ideal idMinEmbedding(ideal arg,BOOLEAN inPlace, intvec **w)
2346{
2347  if (idIs0(arg)) return idInit(1,arg->rank);
2348  int i,next_gen,next_comp;
2349  ideal res=arg;
2350  if (!inPlace) res = idCopy(arg);
2351  res->rank=si_max(res->rank,id_RankFreeModule(res,currRing));
2352  int *red_comp=(int*)omAlloc((res->rank+1)*sizeof(int));
2353  for (i=res->rank;i>=0;i--) red_comp[i]=i;
2354
2355  int del=0;
2356  loop
2357  {
2358    next_gen = id_ReadOutPivot(res, &next_comp, currRing);
2359    if (next_gen<0) break;
2360    del++;
2361    syGaussForOne(res,next_gen,next_comp,0,IDELEMS(res));
2362    for(i=next_comp+1;i<=arg->rank;i++) red_comp[i]--;
2363    if ((w !=NULL)&&(*w!=NULL))
2364    {
2365      for(i=next_comp;i<(*w)->length();i++) (**w)[i-1]=(**w)[i];
2366    }
2367  }
2368
2369  idDeleteComps(res,red_comp,del);
2370  idSkipZeroes(res);
2371  omFree(red_comp);
2372
2373  if ((w !=NULL)&&(*w!=NULL) &&(del>0))
2374  {
2375    int nl=si_max((*w)->length()-del,1);
2376    intvec *wtmp=new intvec(nl);
2377    for(i=0;i<res->rank;i++) (*wtmp)[i]=(**w)[i];
2378    delete *w;
2379    *w=wtmp;
2380  }
2381  return res;
2382}
2383
2384#include <polys/clapsing.h>
2385
2386#if 0
2387poly id_GCD(poly f, poly g, const ring r)
2388{
2389  ring save_r=currRing;
2390  rChangeCurrRing(r);
2391  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2392  intvec *w = NULL;
2393  ideal S=idSyzygies(I,testHomog,&w);
2394  if (w!=NULL) delete w;
2395  poly gg=pTakeOutComp(&(S->m[0]),2);
2396  idDelete(&S);
2397  poly gcd_p=singclap_pdivide(f,gg,r);
2398  p_Delete(&gg,r);
2399  rChangeCurrRing(save_r);
2400  return gcd_p;
2401}
2402#else
2403poly id_GCD(poly f, poly g, const ring r)
2404{
2405  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2406  intvec *w = NULL;
2407
2408  ring save_r = currRing; rChangeCurrRing(r); ideal S=idSyzygies(I,testHomog,&w); rChangeCurrRing(save_r);
2409
2410  if (w!=NULL) delete w;
2411  poly gg=p_TakeOutComp(&(S->m[0]), 2, r);
2412  id_Delete(&S, r);
2413  poly gcd_p=singclap_pdivide(f,gg, r);
2414  p_Delete(&gg, r);
2415
2416  return gcd_p;
2417}
2418#endif
2419
2420#if 0
2421/*2
2422* xx,q: arrays of length 0..rl-1
2423* xx[i]: SB mod q[i]
2424* assume: char=0
2425* assume: q[i]!=0
2426* destroys xx
2427*/
2428ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring R)
2429{
2430  int cnt=IDELEMS(xx[0])*xx[0]->nrows;
2431  ideal result=idInit(cnt,xx[0]->rank);
2432  result->nrows=xx[0]->nrows; // for lifting matrices
2433  result->ncols=xx[0]->ncols; // for lifting matrices
2434  int i,j;
2435  poly r,h,hh,res_p;
2436  number *x=(number *)omAlloc(rl*sizeof(number));
2437  for(i=cnt-1;i>=0;i--)
2438  {
2439    res_p=NULL;
2440    loop
2441    {
2442      r=NULL;
2443      for(j=rl-1;j>=0;j--)
2444      {
2445        h=xx[j]->m[i];
2446        if ((h!=NULL)
2447        &&((r==NULL)||(p_LmCmp(r,h,R)==-1)))
2448          r=h;
2449      }
2450      if (r==NULL) break;
2451      h=p_Head(r, R);
2452      for(j=rl-1;j>=0;j--)
2453      {
2454        hh=xx[j]->m[i];
2455        if ((hh!=NULL) && (p_LmCmp(r,hh, R)==0))
2456        {
2457          x[j]=p_GetCoeff(hh, R);
2458          hh=p_LmFreeAndNext(hh, R);
2459          xx[j]->m[i]=hh;
2460        }
2461        else
2462          x[j]=n_Init(0, R->cf); // is R->cf really n_Q???, yes!
2463      }
2464
2465      number n=n_ChineseRemainder(x,q,rl, R->cf);
2466
2467      for(j=rl-1;j>=0;j--)
2468      {
2469        x[j]=NULL; // nlInit(0...) takes no memory
2470      }
2471      if (n_IsZero(n, R->cf)) p_Delete(&h, R);
2472      else
2473      {
2474        p_SetCoeff(h,n, R);
2475        //Print("new mon:");pWrite(h);
2476        res_p=p_Add_q(res_p, h, R);
2477      }
2478    }
2479    result->m[i]=res_p;
2480  }
2481  omFree(x);
2482  for(i=rl-1;i>=0;i--) id_Delete(&(xx[i]), R);
2483  omFree(xx);
2484  return result;
2485}
2486#endif
2487/* currently unsed:
2488ideal idChineseRemainder(ideal *xx, intvec *iv)
2489{
2490  int rl=iv->length();
2491  number *q=(number *)omAlloc(rl*sizeof(number));
2492  int i;
2493  for(i=0; i<rl; i++)
2494  {
2495    q[i]=nInit((*iv)[i]);
2496  }
2497  return idChineseRemainder(xx,q,rl);
2498}
2499*/
2500/*
2501 * lift ideal with coeffs over Z (mod N) to Q via Farey
2502 */
2503ideal id_Farey(ideal x, number N, const ring r)
2504{
2505  int cnt=IDELEMS(x)*x->nrows;
2506  ideal result=idInit(cnt,x->rank);
2507  result->nrows=x->nrows; // for lifting matrices
2508  result->ncols=x->ncols; // for lifting matrices
2509
2510  int i;
2511  for(i=cnt-1;i>=0;i--)
2512  {
2513    result->m[i]=p_Farey(x->m[i],N,r);
2514  }
2515  return result;
2516}
2517
2518
2519
2520
2521// uses glabl vars via pSetModDeg
2522/*
2523BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
2524{
2525  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
2526  if (idIs0(m)) return TRUE;
2527
2528  int cmax=-1;
2529  int i;
2530  poly p=NULL;
2531  int length=IDELEMS(m);
2532  poly* P=m->m;
2533  for (i=length-1;i>=0;i--)
2534  {
2535    p=P[i];
2536    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
2537  }
2538  if (w != NULL)
2539  if (w->length()+1 < cmax)
2540  {
2541    // Print("length: %d - %d \n", w->length(),cmax);
2542    return FALSE;
2543  }
2544
2545  if(w!=NULL)
2546    p_SetModDeg(w, currRing);
2547
2548  for (i=length-1;i>=0;i--)
2549  {
2550    p=P[i];
2551    poly q=p;
2552    if (p!=NULL)
2553    {
2554      int d=p_FDeg(p,currRing);
2555      loop
2556      {
2557        pIter(p);
2558        if (p==NULL) break;
2559        if (d!=p_FDeg(p,currRing))
2560        {
2561          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
2562          if(w!=NULL)
2563            p_SetModDeg(NULL, currRing);
2564          return FALSE;
2565        }
2566      }
2567    }
2568  }
2569
2570  if(w!=NULL)
2571    p_SetModDeg(NULL, currRing);
2572
2573  return TRUE;
2574}
2575*/
2576
2577/// keeps the first k (>= 1) entries of the given ideal
2578/// (Note that the kept polynomials may be zero.)
2579void idKeepFirstK(ideal id, const int k)
2580{
2581   for (int i = IDELEMS(id)-1; i >= k; i--)
2582   {
2583      if (id->m[i] != NULL) pDelete(&id->m[i]);
2584   }
2585   int kk=k;
2586   if (k==0) kk=1; /* ideals must have at least one element(0)*/
2587   pEnlargeSet(&(id->m), IDELEMS(id), kk-IDELEMS(id));
2588   IDELEMS(id) = kk;
2589}
2590
2591/*
2592* compare the leading terms of a and b
2593*/
2594static int tCompare(const poly a, const poly b)
2595{
2596  if (b == NULL) return(a != NULL);
2597  if (a == NULL) return(-1);
2598
2599  /* a != NULL && b != NULL */
2600  int r = pLmCmp(a, b);
2601  if (r != 0) return(r);
2602  number h = nSub(pGetCoeff(a), pGetCoeff(b));
2603  r = -1 + nIsZero(h) + 2*nGreaterZero(h);   /* -1: <, 0:==, 1: > */
2604  nDelete(&h);
2605  return(r);
2606}
2607
2608/*
2609* compare a and b (rev-lex on terms)
2610*/
2611static int pCompare(const poly a, const poly b)
2612{
2613  int r = tCompare(a, b);
2614  if (r != 0) return(r);
2615
2616  poly aa = a;
2617  poly bb = b;
2618  while (r == 0 && aa != NULL && bb != NULL)
2619  {
2620    pIter(aa);
2621    pIter(bb);
2622    r = tCompare(aa, bb);
2623  }
2624  return(r);
2625}
2626
2627typedef struct
2628{
2629  poly p;
2630  int index;
2631} poly_sort;
2632
2633int pCompare_qsort(const void *a, const void *b)
2634{
2635  int res = pCompare(((poly_sort *)a)->p, ((poly_sort *)b)->p);
2636  return(res);
2637}
2638
2639void idSort_qsort(poly_sort *id_sort, int idsize)
2640{
2641  qsort(id_sort, idsize, sizeof(poly_sort), pCompare_qsort);
2642}
2643
2644/*2
2645* ideal id = (id[i])
2646* if id[i] = id[j] then id[j] is deleted for j > i
2647*/
2648void idDelEquals(ideal id)
2649{
2650  int idsize = IDELEMS(id);
2651  poly_sort *id_sort = (poly_sort *)omAlloc0(idsize*sizeof(poly_sort));
2652  for (int i = 0; i < idsize; i++)
2653  {
2654    id_sort[i].p = id->m[i];
2655    id_sort[i].index = i;
2656  }
2657  idSort_qsort(id_sort, idsize);
2658  int index, index_i, index_j;
2659  int i = 0;
2660  for (int j = 1; j < idsize; j++)
2661  {
2662    if (id_sort[i].p != NULL && pEqualPolys(id_sort[i].p, id_sort[j].p))
2663    {
2664      index_i = id_sort[i].index;
2665      index_j = id_sort[j].index;
2666      if (index_j > index_i)
2667      {
2668        index = index_j;
2669      }
2670      else
2671      {
2672        index = index_i;
2673        i = j;
2674      }
2675      pDelete(&id->m[index]);
2676    }
2677    else
2678    {
2679      i = j;
2680    }
2681  }
2682  omFreeSize((ADDRESS)(id_sort), idsize*sizeof(poly_sort));
2683}
Note: See TracBrowser for help on using the repository browser.