source: git/kernel/syz0.cc @ 2e4ec14

spielwiese
Last change on this file since 2e4ec14 was 2e4ec14, checked in by Yue Ren <ren@…>, 11 years ago
fix: -Wunused-parameter warnings
  • Property mode set to 100644
File size: 27.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: resolutions
6*/
7
8
9#include "config.h"
10#include <kernel/mod2.h>
11#include <misc/options.h>
12#include <omalloc/omalloc.h>
13#include <kernel/polys.h>
14#include <kernel/febase.h>
15#include <kernel/kstd1.h>
16#include <kernel/kutil.h>
17#include <kernel/stairc.h>
18//#include "cntrlc.h"
19#include <misc/intvec.h>
20#include <coeffs/numbers.h>
21#include <kernel/ideals.h>
22#include <misc/intvec.h>
23#include <polys/monomials/ring.h>
24#include <kernel/syz.h>
25#include <polys/kbuckets.h>
26#include <polys/prCopy.h>
27
28static void syInitSort(ideal arg,intvec **modcomp)
29{
30  int i,j,k,kk,kkk,jj;
31  idSkipZeroes(arg);
32  polyset F,oldF=arg->m;
33  int Fl=IDELEMS(arg);
34  int rkF=id_RankFreeModule(arg,currRing);
35  int syComponentOrder=currRing->ComponentOrder;
36
37  while ((Fl!=0) && (oldF[Fl-1]==NULL)) Fl--;
38  if (*modcomp!=NULL) delete modcomp;
39  *modcomp = new intvec(rkF+2);
40  F=(polyset)omAlloc0(IDELEMS(arg)*sizeof(poly));
41  j=0;
42  for(i=0;i<=rkF;i++)
43  {
44    k=0;
45    jj = j;
46    (**modcomp)[i] = j;
47    while (k<Fl)
48    {
49      while ((k<Fl) && (pGetComp(oldF[k]) != i)) k++;
50      if (k<Fl)
51      {
52        kk=jj;
53        while ((kk<Fl) && (F[kk]) && (pLmCmp(oldF[k],F[kk])!=syComponentOrder))
54        {
55            kk++;
56        }
57        for (kkk=j;kkk>kk;kkk--)
58        {
59          F[kkk] = F[kkk-1];
60        }
61        F[kk] = oldF[k];
62//Print("Element %d: ",kk);pWrite(F[kk]);
63        j++;
64        k++;
65      }
66    }
67  }
68  (**modcomp)[rkF+1] = Fl;
69  arg->m = F;
70  omFreeSize((ADDRESS)oldF,IDELEMS(arg)*sizeof(poly));
71}
72
73static void syCreatePairs(polyset F,int lini,int wend,int k,int j,int i,
74           polyset pairs,int regularPairs=0,ideal mW=NULL)
75{
76  int l,ii=0,jj;
77  poly p,q;
78
79  while (((k<wend) && (pGetComp(F[k]) == i)) ||
80         ((currQuotient!=NULL) && (k<regularPairs+IDELEMS(currQuotient))))
81  {
82    p = pOne();
83    if ((k<wend) && (pGetComp(F[k]) == i) && (k!=j))
84      pLcm(F[j],F[k],p);
85    else if (ii<IDELEMS(currQuotient))
86    {
87      q = pHead(F[j]);
88      if (mW!=NULL)
89      {
90        for(jj=1;jj<=(currRing->N);jj++)
91          pSetExp(q,jj,pGetExp(q,jj) -pGetExp(mW->m[pGetComp(q)-1],jj));
92        pSetm(q);
93      }
94      pLcm(q,currQuotient->m[ii],p);
95      if (mW!=NULL)
96      {
97        for(jj=1;jj<=(currRing->N);jj++)
98          pSetExp(p,jj,pGetExp(p,jj) +pGetExp(mW->m[pGetComp(p)-1],jj));
99        pSetm(p);
100      }
101      pDelete(&q);
102      k = regularPairs+ii;
103      ii++;
104    }
105    l=lini;
106    while ((l<k) && ((pairs[l]==NULL) || (!pDivisibleBy(pairs[l],p))))
107    {
108      if ((pairs[l]!=NULL) && (pDivisibleBy(p,pairs[l])))
109        pDelete(&(pairs[l]));
110      l++;
111    }
112    if (l==k)
113    {
114      pSetm(p);
115      pairs[l] = p;
116    }
117    else
118      pDelete(&p);
119    k++;
120  }
121}
122
123static poly syRedtail2(poly p, polyset redWith, intvec *modcomp)
124{
125  poly h, hn;
126  int hncomp,nxt;
127  int j;
128
129  h = p;
130  hn = pNext(h);
131  while(hn != NULL)
132  {
133    hncomp = pGetComp(hn);
134    j = (*modcomp)[hncomp];
135    nxt = (*modcomp)[hncomp+1];
136    while (j < nxt)
137    {
138      if (pLmDivisibleByNoComp(redWith[j], hn))
139      {
140        //if (TEST_OPT_PROT) PrintS("r");
141        hn = ksOldSpolyRed(redWith[j],hn);
142        if (hn == NULL)
143        {
144          pNext(h) = NULL;
145          return p;
146        }
147        hncomp = pGetComp(hn);
148        j = (*modcomp)[hncomp];
149        nxt = (*modcomp)[hncomp+1];
150      }
151      else
152      {
153        j++;
154      }
155    }
156    h = pNext(h) = hn;
157    hn = pNext(h);
158  }
159  return p;
160}
161
162/*2
163* computes the Schreyer syzygies in the local case
164* input: arg   (only allocated: Shdl, Smax)
165* output: Shdl, Smax
166*/
167static ideal sySchreyersSyzygiesFM(ideal arg,intvec ** modcomp)
168{
169  int Fl=IDELEMS(arg);
170  while ((Fl!=0) && (arg->m[Fl-1]==NULL)) Fl--;
171  ideal result=idInit(16,arg->rank+Fl);
172  polyset F=arg->m,*Shdl=&(result->m);
173  if (Fl==0) return result;
174
175  int i,j,l,k,totalToRed,ecartToRed,kk;
176  int bestEcart,totalmax,rkF,Sl=0,smax,tmax,tl;
177  int *ecartS, *ecartT, *totalS,
178    *totalT=NULL, *temp=NULL;
179  polyset pairs,S,T,ST/*,oldF*/;
180  poly p,q,toRed;
181  BOOLEAN notFound = FALSE;
182  intvec * newmodcomp = new intvec(Fl+2);
183  intvec * tempcomp;
184
185//Print("Naechster Modul\n");
186//idPrint(arg);
187/*-------------initializing the sets--------------------*/
188  ST=(polyset)omAlloc0(Fl*sizeof(poly));
189  S=(polyset)omAlloc0(Fl*sizeof(poly));
190  ecartS=(int*)omAlloc(Fl*sizeof(int));
191  totalS=(int*)omAlloc(Fl*sizeof(int));
192  T=(polyset)omAlloc0(2*Fl*sizeof(poly));
193  ecartT=(int*)omAlloc(2*Fl*sizeof(int));
194  totalT=(int*)omAlloc(2*Fl*sizeof(int));
195  pairs=(polyset)omAlloc0(Fl*sizeof(poly));
196
197  smax = Fl;
198  tmax = 2*Fl;
199  for (j=1;j<IDELEMS(arg);j++)
200  {
201    if (arg->m[j] != NULL)
202    {
203      assume (arg->m[j-1] != NULL);
204      assume (pGetComp(arg->m[j-1])-pGetComp(arg->m[j])<=0);
205    }
206  }
207  rkF=id_RankFreeModule(arg,currRing);
208/*----------------construction of the new ordering----------*/
209  if (rkF>0)
210    rSetSyzComp(rkF, currRing);
211  else
212    rSetSyzComp(1, currRing);
213/*----------------creating S--------------------------------*/
214  for(j=0;j<Fl;j++)
215  {
216    S[j] = pCopy(F[j]);
217    totalS[j] = p_LDeg(S[j],&k,currRing);
218    ecartS[j] = totalS[j]-p_FDeg(S[j],currRing);
219//Print("%d", pGetComp(S[j]));PrintS("  ");
220    p = S[j];
221    if (rkF==0) pSetCompP(p,1);
222    while (pNext(p)!=NULL) pIter(p);
223    pNext(p) = pHead(F[j]);
224    pIter(p);
225    if (rkF==0)
226      pSetComp(p,j+2);
227    else
228      pSetComp(p,rkF+j+1);
229    pSetmComp(p);
230  }
231//PrintLn();
232  if (rkF==0) rkF = 1;
233/*---------------creating the initial for T----------------*/
234  j=0;
235  l=-1;
236  totalmax=-1;
237  for (k=0;k<smax;k++)
238    if (totalS[k]>totalmax) totalmax=totalS[k];
239  for (kk=1;kk<=rkF;kk++)
240  {
241    for (k=0;k<=totalmax;k++)
242    {
243      for (l=0;l<smax;l++)
244      {
245        if ((pGetComp(S[l])==kk) && (totalS[l]==k))
246        {
247          ST[j] = S[l];
248          totalT[j] = totalS[l];
249          ecartT[j] = ecartS[l];
250//Print("%d", totalS[l]);PrintS("  ");
251          j++;
252        }
253      }
254    }
255  }
256//PrintLn();
257  for (j=0;j<smax;j++)
258  {
259     totalS[j] = totalT[j];
260     ecartS[j] = ecartT[j];
261  }
262
263/*---------------computing---------------------------------*/
264  for(j=0;j<smax;j++)
265  {
266    (*newmodcomp)[j+1] = Sl;
267    i = pGetComp(S[j]);
268    int syComponentOrder= currRing->ComponentOrder;
269    int lini,wend;
270    if (syComponentOrder==1)
271    {
272      lini=k=j+1;
273      wend=Fl;
274    }
275    else
276    {
277      lini=k=0;
278      while ((k<j) && (pGetComp(S[k]) != i)) k++;
279      wend=j;
280    }
281    if (TEST_OPT_PROT)
282    {
283      Print("(%d)",Fl-j);
284      mflush();
285    }
286    syCreatePairs(S,lini,wend,k,j,i,pairs);
287    for (k=lini;k<wend;k++)
288    {
289      if (pairs[k]!=NULL)
290      {
291/*--------------creating T----------------------------------*/
292        for (l=0;l<smax;l++)
293        {
294          ecartT[l] = ecartS[l];
295          totalT[l] = totalS[l];
296          T[l] = ST[l];
297        }
298        tl = smax;
299        tempcomp = ivCopy(*modcomp);
300/*--------------begin to reduce-----------------------------*/
301        toRed = ksOldCreateSpoly(S[j],S[k]);
302        ecartToRed = 1;
303        bestEcart = 1;
304        if (TEST_OPT_DEBUG)
305        {
306          PrintS("pair: ");pWrite0(S[j]);PrintS(" ");pWrite(S[k]);
307        }
308        if (TEST_OPT_PROT)
309        {
310           PrintS(".");
311           mflush();
312        }
313//Print("Reduziere Paar %d,%d (ecart %d): \n",j,k,ecartToRed);
314//Print("Poly %d: ",j);pWrite(S[j]);
315//Print("Poly %d: ",k);pWrite(S[k]);
316//Print("Spoly: ");pWrite(toRed);
317        while (pGetComp(toRed)<=rkF)
318        {
319          if (TEST_OPT_DEBUG)
320          {
321            PrintS("toRed: ");pWrite(toRed);
322          }
323/*
324*         if ((bestEcart) || (ecartToRed!=0))
325*         {
326*/
327            totalToRed = p_LDeg(toRed,&kk,currRing);
328            ecartToRed = totalToRed-p_FDeg(toRed,currRing);
329/*
330*         }
331*/
332//Print("toRed now (neuer ecart %d): ",ecartToRed);pWrite(toRed);
333          notFound = TRUE;
334          bestEcart = 32000;  //a very large integer
335          p = NULL;
336          int l=0;
337#define OLD_SEARCH
338#ifdef OLD_SEARCH
339          while ((l<tl) && (pGetComp(T[l])<pGetComp(toRed))) l++;
340          //assume (l==(**modcomp)[pGetComp(toRed)]);
341          while ((l<tl) && (notFound))
342#else
343          l = (**modcomp)[pGetComp(toRed)];
344          int kkk = (**modcomp)[pGetComp(toRed)+1];
345          while ((l<kkk) && (notFound))
346#endif
347          {
348            if ((ecartT[l]<bestEcart) && (pDivisibleBy(T[l],toRed)))
349            {
350              if (ecartT[l]<=ecartToRed) notFound = FALSE;
351              p = T[l];
352              bestEcart = ecartT[l];
353            }
354            l++;
355          }
356          if (p==NULL)
357          {
358            WerrorS("ideal not a standard basis");//no polynom for reduction
359            pDelete(&toRed);
360            for(k=j;k<Fl;k++) pDelete(&(pairs[k]));
361            omFreeSize((ADDRESS)pairs,Fl*sizeof(poly));
362            omFreeSize((ADDRESS)ST,Fl*sizeof(poly));
363            omFreeSize((ADDRESS)S,Fl*sizeof(poly));
364            omFreeSize((ADDRESS)T,tmax*sizeof(poly));
365            omFreeSize((ADDRESS)ecartT,tmax*sizeof(int));
366            omFreeSize((ADDRESS)totalT,tmax*sizeof(int));
367            omFreeSize((ADDRESS)ecartS,Fl*sizeof(int));
368            omFreeSize((ADDRESS)totalS,Fl*sizeof(int));
369            for(k=0;k<IDELEMS(result);k++) pDelete(&((*Shdl)[k]));
370            return result;
371          }
372          else
373          {
374//Print("reduced with (ecart %d): ",bestEcart);wrp(p);PrintLn();
375            if (notFound)
376            {
377              if (tl>=tmax)
378              {
379                pEnlargeSet(&T,tmax,16);
380                tmax += 16;
381                temp = (int*)omAlloc((tmax+16)*sizeof(int));
382                for(l=0;l<tmax;l++) temp[l]=totalT[l];
383                totalT = temp;
384                temp = (int*)omAlloc((tmax+16)*sizeof(int));
385                for(l=0;l<tmax;l++) temp[l]=ecartT[l];
386                ecartT = temp;
387              }
388//PrintS("t");
389              int comptR=pGetComp(toRed);
390              for (l=tempcomp->length()-1;l>comptR;l--)
391              {
392                if ((*tempcomp)[l]>0)
393                  (*tempcomp)[l]++;
394              }
395              l=0;
396              while ((l<tl) && (comptR>pGetComp(T[l]))) l++;
397              while ((l<tl) && (totalT[l]<=totalToRed)) l++;
398              for (kk=tl;kk>l;kk--)
399              {
400                T[kk]=T[kk-1];
401                totalT[kk]=totalT[kk-1];
402                ecartT[kk]=ecartT[kk-1];
403              }
404              q = pCopy(toRed);
405              pNorm(q);
406              T[l] = q;
407              totalT[l] = totalToRed;
408              ecartT[l] = ecartToRed;
409              tl++;
410            }
411            toRed = ksOldSpolyRed(p,toRed);
412          }
413        }
414//Print("toRed finally (neuer ecart %d): ",ecartToRed);pWrite(toRed);
415//PrintS("s");
416        if (pGetComp(toRed)>rkF)
417        {
418          if (Sl>=IDELEMS(result))
419          {
420            pEnlargeSet(Shdl,IDELEMS(result),16);
421            IDELEMS(result) += 16;
422          }
423          //p_Shift(&toRed,-rkF,currRing);
424          pNorm(toRed);
425          (*Shdl)[Sl] = toRed;
426          Sl++;
427        }
428/*----------------deleting all polys not from ST--------------*/
429        for(l=0;l<tl;l++)
430        {
431          kk=0;
432          while ((kk<smax) && (T[l] != S[kk])) kk++;
433          if (kk>=smax)
434          {
435            pDelete(&T[l]);
436//Print ("#");
437          }
438        }
439        delete tempcomp;
440      }
441    }
442    for(k=lini;k<wend;k++) pDelete(&(pairs[k]));
443  }
444  (*newmodcomp)[Fl+1] = Sl;
445  omFreeSize((ADDRESS)pairs,Fl*sizeof(poly));
446  omFreeSize((ADDRESS)ST,Fl*sizeof(poly));
447  omFreeSize((ADDRESS)S,Fl*sizeof(poly));
448  omFreeSize((ADDRESS)T,tmax*sizeof(poly));
449  omFreeSize((ADDRESS)ecartT,tmax*sizeof(int));
450  omFreeSize((ADDRESS)totalT,tmax*sizeof(int));
451  omFreeSize((ADDRESS)ecartS,Fl*sizeof(int));
452  omFreeSize((ADDRESS)totalS,Fl*sizeof(int));
453  delete *modcomp;
454  *modcomp = newmodcomp;
455  return result;
456}
457
458/*3
459*special Normalform for Schreyer in factor rings
460*/
461poly sySpecNormalize(poly toNorm,ideal mW=NULL)
462{
463  int j,i=0;
464  poly p;
465
466  if (toNorm==NULL) return NULL;
467  p = pHead(toNorm);
468  if (mW!=NULL)
469  {
470    for(j=1;j<=(currRing->N);j++)
471      pSetExp(p,j,pGetExp(p,j) -pGetExp(mW->m[pGetComp(p)-1],j));
472  }
473  while ((p!=NULL) && (i<IDELEMS(currQuotient)))
474  {
475    if (pDivisibleBy(currQuotient->m[i],p))
476    {
477      //pNorm(toNorm);
478      toNorm = ksOldSpolyRed(currQuotient->m[i],toNorm);
479      pDelete(&p);
480      if (toNorm==NULL) return NULL;
481      p = pHead(toNorm);
482      if (mW!=NULL)
483      {
484        for(j=1;j<=(currRing->N);j++)
485          pSetExp(p,j,pGetExp(p,j) -pGetExp(mW->m[pGetComp(p)-1],j));
486      }
487      i = 0;
488    }
489    else
490    {
491      i++;
492    }
493  }
494  pDelete(&p);
495  return toNorm;
496}
497
498/*2
499* computes the Schreyer syzygies in the global case
500* input: F
501* output: Shdl, Smax
502* modcomp, length stores the start position of the module comp. in arg
503*/
504static ideal sySchreyersSyzygiesFB(ideal arg,intvec ** modcomp,ideal mW,BOOLEAN redTail=TRUE)
505{
506  kBucket_pt sy0buck = kBucketCreate(currRing);
507
508  int Fl=IDELEMS(arg);
509  while ((Fl!=0) && (arg->m[Fl-1]==NULL)) Fl--;
510  ideal result=idInit(16,Fl);
511  int i,j,l,k,kkk,rkF,Sl=0,syComponentOrder=currRing->ComponentOrder;
512  int /*fstart,*/wend,lini,ltR,gencQ=0;
513  intvec *newmodcomp;
514  int *Flength;
515  polyset pairs,F=arg->m,*Shdl=&(result->m);
516  poly /*p,*/q,toRed,syz,lastmonom,multWith;
517  BOOLEAN isNotReduced=TRUE;
518
519//#define WRITE_BUCKETS
520#ifdef WRITE_BUCKETS
521  PrintS("Input: \n");
522  ideal twr=idHead(arg);
523  idPrint(arg);
524  idDelete(&twr);
525  if (modcomp!=NULL) (*modcomp)->show(0,0);
526#endif
527   
528  newmodcomp = new intvec(Fl+2);
529  //for (j=0;j<Fl;j++) pWrite(F[j]);
530  //PrintLn();
531  if (currQuotient==NULL)
532    pairs=(polyset)omAlloc0(Fl*sizeof(poly));
533  else
534  {
535    gencQ = IDELEMS(currQuotient);
536    pairs=(polyset)omAlloc0((Fl+gencQ)*sizeof(poly));
537  }
538  rkF=id_RankFreeModule(arg,currRing);
539  Flength = (int*)omAlloc0(Fl*sizeof(int));
540  for(j=0;j<Fl;j++)
541  {
542    Flength[j] = pLength(F[j]);
543  }
544  for(j=0;j<Fl;j++)
545  {
546    (*newmodcomp)[j+1] = Sl;
547    if (TEST_OPT_PROT)
548    {
549      Print("(%d)",Fl-j);
550      mflush();
551    }
552    i = pGetComp(F[j]);
553    if (syComponentOrder==1)
554    {
555      lini=k=j+1;
556      wend=Fl;
557    }
558    else
559    {
560      lini=k=0;
561      while ((k<j) && (pGetComp(F[k]) != i)) k++;
562      wend=j;
563    }
564    syCreatePairs(F,lini,wend,k,j,i,pairs,Fl,mW);
565    if (currQuotient!=NULL) wend = Fl+gencQ;
566    for (k=lini;k<wend;k++)
567    {
568      if (pairs[k]!=NULL)
569      {
570        if (TEST_OPT_PROT)
571        {
572           PrintS(".");
573           mflush();
574        }
575        //begins to construct the syzygy
576        if (k<Fl)
577        {
578          number an=nCopy(pGetCoeff(F[k])),bn=nCopy(pGetCoeff(F[j]));
579          /*int ct =*/ (void) ksCheckCoeff(&an, &bn, currRing->cf);
580          syz = pCopy(pairs[k]);
581          //syz->coef = nCopy(F[k]->coef);
582          syz->coef = an;
583          //syz->coef = nNeg(syz->coef);
584          pNext(syz) = pairs[k];
585          lastmonom = pNext(syz);
586          //lastmonom->coef = nCopy(F[j]->coef);
587          lastmonom->coef = bn;
588          lastmonom->coef = nNeg(lastmonom->coef);
589          pSetComp(lastmonom,k+1);
590        }
591        else
592        {
593          syz = pairs[k];
594          syz->coef = nCopy(currQuotient->m[k-Fl]->coef);
595          syz->coef = nNeg(syz->coef);
596          lastmonom = syz;
597          multWith = pDivide(syz,F[j]);
598          multWith->coef = nCopy(currQuotient->m[k-Fl]->coef);
599        }
600        pSetComp(syz,j+1);
601        pairs[k] = NULL;
602        //the next term of the syzygy
603        //constructs the spoly
604        if (TEST_OPT_DEBUG)
605        {
606          if (k<Fl)
607          {
608            PrintS("pair: ");pWrite0(F[j]);PrintS("  ");pWrite(F[k]);
609          }
610          else
611          {
612            PrintS("pair: ");pWrite0(F[j]);PrintS("  ");pWrite(currQuotient->m[k-Fl]);
613          }
614        }
615        if (k<Fl)
616          toRed = ksOldCreateSpoly(F[j],F[k]);
617        else
618        {
619          q = pMult_mm(pCopy(F[j]),multWith);
620          toRed = sySpecNormalize(q,mW);
621          pDelete(&multWith);
622        }
623        kBucketInit(sy0buck,toRed,-1);
624        toRed = kBucketGetLm(sy0buck);
625        isNotReduced = TRUE;
626        while (toRed!=NULL)
627        {
628          if (TEST_OPT_DEBUG)
629          {
630            PrintS("toRed: ");pWrite(toRed);
631          }
632//          l=0;
633//          while ((l<Fl) && (!pDivisibleBy(F[l],toRed))) l++;
634//          if (l>=Fl)
635          l = (**modcomp)[pGetComp(toRed)+1]-1;
636          kkk = (**modcomp)[pGetComp(toRed)];
637          while ((l>=kkk) && (!pDivisibleBy(F[l],toRed))) l--;
638#ifdef WRITE_BUCKETS
639          kBucketClear(sy0buck,&toRed,&ltR);
640          printf("toRed in Pair[%d, %d]:", j, k);
641          pWrite(toRed);
642          kBucketInit(sy0buck,toRed,-1);
643#endif
644
645          if (l<kkk)
646          {
647            if ((currQuotient!=NULL) && (isNotReduced))
648            {
649              kBucketClear(sy0buck,&toRed,&ltR);
650              toRed = sySpecNormalize(toRed,mW);
651#ifdef WRITE_BUCKETS
652              printf("toRed in Pair[%d, %d]:", j, k);
653              pWrite(toRed);
654#endif
655              kBucketInit(sy0buck,toRed,-1);
656              toRed = kBucketGetLm(sy0buck);
657              isNotReduced = FALSE;
658            }
659            else
660            {
661              //no polynom for reduction
662              WerrorS("ideal not a standard basis");
663              pDelete(&toRed);
664              pDelete(&syz);
665              for(k=j;k<Fl;k++) pDelete(&(pairs[k]));
666              omFreeSize((ADDRESS)pairs,(Fl + gencQ)*sizeof(poly));
667              for(k=0;k<IDELEMS(result);k++) pDelete(&((*Shdl)[k]));
668               
669              kBucketDestroy(&(sy0buck));
670              return result;
671            }
672          }
673          else
674          {
675            //the next monom of the syzygy
676            isNotReduced = TRUE;
677            if (TEST_OPT_DEBUG)
678            {
679              PrintS("reduced with: ");pWrite(F[l]);
680            }
681            pNext(lastmonom) = pHead(toRed);
682            pIter(lastmonom);
683            lastmonom->coef = nDiv(lastmonom->coef,F[l]->coef);
684            //lastmonom->coef = nNeg(lastmonom->coef);
685            pSetComp(lastmonom,l+1);
686            //computes the new toRed
687            number up = kBucketPolyRed(sy0buck,F[l],Flength[l],NULL);
688            if (! nIsOne(up))
689            {
690              // Thomas: Now do whatever you need to do
691#ifdef WRITE_BUCKETS
692              PrintS("multiplied with: ");nWrite(up);PrintLn();
693#endif
694              pMult_nn(syz,up);
695            }
696            nDelete(&up);
697
698            toRed = kBucketGetLm(sy0buck);
699            //the module component of the new monom
700//pWrite(toRed);
701          }
702        }
703        kBucketClear(sy0buck,&toRed,&ltR); //Zur Sichereheit
704//PrintLn();
705        if (syz!=NULL)
706        {
707          if (Sl>=IDELEMS(result))
708          {
709            pEnlargeSet(Shdl,IDELEMS(result),16);
710            IDELEMS(result) += 16;
711          }
712          pNorm(syz);
713          if (BTEST1(OPT_REDTAIL) && redTail)
714          {
715            (*newmodcomp)[j+2] = Sl;
716            (*Shdl)[Sl] = syRedtail2(syz,*Shdl,newmodcomp);
717            (*newmodcomp)[j+2] = 0;
718          }
719          else
720            (*Shdl)[Sl] = syz;
721          Sl++;
722        }
723      }
724    }
725//    for(k=j;k<Fl;k++) pDelete(&(pairs[k]));
726  }
727  (*newmodcomp)[Fl+1] = Sl;
728  if (currQuotient==NULL)
729    omFreeSize((ADDRESS)pairs,Fl*sizeof(poly));
730  else
731    omFreeSize((ADDRESS)pairs,(Fl+IDELEMS(currQuotient))*sizeof(poly));
732  omFreeSize((ADDRESS)Flength,Fl*sizeof(int));
733  delete *modcomp;
734  *modcomp = newmodcomp;
735   
736  kBucketDestroy(&(sy0buck));
737  return result;
738}
739
740void syReOrderResolventFB(resolvente res,int length, int initial)
741{
742  int syzIndex=length-1,i,j;
743  poly p;
744
745  while ((syzIndex!=0) && (res[syzIndex]==NULL)) syzIndex--;
746  while (syzIndex>=initial)
747  {
748    for(i=0;i<IDELEMS(res[syzIndex]);i++)
749    {
750      p = res[syzIndex]->m[i];
751
752      while (p!=NULL)
753      {
754        if (res[syzIndex-1]->m[pGetComp(p)-1]!=NULL)
755        {
756          for(j=1;j<=(currRing->N);j++)
757          {
758            pSetExp(p,j,pGetExp(p,j)
759                        -pGetExp(res[syzIndex-1]->m[pGetComp(p)-1],j));
760          }
761        }
762        else
763          PrintS("error in the resolvent\n");
764        pSetm(p);
765        pIter(p);
766      }
767    }
768    syzIndex--;
769  }
770}
771
772static void syMergeSortResolventFB(resolvente res,int length, int initial=1)
773{
774  int syzIndex=length-1,i,j;
775  poly qq,pp,result=NULL;
776  poly p;
777
778  while ((syzIndex!=0) && (res[syzIndex]==NULL)) syzIndex--;
779  while (syzIndex>=initial)
780  {
781    for(i=0;i<IDELEMS(res[syzIndex]);i++)
782    {
783      p = res[syzIndex]->m[i];
784      if (p != NULL)
785      {
786        for (;;)
787        {
788          qq = p;
789          for(j=1;j<=(currRing->N);j++)
790          {
791            pSetExp(p,j,pGetExp(p,j)
792                        -pGetExp(res[syzIndex-1]->m[pGetComp(p)-1],j));
793          }
794          pSetm(p);
795          for (;;)
796          {
797            if (pNext(p) == NULL)
798            {
799              pAdd(result, qq);
800              break;
801            }
802            pp = pNext(p);
803            for(j=1;j<=(currRing->N);j++)
804            {
805              pSetExp(pp,j,pGetExp(pp,j)
806                          -pGetExp(res[syzIndex-1]->m[pGetComp(pp)-1],j));
807            }
808            pSetm(pp);
809            if (pCmp(p,pNext(p)) != 1)
810            {
811              pp = p;
812              pIter(p);
813              pNext(pp) = NULL;
814              result = pAdd(result, qq);
815              break;
816            }
817            pIter(p);
818          }
819        }
820      }
821      res[syzIndex]->m[i] = p;
822    }
823    syzIndex--;
824  }
825}
826
827BOOLEAN syTestOrder(ideal M)
828{
829  int i=id_RankFreeModule(M,currRing);
830  if (i == 0) return FALSE;
831  int j=0;
832
833  while ((currRing->order[j]!=ringorder_c) && (currRing->order[j]!=ringorder_C))
834    j++;
835  if (currRing->order[j+1]!=0)
836    return TRUE;
837  return FALSE;
838}
839
840static void idShift(ideal arg,int index)
841{
842  int i,j=rGetMaxSyzComp(index, currRing);
843  for (i=0;i<IDELEMS(arg);i++)
844  {
845    if (arg->m[i]!=NULL)
846      p_Shift(&arg->m[i],-j,currRing);
847  }
848}
849
850#if 0 /*debug only */
851static void syPrintResolution(resolvente res,int start,int length)
852{
853  while ((start < length) && (res[start]))
854  {
855    Print("Syz(%d): \n",start);
856    idTest(res[start]);
857    //idPrint(res[start]);
858    start++;
859  }
860}
861#endif
862
863resolvente sySchreyerResolvente(ideal arg, int maxlength, int * length,
864                                BOOLEAN isMonomial, BOOLEAN /*notReplace*/)
865{
866  ideal mW=NULL;
867  int i,syzIndex = 0,j=0;
868  intvec * modcomp=NULL,*w=NULL;
869  // int ** wv=NULL;
870  tHomog hom=(tHomog)idHomModule(arg,NULL,&w);
871  ring origR = currRing;
872  ring syRing = NULL;
873
874  if ((!isMonomial) && syTestOrder(arg))
875  {
876    WerrorS("sres only implemented for modules with ordering  ..,c or ..,C");
877    return NULL;
878  }
879  *length = 4;
880  resolvente res = (resolvente)omAlloc0(4*sizeof(ideal)),newres;
881  res[0] = idCopy(arg);
882
883  while ((!idIs0(res[syzIndex])) && ((maxlength==-1) || (syzIndex<maxlength)))
884  {
885    i = IDELEMS(res[syzIndex]);
886    //while ((i!=0) && (!res[syzIndex]->m[i-1])) i--;
887    if (syzIndex+1==*length)
888    {
889      newres = (resolvente)omAlloc0((*length+4)*sizeof(ideal));
890      // for (j=0;j<*length+4;j++) newres[j] = NULL;
891      for (j=0;j<*length;j++) newres[j] = res[j];
892      omFreeSize((ADDRESS)res,*length*sizeof(ideal));
893      *length += 4;
894      res=newres;
895    }
896     
897    if ((hom==isHomog)|| (rHasGlobalOrdering(origR)))
898    {
899      if (syzIndex==0) syInitSort(res[0],&modcomp);
900       
901      if ((syzIndex==0) && !rRing_has_CompLastBlock(currRing))
902        res[syzIndex+1] = sySchreyersSyzygiesFB(res[syzIndex],&modcomp,mW,FALSE);
903      else
904        res[syzIndex+1] = sySchreyersSyzygiesFB(res[syzIndex],&modcomp,mW);
905       
906      mW = res[syzIndex];
907    }
908//idPrint(res[syzIndex+1]);
909
910    if ((syzIndex==0))
911    {
912      if ((hom==isHomog)|| (rHasGlobalOrdering(origR)))
913      {
914        syRing = rAssure_CompLastBlock(origR, TRUE);
915        if (syRing != origR)
916        {
917          rChangeCurrRing(syRing);
918          for (i=0; i<IDELEMS(res[1]); i++)
919          {
920            res[1]->m[i] = prMoveR( res[1]->m[i], origR, syRing);
921          }
922        }
923        idTest(res[1]);
924      }
925      else
926      {
927        syRing = rAssure_SyzComp_CompLastBlock(origR, TRUE);
928        if (syRing != origR)
929        {
930          rChangeCurrRing(syRing);
931          for (i=0; i<IDELEMS(res[0]); i++)
932          {
933            res[0]->m[i] = prMoveR( res[0]->m[i], origR, syRing);
934          }
935        }
936        idTest(res[0]);
937      }
938    }
939    if ((hom!=isHomog) && (rHasLocalOrMixedOrdering(origR)))
940    {
941      if (syzIndex==0) syInitSort(res[0],&modcomp);
942      res[syzIndex+1] = sySchreyersSyzygiesFM(res[syzIndex],&modcomp);
943    }
944    syzIndex++;
945    if (TEST_OPT_PROT) Print("[%d]\n",syzIndex);
946  }
947  //syPrintResolution(res,1,*length);
948  if ((hom!=isHomog) && (rHasLocalOrMixedOrdering(origR)))
949  {
950    syzIndex = 1;
951    while ((syzIndex < *length) && (!idIs0(res[syzIndex])))
952    {
953      idShift(res[syzIndex],syzIndex);
954      syzIndex++;
955    }
956  }
957  if ((hom==isHomog) || (rHasGlobalOrdering(origR)))
958    syzIndex = 1;
959  else
960    syzIndex = 0;
961  syReOrderResolventFB(res,*length,syzIndex+1);
962  if (/*ringOrderChanged:*/ origR!=syRing && syRing != NULL)
963  {
964    rChangeCurrRing(origR);
965    // Thomas: Here I assume that all (!) polys of res live in tmpR
966    while ((syzIndex < *length) && (res[syzIndex]))
967    {
968      for (i=0;i<IDELEMS(res[syzIndex]);i++)
969      {
970        if (res[syzIndex]->m[i])
971        {
972          res[syzIndex]->m[i] = prMoveR( res[syzIndex]->m[i], syRing, origR);
973        }
974      }
975      syzIndex++;
976    }
977//    j = 0; while (currRing->order[j]!=0) j++; // What was this for???!
978    rDelete(syRing);
979  }
980  else
981  {
982    // Thomas -- are you sure that you have to "reorder" here?
983    while ((syzIndex < *length) && (res[syzIndex]))
984    {
985      for (i=0;i<IDELEMS(res[syzIndex]);i++)
986      {
987        if (res[syzIndex]->m[i])
988          res[syzIndex]->m[i] = pSortCompCorrect(res[syzIndex]->m[i]);
989      }
990      syzIndex++;
991    }
992  }
993  if ((hom==isHomog) || (rHasGlobalOrdering(origR)))
994  {
995    if (res[1]!=NULL)
996    {
997      syReOrderResolventFB(res,2,1);
998      for (i=0;i<IDELEMS(res[1]);i++)
999      {
1000        if (res[1]->m[i])
1001          res[1]->m[i] = pSort(res[1]->m[i]);
1002      }
1003    }
1004  }
1005  //syPrintResolution(res,0,*length);
1006
1007  //syMergeSortResolventFB(res,*length);
1008  if (modcomp!=NULL) delete modcomp;
1009  if (w!=NULL) delete w;
1010  return res;
1011}
1012
1013syStrategy sySchreyer(ideal arg, int maxlength)
1014{
1015  int rl;
1016  resolvente fr = sySchreyerResolvente(arg,maxlength,&(rl));
1017  if (fr==NULL) return NULL;
1018
1019  // int typ0;
1020  syStrategy result=(syStrategy)omAlloc0(sizeof(ssyStrategy));
1021  result->length=rl;
1022  result->fullres = (resolvente)omAlloc0((rl /*result->length*/+1)*sizeof(ideal));
1023  for (int i=rl /*result->length*/-1;i>=0;i--)
1024  {
1025    if (fr[i]!=NULL)
1026      result->fullres[i] = fr[i];
1027      fr[i] = NULL;
1028  }
1029  if (currQuotient!=NULL)
1030  {
1031    for (int i=0; i<rl; i++)
1032    {
1033      if (result->fullres[i]!=NULL)
1034      {
1035        ideal t=kNF(currQuotient,NULL,result->fullres[i]);
1036        idDelete(&result->fullres[i]);
1037        result->fullres[i]=t;
1038        if (i<rl-1)
1039        {
1040          for(int j=IDELEMS(t)-1;j>=0; j--)
1041          {
1042            if ((t->m[j]==NULL) && (result->fullres[i+1]!=NULL))
1043            {
1044              for(int k=IDELEMS(result->fullres[i+1])-1;k>=0; k--)
1045              {
1046                if (result->fullres[i+1]->m[k]!=NULL)
1047                {
1048                  pDeleteComp(&(result->fullres[i+1]->m[k]),j+1);
1049                }
1050              }
1051            }
1052          }
1053        }
1054        idSkipZeroes(result->fullres[i]);
1055      }
1056    }
1057    if ((rl>maxlength) && (result->fullres[rl-1]!=NULL))
1058    {
1059      idDelete(&result->fullres[rl-1]);
1060    }
1061  }
1062  omFreeSize((ADDRESS)fr,(rl /*result->length*/)*sizeof(ideal));
1063  return result;
1064}
1065
Note: See TracBrowser for help on using the repository browser.