source: git/kernel/ideals.cc @ abe5c8

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