source: git/Singular/syz2.cc @ 551fd7

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