source: git/kernel/ideals.cc @ 9e8bfa

spielwiese
Last change on this file since 9e8bfa was 9e8bfa, checked in by Hans Schoenemann <hannes@…>, 9 years ago
allow matrix/module 0x0, 0xr, cx0
  • Property mode set to 100644
File size: 58.3 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/*2
1784* compute all ar-minors of the matrix a
1785* the caller of mpRecMin
1786* the elements of the result are not in R (if R!=NULL)
1787*/
1788ideal idMinors(matrix a, int ar, ideal R)
1789{
1790  int elems=0;
1791  int r=a->nrows,c=a->ncols;
1792  int i;
1793  matrix b;
1794  ideal result,h;
1795  ring origR=currRing;
1796  ring tmpR;
1797  long bound;
1798
1799  if((ar<=0) || (ar>r) || (ar>c))
1800  {
1801    Werror("%d-th minor, matrix is %dx%d",ar,r,c);
1802    return NULL;
1803  }
1804  h = id_Matrix2Module(mp_Copy(a,origR),origR);
1805  bound = sm_ExpBound(h,c,r,ar,origR);
1806  idDelete(&h);
1807  tmpR=sm_RingChange(origR,bound);
1808  b = mpNew(r,c);
1809  for (i=r*c-1;i>=0;i--)
1810  {
1811    if (a->m[i])
1812      b->m[i] = prCopyR(a->m[i],origR,tmpR);
1813  }
1814  if (R!=NULL)
1815  {
1816    R = idrCopyR(R,origR,tmpR);
1817    //if (ar>1) // otherwise done in mpMinorToResult
1818    //{
1819    //  matrix bb=(matrix)kNF(R,currRing->qideal,(ideal)b);
1820    //  bb->rank=b->rank; bb->nrows=b->nrows; bb->ncols=b->ncols;
1821    //  idDelete((ideal*)&b); b=bb;
1822    //}
1823  }
1824  result=idInit(32,1);
1825  if(ar>1) mp_RecMin(ar-1,result,elems,b,r,c,NULL,R,tmpR);
1826  else mp_MinorToResult(result,elems,b,r,c,R,tmpR);
1827  idDelete((ideal *)&b);
1828  if (R!=NULL) idDelete(&R);
1829  idSkipZeroes(result);
1830  rChangeCurrRing(origR);
1831  result = idrMoveR(result,tmpR,origR);
1832  sm_KillModifiedRing(tmpR);
1833  idTest(result);
1834  return result;
1835}
1836#endif
1837
1838/*2
1839*returns TRUE if id1 is a submodule of id2
1840*/
1841BOOLEAN idIsSubModule(ideal id1,ideal id2)
1842{
1843  int i;
1844  poly p;
1845
1846  if (idIs0(id1)) return TRUE;
1847  for (i=0;i<IDELEMS(id1);i++)
1848  {
1849    if (id1->m[i] != NULL)
1850    {
1851      p = kNF(id2,currRing->qideal,id1->m[i]);
1852      if (p != NULL)
1853      {
1854        p_Delete(&p,currRing);
1855        return FALSE;
1856      }
1857    }
1858  }
1859  return TRUE;
1860}
1861
1862BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
1863{
1864  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
1865  if (idIs0(m)) return TRUE;
1866
1867  int cmax=-1;
1868  int i;
1869  poly p=NULL;
1870  int length=IDELEMS(m);
1871  polyset P=m->m;
1872  for (i=length-1;i>=0;i--)
1873  {
1874    p=P[i];
1875    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
1876  }
1877  if (w != NULL)
1878  if (w->length()+1 < cmax)
1879  {
1880    // Print("length: %d - %d \n", w->length(),cmax);
1881    return FALSE;
1882  }
1883
1884  if(w!=NULL)
1885    p_SetModDeg(w, currRing);
1886
1887  for (i=length-1;i>=0;i--)
1888  {
1889    p=P[i];
1890    if (p!=NULL)
1891    {
1892      int d=currRing->pFDeg(p,currRing);
1893      loop
1894      {
1895        pIter(p);
1896        if (p==NULL) break;
1897        if (d!=currRing->pFDeg(p,currRing))
1898        {
1899          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
1900          if(w!=NULL)
1901            p_SetModDeg(NULL, currRing);
1902          return FALSE;
1903        }
1904      }
1905    }
1906  }
1907
1908  if(w!=NULL)
1909    p_SetModDeg(NULL, currRing);
1910
1911  return TRUE;
1912}
1913
1914ideal idSeries(int n,ideal M,matrix U,intvec *w)
1915{
1916  for(int i=IDELEMS(M)-1;i>=0;i--)
1917  {
1918    if(U==NULL)
1919      M->m[i]=pSeries(n,M->m[i],NULL,w);
1920    else
1921    {
1922      M->m[i]=pSeries(n,M->m[i],MATELEM(U,i+1,i+1),w);
1923      MATELEM(U,i+1,i+1)=NULL;
1924    }
1925  }
1926  if(U!=NULL)
1927    idDelete((ideal*)&U);
1928  return M;
1929}
1930
1931matrix idDiff(matrix i, int k)
1932{
1933  int e=MATCOLS(i)*MATROWS(i);
1934  matrix r=mpNew(MATROWS(i),MATCOLS(i));
1935  r->rank=i->rank;
1936  int j;
1937  for(j=0; j<e; j++)
1938  {
1939    r->m[j]=pDiff(i->m[j],k);
1940  }
1941  return r;
1942}
1943
1944matrix idDiffOp(ideal I, ideal J,BOOLEAN multiply)
1945{
1946  matrix r=mpNew(IDELEMS(I),IDELEMS(J));
1947  int i,j;
1948  for(i=0; i<IDELEMS(I); i++)
1949  {
1950    for(j=0; j<IDELEMS(J); j++)
1951    {
1952      MATELEM(r,i+1,j+1)=pDiffOp(I->m[i],J->m[j],multiply);
1953    }
1954  }
1955  return r;
1956}
1957
1958/*3
1959*handles for some ideal operations the ring/syzcomp managment
1960*returns all syzygies (componentwise-)shifted by -syzcomp
1961*or -syzcomp-1 (in case of ideals as input)
1962static ideal idHandleIdealOp(ideal arg,int syzcomp,int isIdeal=FALSE)
1963{
1964  ring orig_ring=currRing;
1965  ring syz_ring=rAssure_SyzComp(orig_ring, TRUE); rChangeCurrRing(syz_ring);
1966  rSetSyzComp(length, syz_ring);
1967
1968  ideal s_temp;
1969  if (orig_ring!=syz_ring)
1970    s_temp=idrMoveR_NoSort(arg,orig_ring, syz_ring);
1971  else
1972    s_temp=arg;
1973
1974  ideal s_temp1 = kStd(s_temp,currRing->qideal,testHomog,&w,NULL,length);
1975  if (w!=NULL) delete w;
1976
1977  if (syz_ring!=orig_ring)
1978  {
1979    idDelete(&s_temp);
1980    rChangeCurrRing(orig_ring);
1981  }
1982
1983  idDelete(&temp);
1984  ideal temp1=idRingCopy(s_temp1,syz_ring);
1985
1986  if (syz_ring!=orig_ring)
1987  {
1988    rChangeCurrRing(syz_ring);
1989    idDelete(&s_temp1);
1990    rChangeCurrRing(orig_ring);
1991    rDelete(syz_ring);
1992  }
1993
1994  for (i=0;i<IDELEMS(temp1);i++)
1995  {
1996    if ((temp1->m[i]!=NULL)
1997    && (pGetComp(temp1->m[i])<=length))
1998    {
1999      pDelete(&(temp1->m[i]));
2000    }
2001    else
2002    {
2003      p_Shift(&(temp1->m[i]),-length,currRing);
2004    }
2005  }
2006  temp1->rank = rk;
2007  idSkipZeroes(temp1);
2008
2009  return temp1;
2010}
2011*/
2012/*2
2013* represents (h1+h2)/h2=h1/(h1 intersect h2)
2014*/
2015//ideal idModulo (ideal h2,ideal h1)
2016ideal idModulo (ideal h2,ideal h1, tHomog hom, intvec ** w)
2017{
2018  intvec *wtmp=NULL;
2019
2020  int i,k,rk,flength=0,slength,length;
2021  poly p,q;
2022
2023  if (idIs0(h2))
2024    return idFreeModule(si_max(1,h2->ncols));
2025  if (!idIs0(h1))
2026    flength = id_RankFreeModule(h1,currRing);
2027  slength = id_RankFreeModule(h2,currRing);
2028  length  = si_max(flength,slength);
2029  if (length==0)
2030  {
2031    length = 1;
2032  }
2033  ideal temp = idInit(IDELEMS(h2),length+IDELEMS(h2));
2034  if ((w!=NULL)&&((*w)!=NULL))
2035  {
2036    //Print("input weights:");(*w)->show(1);PrintLn();
2037    int d;
2038    int k;
2039    wtmp=new intvec(length+IDELEMS(h2));
2040    for (i=0;i<length;i++)
2041      ((*wtmp)[i])=(**w)[i];
2042    for (i=0;i<IDELEMS(h2);i++)
2043    {
2044      poly p=h2->m[i];
2045      if (p!=NULL)
2046      {
2047        d = p_Deg(p,currRing);
2048        k= pGetComp(p);
2049        if (slength>0) k--;
2050        d +=((**w)[k]);
2051        ((*wtmp)[i+length]) = d;
2052      }
2053    }
2054    //Print("weights:");wtmp->show(1);PrintLn();
2055  }
2056  for (i=0;i<IDELEMS(h2);i++)
2057  {
2058    temp->m[i] = pCopy(h2->m[i]);
2059    q = pOne();
2060    pSetComp(q,i+1+length);
2061    pSetmComp(q);
2062    if(temp->m[i]!=NULL)
2063    {
2064      if (slength==0) p_Shift(&(temp->m[i]),1,currRing);
2065      p = temp->m[i];
2066      while (pNext(p)!=NULL) pIter(p);
2067      pNext(p) = q; // will be sorted later correctly
2068    }
2069    else
2070      temp->m[i]=q;
2071  }
2072  rk = k = IDELEMS(h2);
2073  if (!idIs0(h1))
2074  {
2075    pEnlargeSet(&(temp->m),IDELEMS(temp),IDELEMS(h1));
2076    IDELEMS(temp) += IDELEMS(h1);
2077    for (i=0;i<IDELEMS(h1);i++)
2078    {
2079      if (h1->m[i]!=NULL)
2080      {
2081        temp->m[k] = pCopy(h1->m[i]);
2082        if (flength==0) p_Shift(&(temp->m[k]),1,currRing);
2083        k++;
2084      }
2085    }
2086  }
2087
2088  ring orig_ring=currRing;
2089  ring syz_ring=rAssure_SyzComp(orig_ring, TRUE); rChangeCurrRing(syz_ring);
2090  // we can use OPT_RETURN_SB only, if syz_ring==orig_ring,
2091  // therefore we disable OPT_RETURN_SB for modulo:
2092  // (see tr. #701)
2093  //if (TEST_OPT_RETURN_SB)
2094  //  rSetSyzComp(IDELEMS(h2)+length, syz_ring);
2095  //else
2096    rSetSyzComp(length, syz_ring);
2097  ideal s_temp;
2098
2099  if (syz_ring != orig_ring)
2100  {
2101    s_temp = idrMoveR_NoSort(temp, orig_ring, syz_ring);
2102  }
2103  else
2104  {
2105    s_temp = temp;
2106  }
2107
2108  idTest(s_temp);
2109  ideal s_temp1 = kStd(s_temp,currRing->qideal,hom,&wtmp,NULL,length);
2110
2111  //if (wtmp!=NULL)  Print("output weights:");wtmp->show(1);PrintLn();
2112  if ((w!=NULL) && (*w !=NULL) && (wtmp!=NULL))
2113  {
2114    delete *w;
2115    *w=new intvec(IDELEMS(h2));
2116    for (i=0;i<IDELEMS(h2);i++)
2117      ((**w)[i])=(*wtmp)[i+length];
2118  }
2119  if (wtmp!=NULL) delete wtmp;
2120
2121  for (i=0;i<IDELEMS(s_temp1);i++)
2122  {
2123    if ((s_temp1->m[i]!=NULL)
2124    && (((int)pGetComp(s_temp1->m[i]))<=length))
2125    {
2126      p_Delete(&(s_temp1->m[i]),currRing);
2127    }
2128    else
2129    {
2130      p_Shift(&(s_temp1->m[i]),-length,currRing);
2131    }
2132  }
2133  s_temp1->rank = rk;
2134  idSkipZeroes(s_temp1);
2135
2136  if (syz_ring!=orig_ring)
2137  {
2138    rChangeCurrRing(orig_ring);
2139    s_temp1 = idrMoveR_NoSort(s_temp1, syz_ring, orig_ring);
2140    rDelete(syz_ring);
2141    // Hmm ... here seems to be a memory leak
2142    // However, simply deleting it causes memory trouble
2143    // idDelete(&s_temp);
2144  }
2145  else
2146  {
2147    idDelete(&temp);
2148  }
2149  idTest(s_temp1);
2150  return s_temp1;
2151}
2152
2153/*
2154*computes module-weights for liftings of homogeneous modules
2155*/
2156intvec * idMWLift(ideal mod,intvec * weights)
2157{
2158  if (idIs0(mod)) return new intvec(2);
2159  int i=IDELEMS(mod);
2160  while ((i>0) && (mod->m[i-1]==NULL)) i--;
2161  intvec *result = new intvec(i+1);
2162  while (i>0)
2163  {
2164    (*result)[i]=currRing->pFDeg(mod->m[i],currRing)+(*weights)[pGetComp(mod->m[i])];
2165  }
2166  return result;
2167}
2168
2169/*2
2170*sorts the kbase for idCoef* in a special way (lexicographically
2171*with x_max,...,x_1)
2172*/
2173ideal idCreateSpecialKbase(ideal kBase,intvec ** convert)
2174{
2175  int i;
2176  ideal result;
2177
2178  if (idIs0(kBase)) return NULL;
2179  result = idInit(IDELEMS(kBase),kBase->rank);
2180  *convert = idSort(kBase,FALSE);
2181  for (i=0;i<(*convert)->length();i++)
2182  {
2183    result->m[i] = pCopy(kBase->m[(**convert)[i]-1]);
2184  }
2185  return result;
2186}
2187
2188/*2
2189*returns the index of a given monom in the list of the special kbase
2190*/
2191int idIndexOfKBase(poly monom, ideal kbase)
2192{
2193  int j=IDELEMS(kbase);
2194
2195  while ((j>0) && (kbase->m[j-1]==NULL)) j--;
2196  if (j==0) return -1;
2197  int i=(currRing->N);
2198  while (i>0)
2199  {
2200    loop
2201    {
2202      if (pGetExp(monom,i)>pGetExp(kbase->m[j-1],i)) return -1;
2203      if (pGetExp(monom,i)==pGetExp(kbase->m[j-1],i)) break;
2204      j--;
2205      if (j==0) return -1;
2206    }
2207    if (i==1)
2208    {
2209      while(j>0)
2210      {
2211        if (pGetComp(monom)==pGetComp(kbase->m[j-1])) return j-1;
2212        if (pGetComp(monom)>pGetComp(kbase->m[j-1])) return -1;
2213        j--;
2214      }
2215    }
2216    i--;
2217  }
2218  return -1;
2219}
2220
2221/*2
2222*decomposes the monom in a part of coefficients described by the
2223*complement of how and a monom in variables occuring in how, the
2224*index of which in kbase is returned as integer pos (-1 if it don't
2225*exists)
2226*/
2227poly idDecompose(poly monom, poly how, ideal kbase, int * pos)
2228{
2229  int i;
2230  poly coeff=pOne(), base=pOne();
2231
2232  for (i=1;i<=(currRing->N);i++)
2233  {
2234    if (pGetExp(how,i)>0)
2235    {
2236      pSetExp(base,i,pGetExp(monom,i));
2237    }
2238    else
2239    {
2240      pSetExp(coeff,i,pGetExp(monom,i));
2241    }
2242  }
2243  pSetComp(base,pGetComp(monom));
2244  pSetm(base);
2245  pSetCoeff(coeff,nCopy(pGetCoeff(monom)));
2246  pSetm(coeff);
2247  *pos = idIndexOfKBase(base,kbase);
2248  if (*pos<0)
2249    p_Delete(&coeff,currRing);
2250  p_Delete(&base,currRing);
2251  return coeff;
2252}
2253
2254/*2
2255*returns a matrix A of coefficients with kbase*A=arg
2256*if all monomials in variables of how occur in kbase
2257*the other are deleted
2258*/
2259matrix idCoeffOfKBase(ideal arg, ideal kbase, poly how)
2260{
2261  matrix result;
2262  ideal tempKbase;
2263  poly p,q;
2264  intvec * convert;
2265  int i=IDELEMS(kbase),j=IDELEMS(arg),k,pos;
2266#if 0
2267  while ((i>0) && (kbase->m[i-1]==NULL)) i--;
2268  if (idIs0(arg))
2269    return mpNew(i,1);
2270  while ((j>0) && (arg->m[j-1]==NULL)) j--;
2271  result = mpNew(i,j);
2272#else
2273  result = mpNew(i, j);
2274  while ((j>0) && (arg->m[j-1]==NULL)) j--;
2275#endif
2276
2277  tempKbase = idCreateSpecialKbase(kbase,&convert);
2278  for (k=0;k<j;k++)
2279  {
2280    p = arg->m[k];
2281    while (p!=NULL)
2282    {
2283      q = idDecompose(p,how,tempKbase,&pos);
2284      if (pos>=0)
2285      {
2286        MATELEM(result,(*convert)[pos],k+1) =
2287            pAdd(MATELEM(result,(*convert)[pos],k+1),q);
2288      }
2289      else
2290        p_Delete(&q,currRing);
2291      pIter(p);
2292    }
2293  }
2294  idDelete(&tempKbase);
2295  return result;
2296}
2297
2298static void idDeleteComps(ideal arg,int* red_comp,int del)
2299// red_comp is an array [0..args->rank]
2300{
2301  int i,j;
2302  poly p;
2303
2304  for (i=IDELEMS(arg)-1;i>=0;i--)
2305  {
2306    p = arg->m[i];
2307    while (p!=NULL)
2308    {
2309      j = pGetComp(p);
2310      if (red_comp[j]!=j)
2311      {
2312        pSetComp(p,red_comp[j]);
2313        pSetmComp(p);
2314      }
2315      pIter(p);
2316    }
2317  }
2318  (arg->rank) -= del;
2319}
2320
2321/*2
2322* returns the presentation of an isomorphic, minimally
2323* embedded  module (arg represents the quotient!)
2324*/
2325ideal idMinEmbedding(ideal arg,BOOLEAN inPlace, intvec **w)
2326{
2327  if (idIs0(arg)) return idInit(1,arg->rank);
2328  int i,next_gen,next_comp;
2329  ideal res=arg;
2330  if (!inPlace) res = idCopy(arg);
2331  res->rank=si_max(res->rank,id_RankFreeModule(res,currRing));
2332  int *red_comp=(int*)omAlloc((res->rank+1)*sizeof(int));
2333  for (i=res->rank;i>=0;i--) red_comp[i]=i;
2334
2335  int del=0;
2336  loop
2337  {
2338    next_gen = id_ReadOutPivot(res, &next_comp, currRing);
2339    if (next_gen<0) break;
2340    del++;
2341    syGaussForOne(res,next_gen,next_comp,0,IDELEMS(res));
2342    for(i=next_comp+1;i<=arg->rank;i++) red_comp[i]--;
2343    if ((w !=NULL)&&(*w!=NULL))
2344    {
2345      for(i=next_comp;i<(*w)->length();i++) (**w)[i-1]=(**w)[i];
2346    }
2347  }
2348
2349  idDeleteComps(res,red_comp,del);
2350  idSkipZeroes(res);
2351  omFree(red_comp);
2352
2353  if ((w !=NULL)&&(*w!=NULL) &&(del>0))
2354  {
2355    int nl=si_max((*w)->length()-del,1);
2356    intvec *wtmp=new intvec(nl);
2357    for(i=0;i<res->rank;i++) (*wtmp)[i]=(**w)[i];
2358    delete *w;
2359    *w=wtmp;
2360  }
2361  return res;
2362}
2363
2364#include <polys/clapsing.h>
2365
2366#if 0
2367poly id_GCD(poly f, poly g, const ring r)
2368{
2369  ring save_r=currRing;
2370  rChangeCurrRing(r);
2371  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2372  intvec *w = NULL;
2373  ideal S=idSyzygies(I,testHomog,&w);
2374  if (w!=NULL) delete w;
2375  poly gg=pTakeOutComp(&(S->m[0]),2);
2376  idDelete(&S);
2377  poly gcd_p=singclap_pdivide(f,gg,r);
2378  p_Delete(&gg,r);
2379  rChangeCurrRing(save_r);
2380  return gcd_p;
2381}
2382#else
2383poly id_GCD(poly f, poly g, const ring r)
2384{
2385  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2386  intvec *w = NULL;
2387
2388  ring save_r = currRing; rChangeCurrRing(r); ideal S=idSyzygies(I,testHomog,&w); rChangeCurrRing(save_r);
2389
2390  if (w!=NULL) delete w;
2391  poly gg=p_TakeOutComp(&(S->m[0]), 2, r);
2392  id_Delete(&S, r);
2393  poly gcd_p=singclap_pdivide(f,gg, r);
2394  p_Delete(&gg, r);
2395
2396  return gcd_p;
2397}
2398#endif
2399
2400#if 0
2401/*2
2402* xx,q: arrays of length 0..rl-1
2403* xx[i]: SB mod q[i]
2404* assume: char=0
2405* assume: q[i]!=0
2406* destroys xx
2407*/
2408ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring R)
2409{
2410  int cnt=IDELEMS(xx[0])*xx[0]->nrows;
2411  ideal result=idInit(cnt,xx[0]->rank);
2412  result->nrows=xx[0]->nrows; // for lifting matrices
2413  result->ncols=xx[0]->ncols; // for lifting matrices
2414  int i,j;
2415  poly r,h,hh,res_p;
2416  number *x=(number *)omAlloc(rl*sizeof(number));
2417  for(i=cnt-1;i>=0;i--)
2418  {
2419    res_p=NULL;
2420    loop
2421    {
2422      r=NULL;
2423      for(j=rl-1;j>=0;j--)
2424      {
2425        h=xx[j]->m[i];
2426        if ((h!=NULL)
2427        &&((r==NULL)||(p_LmCmp(r,h,R)==-1)))
2428          r=h;
2429      }
2430      if (r==NULL) break;
2431      h=p_Head(r, R);
2432      for(j=rl-1;j>=0;j--)
2433      {
2434        hh=xx[j]->m[i];
2435        if ((hh!=NULL) && (p_LmCmp(r,hh, R)==0))
2436        {
2437          x[j]=p_GetCoeff(hh, R);
2438          hh=p_LmFreeAndNext(hh, R);
2439          xx[j]->m[i]=hh;
2440        }
2441        else
2442          x[j]=n_Init(0, R->cf); // is R->cf really n_Q???, yes!
2443      }
2444
2445      number n=n_ChineseRemainder(x,q,rl, R->cf);
2446
2447      for(j=rl-1;j>=0;j--)
2448      {
2449        x[j]=NULL; // nlInit(0...) takes no memory
2450      }
2451      if (n_IsZero(n, R->cf)) p_Delete(&h, R);
2452      else
2453      {
2454        p_SetCoeff(h,n, R);
2455        //Print("new mon:");pWrite(h);
2456        res_p=p_Add_q(res_p, h, R);
2457      }
2458    }
2459    result->m[i]=res_p;
2460  }
2461  omFree(x);
2462  for(i=rl-1;i>=0;i--) id_Delete(&(xx[i]), R);
2463  omFree(xx);
2464  return result;
2465}
2466#endif
2467/* currently unsed:
2468ideal idChineseRemainder(ideal *xx, intvec *iv)
2469{
2470  int rl=iv->length();
2471  number *q=(number *)omAlloc(rl*sizeof(number));
2472  int i;
2473  for(i=0; i<rl; i++)
2474  {
2475    q[i]=nInit((*iv)[i]);
2476  }
2477  return idChineseRemainder(xx,q,rl);
2478}
2479*/
2480/*
2481 * lift ideal with coeffs over Z (mod N) to Q via Farey
2482 */
2483ideal id_Farey(ideal x, number N, const ring r)
2484{
2485  int cnt=IDELEMS(x)*x->nrows;
2486  ideal result=idInit(cnt,x->rank);
2487  result->nrows=x->nrows; // for lifting matrices
2488  result->ncols=x->ncols; // for lifting matrices
2489
2490  int i;
2491  for(i=cnt-1;i>=0;i--)
2492  {
2493    result->m[i]=p_Farey(x->m[i],N,r);
2494  }
2495  return result;
2496}
2497
2498
2499
2500
2501// uses glabl vars via pSetModDeg
2502/*
2503BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
2504{
2505  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
2506  if (idIs0(m)) return TRUE;
2507
2508  int cmax=-1;
2509  int i;
2510  poly p=NULL;
2511  int length=IDELEMS(m);
2512  poly* P=m->m;
2513  for (i=length-1;i>=0;i--)
2514  {
2515    p=P[i];
2516    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
2517  }
2518  if (w != NULL)
2519  if (w->length()+1 < cmax)
2520  {
2521    // Print("length: %d - %d \n", w->length(),cmax);
2522    return FALSE;
2523  }
2524
2525  if(w!=NULL)
2526    p_SetModDeg(w, currRing);
2527
2528  for (i=length-1;i>=0;i--)
2529  {
2530    p=P[i];
2531    poly q=p;
2532    if (p!=NULL)
2533    {
2534      int d=p_FDeg(p,currRing);
2535      loop
2536      {
2537        pIter(p);
2538        if (p==NULL) break;
2539        if (d!=p_FDeg(p,currRing))
2540        {
2541          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
2542          if(w!=NULL)
2543            p_SetModDeg(NULL, currRing);
2544          return FALSE;
2545        }
2546      }
2547    }
2548  }
2549
2550  if(w!=NULL)
2551    p_SetModDeg(NULL, currRing);
2552
2553  return TRUE;
2554}
2555*/
2556
2557/// keeps the first k (>= 1) entries of the given ideal
2558/// (Note that the kept polynomials may be zero.)
2559void idKeepFirstK(ideal id, const int k)
2560{
2561   for (int i = IDELEMS(id)-1; i >= k; i--)
2562   {
2563      if (id->m[i] != NULL) pDelete(&id->m[i]);
2564   }
2565   int kk=k;
2566   if (k==0) kk=1; /* ideals must have at least one element(0)*/
2567   pEnlargeSet(&(id->m), IDELEMS(id), kk-IDELEMS(id));
2568   IDELEMS(id) = kk;
2569}
2570
2571/*
2572* compare the leading terms of a and b
2573*/
2574static int tCompare(const poly a, const poly b)
2575{
2576  if (b == NULL) return(a != NULL);
2577  if (a == NULL) return(-1);
2578
2579  /* a != NULL && b != NULL */
2580  int r = pLmCmp(a, b);
2581  if (r != 0) return(r);
2582  number h = nSub(pGetCoeff(a), pGetCoeff(b));
2583  r = -1 + nIsZero(h) + 2*nGreaterZero(h);   /* -1: <, 0:==, 1: > */
2584  nDelete(&h);
2585  return(r);
2586}
2587
2588/*
2589* compare a and b (rev-lex on terms)
2590*/
2591static int pCompare(const poly a, const poly b)
2592{
2593  int r = tCompare(a, b);
2594  if (r != 0) return(r);
2595
2596  poly aa = a;
2597  poly bb = b;
2598  while (r == 0 && aa != NULL && bb != NULL)
2599  {
2600    pIter(aa);
2601    pIter(bb);
2602    r = tCompare(aa, bb);
2603  }
2604  return(r);
2605}
2606
2607typedef struct
2608{
2609  poly p;
2610  int index;
2611} poly_sort;
2612
2613int pCompare_qsort(const void *a, const void *b)
2614{
2615  int res = pCompare(((poly_sort *)a)->p, ((poly_sort *)b)->p);
2616  return(res);
2617}
2618
2619void idSort_qsort(poly_sort *id_sort, int idsize)
2620{
2621  qsort(id_sort, idsize, sizeof(poly_sort), pCompare_qsort);
2622}
2623
2624/*2
2625* ideal id = (id[i])
2626* if id[i] = id[j] then id[j] is deleted for j > i
2627*/
2628void idDelEquals(ideal id)
2629{
2630  int idsize = IDELEMS(id);
2631  poly_sort *id_sort = (poly_sort *)omAlloc0(idsize*sizeof(poly_sort));
2632  for (int i = 0; i < idsize; i++)
2633  {
2634    id_sort[i].p = id->m[i];
2635    id_sort[i].index = i;
2636  }
2637  idSort_qsort(id_sort, idsize);
2638  int index, index_i, index_j;
2639  int i = 0;
2640  for (int j = 1; j < idsize; j++)
2641  {
2642    if (id_sort[i].p != NULL && pEqualPolys(id_sort[i].p, id_sort[j].p))
2643    {
2644      index_i = id_sort[i].index;
2645      index_j = id_sort[j].index;
2646      if (index_j > index_i)
2647      {
2648        index = index_j;
2649      }
2650      else
2651      {
2652        index = index_i;
2653        i = j;
2654      }
2655      pDelete(&id->m[index]);
2656    }
2657    else
2658    {
2659      i = j;
2660    }
2661  }
2662  omFreeSize((ADDRESS)(id_sort), idsize*sizeof(poly_sort));
2663}
Note: See TracBrowser for help on using the repository browser.