source: git/kernel/ideals.cc @ 49b748

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