source: git/kernel/ideals.cc @ b7cfaf

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