source: git/kernel/ideals.cc @ a5b80a

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