source: git/kernel/ideals.cc @ 0fc231

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