source: git/kernel/ideals.cc @ f89565

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