source: git/kernel/syz2.cc @ 762407

spielwiese
Last change on this file since 762407 was 762407, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
config.h is for sources files only FIX: config.h should only be used by source (not from inside kernel/mod2.h!) NOTE: each source file should better include mod2.h right after config.h, while headers should better not include mod2.h.
  • Property mode set to 100644
File size: 29.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT: resolutions
7*/
8
9#include "config.h"
10#include "mod2.h"
11
12#include <omalloc/omalloc.h>
13
14#include <misc/mylimits.h>
15#include <misc/options.h>
16#include <misc/intvec.h>
17//#include "cntrlc.h"
18
19#include <coeffs/coeffs.h>
20#include <coeffs/numbers.h>
21
22#include <polys/monomials/ring.h>
23#include <polys/kbuckets.h>
24#include <polys/prCopy.h>
25
26#include <kernel/polys.h>
27
28// #include <kernel/modulop.h>
29
30#include <kernel/febase.h>
31#include <kernel/kstd1.h>
32#include <kernel/kutil.h>
33#include <kernel/stairc.h>
34
35#include <kernel/syz.h>
36#include <kernel/ideals.h>
37
38
39
40
41
42//#define SHOW_PROT
43//#define SHOW_RED
44//#define SHOW_HILB
45//#define SHOW_RESULT
46//#define INVERT_PAIRS
47//#define SHOW_CRIT
48//#define SHOW_SPRFL
49#define USE_CHAINCRIT
50#define poly_write(p) wrp(p);PrintLn()
51/*--- some heuristics to optimize the pair sets---*/
52/*--- chose only one (!!!) at the same time ------*/
53//#define USE_HEURISTIC1
54#define USE_HEURISTIC2
55
56#ifdef SHOW_CRIT
57static int crit;
58static int crit1;
59static int spfl;
60static int cons_pairs;
61static int crit_fails;
62#endif
63typedef struct sopen_pairs open_pairs;
64typedef open_pairs* crit_pairs;
65struct sopen_pairs
66{
67  crit_pairs next;
68  int first_poly;
69  int second_poly;
70};
71/*3
72* computes pairs from the new elements (beginning with the element newEl)
73* in the module index
74*/
75static void syCreateNewPairs_Hilb(syStrategy syzstr, int index,
76            int actdeg)
77{
78  SSet temp;
79  SObject tso;
80  poly toHandle,tsyz=NULL,p,pp;
81  int r1,r2=0,rr,l=(*syzstr->Tl)[index];
82  int i,j,r=0,ti;
83  BOOLEAN toComp=FALSE;
84#ifdef USE_CHAINCRIT
85  crit_pairs cp=NULL,tcp;
86#endif
87  actdeg += index;
88  long * ShiftedComponents = syzstr->ShiftedComponents[index-1];
89  int* Components = syzstr->truecomponents[index-1];
90
91  while ((l>0) && ((syzstr->resPairs[index])[l-1].lcm==NULL)) l--;
92  rr = l-1;
93  while ((rr>=0) && (((syzstr->resPairs[index])[rr].p==NULL) || 
94        ((syzstr->resPairs[index])[rr].order>actdeg))) rr--;
95  r2 = rr+1; 
96  while ((rr>=0) && ((syzstr->resPairs[index])[rr].order==actdeg)
97         && ((syzstr->resPairs[index])[rr].syzind<0))
98  {
99    rr--;
100    r++;
101  }
102  if (r==0) return;
103  ideal nP=idInit(l,syzstr->res[index]->rank);
104#ifdef INVERT_PAIRS
105  r1 = rr+1;
106#else
107  r1 = r2-1;
108#endif
109/*---------- there are new pairs ------------------------------*/
110  loop
111  {
112/*--- chose first new elements --------------------------------*/
113    toComp = FALSE;
114    toHandle = (syzstr->resPairs[index])[r1].p;
115    if (toHandle!=NULL)
116    {
117      int tc=pGetComp(toHandle);
118      (syzstr->resPairs[index])[r1].syzind = 0;
119      for (i=0; i<r1;i++)
120      {
121        if (((syzstr->resPairs[index])[i].p!=NULL) &&
122            (pGetComp((syzstr->resPairs[index])[i].p)==tc))
123        {
124#ifdef USE_CHAINCRIT
125          tcp = cp;
126          if (tcp!=NULL)
127          {
128            while ((tcp!=NULL) && 
129              ((tcp->first_poly!=i)||(tcp->second_poly!=r1))) tcp = tcp->next;
130          }
131          if (tcp==NULL)
132          {
133#endif
134            p = pOne();
135            pLcm((syzstr->resPairs[index])[i].p,toHandle,p);
136            pSetm(p);
137            j = 0;
138            while (j<i)
139            { 
140              if (nP->m[j]!=NULL)
141              {
142                if (pLmDivisibleByNoComp(nP->m[j],p))
143                {
144                  pDelete(&p);
145                  /* p = NULL;*/
146                  break;
147                }
148                else if (pLmDivisibleByNoComp(p,nP->m[j]))
149                {
150                  pDelete(&(nP->m[j]));
151                  /* nP->m[j] = NULL;*/
152                }
153#ifdef USE_CHAINCRIT
154                else
155                {
156                  poly p1,p2;
157                  int ip= currRing->N;
158                  p1 = pDivide(p,(syzstr->resPairs[index])[i].p);
159                  p2 = pDivide(nP->m[j],(syzstr->resPairs[index])[j].p);
160                  while ((ip>0) && (pGetExp(p1,ip)*pGetExp(p2,ip)==0)) ip--;
161                  if (ip==0)
162                  {
163#ifdef SHOW_SPRFL
164Print("Hier: %d, %d\n",j,i);
165#endif
166                    if (i>rr)
167                    {
168                      tcp=(crit_pairs)omAlloc0(sizeof(sopen_pairs));
169                      tcp->next = cp;
170                      tcp->first_poly = j;
171                      tcp->second_poly = i;
172                      cp = tcp;
173                      tcp = NULL;
174                    }
175                    else
176                    {
177                      ti=0;
178                      while ((ti<l) && (((syzstr->resPairs[index])[ti].ind1!=j)||
179                             ((syzstr->resPairs[index])[ti].ind2!=i))) ti++;
180                      if (ti<l) 
181                      {
182#ifdef SHOW_SPRFL
183Print("gefunden in Mod %d: ",index); poly_write((syzstr->resPairs[index])[ti].lcm);
184#endif
185                        syDeletePair(&(syzstr->resPairs[index])[ti]);
186#ifdef SHOW_CRIT
187                        crit1++;
188#endif
189                        toComp = TRUE;
190                      }
191                    }
192                  }
193                  pLmDelete(&p1);
194                  pLmDelete(&p2);
195                }
196#endif
197              }
198              j++;
199            }
200            if (p!=NULL)
201            {
202              nP->m[i] = p;
203            }
204#ifdef USE_CHAINCRIT
205          }
206          else
207          {
208#ifdef SHOW_CRIT
209            crit1++;
210#endif
211          }
212#endif
213        }
214      }
215      if (toComp) syCompactify1(syzstr->resPairs[index],&l,r1);
216      for (i=0;i<r1;i++)
217      {
218        if (nP->m[i]!=NULL)
219        {
220          tso.lcm = p = nP->m[i];
221          nP->m[i] = NULL;
222          tso.order = pTotaldegree(p);
223          if ((syzstr->cw!=NULL) && (index>0) && (pGetComp(p)>0))
224          {
225            int ii=index-1,jj=pGetComp(p);
226            while (ii>0)
227            {
228              jj = pGetComp(syzstr->res[ii]->m[jj-1]);
229              ii--;
230            }
231            tso.order += (*syzstr->cw)[jj-1];
232          }
233          tso.p1 = (syzstr->resPairs[index])[i].p;
234          tso.p2 = toHandle;
235          tso.ind1 = i;
236          tso.ind2 = r1;
237          tso.syzind = -1;
238          tso.isNotMinimal = (poly)1;
239          tso.p = NULL;
240          tso.length = -1;
241          number coefgcd =
242            nGcd(pGetCoeff(tso.p1),pGetCoeff(tso.p2),currRing);
243          tso.syz = pCopy((syzstr->resPairs[index])[i].syz);
244          poly tt = pDivide(tso.lcm,tso.p1);
245          pSetCoeff(tt,nDiv(pGetCoeff(tso.p1),coefgcd));
246          tso.syz = pMult_mm(tso.syz,tt);
247          pLmDelete(&tt);
248          coefgcd = nNeg(coefgcd);
249          pp = pCopy((syzstr->resPairs[index])[r1].syz);
250          tt = pDivide(tso.lcm,tso.p2);
251          pSetCoeff(tt,nDiv(pGetCoeff(tso.p2),coefgcd));
252          pp = pMult_mm(pp,tt);
253          pLmDelete(&tt);
254          tso.syz = pAdd(pp,tso.syz);
255          nDelete(&coefgcd);
256          pSetComp(tso.lcm,pGetComp((syzstr->resPairs[index])[r1].syz));
257#ifdef SHOW_PROT
258Print("erzeuge Paar im Modul %d,%d mit: \n",index,tso.order);
259PrintS("poly1: ");poly_write(tso.p1);
260PrintS("poly2: ");poly_write(tso.p2);
261PrintS("syz: ");poly_write(tso.syz);
262PrintS("sPoly: ");poly_write(tso.p);
263PrintLn();
264#endif
265          syEnterPair(syzstr,&tso,&l,index);
266        }
267      }
268    }
269#ifdef INVERT_PAIRS
270    r1++;
271    if (r1>=r2) break;
272#else
273    r1--;
274    if (r1<=rr) break;
275#endif
276  }
277  idDelete(&nP);
278#ifdef USE_CHAINCRIT
279  while (cp!=NULL)
280  {
281    tcp = cp;
282    cp = cp->next;
283    omFreeSize((ADDRESS)tcp,sizeof(sopen_pairs));
284  }
285#endif
286}
287
288/*3
289* determines the place of a polynomial in the right ordered resolution
290* set the vectors of truecomponents
291*/
292static void syOrder_Hilb(poly p,syStrategy syzstr,int index)
293{
294  int i=IDELEMS(syzstr->orderedRes[index]);
295
296  while ((i>0) && (syzstr->orderedRes[index]->m[i-1]==NULL)) i--;
297  syzstr->orderedRes[index]->m[i] = p;
298}
299
300static void syHalfPair(poly syz, int newEl, syStrategy syzstr, int index)
301{
302  SObject tso;
303  memset(&tso,0,sizeof(tso));
304  int l=(*syzstr->Tl)[index];
305
306  while ((l>0) && ((syzstr->resPairs[index])[l-1].syz==NULL)) l--;
307  if ((syzstr->cw!=NULL) && (index>0) && (pGetComp(syz)>0))
308  {
309    int ii=index-1,jj=pGetComp(syz);
310    while (ii>0)
311    {
312      jj = pGetComp(syzstr->res[ii]->m[jj-1]);
313      ii--;
314    }
315    tso.order += (*syzstr->cw)[jj-1];
316  }
317  tso.p1 = NULL;
318  tso.p2 = NULL;
319  tso.ind1 = 0;
320  tso.ind2 = 0;
321  tso.syzind = -1;
322  tso.isNotMinimal = NULL;
323  tso.p = syz;
324  tso.order = pTotaldegree(syz);
325  tso.syz = pHead(syz);
326  pSetComp(tso.syz,newEl+1);
327  pSetm(tso.syz);
328  tso.lcm = pHead(tso.syz);
329  tso.length = pLength(syz);
330  syOrder_Hilb(syz,syzstr,index);
331#ifdef SHOW_PROT
332Print("erzeuge Halbpaar im Module %d,%d mit: \n",index,tso.order);
333PrintS("syz: ");poly_write(tso.syz);
334PrintS("sPoly: ");poly_write(tso.p);
335PrintLn();
336#endif
337  syEnterPair(syzstr,&tso,&l,index);
338}
339/*3
340* computes the order of pairs of same degree
341* must be monoton
342*/
343static intvec* syLinStrat2(SSet nextPairs, syStrategy syzstr,
344                          int howmuch, int index,intvec ** secondpairs)
345{
346  ideal o_r=syzstr->res[index+1];
347  int i=0,i1=0,i2=0,l,ll=IDELEMS(o_r);
348  intvec *result=new intvec(howmuch+1);
349  BOOLEAN isDivisible;
350  SObject tso;
351
352#ifndef USE_HEURISTIC2
353  while (i1<howmuch)
354  {
355    (*result)[i1] = i1+1;
356    i1++;
357  }
358  return result;
359#else
360  while ((ll>0) && (o_r->m[ll-1]==NULL)) ll--;
361  while (i<howmuch)
362  {
363    tso = nextPairs[i];
364    isDivisible = FALSE;
365    l = 0;
366    while ((l<ll) && (!isDivisible))
367    {
368      if (o_r->m[l]!=NULL)
369      {
370        isDivisible = isDivisible ||
371          pLmDivisibleBy(o_r->m[l],tso.lcm);
372      }
373      l++;
374    }
375    if (isDivisible)
376    {
377#ifdef SHOW_PROT
378Print("streiche Paar im Modul %d,%d mit: \n",index,nextPairs[i].order);
379PrintS("poly1: ");poly_write(nextPairs[i].p1);
380PrintS("poly2: ");poly_write(nextPairs[i].p2);
381PrintS("syz: ");poly_write(nextPairs[i].syz);
382PrintS("sPoly: ");poly_write(nextPairs[i].p);
383PrintLn();
384#endif
385      //syDeletePair(&nextPairs[i]);
386      if (*secondpairs==NULL) *secondpairs = new intvec(howmuch);
387      (**secondpairs)[i2] = i+1;
388      i2++;
389#ifdef SHOW_CRIT
390      crit++;
391#endif
392    }
393    else
394    {
395//      nextPairs[i].p = sySPoly(tso.p1, tso.p2,tso.lcm);
396      (*result)[i1] = i+1;
397      i1++;
398    }
399    i++;
400  }
401  return result;
402#endif
403}
404
405inline void sySPRedSyz(syStrategy syzstr,sSObject redWith,poly q=NULL)
406{
407  poly p=pDivide(q,redWith.p);
408  pSetCoeff(p,nDiv(pGetCoeff(q),pGetCoeff(redWith.p)));
409  int il=-1;
410  kBucket_Minus_m_Mult_p(syzstr->syz_bucket,p,redWith.syz,&il,NULL);
411  pLmDelete(&p);
412}
413
414static poly syRed_Hilb(poly toRed,syStrategy syzstr,int index)
415{
416  ideal redWith=syzstr->res[index];
417  if (redWith==NULL) return toRed;
418  int j=IDELEMS(redWith),i;
419  poly q,result=NULL,resultp;
420
421  while ((j>0) && (redWith->m[j-1]==NULL)) j--;
422  if ((toRed==NULL) || (j==0)) return toRed;
423  kBucketInit(syzstr->bucket,toRed,-1);
424  q = kBucketGetLm(syzstr->bucket);
425  loop
426  {
427    if (q==NULL)
428    {
429      break;
430    }
431    i = 0;
432    loop
433    {
434      if (pLmDivisibleBy(redWith->m[i],q))
435      {
436        number up = kBucketPolyRed(syzstr->bucket,redWith->m[i],
437                         pLength(redWith->m[i]), NULL);
438        nDelete(&up);
439        q = kBucketGetLm(syzstr->bucket);
440        if (toRed==NULL) break;
441        i = 0;
442      }
443      else
444      {
445        i++;
446      }
447      if ((i>=j) || (q==NULL)) break;
448    }
449    if (q!=NULL)
450    {
451      if (result==NULL)
452      {
453        resultp = result = kBucketExtractLm(syzstr->bucket);
454      }
455      else
456      {
457        pNext(resultp) = kBucketExtractLm(syzstr->bucket);
458        pIter(resultp);
459      }
460      q = kBucketGetLm(syzstr->bucket);
461    }
462  }
463  kBucketClear(syzstr->bucket,&q,&i);
464  if (q!=NULL) PrintS("Hier ist was schief gelaufen!\n");
465  return result;
466}
467
468#ifdef USE_HEURISTIC1
469intvec *ivStrip(intvec* arg)
470{
471  int l=arg->rows()*arg->cols(),i=0,ii=0;
472  intvec *tempV=new intvec(l);
473
474  while (i+ii<l)
475  {
476    if ((*arg)[i+ii]==0)
477    {
478      ii++;
479    }
480    else
481    {
482      (*tempV)[i] = (*arg)[i+ii];
483      i++;
484    }
485  }
486  if (i==0)
487  {
488    delete tempV;
489    return NULL;
490  }
491  intvec * result=new intvec(i+1);
492  for (ii=0;ii<i;ii++)
493   (*result)[ii] = (*tempV)[ii];
494  delete tempV;
495  return result;
496}
497#endif
498
499/*3
500* reduces all pairs of degree deg in the module index
501* put the reduced generators to the resolvente which contains
502* the truncated kStd
503*/
504static void syRedNextPairs_Hilb(SSet nextPairs, syStrategy syzstr,
505               int howmuch, int index,int actord,int* toSub,
506               int *maxindex,int *maxdeg)
507{
508  int i,j,k=IDELEMS(syzstr->res[index]);
509  int ks=IDELEMS(syzstr->res[index+1]),kk,l,ll;
510  int ks1=IDELEMS(syzstr->orderedRes[index+1]);
511  int kres=(*syzstr->Tl)[index];
512  int toGo=0;
513  int il;
514  number coefgcd,n;
515  SSet redset=syzstr->resPairs[index];
516  poly p=NULL,q,tp;
517  intvec *spl1;
518  SObject tso;
519  intvec *spl3=NULL;
520#ifdef USE_HEURISTIC1
521  intvec *spl2=new intvec(howmuch+1,howmuch+1,0);
522  int there_are_superfluous=0;
523  int step=1,jj,j1,j2;
524#endif
525  long * ShiftedComponents = syzstr->ShiftedComponents[index];
526  int* Components = syzstr->truecomponents[index];
527  assume(Components != NULL && ShiftedComponents != NULL);
528  BOOLEAN need_reset;
529
530  actord += index;
531  if ((nextPairs==NULL) || (howmuch==0)) return;
532  while ((k>0) && (syzstr->res[index]->m[k-1]==NULL)) k--;
533  while ((ks>0) && (syzstr->res[index+1]->m[ks-1]==NULL)) ks--;
534  while ((ks1>0) && (syzstr->orderedRes[index+1]->m[ks1-1]==NULL)) ks1--;
535  while ((kres>0) &&
536        ((redset[kres-1].p==NULL) || (redset[kres-1].order>actord))) kres--;
537  while ((kres<(*syzstr->Tl)[index]) &&
538         (redset[kres-1].order!=0) && (redset[kres-1].order<=actord)) kres++;
539  spl1 = syLinStrat2(nextPairs,syzstr,howmuch,index,&spl3);
540#ifdef SHOW_PROT
541PrintS("spl1 ist hier: ");spl1->show(0,0);
542#endif
543  i=0;
544  kk = (*spl1)[i]-1;
545  if (index==1)
546  {
547    intvec * temp1_hilb = hHstdSeries(syzstr->res[index],NULL,NULL,NULL);
548    if (actord<temp1_hilb->length())
549    {
550      toGo = (*temp1_hilb)[actord];
551#ifdef SHOW_HILB
552Print("\nStze toGo im Modul %d und Grad %d auf: %d\n",1,actord-1,toGo);
553#endif
554    }
555    delete temp1_hilb;
556  }
557  else
558  {
559    if (actord<=(syzstr->hilb_coeffs[index])->length())
560    {
561      toGo = (*syzstr->hilb_coeffs[index])[actord-1];
562#ifdef SHOW_HILB
563Print("\nStze toGo im Modul %d und Grad %d auf: %d\n",index,actord-1,toGo);
564#endif
565    }
566  }
567  if ((syzstr->hilb_coeffs[index+1]!=NULL) &&
568      (actord<=(syzstr->hilb_coeffs[index+1])->length()))
569  {
570    toGo += (*syzstr->hilb_coeffs[index+1])[actord-1];
571#ifdef SHOW_HILB
572Print("\nAddiere zu toGo aus Modul %d und Grad %d: %d\n",index+1,actord-1,(*syzstr->hilb_coeffs[index+1])[actord-1]);
573#endif
574  }
575#ifdef SHOW_HILB
576Print("<H%d>",toGo);
577#endif
578  while (kk>=0)
579  {
580    if (toGo==0) 
581    {
582      while (kk>=0)
583      {
584        pDelete(&nextPairs[kk].p);
585        pDelete(&nextPairs[kk].syz);
586        syDeletePair(&nextPairs[kk]);
587        nextPairs[kk].p = nextPairs[kk].syz = nextPairs[kk].lcm = NULL;
588        i++;
589        kk = (*spl1)[i]-1;
590#ifdef USE_HEURISTIC2
591        if (kk<0)
592        {
593          i = 0;
594          delete spl1;
595          spl1 = spl3;
596          spl3 = NULL;
597          if (spl1!=NULL)
598            kk = (*spl1)[i]-1;
599        }
600#endif
601      }
602      if (spl1!=NULL) delete spl1;
603      break;
604    }
605    tso = nextPairs[kk];
606    if ((tso.p1!=NULL) && (tso.p2!=NULL))
607    {
608#ifdef SHOW_CRIT
609      cons_pairs++;
610#endif
611      //tso.p = sySPoly(tso.p1, tso.p2,tso.lcm);
612      tso.p = ksOldCreateSpoly(tso.p2, tso.p1);
613#ifdef SHOW_PROT
614PrintS("reduziere Paar mit: \n");
615PrintS("poly1: ");poly_write(tso.p1);
616PrintS("poly2: ");poly_write(tso.p2);
617PrintS("syz: ");poly_write(tso.syz);
618PrintS("sPoly: ");poly_write(tso.p);
619#endif
620      if (tso.p != NULL)
621      {
622        kBucketInit(syzstr->bucket,tso.p,-1);
623        kBucketInit(syzstr->syz_bucket,tso.syz,-1);
624        q = kBucketGetLm(syzstr->bucket);
625        j = 0;
626        while (j<kres) 
627        {
628          if ((redset[j].p!=NULL) && (pLmDivisibleBy(redset[j].p,q)) 
629              && ((redset[j].ind1!=tso.ind1) || (redset[j].ind2!=tso.ind2)))
630          {
631#ifdef SHOW_RED
632kBucketClear(syzstr->bucket,&tso.p,&tso.length);
633kBucketClear(syzstr->syz_bucket,&tso.syz,&il);
634PrintS("reduziere: ");poly_write(tso.p);
635PrintS("syz: ");poly_write(tso.syz);
636PrintS("mit: ");poly_write(redset[j].p);
637PrintS("syz: ");poly_write(redset[j].syz);
638kBucketInit(syzstr->bucket,tso.p,tso.length);
639kBucketInit(syzstr->syz_bucket,tso.syz,il);
640#endif
641            sySPRedSyz(syzstr,redset[j],q);
642            number up = kBucketPolyRed(syzstr->bucket,redset[j].p,
643                         redset[j].length, NULL);
644            nDelete(&up);
645            q = kBucketGetLm(syzstr->bucket);
646#ifdef SHOW_RED
647kBucketClear(syzstr->bucket,&tso.p,&tso.length);
648kBucketClear(syzstr->syz_bucket,&tso.syz,&il);
649PrintS("zu: ");poly_write(tso.p);
650PrintS("syz: ");poly_write(tso.syz);
651kBucketInit(syzstr->bucket,tso.p,tso.length);
652kBucketInit(syzstr->syz_bucket,tso.syz,il);
653PrintLn();
654#endif
655            if (q==NULL) break;
656            j = 0;
657          }
658          else
659          {
660            j++;
661          }
662        }
663        kBucketClear(syzstr->bucket,&tso.p,&tso.length);
664        kBucketClear(syzstr->syz_bucket,&tso.syz,&il);
665      }
666#ifdef SHOW_PROT
667PrintS("erhalte Paar mit: \n");
668PrintS("syz: ");poly_write(tso.syz);
669PrintS("sPoly: ");poly_write(tso.p);
670PrintLn();
671#endif
672#ifdef SHOW_SPRFL
673//PrintLn();
674wrp(tso.lcm);
675Print(" mit index %d, %d ",tso.ind1,tso.ind2);
676#endif
677      if (tso.p != NULL)
678      {
679        if (TEST_OPT_PROT) PrintS("g");
680        (*toSub)++;
681        toGo--;
682        if (!nIsOne(pGetCoeff(tso.p)))
683        {
684          number n=nInvers(pGetCoeff(tso.p));
685          pNorm(tso.p);
686          pMult_nn(tso.syz,n);
687          nDelete(&n);
688        }
689        if (k==IDELEMS((syzstr->res)[index]))
690          syEnlargeFields(syzstr,index);
691        syzstr->res[index]->m[k] = tso.p;
692        k++;
693      }
694      else
695      {
696        if (ks==IDELEMS(syzstr->res[index+1]))
697          syEnlargeFields(syzstr,index+1);
698        syzstr->res[index+1]->m[ks] = syRed_Hilb(tso.syz,syzstr,index+1);
699        if (syzstr->res[index+1]->m[ks]!=NULL)
700        {
701          if (TEST_OPT_PROT) PrintS("s");
702          toGo--;
703          pNorm(syzstr->res[index+1]->m[ks]);
704          syHalfPair(syzstr->res[index+1]->m[ks],ks1,syzstr,index+1);
705          ks++;
706          ks1++;
707          if (index+1>*maxindex) *maxindex = index+1;
708          if (actord-index>*maxdeg) *maxdeg = actord-index;
709        }
710        else
711        {
712          if (TEST_OPT_PROT) PrintS("-");
713#ifdef SHOW_CRIT
714          spfl++;
715#endif
716#ifdef USE_HEURISTIC1
717          if (there_are_superfluous>=0)
718          {
719            j = i+1;
720            jj = (*spl1)[j]-1;
721            j1 = 1;
722            while (jj>=0)
723            {
724              if (tso.ind2==nextPairs[jj].ind2)
725              {
726                IMATELEM(*spl2,j1,step) = jj+1;
727                j1++;
728                for (j2=j;j2<spl1->length()-1;j2++)
729                {
730                  (*spl1)[j2] = (*spl1)[j2+1];
731                }
732              }
733              else
734              {
735                j++;
736              }
737              jj = (*spl1)[j]-1;
738            }
739            step++;
740            if (there_are_superfluous==0) there_are_superfluous = 1;
741          }
742#endif
743#ifdef SHOW_SPRFL
744Print("ist ueberfluessig in Mod %d",index);
745//Print("\n ueberfluessig in Mod %d:",index);
746//wrp(tso.lcm);
747//PrintLn();
748#endif
749        }
750        tso.syz = NULL;
751        syDeletePair(&tso);
752        tso.p = tso.syz = tso.lcm = NULL;
753      }
754      nextPairs[kk] = tso;
755    }
756#ifdef SHOW_SPRFL
757PrintLn();
758#endif
759    i++;
760#ifdef SHOW_PROT
761PrintS("spl1 ist hier: ");spl1->show(0,0);
762Print("naechstes i ist: %d",i);
763#endif
764    kk = (*spl1)[i]-1;
765#ifdef USE_HEURISTIC1
766    if ((kk<0) && (there_are_superfluous>0))
767    {
768      i = 0;
769      delete spl1;
770      spl1 = ivStrip(spl2); 
771      delete spl2;
772      if (spl1!=NULL)
773      {
774        there_are_superfluous = -1;
775        kk = (*spl1)[i]-1;
776      }
777    } 
778#endif
779#ifdef USE_HEURISTIC2
780    if ((kk<0) && (toGo>0))
781    {
782#ifdef SHOW_CRIT
783      crit_fails++;
784#endif
785      i = 0;
786      delete spl1;
787      spl1 = spl3;
788      spl3 = NULL;
789      if (spl1!=NULL)
790        kk = (*spl1)[i]-1;
791    }
792#endif
793  }
794  delete spl1;
795  if (spl3!=NULL) delete spl3;
796}
797
798void sySetNewHilb(syStrategy syzstr, int toSub,int index,int actord)
799{
800  int i;
801  actord += index;
802  intvec * temp_hilb = hHstdSeries(syzstr->res[index+1],NULL,NULL,NULL);
803  intvec * cont_hilb = hHstdSeries(syzstr->res[index],NULL,NULL,NULL);
804  if ((index+1<syzstr->length) && (syzstr->hilb_coeffs[index+1]==NULL))
805  {
806    syzstr->hilb_coeffs[index+1] = new intvec(16*((actord/16)+1));
807  }
808  else if (actord>=syzstr->hilb_coeffs[index+1]->length())
809  {
810    intvec * ttt=new intvec(16*((actord/16)+1));
811    for (i=syzstr->hilb_coeffs[index+1]->length()-1;i>=0;i--)
812    {
813      (*ttt)[i] = (*(syzstr->hilb_coeffs[index+1]))[i];
814    }
815    delete syzstr->hilb_coeffs[index+1];
816    syzstr->hilb_coeffs[index+1] = ttt;
817  }
818  if (actord+1<temp_hilb->length())
819  {
820#ifdef SHOW_HILB
821Print("\nSetze fuer Modul %d im Grad %d die Wert: \n",index+1,actord);
822(temp_hilb)->show(0,0);
823#endif
824    int k=si_min(temp_hilb->length()-1,(syzstr->hilb_coeffs[index+1])->length());
825    for (int j=k;j>actord;j--)
826      (*(syzstr->hilb_coeffs[index+1]))[j-1] = (*temp_hilb)[j];
827  }
828  else
829  {
830    (*(syzstr->hilb_coeffs[index+1]))[actord] = 0;
831  }
832  delete temp_hilb;
833  if ((index>1) && (actord<=syzstr->hilb_coeffs[index]->length()))
834  {
835#ifdef SHOW_HILB
836Print("\nSubtrahiere im Modul %d im Grad %d den Wert: %d\n",index,actord-1,toSub);
837#endif
838    (*syzstr->hilb_coeffs[index])[actord-1]-=toSub;
839  }
840  if (syzstr->hilb_coeffs[index]!=NULL)
841  {
842    if (cont_hilb->length()>syzstr->hilb_coeffs[index]->length())
843      syzstr->hilb_coeffs[index]->resize(cont_hilb->length());
844    for (int j=cont_hilb->length()-1;j>actord;j--)
845      (*(syzstr->hilb_coeffs[index]))[j-1] = (*cont_hilb)[j];
846  }
847  delete cont_hilb;
848#ifdef SHOW_HILB
849Print("<h,%d>",(*(syzstr->hilb_coeffs[index+1]))[actord]);
850#endif
851}
852
853/*3
854* reduces the generators of the module index in degree deg
855* (which are actual syzygies of the module index-1)
856* wrt. the ideal generated by elements of lower degrees
857*/
858static void syRedGenerOfCurrDeg_Hilb(syStrategy syzstr, int deg,int *maxindex,int *maxdeg)
859{
860  ideal res=syzstr->res[1];
861  int i=0,j,k=IDELEMS(res),k1=IDELEMS(syzstr->orderedRes[1]);
862  SSet sPairs1=syzstr->resPairs[1];
863  SSet sPairs=syzstr->resPairs[0];
864
865  while ((k>0) && (res->m[k-1]==NULL)) k--;
866  while ((k1>0) && (syzstr->orderedRes[1]->m[k1-1]==NULL)) k1--;
867  while ((i<(*syzstr->Tl)[0]) && (((sPairs)[i].syz==NULL) ||
868          ((sPairs)[i].order<deg)))
869    i++;
870  if ((i>=(*syzstr->Tl)[0]) || ((sPairs)[i].order>deg)) return;
871  while ((i<(*syzstr->Tl)[0]) && (((sPairs)[i].syz==NULL) ||
872         ((sPairs)[i].order==deg)))
873  {
874    if ((sPairs)[i].syz!=NULL)
875    {
876#ifdef SHOW_PROT
877PrintS("reduziere Erzeuger: \n");
878PrintS("syz: ");poly_write((sPairs)[i].syz);
879#endif
880      (sPairs)[i].syz = syRed_Hilb((sPairs)[i].syz,syzstr,1);
881#ifdef SHOW_PROT
882PrintS("erhalte Erzeuger: \n");
883PrintS("syz: ");poly_write((sPairs)[i].syz);
884PrintLn();
885#endif
886      if ((sPairs)[i].syz != NULL)
887      {
888        if (k==IDELEMS(res))
889        {
890          syEnlargeFields(syzstr,1);
891          res=syzstr->res[1];
892        }
893        if (TEST_OPT_DEBUG)
894        {
895          if ((sPairs)[i].isNotMinimal==NULL)
896          {
897            PrintS("\nminimal generator: ");
898            pWrite((syzstr->resPairs[0])[i].syz);
899            PrintS("comes from: ");pWrite((syzstr->resPairs[0])[i].p1);
900            PrintS("and: ");pWrite((syzstr->resPairs[0])[i].p2);
901          }
902        }
903        res->m[k] = (sPairs)[i].syz;
904        pNorm(res->m[k]);
905        syHalfPair(res->m[k],k1,syzstr,1);
906        k1++;
907        k++;
908        if (1>*maxindex) *maxindex = 1;
909        if (deg-1>*maxdeg) *maxdeg = deg-1;
910      }
911    }
912    i++;
913  }
914}
915
916/*3
917* reorders the result (stored in orderedRes) according
918* to the seqence given by res
919*/
920static void syReOrdResult_Hilb(syStrategy syzstr,int maxindex,int maxdeg)
921{
922  ideal reor,toreor;
923  int i,j,k,l,m,togo;
924  syzstr->betti = new intvec(maxdeg,maxindex+1,0);
925  (*syzstr->betti)[0] = 1;
926  for (i=1;i<=syzstr->length;i++)
927  {
928    if (!idIs0(syzstr->orderedRes[i])) 
929    {
930      toreor = syzstr->orderedRes[i];
931      k = IDELEMS(toreor);
932      while ((k>0) && (toreor->m[k-1]==NULL)) k--;
933      reor = idInit(k,toreor->rank);
934      togo = IDELEMS(syzstr->res[i]);
935      for (j=0;j<k;j++)
936      {
937        if (toreor->m[j]!=NULL) (IMATELEM(*syzstr->betti,p_FDeg(toreor->m[j],currRing)-i+1,i+1))++;
938        reor->m[j] = toreor->m[j];
939        toreor->m[j] = NULL;
940      }
941      m = 0; 
942      for (j=0;j<togo;j++)
943      {
944        if (syzstr->res[i]->m[j]!=NULL)
945        {
946          l = 0;
947          while ((l<k) && (syzstr->res[i]->m[j]!=reor->m[l])) l++;
948          if (l<k)
949          {
950            toreor->m[m] = reor->m[l];
951            reor->m[l] = NULL;
952            m++;
953          }
954        }
955      }
956      idDelete(&reor);
957    }
958  }
959}
960
961/*2
962* the CoCoA-algorithm for free resolutions, using a formula
963* for remaining pairs based on Hilbert-functions
964*/
965syStrategy syHilb(ideal arg,int * length)
966{
967  int i,j,actdeg=32000,index=0,reg=-1;
968  int startdeg,howmuch,toSub=0;
969  int maxindex=0,maxdeg=0;
970  ideal temp=NULL;
971  SSet nextPairs;
972  ring origR = currRing;
973  syStrategy syzstr=(syStrategy)omAlloc0(sizeof(ssyStrategy));
974
975  if ((idIs0(arg)) || (id_RankFreeModule(arg,currRing)>0))
976  {
977    syzstr->minres = (resolvente)omAllocBin(sip_sideal_bin);
978    syzstr->length = 1;
979    syzstr->minres[0] = idInit(1,arg->rank);
980    return syzstr;
981  }
982 
983  // Creare dp,S ring and change to it
984  syzstr->syRing = rAssure_dp_C(origR);
985  rChangeCurrRing(syzstr->syRing);
986
987  // set initial ShiftedComps
988  currcomponents = (int*)omAlloc0((arg->rank+1)*sizeof(int));
989  currShiftedComponents = (long*)omAlloc0((arg->rank+1)*sizeof(long));
990
991/*--- initializes the data structures---------------*/
992#ifdef SHOW_CRIT
993  crit = 0;
994  crit1 = 0;
995  spfl = 0;
996  cons_pairs = 0;
997  crit_fails = 0;
998#endif
999  syzstr->length = *length = currRing->N+2;
1000  syzstr->Tl = new intvec(*length+1);
1001  temp = idInit(IDELEMS(arg),arg->rank);
1002  for (i=0;i<IDELEMS(arg);i++)
1003  {
1004    if (origR != syzstr->syRing)
1005      temp->m[i] = prCopyR( arg->m[i], origR, syzstr->syRing);
1006    else
1007      temp->m[i] = pCopy( arg->m[i]);
1008    if (temp->m[i]!=NULL)
1009    {
1010      j = pTotaldegree(temp->m[i]);
1011      if (j<actdeg) actdeg = j;
1012    }
1013  }
1014  idTest(temp);
1015  idSkipZeroes(temp);
1016  syzstr->resPairs = syInitRes(temp,length,syzstr->Tl,syzstr->cw);
1017  omFreeSize((ADDRESS)currcomponents,(arg->rank+1)*sizeof(int));
1018  omFreeSize((ADDRESS)currShiftedComponents,(arg->rank+1)*sizeof(int));
1019  syzstr->res = (resolvente)omAlloc0((*length+1)*sizeof(ideal));
1020  syzstr->orderedRes = (resolvente)omAlloc0((*length+1)*sizeof(ideal));
1021  syzstr->elemLength = (int**)omAlloc0((*length+1)*sizeof(int*));
1022  syzstr->truecomponents = (int**)omAlloc0((*length+1)*sizeof(int*));
1023  syzstr->backcomponents = (int**)omAlloc0((*length+1)*sizeof(int*));
1024  syzstr->ShiftedComponents = (long**)omAlloc0((*length+1)*sizeof(long*));
1025  syzstr->Howmuch = (int**)omAlloc0((*length+1)*sizeof(int*));
1026  syzstr->Firstelem = (int**)omAlloc0((*length+1)*sizeof(int*));
1027  syzstr->hilb_coeffs = (intvec**)omAlloc0((*length+1)*sizeof(intvec*));
1028  syzstr->sev = (unsigned long **)omAlloc0((*length+1)*sizeof(unsigned long*));
1029  syzstr->bucket = kBucketCreate(currRing);
1030  syzstr->syz_bucket = kBucketCreate(currRing);
1031  startdeg = actdeg;
1032  nextPairs = syChosePairs(syzstr,&index,&howmuch,&actdeg);
1033/*--- computes the resolution ----------------------*/
1034  while (nextPairs!=NULL)
1035  {
1036#ifdef SHOW_PROT
1037Print("compute %d Paare im Module %d im Grad %d \n",howmuch,index,actdeg+index);
1038#endif
1039    if (TEST_OPT_PROT) Print("%d",actdeg);
1040    if (TEST_OPT_PROT) Print("(m%d)",index);
1041    if (index==0)
1042      i = syInitSyzMod(syzstr,index,id_RankFreeModule(arg, origR)+1);
1043    else
1044      i = syInitSyzMod(syzstr,index);
1045    j = syInitSyzMod(syzstr,index+1);
1046    if (index>0)
1047    {
1048      syRedNextPairs_Hilb(nextPairs,syzstr,howmuch,index,actdeg,&toSub,&maxindex,&maxdeg);
1049      sySetNewHilb(syzstr,toSub,index,actdeg);
1050      toSub = 0;
1051      syCompactifyPairSet(syzstr->resPairs[index],(*syzstr->Tl)[index],0);
1052    }
1053    else
1054      syRedGenerOfCurrDeg_Hilb(syzstr,actdeg,&maxindex,&maxdeg);
1055/*--- creates new pairs -----------------------------*/
1056#ifdef SHOW_PROT
1057Print("Bilde neue Paare in Modul %d!\n",index);
1058#endif
1059    syCreateNewPairs_Hilb(syzstr,index,actdeg);
1060    if (index<(*length)-1)
1061    {
1062#ifdef SHOW_PROT
1063Print("Bilde neue Paare in Modul %d!\n",index+1);
1064#endif
1065      syCreateNewPairs_Hilb(syzstr,index+1,actdeg-1);
1066    }
1067    index++;
1068    nextPairs = syChosePairs(syzstr,&index,&howmuch,&actdeg);
1069  }
1070  syReOrdResult_Hilb(syzstr,maxindex,maxdeg);
1071#ifdef SHOW_RESULT
1072PrintS("minimal resolution:\n");
1073for (int ti=1;ti<=*length;ti++)
1074{
1075  if (!idIs0(syzstr->orderedRes[ti])) idPrint(syzstr->orderedRes[ti]);
1076}
1077PrintS("full resolution:\n");
1078for (int ti=1;ti<=*length;ti++)
1079{
1080  if (!idIs0(syzstr->res[ti])) idPrint(syzstr->res[ti]);
1081}
1082#endif
1083#ifdef SHOW_CRIT
1084Print("Criterion %d times applied\n",crit);
1085Print("Criterion1 %d times applied\n",crit1);
1086Print("%d superfluous pairs\n",spfl);
1087Print("%d pairs considered\n",cons_pairs);
1088Print("Criterion fails %d times\n",crit_fails);
1089crit = 0;
1090crit1 = 0;
1091spfl = 0;
1092cons_pairs = 0;
1093crit_fails = 0;
1094#endif
1095  if (temp!=NULL) idDelete(&temp);
1096  kBucketDestroy(&(syzstr->bucket));
1097  kBucketDestroy(&(syzstr->syz_bucket));
1098  if (origR != syzstr->syRing) 
1099    rChangeCurrRing(origR);
1100  else
1101    currRing =  origR;
1102  if (TEST_OPT_PROT) PrintLn();
1103  return syzstr;
1104}
Note: See TracBrowser for help on using the repository browser.