source: git/kernel/ideals.cc @ 9f7665

spielwiese
Last change on this file since 9f7665 was 9f7665, checked in by Oleksandr Motsak <motsak@…>, 10 years ago
Removed HAVE_CONFIG guards fix: fixed the inclusion of configure-generated *config.h's
  • Property mode set to 100644
File size: 58.4 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT - all basic methods to manipulate ideals
6*/
7
8/* includes */
9
10#include <kernel/mod2.h>
11
12#include "mod2.h"
13
14#include <omalloc/omalloc.h>
15
16#ifndef SING_NDEBUG
17# define MYTEST 0
18#else /* ifndef SING_NDEBUG */
19# define MYTEST 0
20#endif /* ifndef SING_NDEBUG */
21
22#include <omalloc/omalloc.h>
23
24#include <misc/options.h>
25#include <misc/intvec.h>
26
27#include <coeffs/coeffs.h>
28#include <coeffs/numbers.h>
29
30#include <kernel/polys.h>
31#include <polys/monomials/ring.h>
32#include <polys/matpol.h>
33#include <polys/weight.h>
34#include <polys/sparsmat.h>
35#include <polys/prCopy.h>
36#include <polys/nc/nc.h>
37
38
39#include <kernel/ideals.h>
40
41#include <kernel/febase.h>
42#include <kernel/GBEngine/kstd1.h>
43#include <kernel/GBEngine/syz.h>
44
45#include <libpolys/coeffs/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  #ifdef HAVE_RINGS
665  && (!rField_is_Ring(currRing))
666  #endif
667  )
668  {
669    ring dp_C_ring = rAssure_dp_C(syz_ring); // will do rChangeCurrRing later
670    if (dp_C_ring != syz_ring)
671    {
672      rChangeCurrRing(dp_C_ring);
673      e = idrMoveR_NoSort(e, syz_ring, dp_C_ring);
674    }
675    resolvente res = sySchreyerResolvente(e,-1,&length,TRUE, TRUE);
676    intvec * dummy = syBetti(res,length,&reg, *w);
677    *deg = reg+2;
678    delete dummy;
679    for (j=0;j<length;j++)
680    {
681      if (res[j]!=NULL) idDelete(&(res[j]));
682    }
683    omFreeSize((ADDRESS)res,length*sizeof(ideal));
684    idDelete(&e);
685    if (dp_C_ring != syz_ring)
686    {
687      rChangeCurrRing(syz_ring);
688      rDelete(dp_C_ring);
689    }
690  }
691  else
692  {
693    idDelete(&e);
694  }
695  idTest(s_h3);
696  if (currQuotient != NULL)
697  {
698    ideal ts_h3=kStd(s_h3,currQuotient,h,w);
699    idDelete(&s_h3);
700    s_h3 = ts_h3;
701  }
702  return s_h3;
703}
704
705/*2
706*/
707ideal idXXX (ideal  h1, int k)
708{
709  ideal s_h1;
710  intvec *w=NULL;
711
712  assume(currRing != NULL);
713  ring orig_ring=currRing;
714  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE); rChangeCurrRing(syz_ring);
715
716  rSetSyzComp(k,syz_ring);
717
718  if (orig_ring != syz_ring)
719  {
720    s_h1=idrCopyR_NoSort(h1,orig_ring, syz_ring);
721  }
722  else
723  {
724    s_h1 = h1;
725  }
726
727  ideal s_h3=kStd(s_h1,NULL,testHomog,&w,NULL,k);
728
729  if (s_h3==NULL)
730  {
731    return idFreeModule(IDELEMS(h1));
732  }
733
734  if (orig_ring != syz_ring)
735  {
736    idDelete(&s_h1);
737    idSkipZeroes(s_h3);
738    rChangeCurrRing(orig_ring);
739    s_h3 = idrMoveR_NoSort(s_h3, syz_ring, orig_ring);
740    rDelete(syz_ring);
741    idTest(s_h3);
742    return s_h3;
743  }
744
745  idSkipZeroes(s_h3);
746  idTest(s_h3);
747  return s_h3;
748}
749
750/*
751*computes a standard basis for h1 and stores the transformation matrix
752* in ma
753*/
754ideal idLiftStd (ideal  h1, matrix* ma, tHomog hi, ideal * syz)
755{
756  int  i, j, t, inputIsIdeal=id_RankFreeModule(h1,currRing);
757  long k;
758  poly  p=NULL, q;
759  intvec *w=NULL;
760
761  idDelete((ideal*)ma);
762  BOOLEAN lift3=FALSE;
763  if (syz!=NULL) { lift3=TRUE; idDelete(syz); }
764  if (idIs0(h1))
765  {
766    *ma=mpNew(1,0);
767    if (lift3)
768    {
769      *syz=idFreeModule(IDELEMS(h1));
770      int curr_syz_limit=rGetCurrSyzLimit(currRing);
771      if (curr_syz_limit>0)
772      for (int ii=0;ii<IDELEMS(h1);ii++)
773      {
774        if (h1->m[ii]!=NULL)
775          p_Shift(&h1->m[ii],curr_syz_limit,currRing);
776      }
777    }
778    return idInit(1,h1->rank);
779  }
780
781  BITSET save2;
782  SI_SAVE_OPT2(save2);
783
784  k=si_max((long)1,id_RankFreeModule(h1,currRing));
785
786  if ((k==1) && (!lift3)) si_opt_2 |=Sy_bit(V_IDLIFT);
787
788  ring orig_ring = currRing;
789  ring syz_ring = rAssure_SyzComp(orig_ring,TRUE);  rChangeCurrRing(syz_ring);
790  rSetSyzComp(k,syz_ring);
791
792  ideal s_h1=h1;
793
794  if (orig_ring != syz_ring)
795    s_h1 = idrCopyR_NoSort(h1,orig_ring,syz_ring);
796  else
797    s_h1 = h1;
798
799  ideal s_h3=idPrepare(s_h1,hi,k,&w); // main (syz) GB computation
800
801  ideal s_h2 = idInit(IDELEMS(s_h3), s_h3->rank);
802
803  if (lift3) (*syz)=idInit(IDELEMS(s_h3),IDELEMS(h1));
804
805  if (w!=NULL) delete w;
806  i = 0;
807
808  // now sort the result, SB : leave in s_h3
809  //                      T:  put in s_h2
810  //                      syz: put in *syz
811  for (j=0; j<IDELEMS(s_h3); j++)
812  {
813    if (s_h3->m[j] != NULL)
814    {
815      //if (p_MinComp(s_h3->m[j],syz_ring) <= k)
816      if (pGetComp(s_h3->m[j]) <= k) // syz_ring == currRing
817      {
818        i++;
819        q = s_h3->m[j];
820        while (pNext(q) != NULL)
821        {
822          if (pGetComp(pNext(q)) > k)
823          {
824            s_h2->m[j] = pNext(q);
825            pNext(q) = NULL;
826          }
827          else
828          {
829            pIter(q);
830          }
831        }
832        if (!inputIsIdeal) p_Shift(&(s_h3->m[j]), -1,currRing);
833      }
834      else
835      {
836        // we a syzygy here:
837        if (lift3)
838        {
839          p_Shift(&s_h3->m[j], -k,currRing);
840          (*syz)->m[j]=s_h3->m[j];
841          s_h3->m[j]=NULL;
842        }
843        else
844          p_Delete(&(s_h3->m[j]),currRing);
845      }
846    }
847  }
848  idSkipZeroes(s_h3);
849  //extern char * iiStringMatrix(matrix im, int dim,char ch);
850  //PrintS("SB: ----------------------------------------\n");
851  //PrintS(iiStringMatrix((matrix)s_h3,k,'\n'));
852  //PrintLn();
853  //PrintS("T: ----------------------------------------\n");
854  //PrintS(iiStringMatrix((matrix)s_h2,h1->rank,'\n'));
855  //PrintLn();
856
857  if (lift3) idSkipZeroes(*syz);
858
859  j = IDELEMS(s_h1);
860
861
862  if (syz_ring!=orig_ring)
863  {
864    idDelete(&s_h1);
865    rChangeCurrRing(orig_ring);
866  }
867
868  *ma = mpNew(j,i);
869
870  i = 1;
871  for (j=0; j<IDELEMS(s_h2); j++)
872  {
873    if (s_h2->m[j] != NULL)
874    {
875      q = prMoveR( s_h2->m[j], syz_ring,orig_ring);
876      s_h2->m[j] = NULL;
877
878      while (q != NULL)
879      {
880        p = q;
881        pIter(q);
882        pNext(p) = NULL;
883        t=pGetComp(p);
884        pSetComp(p,0);
885        pSetmComp(p);
886        MATELEM(*ma,t-k,i) = pAdd(MATELEM(*ma,t-k,i),p);
887      }
888      i++;
889    }
890  }
891  idDelete(&s_h2);
892
893  for (i=0; i<IDELEMS(s_h3); i++)
894  {
895    s_h3->m[i] = prMoveR_NoSort(s_h3->m[i], syz_ring,orig_ring);
896  }
897  if (lift3)
898  {
899    for (i=0; i<IDELEMS(*syz); i++)
900    {
901      (*syz)->m[i] = prMoveR_NoSort((*syz)->m[i], syz_ring,orig_ring);
902    }
903  }
904
905  if (syz_ring!=orig_ring) rDelete(syz_ring);
906  SI_RESTORE_OPT2(save2);
907  return s_h3;
908}
909
910static void idPrepareStd(ideal s_temp, int k)
911{
912  int j,rk=id_RankFreeModule(s_temp,currRing);
913  poly p,q;
914
915  if (rk == 0)
916  {
917    for (j=0; j<IDELEMS(s_temp); j++)
918    {
919      if (s_temp->m[j]!=NULL) pSetCompP(s_temp->m[j],1);
920    }
921    k = si_max(k,1);
922  }
923  for (j=0; j<IDELEMS(s_temp); j++)
924  {
925    if (s_temp->m[j]!=NULL)
926    {
927      p = s_temp->m[j];
928      q = pOne();
929      //pGetCoeff(q)=nNeg(pGetCoeff(q));   //set q to -1
930      pSetComp(q,k+1+j);
931      pSetmComp(q);
932      while (pNext(p)) pIter(p);
933      pNext(p) = q;
934    }
935  }
936}
937
938/*2
939*computes a representation of the generators of submod with respect to those
940* of mod
941*/
942
943ideal idLift(ideal mod, ideal submod,ideal *rest, BOOLEAN goodShape,
944             BOOLEAN isSB, BOOLEAN divide, matrix *unit)
945{
946  int lsmod =id_RankFreeModule(submod,currRing), j, k;
947  int comps_to_add=0;
948  poly p;
949
950  if (idIs0(submod))
951  {
952    if (unit!=NULL)
953    {
954      *unit=mpNew(1,1);
955      MATELEM(*unit,1,1)=pOne();
956    }
957    if (rest!=NULL)
958    {
959      *rest=idInit(1,mod->rank);
960    }
961    return idInit(1,mod->rank);
962  }
963  if (idIs0(mod)) /* and not idIs0(submod) */
964  {
965    WerrorS("2nd module does not lie in the first");
966    return NULL;
967  }
968  if (unit!=NULL)
969  {
970    comps_to_add = IDELEMS(submod);
971    while ((comps_to_add>0) && (submod->m[comps_to_add-1]==NULL))
972      comps_to_add--;
973  }
974  k=si_max(id_RankFreeModule(mod,currRing),id_RankFreeModule(submod,currRing));
975  if  ((k!=0) && (lsmod==0)) lsmod=1;
976  k=si_max(k,(int)mod->rank);
977  if (k<submod->rank) { WarnS("rk(submod) > rk(mod) ?");k=submod->rank; }
978
979  ring orig_ring=currRing;
980  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE);  rChangeCurrRing(syz_ring);
981  rSetSyzComp(k,syz_ring);
982
983  ideal s_mod, s_temp;
984  if (orig_ring != syz_ring)
985  {
986    s_mod = idrCopyR_NoSort(mod,orig_ring,syz_ring);
987    s_temp = idrCopyR_NoSort(submod,orig_ring,syz_ring);
988  }
989  else
990  {
991    s_mod = mod;
992    s_temp = idCopy(submod);
993  }
994  ideal s_h3;
995  if (isSB)
996  {
997    s_h3 = idCopy(s_mod);
998    idPrepareStd(s_h3, k+comps_to_add);
999  }
1000  else
1001  {
1002    s_h3 = idPrepare(s_mod,(tHomog)FALSE,k+comps_to_add,NULL);
1003  }
1004  if (!goodShape)
1005  {
1006    for (j=0;j<IDELEMS(s_h3);j++)
1007    {
1008      if ((s_h3->m[j] != NULL) && (pMinComp(s_h3->m[j]) > k))
1009        p_Delete(&(s_h3->m[j]),currRing);
1010    }
1011  }
1012  idSkipZeroes(s_h3);
1013  if (lsmod==0)
1014  {
1015    for (j=IDELEMS(s_temp);j>0;j--)
1016    {
1017      if (s_temp->m[j-1]!=NULL)
1018        p_Shift(&(s_temp->m[j-1]),1,currRing);
1019    }
1020  }
1021  if (unit!=NULL)
1022  {
1023    for(j = 0;j<comps_to_add;j++)
1024    {
1025      p = s_temp->m[j];
1026      if (p!=NULL)
1027      {
1028        while (pNext(p)!=NULL) pIter(p);
1029        pNext(p) = pOne();
1030        pIter(p);
1031        pSetComp(p,1+j+k);
1032        pSetmComp(p);
1033        p = pNeg(p);
1034      }
1035    }
1036  }
1037  ideal s_result = kNF(s_h3,currQuotient,s_temp,k);
1038  s_result->rank = s_h3->rank;
1039  ideal s_rest = idInit(IDELEMS(s_result),k);
1040  idDelete(&s_h3);
1041  idDelete(&s_temp);
1042
1043  for (j=0;j<IDELEMS(s_result);j++)
1044  {
1045    if (s_result->m[j]!=NULL)
1046    {
1047      if (pGetComp(s_result->m[j])<=k)
1048      {
1049        if (!divide)
1050        {
1051          if (isSB)
1052          {
1053            WarnS("first module not a standardbasis\n"
1054              "// ** or second not a proper submodule");
1055          }
1056          else
1057            WerrorS("2nd module does not lie in the first");
1058          idDelete(&s_result);
1059          idDelete(&s_rest);
1060          s_result=idInit(IDELEMS(submod),submod->rank);
1061          break;
1062        }
1063        else
1064        {
1065          p = s_rest->m[j] = s_result->m[j];
1066          while ((pNext(p)!=NULL) && (pGetComp(pNext(p))<=k)) pIter(p);
1067          s_result->m[j] = pNext(p);
1068          pNext(p) = NULL;
1069        }
1070      }
1071      p_Shift(&(s_result->m[j]),-k,currRing);
1072      pNeg(s_result->m[j]);
1073    }
1074  }
1075  if ((lsmod==0) && (!idIs0(s_rest)))
1076  {
1077    for (j=IDELEMS(s_rest);j>0;j--)
1078    {
1079      if (s_rest->m[j-1]!=NULL)
1080      {
1081        p_Shift(&(s_rest->m[j-1]),-1,currRing);
1082        s_rest->m[j-1] = s_rest->m[j-1];
1083      }
1084    }
1085  }
1086  if(syz_ring!=orig_ring)
1087  {
1088    idDelete(&s_mod);
1089    rChangeCurrRing(orig_ring);
1090    s_result = idrMoveR_NoSort(s_result, syz_ring, orig_ring);
1091    s_rest = idrMoveR_NoSort(s_rest, syz_ring, orig_ring);
1092    rDelete(syz_ring);
1093  }
1094  if (rest!=NULL)
1095    *rest = s_rest;
1096  else
1097    idDelete(&s_rest);
1098//idPrint(s_result);
1099  if (unit!=NULL)
1100  {
1101    *unit=mpNew(comps_to_add,comps_to_add);
1102    int i;
1103    for(i=0;i<IDELEMS(s_result);i++)
1104    {
1105      poly p=s_result->m[i];
1106      poly q=NULL;
1107      while(p!=NULL)
1108      {
1109        if(pGetComp(p)<=comps_to_add)
1110        {
1111          pSetComp(p,0);
1112          if (q!=NULL)
1113          {
1114            pNext(q)=pNext(p);
1115          }
1116          else
1117          {
1118            pIter(s_result->m[i]);
1119          }
1120          pNext(p)=NULL;
1121          MATELEM(*unit,i+1,i+1)=pAdd(MATELEM(*unit,i+1,i+1),p);
1122          if(q!=NULL)   p=pNext(q);
1123          else          p=s_result->m[i];
1124        }
1125        else
1126        {
1127          q=p;
1128          pIter(p);
1129        }
1130      }
1131      p_Shift(&s_result->m[i],-comps_to_add,currRing);
1132    }
1133  }
1134  return s_result;
1135}
1136
1137/*2
1138*computes division of P by Q with remainder up to (w-weighted) degree n
1139*P, Q, and w are not changed
1140*/
1141void idLiftW(ideal P,ideal Q,int n,matrix &T, ideal &R,short *w)
1142{
1143  long N=0;
1144  int i;
1145  for(i=IDELEMS(Q)-1;i>=0;i--)
1146    if(w==NULL)
1147      N=si_max(N,p_Deg(Q->m[i],currRing));
1148    else
1149      N=si_max(N,p_DegW(Q->m[i],w,currRing));
1150  N+=n;
1151
1152  T=mpNew(IDELEMS(Q),IDELEMS(P));
1153  R=idInit(IDELEMS(P),P->rank);
1154
1155  for(i=IDELEMS(P)-1;i>=0;i--)
1156  {
1157    poly p;
1158    if(w==NULL)
1159      p=ppJet(P->m[i],N);
1160    else
1161      p=ppJetW(P->m[i],N,w);
1162
1163    int j=IDELEMS(Q)-1;
1164    while(p!=NULL)
1165    {
1166      if(pDivisibleBy(Q->m[j],p))
1167      {
1168        poly p0=p_DivideM(pHead(p),pHead(Q->m[j]),currRing);
1169        if(w==NULL)
1170          p=pJet(pSub(p,ppMult_mm(Q->m[j],p0)),N);
1171        else
1172          p=pJetW(pSub(p,ppMult_mm(Q->m[j],p0)),N,w);
1173        pNormalize(p);
1174        if(((w==NULL)&&(p_Deg(p0,currRing)>n))||((w!=NULL)&&(p_DegW(p0,w,currRing)>n)))
1175          p_Delete(&p0,currRing);
1176        else
1177          MATELEM(T,j+1,i+1)=pAdd(MATELEM(T,j+1,i+1),p0);
1178        j=IDELEMS(Q)-1;
1179      }
1180      else
1181      {
1182        if(j==0)
1183        {
1184          poly p0=p;
1185          pIter(p);
1186          pNext(p0)=NULL;
1187          if(((w==NULL)&&(p_Deg(p0,currRing)>n))
1188          ||((w!=NULL)&&(p_DegW(p0,w,currRing)>n)))
1189            p_Delete(&p0,currRing);
1190          else
1191            R->m[i]=pAdd(R->m[i],p0);
1192          j=IDELEMS(Q)-1;
1193        }
1194        else
1195          j--;
1196      }
1197    }
1198  }
1199}
1200
1201/*2
1202*computes the quotient of h1,h2 : internal routine for idQuot
1203*BEWARE: the returned ideals may contain incorrectly ordered polys !
1204*
1205*/
1206static ideal idInitializeQuot (ideal  h1, ideal h2, BOOLEAN h1IsStb,
1207                               BOOLEAN *addOnlyOne, int *kkmax)
1208{
1209  ideal temph1;
1210  poly     p,q = NULL;
1211  int i,l,ll,k,kkk,kmax;
1212  int j = 0;
1213  int k1 = id_RankFreeModule(h1,currRing);
1214  int k2 = id_RankFreeModule(h2,currRing);
1215  tHomog   hom=isNotHomog;
1216  k=si_max(k1,k2);
1217  if (k==0)
1218    k = 1;
1219  if ((k2==0) && (k>1)) *addOnlyOne = FALSE;
1220  intvec * weights;
1221  hom = (tHomog)idHomModule(h1,currQuotient,&weights);
1222  if /**addOnlyOne &&*/ (/*(*/ !h1IsStb /*)*/)
1223    temph1 = kStd(h1,currQuotient,hom,&weights,NULL);
1224  else
1225    temph1 = idCopy(h1);
1226  if (weights!=NULL) delete weights;
1227  idTest(temph1);
1228/*--- making a single vector from h2 ---------------------*/
1229  for (i=0; i<IDELEMS(h2); i++)
1230  {
1231    if (h2->m[i] != NULL)
1232    {
1233      p = pCopy(h2->m[i]);
1234      if (k2 == 0)
1235        p_Shift(&p,j*k+1,currRing);
1236      else
1237        p_Shift(&p,j*k,currRing);
1238      q = pAdd(q,p);
1239      j++;
1240    }
1241  }
1242  *kkmax = kmax = j*k+1;
1243/*--- adding a monomial for the result (syzygy) ----------*/
1244  p = q;
1245  while (pNext(p)!=NULL) pIter(p);
1246  pNext(p) = pOne();
1247  pIter(p);
1248  pSetComp(p,kmax);
1249  pSetmComp(p);
1250/*--- constructing the big matrix ------------------------*/
1251  ideal h4 = idInit(16,kmax+k-1);
1252  h4->m[0] = q;
1253  if (k2 == 0)
1254  {
1255    if (k > IDELEMS(h4))
1256    {
1257      pEnlargeSet(&(h4->m),IDELEMS(h4),k-IDELEMS(h4));
1258      IDELEMS(h4) = k;
1259    }
1260    for (i=1; i<k; i++)
1261    {
1262      if (h4->m[i-1]!=NULL)
1263      {
1264        p = pCopy_noCheck(h4->m[i-1]);
1265        p_Shift(&p,1,currRing);
1266        h4->m[i] = p;
1267      }
1268    }
1269  }
1270  idSkipZeroes(h4);
1271  kkk = IDELEMS(h4);
1272  i = IDELEMS(temph1);
1273  for (l=0; l<i; l++)
1274  {
1275    if(temph1->m[l]!=NULL)
1276    {
1277      for (ll=0; ll<j; ll++)
1278      {
1279        p = pCopy(temph1->m[l]);
1280        if (k1 == 0)
1281          p_Shift(&p,ll*k+1,currRing);
1282        else
1283          p_Shift(&p,ll*k,currRing);
1284        if (kkk >= IDELEMS(h4))
1285        {
1286          pEnlargeSet(&(h4->m),IDELEMS(h4),16);
1287          IDELEMS(h4) += 16;
1288        }
1289        h4->m[kkk] = p;
1290        kkk++;
1291      }
1292    }
1293  }
1294/*--- if h2 goes in as single vector - the h1-part is just SB ---*/
1295  if (*addOnlyOne)
1296  {
1297    idSkipZeroes(h4);
1298    p = h4->m[0];
1299    for (i=0;i<IDELEMS(h4)-1;i++)
1300    {
1301      h4->m[i] = h4->m[i+1];
1302    }
1303    h4->m[IDELEMS(h4)-1] = p;
1304    #ifdef HAVE_RINGS
1305    if(!rField_is_Ring(currRing))
1306    #endif
1307    si_opt_1 |= Sy_bit(OPT_SB_1);
1308  }
1309  idDelete(&temph1);
1310  return h4;
1311}
1312/*2
1313*computes the quotient of h1,h2
1314*/
1315ideal idQuot (ideal  h1, ideal h2, BOOLEAN h1IsStb, BOOLEAN resultIsIdeal)
1316{
1317  // first check for special case h1:(0)
1318  if (idIs0(h2))
1319  {
1320    ideal res;
1321    if (resultIsIdeal)
1322    {
1323      res = idInit(1,1);
1324      res->m[0] = pOne();
1325    }
1326    else
1327      res = idFreeModule(h1->rank);
1328    return res;
1329  }
1330  BITSET old_test1;
1331  SI_SAVE_OPT1(old_test1);
1332  int i, kmax;
1333  BOOLEAN  addOnlyOne=TRUE;
1334  tHomog   hom=isNotHomog;
1335  intvec * weights1;
1336
1337  ideal s_h4 = idInitializeQuot (h1,h2,h1IsStb,&addOnlyOne,&kmax);
1338
1339  hom = (tHomog)idHomModule(s_h4,currQuotient,&weights1);
1340
1341  ring orig_ring=currRing;
1342  ring syz_ring=rAssure_SyzComp(orig_ring,TRUE);  rChangeCurrRing(syz_ring);
1343  rSetSyzComp(kmax-1,syz_ring);
1344  if (orig_ring!=syz_ring)
1345  //  s_h4 = idrMoveR_NoSort(s_h4,orig_ring, syz_ring);
1346    s_h4 = idrMoveR(s_h4,orig_ring, syz_ring);
1347  idTest(s_h4);
1348  #if 0
1349  void ipPrint_MA0(matrix m, const char *name);
1350  matrix m=idModule2Matrix(idCopy(s_h4));
1351  PrintS("start:\n");
1352  ipPrint_MA0(m,"Q");
1353  idDelete((ideal *)&m);
1354  PrintS("last elem:");wrp(s_h4->m[IDELEMS(s_h4)-1]);PrintLn();
1355  #endif
1356  ideal s_h3;
1357  if (addOnlyOne)
1358  {
1359    s_h3 = kStd(s_h4,currQuotient,hom,&weights1,NULL,0/*kmax-1*/,IDELEMS(s_h4)-1);
1360  }
1361  else
1362  {
1363    s_h3 = kStd(s_h4,currQuotient,hom,&weights1,NULL,kmax-1);
1364  }
1365  SI_RESTORE_OPT1(old_test1);
1366  #if 0
1367  // only together with the above debug stuff
1368  idSkipZeroes(s_h3);
1369  m=idModule2Matrix(idCopy(s_h3));
1370  Print("result, kmax=%d:\n",kmax);
1371  ipPrint_MA0(m,"S");
1372  idDelete((ideal *)&m);
1373  #endif
1374  idTest(s_h3);
1375  if (weights1!=NULL) delete weights1;
1376  idDelete(&s_h4);
1377
1378  for (i=0;i<IDELEMS(s_h3);i++)
1379  {
1380    if ((s_h3->m[i]!=NULL) && (pGetComp(s_h3->m[i])>=kmax))
1381    {
1382      if (resultIsIdeal)
1383        p_Shift(&s_h3->m[i],-kmax,currRing);
1384      else
1385        p_Shift(&s_h3->m[i],-kmax+1,currRing);
1386    }
1387    else
1388      p_Delete(&s_h3->m[i],currRing);
1389  }
1390  if (resultIsIdeal)
1391    s_h3->rank = 1;
1392  else
1393    s_h3->rank = h1->rank;
1394  if(syz_ring!=orig_ring)
1395  {
1396    rChangeCurrRing(orig_ring);
1397    s_h3 = idrMoveR_NoSort(s_h3, syz_ring, orig_ring);
1398    rDelete(syz_ring);
1399  }
1400  idSkipZeroes(s_h3);
1401  idTest(s_h3);
1402  return s_h3;
1403}
1404
1405/*2
1406* eliminate delVar (product of vars) in h1
1407*/
1408ideal idElimination (ideal h1,poly delVar,intvec *hilb)
1409{
1410  int    i,j=0,k,l;
1411  ideal  h,hh, h3;
1412  int    *ord,*block0,*block1;
1413  int    ordersize=2;
1414  int    **wv;
1415  tHomog hom;
1416  intvec * w;
1417  ring tmpR;
1418  ring origR = currRing;
1419
1420  if (delVar==NULL)
1421  {
1422    return idCopy(h1);
1423  }
1424  if ((currQuotient!=NULL) && rIsPluralRing(origR))
1425  {
1426    WerrorS("cannot eliminate in a qring");
1427    return NULL;
1428  }
1429  if (idIs0(h1)) return idInit(1,h1->rank);
1430#ifdef HAVE_PLURAL
1431  if (rIsPluralRing(origR))
1432    /* in the NC case, we have to check the admissibility of */
1433    /* the subalgebra to be intersected with */
1434  {
1435    if ((ncRingType(origR) != nc_skew) && (ncRingType(origR) != nc_exterior)) /* in (quasi)-commutative algebras every subalgebra is admissible */
1436    {
1437      if (nc_CheckSubalgebra(delVar,origR))
1438      {
1439        WerrorS("no elimination is possible: subalgebra is not admissible");
1440        return NULL;
1441      }
1442    }
1443  }
1444#endif
1445  hom=(tHomog)idHomModule(h1,NULL,&w); //sets w to weight vector or NULL
1446  h3=idInit(16,h1->rank);
1447  for (k=0;; k++)
1448  {
1449    if (origR->order[k]!=0) ordersize++;
1450    else break;
1451  }
1452#if 0
1453  if (rIsPluralRing(origR)) // we have too keep the odering: it may be needed
1454                            // for G-algebra
1455  {
1456    for (k=0;k<ordersize-1; k++)
1457    {
1458      block0[k+1] = origR->block0[k];
1459      block1[k+1] = origR->block1[k];
1460      ord[k+1] = origR->order[k];
1461      if (origR->wvhdl[k]!=NULL) wv[k+1] = (int*) omMemDup(origR->wvhdl[k]);
1462    }
1463  }
1464  else
1465  {
1466    block0[1] = 1;
1467    block1[1] = (currRing->N);
1468    if (origR->OrdSgn==1) ord[1] = ringorder_wp;
1469    else                  ord[1] = ringorder_ws;
1470    wv[1]=(int*)omAlloc0((currRing->N)*sizeof(int));
1471    double wNsqr = (double)2.0 / (double)(currRing->N);
1472    wFunctional = wFunctionalBuch;
1473    int  *x= (int * )omAlloc(2 * ((currRing->N) + 1) * sizeof(int));
1474    int sl=IDELEMS(h1) - 1;
1475    wCall(h1->m, sl, x, wNsqr);
1476    for (sl = (currRing->N); sl!=0; sl--)
1477      wv[1][sl-1] = x[sl + (currRing->N) + 1];
1478    omFreeSize((ADDRESS)x, 2 * ((currRing->N) + 1) * sizeof(int));
1479
1480    ord[2]=ringorder_C;
1481    ord[3]=0;
1482  }
1483#else
1484#endif
1485  if ((hom==TRUE) && (origR->OrdSgn==1) && (!rIsPluralRing(origR)))
1486  {
1487    #if 1
1488    // we change to an ordering:
1489    // aa(1,1,1,...,0,0,0),wp(...),C
1490    // this seems to be better than version 2 below,
1491    // according to Tst/../elimiate_[3568].tat (- 17 %)
1492    ord=(int*)omAlloc0(4*sizeof(int));
1493    block0=(int*)omAlloc0(4*sizeof(int));
1494    block1=(int*)omAlloc0(4*sizeof(int));
1495    wv=(int**) omAlloc0(4*sizeof(int**));
1496    block0[0] = block0[1] = 1;
1497    block1[0] = block1[1] = rVar(origR);
1498    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1499    // use this special ordering: like ringorder_a, except that pFDeg, pWeights
1500    // ignore it
1501    ord[0] = ringorder_aa;
1502    for (j=0;j<rVar(origR);j++)
1503      if (pGetExp(delVar,j+1)!=0) wv[0][j]=1;
1504    BOOLEAN wp=FALSE;
1505    for (j=0;j<rVar(origR);j++)
1506      if (pWeight(j+1,origR)!=1) { wp=TRUE;break; }
1507    if (wp)
1508    {
1509      wv[1]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1510      for (j=0;j<rVar(origR);j++)
1511        wv[1][j]=pWeight(j+1,origR);
1512      ord[1] = ringorder_wp;
1513    }
1514    else
1515      ord[1] = ringorder_dp;
1516    #else
1517    // we change to an ordering:
1518    // a(w1,...wn),wp(1,...0.....),C
1519    ord=(int*)omAlloc0(4*sizeof(int));
1520    block0=(int*)omAlloc0(4*sizeof(int));
1521    block1=(int*)omAlloc0(4*sizeof(int));
1522    wv=(int**) omAlloc0(4*sizeof(int**));
1523    block0[0] = block0[1] = 1;
1524    block1[0] = block1[1] = rVar(origR);
1525    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1526    wv[1]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1527    ord[0] = ringorder_a;
1528    for (j=0;j<rVar(origR);j++)
1529      wv[0][j]=pWeight(j+1,origR);
1530    ord[1] = ringorder_wp;
1531    for (j=0;j<rVar(origR);j++)
1532      if (pGetExp(delVar,j+1)!=0) wv[1][j]=1;
1533    #endif
1534    ord[2] = ringorder_C;
1535    ord[3] = 0;
1536  }
1537  else
1538  {
1539    // we change to an ordering:
1540    // aa(....),orig_ordering
1541    ord=(int*)omAlloc0(ordersize*sizeof(int));
1542    block0=(int*)omAlloc0(ordersize*sizeof(int));
1543    block1=(int*)omAlloc0(ordersize*sizeof(int));
1544    wv=(int**) omAlloc0(ordersize*sizeof(int**));
1545    for (k=0;k<ordersize-1; k++)
1546    {
1547      block0[k+1] = origR->block0[k];
1548      block1[k+1] = origR->block1[k];
1549      ord[k+1] = origR->order[k];
1550      if (origR->wvhdl[k]!=NULL) wv[k+1] = (int*) omMemDup(origR->wvhdl[k]);
1551    }
1552    block0[0] = 1;
1553    block1[0] = rVar(origR);
1554    wv[0]=(int*)omAlloc0((rVar(origR) + 1)*sizeof(int));
1555    for (j=0;j<rVar(origR);j++)
1556      if (pGetExp(delVar,j+1)!=0) wv[0][j]=1;
1557    // use this special ordering: like ringorder_a, except that pFDeg, pWeights
1558    // ignore it
1559    ord[0] = ringorder_aa;
1560  }
1561  // fill in tmp ring to get back the data later on
1562  tmpR  = rCopy0(origR,FALSE,FALSE); // qring==NULL
1563  //rUnComplete(tmpR);
1564  tmpR->p_Procs=NULL;
1565  tmpR->order = ord;
1566  tmpR->block0 = block0;
1567  tmpR->block1 = block1;
1568  tmpR->wvhdl = wv;
1569  rComplete(tmpR, 1);
1570
1571#ifdef HAVE_PLURAL
1572  /* update nc structure on tmpR */
1573  if (rIsPluralRing(origR))
1574  {
1575    if ( nc_rComplete(origR, tmpR, false) ) // no quotient ideal!
1576    {
1577      Werror("no elimination is possible: ordering condition is violated");
1578      // cleanup
1579      rDelete(tmpR);
1580      if (w!=NULL)
1581        delete w;
1582      return NULL;
1583    }
1584  }
1585#endif
1586  // change into the new ring
1587  //pChangeRing((currRing->N),currRing->OrdSgn,ord,block0,block1,wv);
1588  rChangeCurrRing(tmpR);
1589
1590  //h = idInit(IDELEMS(h1),h1->rank);
1591  // fetch data from the old ring
1592  //for (k=0;k<IDELEMS(h1);k++) h->m[k] = prCopyR( h1->m[k], origR);
1593  h=idrCopyR(h1,origR,currRing);
1594  if (origR->qideal!=NULL)
1595  {
1596    WarnS("eliminate in q-ring: experimental");
1597    ideal q=idrCopyR(origR->qideal,origR,currRing);
1598    ideal s=idSimpleAdd(h,q);
1599    idDelete(&h);
1600    idDelete(&q);
1601    h=s;
1602  }
1603  // compute kStd
1604#if 1
1605  //rWrite(tmpR);PrintLn();
1606  //BITSET save1;
1607  //SI_SAVE_OPT1(save1);
1608  //si_opt_1 |=1;
1609  //Print("h: %d gen, rk=%d\n",IDELEMS(h),h->rank);
1610  //extern char * showOption();
1611  //Print("%s\n",showOption());
1612  hh = kStd(h,NULL,hom,&w,hilb);
1613  //SI_RESTORE_OPT1(save1);
1614  idDelete(&h);
1615#else
1616  extern ideal kGroebner(ideal F, ideal Q);
1617  hh=kGroebner(h,NULL);
1618#endif
1619  // go back to the original ring
1620  rChangeCurrRing(origR);
1621  i = IDELEMS(hh)-1;
1622  while ((i >= 0) && (hh->m[i] == NULL)) i--;
1623  j = -1;
1624  // fetch data from temp ring
1625  for (k=0; k<=i; k++)
1626  {
1627    l=(currRing->N);
1628    while ((l>0) && (p_GetExp( hh->m[k],l,tmpR)*pGetExp(delVar,l)==0)) l--;
1629    if (l==0)
1630    {
1631      j++;
1632      if (j >= IDELEMS(h3))
1633      {
1634        pEnlargeSet(&(h3->m),IDELEMS(h3),16);
1635        IDELEMS(h3) += 16;
1636      }
1637      h3->m[j] = prMoveR( hh->m[k], tmpR,origR);
1638      hh->m[k] = NULL;
1639    }
1640  }
1641  id_Delete(&hh, tmpR);
1642  idSkipZeroes(h3);
1643  rDelete(tmpR);
1644  if (w!=NULL)
1645    delete w;
1646  return h3;
1647}
1648
1649/*2
1650* compute the which-th ar-minor of the matrix a
1651*/
1652poly idMinor(matrix a, int ar, unsigned long which, ideal R)
1653{
1654  int     i,j/*,k,size*/;
1655  unsigned long curr;
1656  int *rowchoise,*colchoise;
1657  BOOLEAN rowch,colch;
1658  // ideal result;
1659  matrix tmp;
1660  poly p,q;
1661
1662  i = binom(a->rows(),ar);
1663  j = binom(a->cols(),ar);
1664
1665  rowchoise=(int *)omAlloc(ar*sizeof(int));
1666  colchoise=(int *)omAlloc(ar*sizeof(int));
1667  // if ((i>512) || (j>512) || (i*j >512)) size=512;
1668  // else size=i*j;
1669  // result=idInit(size,1);
1670  tmp=mpNew(ar,ar);
1671  // k = 0; /* the index in result*/
1672  curr = 0; /* index of current minor */
1673  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
1674  while (!rowch)
1675  {
1676    idInitChoise(ar,1,a->cols(),&colch,colchoise);
1677    while (!colch)
1678    {
1679      if (curr == which)
1680      {
1681        for (i=1; i<=ar; i++)
1682        {
1683          for (j=1; j<=ar; j++)
1684          {
1685            MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1686          }
1687        }
1688        p = mp_DetBareiss(tmp,currRing);
1689        if (p!=NULL)
1690        {
1691          if (R!=NULL)
1692          {
1693            q = p;
1694            p = kNF(R,currQuotient,q);
1695            p_Delete(&q,currRing);
1696          }
1697          /*delete the matrix tmp*/
1698          for (i=1; i<=ar; i++)
1699          {
1700            for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1701          }
1702          idDelete((ideal*)&tmp);
1703          omFreeSize((ADDRESS)rowchoise,ar*sizeof(int));
1704          omFreeSize((ADDRESS)colchoise,ar*sizeof(int));
1705          return (p);
1706        }
1707      }
1708      curr++;
1709      idGetNextChoise(ar,a->cols(),&colch,colchoise);
1710    }
1711    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
1712  }
1713  return (poly) 1;
1714}
1715
1716#ifdef WITH_OLD_MINOR
1717/*2
1718* compute all ar-minors of the matrix a
1719*/
1720ideal idMinors(matrix a, int ar, ideal R)
1721{
1722  int     i,j,/*k,*/size;
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  idInitChoise(ar,1,a->rows(),&rowch,rowchoise);
1740  while (!rowch)
1741  {
1742    idInitChoise(ar,1,a->cols(),&colch,colchoise);
1743    while (!colch)
1744    {
1745      for (i=1; i<=ar; i++)
1746      {
1747        for (j=1; j<=ar; j++)
1748        {
1749          MATELEM(tmp,i,j) = MATELEM(a,rowchoise[i-1],colchoise[j-1]);
1750        }
1751      }
1752      p = mp_DetBareiss(tmp,vcurrRing);
1753      if (p!=NULL)
1754      {
1755        if (R!=NULL)
1756        {
1757          q = p;
1758          p = kNF(R,currQuotient,q);
1759          p_Delete(&q,currRing);
1760        }
1761        if (p!=NULL)
1762        {
1763          if (k>=size)
1764          {
1765            pEnlargeSet(&result->m,size,32);
1766            size += 32;
1767          }
1768          result->m[k] = p;
1769          k++;
1770        }
1771      }
1772      idGetNextChoise(ar,a->cols(),&colch,colchoise);
1773    }
1774    idGetNextChoise(ar,a->rows(),&rowch,rowchoise);
1775  }
1776  /*delete the matrix tmp*/
1777  for (i=1; i<=ar; i++)
1778  {
1779    for (j=1; j<=ar; j++) MATELEM(tmp,i,j) = NULL;
1780  }
1781  idDelete((ideal*)&tmp);
1782  if (k==0)
1783  {
1784    k=1;
1785    result->m[0]=NULL;
1786  }
1787  omFreeSize((ADDRESS)rowchoise,ar*sizeof(int));
1788  omFreeSize((ADDRESS)colchoise,ar*sizeof(int));
1789  pEnlargeSet(&result->m,size,k-size);
1790  IDELEMS(result) = k;
1791  return (result);
1792}
1793#else
1794/*2
1795* compute all ar-minors of the matrix a
1796* the caller of mpRecMin
1797* the elements of the result are not in R (if R!=NULL)
1798*/
1799ideal idMinors(matrix a, int ar, ideal R)
1800{
1801  int elems=0;
1802  int r=a->nrows,c=a->ncols;
1803  int i;
1804  matrix b;
1805  ideal result,h;
1806  ring origR=currRing;
1807  ring tmpR;
1808  long bound;
1809
1810  if((ar<=0) || (ar>r) || (ar>c))
1811  {
1812    Werror("%d-th minor, matrix is %dx%d",ar,r,c);
1813    return NULL;
1814  }
1815  h = id_Matrix2Module(mp_Copy(a,origR),origR);
1816  bound = sm_ExpBound(h,c,r,ar,origR);
1817  idDelete(&h);
1818  tmpR=sm_RingChange(origR,bound);
1819  b = mpNew(r,c);
1820  for (i=r*c-1;i>=0;i--)
1821  {
1822    if (a->m[i])
1823      b->m[i] = prCopyR(a->m[i],origR,tmpR);
1824  }
1825  if (R!=NULL)
1826  {
1827    R = idrCopyR(R,origR,tmpR);
1828    //if (ar>1) // otherwise done in mpMinorToResult
1829    //{
1830    //  matrix bb=(matrix)kNF(R,currQuotient,(ideal)b);
1831    //  bb->rank=b->rank; bb->nrows=b->nrows; bb->ncols=b->ncols;
1832    //  idDelete((ideal*)&b); b=bb;
1833    //}
1834  }
1835  result=idInit(32,1);
1836  if(ar>1) mp_RecMin(ar-1,result,elems,b,r,c,NULL,R,tmpR);
1837  else mp_MinorToResult(result,elems,b,r,c,R,tmpR);
1838  idDelete((ideal *)&b);
1839  if (R!=NULL) idDelete(&R);
1840  idSkipZeroes(result);
1841  rChangeCurrRing(origR);
1842  result = idrMoveR(result,tmpR,origR);
1843  sm_KillModifiedRing(tmpR);
1844  idTest(result);
1845  return result;
1846}
1847#endif
1848
1849/*2
1850*returns TRUE if id1 is a submodule of id2
1851*/
1852BOOLEAN idIsSubModule(ideal id1,ideal id2)
1853{
1854  int i;
1855  poly p;
1856
1857  if (idIs0(id1)) return TRUE;
1858  for (i=0;i<IDELEMS(id1);i++)
1859  {
1860    if (id1->m[i] != NULL)
1861    {
1862      p = kNF(id2,currQuotient,id1->m[i]);
1863      if (p != NULL)
1864      {
1865        p_Delete(&p,currRing);
1866        return FALSE;
1867      }
1868    }
1869  }
1870  return TRUE;
1871}
1872
1873BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
1874{
1875  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
1876  if (idIs0(m)) return TRUE;
1877
1878  int cmax=-1;
1879  int i;
1880  poly p=NULL;
1881  int length=IDELEMS(m);
1882  polyset P=m->m;
1883  for (i=length-1;i>=0;i--)
1884  {
1885    p=P[i];
1886    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
1887  }
1888  if (w != NULL)
1889  if (w->length()+1 < cmax)
1890  {
1891    // Print("length: %d - %d \n", w->length(),cmax);
1892    return FALSE;
1893  }
1894
1895  if(w!=NULL)
1896    p_SetModDeg(w, currRing);
1897
1898  for (i=length-1;i>=0;i--)
1899  {
1900    p=P[i];
1901    if (p!=NULL)
1902    {
1903      int d=currRing->pFDeg(p,currRing);
1904      loop
1905      {
1906        pIter(p);
1907        if (p==NULL) break;
1908        if (d!=currRing->pFDeg(p,currRing))
1909        {
1910          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
1911          if(w!=NULL)
1912            p_SetModDeg(NULL, currRing);
1913          return FALSE;
1914        }
1915      }
1916    }
1917  }
1918
1919  if(w!=NULL)
1920    p_SetModDeg(NULL, currRing);
1921
1922  return TRUE;
1923}
1924
1925ideal idSeries(int n,ideal M,matrix U,intvec *w)
1926{
1927  for(int i=IDELEMS(M)-1;i>=0;i--)
1928  {
1929    if(U==NULL)
1930      M->m[i]=pSeries(n,M->m[i],NULL,w);
1931    else
1932    {
1933      M->m[i]=pSeries(n,M->m[i],MATELEM(U,i+1,i+1),w);
1934      MATELEM(U,i+1,i+1)=NULL;
1935    }
1936  }
1937  if(U!=NULL)
1938    idDelete((ideal*)&U);
1939  return M;
1940}
1941
1942matrix idDiff(matrix i, int k)
1943{
1944  int e=MATCOLS(i)*MATROWS(i);
1945  matrix r=mpNew(MATROWS(i),MATCOLS(i));
1946  r->rank=i->rank;
1947  int j;
1948  for(j=0; j<e; j++)
1949  {
1950    r->m[j]=pDiff(i->m[j],k);
1951  }
1952  return r;
1953}
1954
1955matrix idDiffOp(ideal I, ideal J,BOOLEAN multiply)
1956{
1957  matrix r=mpNew(IDELEMS(I),IDELEMS(J));
1958  int i,j;
1959  for(i=0; i<IDELEMS(I); i++)
1960  {
1961    for(j=0; j<IDELEMS(J); j++)
1962    {
1963      MATELEM(r,i+1,j+1)=pDiffOp(I->m[i],J->m[j],multiply);
1964    }
1965  }
1966  return r;
1967}
1968
1969/*3
1970*handles for some ideal operations the ring/syzcomp managment
1971*returns all syzygies (componentwise-)shifted by -syzcomp
1972*or -syzcomp-1 (in case of ideals as input)
1973static ideal idHandleIdealOp(ideal arg,int syzcomp,int isIdeal=FALSE)
1974{
1975  ring orig_ring=currRing;
1976  ring syz_ring=rAssure_SyzComp(orig_ring, TRUE); rChangeCurrRing(syz_ring);
1977  rSetSyzComp(length, syz_ring);
1978
1979  ideal s_temp;
1980  if (orig_ring!=syz_ring)
1981    s_temp=idrMoveR_NoSort(arg,orig_ring, syz_ring);
1982  else
1983    s_temp=arg;
1984
1985  ideal s_temp1 = kStd(s_temp,currQuotient,testHomog,&w,NULL,length);
1986  if (w!=NULL) delete w;
1987
1988  if (syz_ring!=orig_ring)
1989  {
1990    idDelete(&s_temp);
1991    rChangeCurrRing(orig_ring);
1992  }
1993
1994  idDelete(&temp);
1995  ideal temp1=idRingCopy(s_temp1,syz_ring);
1996
1997  if (syz_ring!=orig_ring)
1998  {
1999    rChangeCurrRing(syz_ring);
2000    idDelete(&s_temp1);
2001    rChangeCurrRing(orig_ring);
2002    rDelete(syz_ring);
2003  }
2004
2005  for (i=0;i<IDELEMS(temp1);i++)
2006  {
2007    if ((temp1->m[i]!=NULL)
2008    && (pGetComp(temp1->m[i])<=length))
2009    {
2010      pDelete(&(temp1->m[i]));
2011    }
2012    else
2013    {
2014      p_Shift(&(temp1->m[i]),-length,currRing);
2015    }
2016  }
2017  temp1->rank = rk;
2018  idSkipZeroes(temp1);
2019
2020  return temp1;
2021}
2022*/
2023/*2
2024* represents (h1+h2)/h2=h1/(h1 intersect h2)
2025*/
2026//ideal idModulo (ideal h2,ideal h1)
2027ideal idModulo (ideal h2,ideal h1, tHomog hom, intvec ** w)
2028{
2029  intvec *wtmp=NULL;
2030
2031  int i,k,rk,flength=0,slength,length;
2032  poly p,q;
2033
2034  if (idIs0(h2))
2035    return idFreeModule(si_max(1,h2->ncols));
2036  if (!idIs0(h1))
2037    flength = id_RankFreeModule(h1,currRing);
2038  slength = id_RankFreeModule(h2,currRing);
2039  length  = si_max(flength,slength);
2040  if (length==0)
2041  {
2042    length = 1;
2043  }
2044  ideal temp = idInit(IDELEMS(h2),length+IDELEMS(h2));
2045  if ((w!=NULL)&&((*w)!=NULL))
2046  {
2047    //Print("input weights:");(*w)->show(1);PrintLn();
2048    int d;
2049    int k;
2050    wtmp=new intvec(length+IDELEMS(h2));
2051    for (i=0;i<length;i++)
2052      ((*wtmp)[i])=(**w)[i];
2053    for (i=0;i<IDELEMS(h2);i++)
2054    {
2055      poly p=h2->m[i];
2056      if (p!=NULL)
2057      {
2058        d = p_Deg(p,currRing);
2059        k= pGetComp(p);
2060        if (slength>0) k--;
2061        d +=((**w)[k]);
2062        ((*wtmp)[i+length]) = d;
2063      }
2064    }
2065    //Print("weights:");wtmp->show(1);PrintLn();
2066  }
2067  for (i=0;i<IDELEMS(h2);i++)
2068  {
2069    temp->m[i] = pCopy(h2->m[i]);
2070    q = pOne();
2071    pSetComp(q,i+1+length);
2072    pSetmComp(q);
2073    if(temp->m[i]!=NULL)
2074    {
2075      if (slength==0) p_Shift(&(temp->m[i]),1,currRing);
2076      p = temp->m[i];
2077      while (pNext(p)!=NULL) pIter(p);
2078      pNext(p) = q;
2079    }
2080    else
2081      temp->m[i]=q;
2082  }
2083  rk = k = IDELEMS(h2);
2084  if (!idIs0(h1))
2085  {
2086    pEnlargeSet(&(temp->m),IDELEMS(temp),IDELEMS(h1));
2087    IDELEMS(temp) += IDELEMS(h1);
2088    for (i=0;i<IDELEMS(h1);i++)
2089    {
2090      if (h1->m[i]!=NULL)
2091      {
2092        temp->m[k] = pCopy(h1->m[i]);
2093        if (flength==0) p_Shift(&(temp->m[k]),1,currRing);
2094        k++;
2095      }
2096    }
2097  }
2098
2099  ring orig_ring=currRing;
2100  ring syz_ring=rAssure_SyzComp(orig_ring, TRUE); rChangeCurrRing(syz_ring);
2101  if (TEST_OPT_RETURN_SB)
2102    rSetSyzComp(id_RankFreeModule(temp,orig_ring), syz_ring);
2103  else
2104    rSetSyzComp(length, syz_ring);
2105  ideal s_temp;
2106
2107  if (syz_ring != orig_ring)
2108  {
2109    s_temp = idrMoveR_NoSort(temp, orig_ring, syz_ring);
2110  }
2111  else
2112  {
2113    s_temp = temp;
2114  }
2115
2116  idTest(s_temp);
2117  ideal s_temp1 = kStd(s_temp,currQuotient,hom,&wtmp,NULL,length);
2118
2119  //if (wtmp!=NULL)  Print("output weights:");wtmp->show(1);PrintLn();
2120  if ((w!=NULL) && (*w !=NULL) && (wtmp!=NULL))
2121  {
2122    delete *w;
2123    *w=new intvec(IDELEMS(h2));
2124    for (i=0;i<IDELEMS(h2);i++)
2125      ((**w)[i])=(*wtmp)[i+length];
2126  }
2127  if (wtmp!=NULL) delete wtmp;
2128
2129  for (i=0;i<IDELEMS(s_temp1);i++)
2130  {
2131    if ((s_temp1->m[i]!=NULL)
2132    && (((int)pGetComp(s_temp1->m[i]))<=length))
2133    {
2134      p_Delete(&(s_temp1->m[i]),currRing);
2135    }
2136    else
2137    {
2138      p_Shift(&(s_temp1->m[i]),-length,currRing);
2139    }
2140  }
2141  s_temp1->rank = rk;
2142  idSkipZeroes(s_temp1);
2143
2144  if (syz_ring!=orig_ring)
2145  {
2146    rChangeCurrRing(orig_ring);
2147    s_temp1 = idrMoveR_NoSort(s_temp1, syz_ring, orig_ring);
2148    rDelete(syz_ring);
2149    // Hmm ... here seems to be a memory leak
2150    // However, simply deleting it causes memory trouble
2151    // idDelete(&s_temp);
2152  }
2153  else
2154  {
2155    idDelete(&temp);
2156  }
2157  idTest(s_temp1);
2158  return s_temp1;
2159}
2160
2161/*
2162*computes module-weights for liftings of homogeneous modules
2163*/
2164intvec * idMWLift(ideal mod,intvec * weights)
2165{
2166  if (idIs0(mod)) return new intvec(2);
2167  int i=IDELEMS(mod);
2168  while ((i>0) && (mod->m[i-1]==NULL)) i--;
2169  intvec *result = new intvec(i+1);
2170  while (i>0)
2171  {
2172    (*result)[i]=currRing->pFDeg(mod->m[i],currRing)+(*weights)[pGetComp(mod->m[i])];
2173  }
2174  return result;
2175}
2176
2177/*2
2178*sorts the kbase for idCoef* in a special way (lexicographically
2179*with x_max,...,x_1)
2180*/
2181ideal idCreateSpecialKbase(ideal kBase,intvec ** convert)
2182{
2183  int i;
2184  ideal result;
2185
2186  if (idIs0(kBase)) return NULL;
2187  result = idInit(IDELEMS(kBase),kBase->rank);
2188  *convert = idSort(kBase,FALSE);
2189  for (i=0;i<(*convert)->length();i++)
2190  {
2191    result->m[i] = pCopy(kBase->m[(**convert)[i]-1]);
2192  }
2193  return result;
2194}
2195
2196/*2
2197*returns the index of a given monom in the list of the special kbase
2198*/
2199int idIndexOfKBase(poly monom, ideal kbase)
2200{
2201  int j=IDELEMS(kbase);
2202
2203  while ((j>0) && (kbase->m[j-1]==NULL)) j--;
2204  if (j==0) return -1;
2205  int i=(currRing->N);
2206  while (i>0)
2207  {
2208    loop
2209    {
2210      if (pGetExp(monom,i)>pGetExp(kbase->m[j-1],i)) return -1;
2211      if (pGetExp(monom,i)==pGetExp(kbase->m[j-1],i)) break;
2212      j--;
2213      if (j==0) return -1;
2214    }
2215    if (i==1)
2216    {
2217      while(j>0)
2218      {
2219        if (pGetComp(monom)==pGetComp(kbase->m[j-1])) return j-1;
2220        if (pGetComp(monom)>pGetComp(kbase->m[j-1])) return -1;
2221        j--;
2222      }
2223    }
2224    i--;
2225  }
2226  return -1;
2227}
2228
2229/*2
2230*decomposes the monom in a part of coefficients described by the
2231*complement of how and a monom in variables occuring in how, the
2232*index of which in kbase is returned as integer pos (-1 if it don't
2233*exists)
2234*/
2235poly idDecompose(poly monom, poly how, ideal kbase, int * pos)
2236{
2237  int i;
2238  poly coeff=pOne(), base=pOne();
2239
2240  for (i=1;i<=(currRing->N);i++)
2241  {
2242    if (pGetExp(how,i)>0)
2243    {
2244      pSetExp(base,i,pGetExp(monom,i));
2245    }
2246    else
2247    {
2248      pSetExp(coeff,i,pGetExp(monom,i));
2249    }
2250  }
2251  pSetComp(base,pGetComp(monom));
2252  pSetm(base);
2253  pSetCoeff(coeff,nCopy(pGetCoeff(monom)));
2254  pSetm(coeff);
2255  *pos = idIndexOfKBase(base,kbase);
2256  if (*pos<0)
2257    p_Delete(&coeff,currRing);
2258  p_Delete(&base,currRing);
2259  return coeff;
2260}
2261
2262/*2
2263*returns a matrix A of coefficients with kbase*A=arg
2264*if all monomials in variables of how occur in kbase
2265*the other are deleted
2266*/
2267matrix idCoeffOfKBase(ideal arg, ideal kbase, poly how)
2268{
2269  matrix result;
2270  ideal tempKbase;
2271  poly p,q;
2272  intvec * convert;
2273  int i=IDELEMS(kbase),j=IDELEMS(arg),k,pos;
2274#if 0
2275  while ((i>0) && (kbase->m[i-1]==NULL)) i--;
2276  if (idIs0(arg))
2277    return mpNew(i,1);
2278  while ((j>0) && (arg->m[j-1]==NULL)) j--;
2279  result = mpNew(i,j);
2280#else
2281  result = mpNew(i, j);
2282  while ((j>0) && (arg->m[j-1]==NULL)) j--;
2283#endif
2284
2285  tempKbase = idCreateSpecialKbase(kbase,&convert);
2286  for (k=0;k<j;k++)
2287  {
2288    p = arg->m[k];
2289    while (p!=NULL)
2290    {
2291      q = idDecompose(p,how,tempKbase,&pos);
2292      if (pos>=0)
2293      {
2294        MATELEM(result,(*convert)[pos],k+1) =
2295            pAdd(MATELEM(result,(*convert)[pos],k+1),q);
2296      }
2297      else
2298        p_Delete(&q,currRing);
2299      pIter(p);
2300    }
2301  }
2302  idDelete(&tempKbase);
2303  return result;
2304}
2305
2306static void idDeleteComps(ideal arg,int* red_comp,int del)
2307// red_comp is an array [0..args->rank]
2308{
2309  int i,j;
2310  poly p;
2311
2312  for (i=IDELEMS(arg)-1;i>=0;i--)
2313  {
2314    p = arg->m[i];
2315    while (p!=NULL)
2316    {
2317      j = pGetComp(p);
2318      if (red_comp[j]!=j)
2319      {
2320        pSetComp(p,red_comp[j]);
2321        pSetmComp(p);
2322      }
2323      pIter(p);
2324    }
2325  }
2326  (arg->rank) -= del;
2327}
2328
2329/*2
2330* returns the presentation of an isomorphic, minimally
2331* embedded  module (arg represents the quotient!)
2332*/
2333ideal idMinEmbedding(ideal arg,BOOLEAN inPlace, intvec **w)
2334{
2335  if (idIs0(arg)) return idInit(1,arg->rank);
2336  int i,next_gen,next_comp;
2337  ideal res=arg;
2338  if (!inPlace) res = idCopy(arg);
2339  res->rank=si_max(res->rank,id_RankFreeModule(res,currRing));
2340  int *red_comp=(int*)omAlloc((res->rank+1)*sizeof(int));
2341  for (i=res->rank;i>=0;i--) red_comp[i]=i;
2342
2343  int del=0;
2344  loop
2345  {
2346    next_gen = id_ReadOutPivot(res, &next_comp, currRing);
2347    if (next_gen<0) break;
2348    del++;
2349    syGaussForOne(res,next_gen,next_comp,0,IDELEMS(res));
2350    for(i=next_comp+1;i<=arg->rank;i++) red_comp[i]--;
2351    if ((w !=NULL)&&(*w!=NULL))
2352    {
2353      for(i=next_comp;i<(*w)->length();i++) (**w)[i-1]=(**w)[i];
2354    }
2355  }
2356
2357  idDeleteComps(res,red_comp,del);
2358  idSkipZeroes(res);
2359  omFree(red_comp);
2360
2361  if ((w !=NULL)&&(*w!=NULL) &&(del>0))
2362  {
2363    intvec *wtmp=new intvec((*w)->length()-del);
2364    for(i=0;i<res->rank;i++) (*wtmp)[i]=(**w)[i];
2365    delete *w;
2366    *w=wtmp;
2367  }
2368  return res;
2369}
2370
2371#include <polys/clapsing.h>
2372
2373#if 0
2374poly id_GCD(poly f, poly g, const ring r)
2375{
2376  ring save_r=currRing;
2377  rChangeCurrRing(r);
2378  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2379  intvec *w = NULL;
2380  ideal S=idSyzygies(I,testHomog,&w);
2381  if (w!=NULL) delete w;
2382  poly gg=pTakeOutComp(&(S->m[0]),2);
2383  idDelete(&S);
2384  poly gcd_p=singclap_pdivide(f,gg,r);
2385  p_Delete(&gg,r);
2386  rChangeCurrRing(save_r);
2387  return gcd_p;
2388}
2389#else
2390poly id_GCD(poly f, poly g, const ring r)
2391{
2392  ideal I=idInit(2,1); I->m[0]=f; I->m[1]=g;
2393  intvec *w = NULL;
2394
2395  ring save_r = currRing; rChangeCurrRing(r); ideal S=idSyzygies(I,testHomog,&w); rChangeCurrRing(save_r);
2396
2397  if (w!=NULL) delete w;
2398  poly gg=p_TakeOutComp(&(S->m[0]), 2, r);
2399  id_Delete(&S, r);
2400  poly gcd_p=singclap_pdivide(f,gg, r);
2401  p_Delete(&gg, r);
2402
2403  return gcd_p;
2404}
2405#endif
2406
2407#if 0
2408/*2
2409* xx,q: arrays of length 0..rl-1
2410* xx[i]: SB mod q[i]
2411* assume: char=0
2412* assume: q[i]!=0
2413* destroys xx
2414*/
2415ideal id_ChineseRemainder(ideal *xx, number *q, int rl, const ring R)
2416{
2417  int cnt=IDELEMS(xx[0])*xx[0]->nrows;
2418  ideal result=idInit(cnt,xx[0]->rank);
2419  result->nrows=xx[0]->nrows; // for lifting matrices
2420  result->ncols=xx[0]->ncols; // for lifting matrices
2421  int i,j;
2422  poly r,h,hh,res_p;
2423  number *x=(number *)omAlloc(rl*sizeof(number));
2424  for(i=cnt-1;i>=0;i--)
2425  {
2426    res_p=NULL;
2427    loop
2428    {
2429      r=NULL;
2430      for(j=rl-1;j>=0;j--)
2431      {
2432        h=xx[j]->m[i];
2433        if ((h!=NULL)
2434        &&((r==NULL)||(p_LmCmp(r,h,R)==-1)))
2435          r=h;
2436      }
2437      if (r==NULL) break;
2438      h=p_Head(r, R);
2439      for(j=rl-1;j>=0;j--)
2440      {
2441        hh=xx[j]->m[i];
2442        if ((hh!=NULL) && (p_LmCmp(r,hh, R)==0))
2443        {
2444          x[j]=p_GetCoeff(hh, R);
2445          hh=p_LmFreeAndNext(hh, R);
2446          xx[j]->m[i]=hh;
2447        }
2448        else
2449          x[j]=n_Init(0, R->cf); // is R->cf really n_Q???, yes!
2450      }
2451
2452      number n=n_ChineseRemainder(x,q,rl, R->cf);
2453
2454      for(j=rl-1;j>=0;j--)
2455      {
2456        x[j]=NULL; // nlInit(0...) takes no memory
2457      }
2458      if (n_IsZero(n, R->cf)) p_Delete(&h, R);
2459      else
2460      {
2461        p_SetCoeff(h,n, R);
2462        //Print("new mon:");pWrite(h);
2463        res_p=p_Add_q(res_p, h, R);
2464      }
2465    }
2466    result->m[i]=res_p;
2467  }
2468  omFree(x);
2469  for(i=rl-1;i>=0;i--) id_Delete(&(xx[i]), R);
2470  omFree(xx);
2471  return result;
2472}
2473#endif
2474/* currently unsed:
2475ideal idChineseRemainder(ideal *xx, intvec *iv)
2476{
2477  int rl=iv->length();
2478  number *q=(number *)omAlloc(rl*sizeof(number));
2479  int i;
2480  for(i=0; i<rl; i++)
2481  {
2482    q[i]=nInit((*iv)[i]);
2483  }
2484  return idChineseRemainder(xx,q,rl);
2485}
2486*/
2487/*
2488 * lift ideal with coeffs over Z (mod N) to Q via Farey
2489 */
2490ideal id_Farey(ideal x, number N, const ring r)
2491{
2492  int cnt=IDELEMS(x)*x->nrows;
2493  ideal result=idInit(cnt,x->rank);
2494  result->nrows=x->nrows; // for lifting matrices
2495  result->ncols=x->ncols; // for lifting matrices
2496
2497  int i;
2498  for(i=cnt-1;i>=0;i--)
2499  {
2500    result->m[i]=p_Farey(x->m[i],N,r);
2501  }
2502  return result;
2503}
2504
2505
2506
2507
2508// uses glabl vars via pSetModDeg
2509/*
2510BOOLEAN idTestHomModule(ideal m, ideal Q, intvec *w)
2511{
2512  if ((Q!=NULL) && (!idHomIdeal(Q,NULL)))  { PrintS(" Q not hom\n"); return FALSE;}
2513  if (idIs0(m)) return TRUE;
2514
2515  int cmax=-1;
2516  int i;
2517  poly p=NULL;
2518  int length=IDELEMS(m);
2519  poly* P=m->m;
2520  for (i=length-1;i>=0;i--)
2521  {
2522    p=P[i];
2523    if (p!=NULL) cmax=si_max(cmax,(int)pMaxComp(p)+1);
2524  }
2525  if (w != NULL)
2526  if (w->length()+1 < cmax)
2527  {
2528    // Print("length: %d - %d \n", w->length(),cmax);
2529    return FALSE;
2530  }
2531
2532  if(w!=NULL)
2533    p_SetModDeg(w, currRing);
2534
2535  for (i=length-1;i>=0;i--)
2536  {
2537    p=P[i];
2538    poly q=p;
2539    if (p!=NULL)
2540    {
2541      int d=p_FDeg(p,currRing);
2542      loop
2543      {
2544        pIter(p);
2545        if (p==NULL) break;
2546        if (d!=p_FDeg(p,currRing))
2547        {
2548          //pWrite(q); wrp(p); Print(" -> %d - %d\n",d,pFDeg(p,currRing));
2549          if(w!=NULL)
2550            p_SetModDeg(NULL, currRing);
2551          return FALSE;
2552        }
2553      }
2554    }
2555  }
2556
2557  if(w!=NULL)
2558    p_SetModDeg(NULL, currRing);
2559
2560  return TRUE;
2561}
2562*/
2563
2564/// keeps the first k (>= 1) entries of the given ideal
2565/// (Note that the kept polynomials may be zero.)
2566void idKeepFirstK(ideal id, const int k)
2567{
2568   for (int i = IDELEMS(id)-1; i >= k; i--)
2569   {
2570      if (id->m[i] != NULL) pDelete(&id->m[i]);
2571   }
2572   int kk=k;
2573   if (k==0) kk=1; /* ideals must have at least one element(0)*/
2574   pEnlargeSet(&(id->m), IDELEMS(id), kk-IDELEMS(id));
2575   IDELEMS(id) = kk;
2576}
2577
2578/*
2579* compare the leading terms of a and b
2580*/
2581static int tCompare(const poly a, const poly b)
2582{
2583  if (b == NULL) return(a != NULL);
2584  if (a == NULL) return(-1);
2585
2586  /* a != NULL && b != NULL */
2587  int r = pLmCmp(a, b);
2588  if (r != 0) return(r);
2589  number h = nSub(pGetCoeff(a), pGetCoeff(b));
2590  r = -1 + nIsZero(h) + 2*nGreaterZero(h);   /* -1: <, 0:==, 1: > */
2591  nDelete(&h);
2592  return(r);
2593}
2594
2595/*
2596* compare a and b (rev-lex on terms)
2597*/
2598static int pCompare(const poly a, const poly b)
2599{
2600  int r = tCompare(a, b);
2601  if (r != 0) return(r);
2602
2603  poly aa = a;
2604  poly bb = b;
2605  while (r == 0 && aa != NULL && bb != NULL)
2606  {
2607    pIter(aa);
2608    pIter(bb);
2609    r = tCompare(aa, bb);
2610  }
2611  return(r);
2612}
2613
2614typedef struct
2615{
2616  poly p;
2617  int index;
2618} poly_sort;
2619
2620int pCompare_qsort(const void *a, const void *b)
2621{
2622  int res = pCompare(((poly_sort *)a)->p, ((poly_sort *)b)->p);
2623  return(res);
2624}
2625
2626void idSort_qsort(poly_sort *id_sort, int idsize)
2627{
2628  qsort(id_sort, idsize, sizeof(poly_sort), pCompare_qsort);
2629}
2630
2631/*2
2632* ideal id = (id[i])
2633* if id[i] = id[j] then id[j] is deleted for j > i
2634*/
2635void idDelEquals(ideal id)
2636{
2637  int idsize = IDELEMS(id);
2638  poly_sort *id_sort = (poly_sort *)omAlloc0(idsize*sizeof(poly_sort));
2639  for (int i = 0; i < idsize; i++)
2640  {
2641    id_sort[i].p = id->m[i];
2642    id_sort[i].index = i;
2643  }
2644  idSort_qsort(id_sort, idsize);
2645  int index, index_i, index_j;
2646  int i = 0;
2647  for (int j = 1; j < idsize; j++)
2648  {
2649    if (id_sort[i].p != NULL && pEqualPolys(id_sort[i].p, id_sort[j].p))
2650    {
2651      index_i = id_sort[i].index;
2652      index_j = id_sort[j].index;
2653      if (index_j > index_i)
2654      {
2655        index = index_j;
2656      }
2657      else
2658      {
2659        index = index_i;
2660        i = j;
2661      }
2662      pDelete(&id->m[index]);
2663    }
2664    else
2665    {
2666      i = j;
2667    }
2668  }
2669  omFreeSize((ADDRESS)(id_sort), idsize*sizeof(poly_sort));
2670}
Note: See TracBrowser for help on using the repository browser.