source: git/kernel/ideals.cc @ 7356be

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