source: git/kernel/syz.cc @ 528f5b7

spielwiese
Last change on this file since 528f5b7 was f815bd7, checked in by Hans Schoenemann <hannes@…>, 13 years ago
syNewBetti currently not used git-svn-id: file:///usr/local/Singular/svn/trunk@13644 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 29.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5
6/*
7* ABSTRACT: resolutions
8*/
9
10
11#include <kernel/mod2.h>
12#include <kernel/options.h>
13#include <omalloc/omalloc.h>
14#include <kernel/polys.h>
15#include <kernel/febase.h>
16#include <kernel/kstd1.h>
17#include <kernel/kutil.h>
18#include <kernel/stairc.h>
19#include <kernel/intvec.h>
20#include <kernel/numbers.h>
21#include <kernel/ideals.h>
22#include <kernel/intvec.h>
23#include <kernel/ring.h>
24#include <kernel/syz.h>
25#include <kernel/prCopy.h>
26
27#include <kernel/sca.h>
28
29static intvec * syPrepareModComp(ideal arg,intvec ** w)
30{
31  intvec *w1 = NULL;
32  int i;
33  BOOLEAN isIdeal=FALSE;
34
35  if ((w==NULL) || (*w==NULL)) return w1;
36  int maxxx = (*w)->length();
37  if (maxxx==1)
38  {
39    maxxx = 2;
40    isIdeal = TRUE;
41  }
42  w1 = new intvec(maxxx+IDELEMS(arg));
43  if (!isIdeal)
44  {
45    for (i=0;i<maxxx;i++)
46    {
47      (*w1)[i] = (**w)[i];
48    }
49  }
50  for (i=maxxx;i<maxxx+IDELEMS(arg);i++)
51  {
52    if (arg->m[i-maxxx]!=NULL)
53    {
54      (*w1)[i] = pFDeg(arg->m[i-maxxx],currRing);
55      if (pGetComp(arg->m[i-maxxx])!=0)
56      {
57        (*w1)[i]+=(**w)[pGetComp(arg->m[i-maxxx])-1];
58      }
59    }
60  }
61  delete (*w);
62  *w = new intvec(IDELEMS(arg)+1);
63  for (i=0;i<IDELEMS(arg);i++)
64  {
65     (**w)[i+1] = (*w1)[i+maxxx];
66  }
67  return w1;
68}
69
70static void syDeleteAbove(ideal up, int k)
71{
72  if (up!=NULL)
73  {
74    for (int i=0;i<IDELEMS(up);i++)
75    {
76      if (up->m[i]!=NULL)
77        pDeleteComp(&(up->m[i]),k+1);
78    }
79  }
80}
81
82/*2
83*minimizes the module mod and cancel superfluous syzygies
84*from syz
85*/
86static void syMinStep(ideal mod,ideal syz,BOOLEAN final=FALSE,ideal up=NULL,
87                      tHomog h=isNotHomog)
88{
89  ideal deg0=NULL;
90  poly Unit1,Unit2,actWith;
91  int len,i,j,ModComp,m,k,l;
92  BOOLEAN searchUnit,existsUnit;
93
94  if (TEST_OPT_PROT) PrintS("m");
95  if ((final) && (h==isHomog))
96  /*minim is TRUE, we are in the module: maxlength, maxlength <>0*/
97  {
98    deg0=idJet(syz,0);
99    idSkipZeroes(deg0);
100    syz=deg0;
101  }
102/*--cancels empty entees and their related components above--*/
103  j = IDELEMS(syz);
104  while ((j>0) && (!syz->m[j-1])) j--;
105  k = 0;
106  while (k<j)
107  {
108    if (syz->m[k]!=NULL)
109      k++;
110    else
111    {
112      if (TEST_OPT_PROT) PrintS(".");
113      for (l=k;l<j-1;l++) syz->m[l] = syz->m[l+1];
114      syz->m[j-1] = NULL;
115      syDeleteAbove(up,k);
116      j--;
117    }
118  }
119/*--searches for syzygies coming from superfluous elements
120* in the module below--*/
121  searchUnit = TRUE;
122  int curr_syz_limit = rGetCurrSyzLimit();
123  while (searchUnit)
124  {
125    i=0;
126    j=IDELEMS(syz);
127    while ((j>0) && (syz->m[j-1]==NULL)) j--;
128    existsUnit = FALSE;
129    if (rHasGlobalOrdering_currRing())
130    {
131      while ((i<j) && (!existsUnit))
132      {
133        existsUnit = pVectorHasUnitB(syz->m[i],&ModComp);
134        i++;
135      }
136    }
137    else
138    {
139      int I=0;
140      l = 0;
141      len=0;
142      for (i=0;i<IDELEMS(syz);i++)
143      {
144        if (syz->m[i]!=NULL)
145        {
146          pVectorHasUnit(syz->m[i],&m, &l);
147          if ((len==0) ||((l>0) && (l<len)))
148          {
149            len = l;
150            ModComp = m;
151            I = i;
152          }
153        }
154      }
155//Print("Laenge ist: %d\n",len);
156      if (len>0) existsUnit = TRUE;
157      i = I+1;
158    }
159    if (existsUnit)
160    {
161      i--;
162//--takes out the founded syzygy--
163      if (TEST_OPT_PROT) PrintS("f");
164      actWith = syz->m[i];
165      if (!rField_has_simple_inverse()) p_Cleardenom(actWith, currRing);
166//Print("actWith: ");pWrite(actWith);
167      syz->m[i] = NULL;
168      for (k=i;k<j-1;k++)  syz->m[k] = syz->m[k+1];
169      syz->m[j-1] = NULL;
170      syDeleteAbove(up,i);
171      j--;
172//--makes Gauss alg. for the column ModComp--
173      Unit1 = pTakeOutComp(&(actWith), ModComp);
174//PrintS("actWith now: ");pWrite(actWith);
175//Print("Unit1: ");pWrite(Unit1);
176      k=0;
177//Print("j= %d",j);
178      while (k<j)
179      {
180        if (syz->m[k]!=NULL)
181        {
182          Unit2 = pTakeOutComp(&(syz->m[k]), ModComp);
183//Print("element %d: ",k);pWrite(syz->m[k]);
184//PrintS("Unit2: ");pWrite(Unit2);
185          syz->m[k] = pMult(pCopy(Unit1),syz->m[k]);
186          syz->m[k] = pSub(syz->m[k],
187            pMult(Unit2,pCopy(actWith)));
188          if (syz->m[k]==NULL)
189          {
190            for (l=k;l<j-1;l++)
191              syz->m[l] = syz->m[l+1];
192            syz->m[j-1] = NULL;
193            j--;
194            syDeleteAbove(up,k);
195            k--;
196          }
197        }
198        k++;
199      }
200      pDelete(&actWith);
201      pDelete(&Unit1);
202//--deletes superfluous elements from the module below---
203      pDelete(&(mod->m[ModComp-1 - curr_syz_limit]));
204      for (k=ModComp-1 - curr_syz_limit;k<IDELEMS(mod)-1;k++)
205        mod->m[k] = mod->m[k+1];
206      mod->m[IDELEMS(mod)-1] = NULL;
207    }
208    else
209      searchUnit = FALSE;
210  }
211  if (TEST_OPT_PROT) PrintLn();
212  idSkipZeroes(mod);
213  idSkipZeroes(syz);
214  if (deg0!=NULL)
215    idDelete(&deg0);
216}
217
218/*2
219* make Gauss with the element elnum in the module component ModComp
220* for the generators from - till
221*/
222void syGaussForOne(ideal syz, int elnum, int ModComp,int from,int till)
223{
224  int k,j,i,lu;
225  poly unit1,unit2;
226  poly actWith=syz->m[elnum];
227
228  if (from<0) from = 0;
229  if ((till<=0) || (till>IDELEMS(syz))) till = IDELEMS(syz);
230  syz->m[elnum] = NULL;
231  if (!rField_has_simple_inverse()) p_Cleardenom(actWith, currRing);
232/*--makes Gauss alg. for the column ModComp--*/
233  pTakeOutComp(&(actWith), ModComp, &unit1, &lu);
234  while (from<till)
235  {
236    poly tmp=syz->m[from];
237    if (/*syz->m[from]*/ tmp!=NULL)
238    {
239      pTakeOutComp(&(tmp), ModComp, &unit2, &lu);
240      tmp = pMult(pCopy(unit1),tmp);
241      syz->m[from] = pSub(tmp,
242        pMult(unit2,pCopy(actWith)));
243    }
244    from++;
245  }
246  pDelete(&actWith);
247  pDelete(&unit1);
248}
249static void syDeleteAbove1(ideal up, int k)
250{
251  poly p,pp;
252  if (up!=NULL)
253  {
254    for (int i=0;i<IDELEMS(up);i++)
255    {
256      p = up->m[i];
257      while ((p!=NULL) && (pGetComp(p)==k))
258      {
259        /*
260        pp = pNext(p);
261        pNext(p) = NULL;
262        pDelete(&p);
263        p = pp;
264        */
265        pLmDelete(&p);
266      }
267      up->m[i] = p;
268      if (p!=NULL)
269      {
270        while (pNext(p)!=NULL)
271        {
272          if (pGetComp(pNext(p))==k)
273          {
274            /*
275            pp = pNext(pNext(p));
276            pNext(pNext(p)) = NULL;
277            pDelete(&pNext(p));
278            pNext(p) = pp;
279            */
280            pLmDelete(&pNext(p));
281          }
282          else
283            pIter(p);
284        }
285      }
286    }
287  }
288}
289/*2
290*minimizes the resolution res
291*assumes homogeneous or local case
292*/
293static void syMinStep1(resolvente res, int length)
294{
295  int i,j,k,l,index=0;
296  poly p;
297  ideal deg0=NULL,reddeg0=NULL;
298  intvec *have_del=NULL,*to_del=NULL;
299
300  while ((index<length) && (res[index]!=NULL))
301  {
302/*---we take out dependend elements from syz---------------------*/
303    if (res[index+1]!=NULL)
304    {
305      deg0 = idJet(res[index+1],0);
306      reddeg0 = kInterRedOld(deg0);
307      idDelete(&deg0);
308      have_del = new intvec(IDELEMS(res[index]));
309      for (i=0;i<IDELEMS(reddeg0);i++)
310      {
311        if (reddeg0->m[i]!=NULL)
312        {
313          j = pGetComp(reddeg0->m[i]);
314          pDelete(&(res[index]->m[j-1]));
315          /*res[index]->m[j-1] = NULL;*/
316          (*have_del)[j-1] = 1;
317        }
318      }
319      idDelete(&reddeg0);
320    }
321    if (index>0)
322    {
323/*--- we search for units and perform Gaussian elimination------*/
324      j = to_del->length();
325      while (j>0)
326      {
327        if ((*to_del)[j-1]==1)
328        {
329          k = 0;
330          while (k<IDELEMS(res[index]))
331          {
332            p = res[index]->m[k];
333            while ((p!=NULL) && ((!pLmIsConstantComp(p)) || (pGetComp(p)!=j)))
334              pIter(p);
335            if ((p!=NULL) && (pLmIsConstantComp(p)) && (pGetComp(p)==j)) break;
336            k++;
337          }
338          if (k>=IDELEMS(res[index]))
339          {
340            PrintS("out of range\n");
341          }
342          syGaussForOne(res[index],k,j);
343          if (res[index+1]!=NULL)
344            syDeleteAbove1(res[index+1],k+1);
345          (*to_del)[j-1] = 0;
346        }
347        j--;
348      }
349    }
350    if (to_del!=NULL) delete to_del;
351    to_del = have_del;
352    have_del = NULL;
353    index++;
354  }
355  if (TEST_OPT_PROT) PrintLn();
356  syKillEmptyEntres(res,length);
357}
358
359void syMinimizeResolvente(resolvente res, int length, int first)
360{
361  int syzIndex=first;
362  intvec *dummy;
363
364  if (syzIndex<1) syzIndex=1;
365  if ((syzIndex==1) && (idHomModule(res[0],currQuotient,&dummy)) && (!rIsPluralRing(currRing)))
366  {
367    syMinStep1(res,length);
368    delete dummy;
369    return;
370  }
371  while ((syzIndex<length-1) && (res[syzIndex]!=NULL) && (res[syzIndex+1]!=NULL))
372  {
373    syMinStep(res[syzIndex-1],res[syzIndex],FALSE,res[syzIndex+1]);
374    syzIndex++;
375  }
376  if (res[syzIndex]!=NULL)
377    syMinStep(res[syzIndex-1],res[syzIndex]);
378  if (!idIs0(res[0]))
379    idMinEmbedding(res[0],TRUE);
380}
381
382/*2
383* resolution of ideal/module arg, <=maxlength steps, (r[0..maxlength])
384*   no limitation in length if maxlength==0
385* input:arg
386*       minim: TRUE means mres cmd, FALSE nres cmd.
387*       if *len!=0: module weights: weights[0]
388*          (and weights is defined:weights[0..len-1]
389*
390* output:resolvente r[0..length-1],
391*        module weights: weights[0..length-1]
392*/
393resolvente syResolvente(ideal arg, int maxlength, int * length,
394                        intvec *** weights, BOOLEAN minim)
395{
396  resolvente res;
397  resolvente newres;
398  tHomog hom=isNotHomog;
399  ideal temp=NULL;
400  intvec *w = NULL,**tempW;
401  int i,k,syzIndex = 0,j,rk_arg=si_max(1,(int)idRankFreeModule(arg));
402  int Kstd1_OldDeg=Kstd1_deg;
403  BOOLEAN completeMinim;
404  BOOLEAN oldDegBound=TEST_OPT_DEGBOUND;
405  BOOLEAN setRegularity=TRUE;
406  int wlength=*length;
407
408  if (maxlength!=-1) *length = maxlength+1;
409  else              *length = 5;
410  if ((wlength!=0) && (*length!=wlength))
411  {
412    intvec **wtmp = (intvec**)omAlloc0((*length)*sizeof(intvec*));
413    wtmp[0]=(*weights)[0];
414    omFreeSize((ADDRESS)*weights,wlength*sizeof(intvec*));
415    *weights=wtmp;
416  }
417  res = (resolvente)omAlloc0((*length)*sizeof(ideal));
418
419/*--- initialize the syzygy-ring -----------------------------*/
420  ring origR = currRing;
421  ring syz_ring = rCurrRingAssure_SyzComp();
422  rSetSyzComp(rk_arg);
423
424  if (syz_ring != origR)
425  {
426    res[0] = idrCopyR_NoSort(arg, origR);
427  }
428  else
429  {
430    res[0] = idCopy(arg);
431  }
432
433/*--- creating weights for the module components ---------------*/
434  if ((weights!=NULL) && (*weights!=NULL)&& ((*weights)[0]!=NULL))
435  {
436    if (!idTestHomModule(res[0],currQuotient,(*weights)[0]))
437    {
438      WarnS("wrong weights given(1):"); (*weights)[0]->show();PrintLn();
439      idHomModule(res[0],currQuotient,&w);
440      w->show();PrintLn();
441      *weights=NULL;
442    }
443  }
444
445  if ((weights==NULL) || (*weights==NULL) || ((*weights)[0]==NULL))
446  {
447    hom=(tHomog)idHomModule(res[0],currQuotient,&w);
448    if (hom==isHomog)
449    {
450      *weights = (intvec**)omAlloc0((*length)*sizeof(intvec*));
451      if (w!=NULL) (*weights)[0] = ivCopy(w);
452    }
453  }
454  else
455  {
456    if ((weights!=NULL) && (*weights!=NULL)&& ((*weights)[0]!=NULL))
457    {
458      w = ivCopy((*weights)[0]);
459      hom = isHomog;
460    }
461  }
462
463#ifdef HAVE_PLURAL
464  if (rIsPluralRing(currRing) && !rIsSCA(currRing) )
465  { 
466// quick solution; need theory to apply homog GB stuff for G-Algebras
467    hom = isNotHomog;
468  }
469#endif // HAVE_PLURAL
470
471  if (hom==isHomog)
472  {
473    intvec *w1 = syPrepareModComp(res[0],&w);
474    if (w!=NULL) { delete w;w=NULL; }
475    w = w1;
476    j = 0;
477    while ((j<IDELEMS(res[0])) && (res[0]->m[j]==NULL)) j++;
478    if (j<IDELEMS(res[0]))
479    {
480      if (pFDeg(res[0]->m[j],currRing)!=pTotaldegree(res[0]->m[j]))
481        setRegularity = FALSE;
482    }
483  }
484  else
485  {
486    setRegularity = FALSE;
487  }
488
489/*--- the main loop --------------------------------------*/
490  while ((!idIs0(res[syzIndex])) &&
491         ((maxlength==-1) || (syzIndex<=maxlength)))
492   // (syzIndex<maxlength+(int)minim)))
493/*--- compute one step more for minimizing-----------------*/
494  {
495    if (Kstd1_deg!=0) Kstd1_deg++;
496    if (syzIndex+1==*length)
497    {
498      newres = (resolvente)omAlloc0((*length+5)*sizeof(ideal));
499      tempW = (intvec**)omAlloc0((*length+5)*sizeof(intvec*));
500      for (j=0;j<*length;j++)
501      {
502        newres[j] = res[j];
503        if (*weights!=NULL) tempW[j] = (*weights)[j];
504        /*else              tempW[j] = NULL;*/
505      }
506      omFreeSize((ADDRESS)res,*length*sizeof(ideal));
507      if (*weights != NULL) omFreeSize((ADDRESS)*weights,*length*sizeof(intvec*));
508      *length += 5;
509      res=newres;
510      *weights = tempW;
511    }
512/*--- interreducing first -----------------------------------*/
513    if (syzIndex>0)
514    {
515      int rkI=idRankFreeModule(res[syzIndex]);
516      rSetSyzComp(rkI);
517    }
518    if (minim || (syzIndex!=0))
519    {
520      temp = kInterRedOld(res[syzIndex],currQuotient);
521      idDelete(&res[syzIndex]);
522      idSkipZeroes(temp);
523      res[syzIndex] = temp;
524    }
525    temp = NULL;
526/*--- computing the syzygy modules --------------------------------*/
527    if ((currQuotient==NULL)&&(syzIndex==0)&& (!TEST_OPT_DEGBOUND))
528    {
529      res[/*syzIndex+*/1] = idSyzygies(res[0/*syzIndex*/],hom,&w,FALSE,setRegularity,&Kstd1_deg);
530      if ((!TEST_OPT_NOTREGULARITY) && (Kstd1_deg>0)) test |= Sy_bit(OPT_DEGBOUND);
531    }
532    else
533    {
534        res[syzIndex+1] = idSyzygies(res[syzIndex],hom,&w,FALSE);
535    }
536    completeMinim=(syzIndex!=maxlength) || (maxlength ==-1) || (hom!=isHomog);
537    syzIndex++;
538    if (TEST_OPT_PROT) Print("[%d]\n",syzIndex);
539    if ((minim)||(syzIndex>1))
540      syMinStep(res[syzIndex-1],res[syzIndex],!completeMinim,NULL,hom);
541    if (!completeMinim)
542    /*minim is TRUE, we are in the module: maxlength, maxlength <>0*/
543    {
544      idDelete(&res[syzIndex]);
545    }
546/*---creating the iterated weights for module components ---------*/
547    if ((hom == isHomog) && (!idIs0(res[syzIndex])))
548    {
549//Print("die %d Modulegewichte sind:\n",w1->length());
550//w1->show();
551//PrintLn();
552      int max_comp = idRankFreeModule(res[syzIndex]);
553      k = max_comp - rGetCurrSyzLimit();
554      assume(w != NULL);
555      if (w != NULL)
556        w->resize(max_comp+IDELEMS(res[syzIndex]));
557      else
558        w = new intvec(max_comp+IDELEMS(res[syzIndex]));
559      (*weights)[syzIndex] = new intvec(k);
560      for (i=0;i<k;i++)
561      {
562        if (res[syzIndex-1]->m[i]!=NULL) // hs
563        {
564          (*w)[i + rGetCurrSyzLimit()] = pFDeg(res[syzIndex-1]->m[i],currRing);
565          if (pGetComp(res[syzIndex-1]->m[i])>0)
566            (*w)[i + rGetCurrSyzLimit()]
567              += (*w)[pGetComp(res[syzIndex-1]->m[i])-1];
568          (*((*weights)[syzIndex]))[i] = (*w)[i+rGetCurrSyzLimit()];
569        }
570      }
571      for (i=k;i<k+IDELEMS(res[syzIndex]);i++)
572      {
573        if (res[syzIndex]->m[i-k]!=NULL)
574          (*w)[i+rGetCurrSyzLimit()] = pFDeg(res[syzIndex]->m[i-k],currRing)
575                    +(*w)[pGetComp(res[syzIndex]->m[i-k])-1];
576      }
577    }
578  }
579/*--- end of the main loop --------------------------------------*/
580/*--- deleting the temporare data structures --------------------*/
581  if ((syzIndex!=0) && (res[syzIndex]!=NULL) && (idIs0(res[syzIndex])))
582    idDelete(&res[syzIndex]);
583  if (w !=NULL) delete w;
584
585  Kstd1_deg=Kstd1_OldDeg;
586  if (!oldDegBound)
587    test &= ~Sy_bit(OPT_DEGBOUND);
588
589  for (i=1; i<=syzIndex; i++)
590  {
591    if (! idIs0(res[i]))
592    {
593      for (j=0; j<IDELEMS(res[i]); j++)
594      {
595        pShift(&res[i]->m[j], -rGetMaxSyzComp(i));
596      }
597    }
598  }
599/*--- going back to the original ring -------------------------*/
600  if (origR != syz_ring)
601  {
602    rChangeCurrRing(origR);
603    for (i=0; i<=syzIndex; i++)
604    {
605      res[i] = idrMoveR_NoSort(res[i], syz_ring);
606    }
607    rKill(syz_ring);
608  }
609  return res;
610}
611
612syStrategy syResolution(ideal arg, int maxlength,intvec * w, BOOLEAN minim)
613{
614
615#ifdef HAVE_PLURAL
616
617  const ideal idSaveCurrQuotient = currQuotient;
618  const ideal idSaveCurrRingQuotient = currRing->qideal;
619
620  if( rIsSCA(currRing) )
621  {
622
623#ifdef RDEBUG
624//    rWrite(currRing);
625//    rDebugPrint(currRing);
626#endif
627
628    if( ncExtensions(TESTSYZSCAMASK) )
629    {
630      currQuotient = SCAQuotient(currRing);
631      currRing->qideal = currQuotient;
632    }
633
634    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
635    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
636   
637    arg = id_KillSquares(arg, m_iFirstAltVar, m_iLastAltVar, currRing, false); // kill suares in input!
638  }
639#endif
640 
641  int typ0;
642  syStrategy result=(syStrategy)omAlloc0(sizeof(ssyStrategy));
643
644  if ((w!=NULL) && (!idTestHomModule(arg,currQuotient,w))) // is this right in SCA case???
645  {
646    WarnS("wrong weights given(2):");w->show();PrintLn();
647    idHomModule(arg,currQuotient,&w);
648    w->show();PrintLn();
649    w=NULL;
650  }
651  if (w!=NULL)
652  {
653    result->weights = (intvec**)omAlloc0Bin(char_ptr_bin);
654    (result->weights)[0] = ivCopy(w);
655    result->length = 1;
656  }
657  resolvente fr = syResolvente(arg,maxlength,&(result->length),&(result->weights),minim),fr1;
658  if (minim)
659  {
660    result->minres = (resolvente)omAlloc0((result->length+1)*sizeof(ideal));
661    fr1 =  result->minres;
662  }
663  else
664  {
665    result->fullres = (resolvente)omAlloc0((result->length+1)*sizeof(ideal));
666    fr1 =  result->fullres;
667  }
668  for (int i=result->length-1;i>=0;i--)
669  {
670    if (fr[i]!=NULL)
671      fr1[i] = fr[i];
672    fr[i] = NULL;
673  }
674  omFreeSize((ADDRESS)fr,(result->length)*sizeof(ideal));
675
676
677#ifdef HAVE_PLURAL
678  if( rIsSCA(currRing) )
679  {
680
681    if( ncExtensions(TESTSYZSCAMASK) )
682    {
683      currQuotient     = idSaveCurrQuotient; 
684      currRing->qideal = idSaveCurrRingQuotient;
685    }
686
687    id_Delete(&arg, currRing);
688  }
689#endif
690
691
692  return result;
693}
694
695static poly sypCopyConstant(poly inp)
696{
697  poly outp=NULL,q;
698
699  while (inp!=NULL)
700  {
701    if (pLmIsConstantComp(inp))
702    {
703      if (outp==NULL)
704      {
705        q = outp = pHead(inp);
706      }
707      else
708      {
709        pNext(q) = pHead(inp);
710        pIter(q);
711      }
712    }
713    pIter(inp);
714  }
715  return outp;
716}
717int syDetect(ideal id,int index,BOOLEAN homog,int * degrees,int * tocancel)
718{
719  int i, j, k, ModComp,subFromRank=0, lu;
720  poly p, q, qq, Unit1, Unit2;
721  ideal temp;
722
723  if (idIs0(id)) return 0;
724  temp = idInit(IDELEMS(id),id->rank);
725  for (i=0;i<IDELEMS(id);i++)
726  {
727    temp->m[i] = sypCopyConstant(id->m[i]);
728  }
729  i = IDELEMS(id);
730  while ((i>0) && (temp->m[i-1]==NULL)) i--;
731  if (i==0)
732  {
733    idDelete(&temp);
734    return 0;
735  }
736  j = 0;
737  p = NULL;
738  while ((j<i) && (temp->m[j]==NULL)) j++;
739  while (j<i)
740  {
741    if (homog)
742    {
743      if (index==0) k = pFDeg(temp->m[j],currRing)+degrees[pGetComp(temp->m[j])];
744      else          k = degrees[pGetComp(temp->m[j])];
745      if (k>=index) tocancel[k-index]++;
746      if ((k>=0) && (index==0)) subFromRank++;
747    }
748    else
749    {
750      tocancel[0]--;
751    }
752    syGaussForOne(temp,j,pGetComp(temp->m[j]),j+1,i);
753    j++;
754    while ((j<i) && (temp->m[j]==NULL)) j++;
755  }
756  idDelete(&temp);
757  return subFromRank;
758}
759
760void syDetect(ideal id,int index,int rsmin, BOOLEAN homog,
761              intvec * degrees,intvec * tocancel)
762{
763  int * deg=NULL;
764  int * tocan=(int*) omAlloc0(tocancel->length()*sizeof(int));
765  int i;
766
767  if (homog)
768  {
769    deg = (int*) omAlloc0(degrees->length()*sizeof(int));
770    for (i=degrees->length();i>0;i--)
771      deg[i-1] = (*degrees)[i-1]-rsmin;
772  }
773  int dummy=syDetect(id,index,homog,deg,tocan);
774  for (i=tocancel->length();i>0;i--)
775    (*tocancel)[i-1] = tocan[i-1];
776  if (homog)
777    omFreeSize((ADDRESS)deg,degrees->length()*sizeof(int));
778  omFreeSize((ADDRESS)tocan,tocancel->length()*sizeof(int));
779}
780
781/*2
782* computes the betti numbers from a given resolution
783* of length 'length' (0..length-1), not necessairily minimal,
784* (if weights are given, they are used)
785* returns the int matrix of betti numbers
786* and the regularity
787*/
788intvec * syBetti(resolvente res,int length, int * regularity,
789                 intvec* weights,BOOLEAN tomin,int * row_shift)
790{
791//#define BETTI_WITH_ZEROS
792  //tomin = FALSE;
793  int i,j=0,k=0,l,rows,cols,mr;
794  int *temp1,*temp2,*temp3;/*used to compute degrees*/
795  int *tocancel; /*(BOOLEAN)tocancel[i]=element is superfluous*/
796  int r0_len;
797
798  /*------ compute size --------------*/
799  *regularity = -1;
800  cols = length;
801  while ((cols>0)
802  && ((res[cols-1]==NULL)
803    || (idIs0(res[cols-1]))))
804  {
805    cols--;
806  }
807  intvec * result;
808  if (idIs0(res[0]))
809  {
810    if (res[0]==NULL)
811      result = new intvec(1,1,1);
812    else
813      result = new intvec(1,1,res[0]->rank);
814    return result;
815  }
816  intvec *w=NULL;
817  if (weights!=NULL)
818  {
819    if (!idTestHomModule(res[0],currQuotient,weights))
820    {
821      WarnS("wrong weights given(3):");weights->show();PrintLn();
822      idHomModule(res[0],currQuotient,&w);
823      if (w!=NULL) { w->show();PrintLn();}
824      weights=NULL;
825    }
826  }
827#if 0
828  if (idHomModule(res[0],currQuotient,&w)!=isHomog)
829  {
830    Warn("betti-command: Input is not homogeneous!");
831    weights=NULL;
832  }
833#endif
834  if (weights==NULL) weights=w;
835  else delete w;
836  r0_len=IDELEMS(res[0]);
837  while ((r0_len>0) && (res[0]->m[r0_len-1]==NULL)) r0_len--;
838  #ifdef SHOW_W
839  PrintS("weights:");if (weights!=NULL) weights->show(); else Print("NULL"); PrintLn();
840  #endif
841  int rkl=l = si_max(idRankFreeModule(res[0]),res[0]->rank);
842  i = 0;
843  while ((i<length) && (res[i]!=NULL))
844  {
845    if (IDELEMS(res[i])>l) l = IDELEMS(res[i]);
846    i++;
847  }
848  temp1 = (int*)omAlloc0((l+1)*sizeof(int));
849  temp2 = (int*)omAlloc((l+1)*sizeof(int));
850  rows = 1;
851  mr = 1;
852  cols++;
853  for (i=0;i<cols-1;i++)
854  {
855    if ((i==0) && (weights!=NULL)) pSetModDeg(weights);
856    memset(temp2,0,(l+1)*sizeof(int));
857    for (j=0;j<IDELEMS(res[i]);j++)
858    {
859      if (res[i]->m[j]!=NULL)
860      {
861        if ((pGetComp(res[i]->m[j])>l)
862        || ((i>1) && (res[i-1]->m[pGetComp(res[i]->m[j])-1]==NULL)))
863        {
864          WerrorS("input not a resolvent");
865          omFreeSize((ADDRESS)temp1,(l+1)*sizeof(int));
866          omFreeSize((ADDRESS)temp2,(l+1)*sizeof(int));
867          return NULL;
868        }
869        temp2[j+1] = pFDeg(res[i]->m[j],currRing)+temp1[pGetComp(res[i]->m[j])];
870        if (temp2[j+1]-i>rows) rows = temp2[j+1]-i;
871        if (temp2[j+1]-i<mr) mr = temp2[j+1]-i;
872      }
873    }
874    if ((i==0) && (weights!=NULL)) pSetModDeg(NULL);
875    temp3 = temp1;
876    temp1 = temp2;
877    temp2 = temp3;
878  }
879  mr--;
880  if (weights!=NULL)
881  {
882    for(j=0;j<weights->length();j++)
883    {
884      if (rows <(*weights)[j]+1) rows=(-mr)+(*weights)[j]+1;
885    }
886  }
887  /*------ computation betti numbers --------------*/
888  rows -= mr;
889  result = new intvec(rows+1,cols,0);
890  if (weights!=NULL)
891  {
892    for(j=0;j<weights->length();j++)
893    {
894      IMATELEM((*result),(-mr)+(*weights)[j]+1,1) ++;
895      //Print("imat(%d,%d)++ -> %d\n",(-mr)+(*weights)[j]+1, 1, IMATELEM((*result),(-mr)+(*weights)[j]+1,1));
896    }
897  }
898  else
899  {
900    (*result)[(-mr)*cols] = /*idRankFreeModule(res[0])*/ rkl;
901    if ((!idIs0(res[0])) && ((*result)[(-mr)*cols]==0))
902      (*result)[(-mr)*cols] = 1;
903  }
904  tocancel = (int*)omAlloc0((rows+1)*sizeof(int));
905  memset(temp1,0,(l+1)*sizeof(int));
906  if (weights!=NULL)
907  {
908    memset(temp2,0,l*sizeof(int));
909    pSetModDeg(weights);
910  }
911  else
912    memset(temp2,0,l*sizeof(int));
913  int dummy = syDetect(res[0],0,TRUE,temp2,tocancel);
914  if (weights!=NULL) pSetModDeg(NULL);
915  if (tomin)
916  {
917    //(*result)[(-mr)*cols] -= dummy;
918    for(j=0;j<=rows+mr;j++)
919    {
920      //Print("tocancel[%d]=%d imat(%d,%d)=%d\n",j,tocancel[j],(-mr)+j+1,1,IMATELEM((*result),(-mr)+j+1,1));
921      IMATELEM((*result),(-mr)+j+1,1) -= tocancel[j];
922    }
923  }
924  for (i=0;i<cols-1;i++)
925  {
926    if ((i==0) && (weights!=NULL)) pSetModDeg(weights);
927    memset(temp2,0,l*sizeof(int));
928    for (j=0;j<IDELEMS(res[i]);j++)
929    {
930      if (res[i]->m[j]!=NULL)
931      {
932        temp2[j+1] = pFDeg(res[i]->m[j],currRing)+temp1[pGetComp(res[i]->m[j])];
933        //(*result)[i+1+(temp2[j+1]-i-1)*cols]++;
934        //if (temp2[j+1]>i) IMATELEM((*result),temp2[j+1]-i-mr,i+2)++;
935        IMATELEM((*result),temp2[j+1]-i-mr,i+2)++;
936      }
937      else if (i==0)
938      {
939        if (j<r0_len) IMATELEM((*result),-mr,2)++;
940      }
941    }
942  /*------ computation betti numbers, if res not minimal --------------*/
943    if (tomin)
944    {
945      for (j=mr;j<rows+mr;j++)
946      {
947        //(*result)[i+1+j*cols] -= tocancel[j+1];
948        IMATELEM((*result),j+1-mr,i+2) -= tocancel[j+1];
949      }
950      if ((i<length-1) && (res[i+1]!=NULL))
951      {
952        memset(tocancel,0,(rows+1)*sizeof(int));
953        dummy = syDetect(res[i+1],i+1,TRUE,temp2,tocancel);
954        for (j=0;j<rows;j++)
955        {
956          //(*result)[i+1+j*cols] -= tocancel[j];
957          IMATELEM((*result),j+1,i+2) -= tocancel[j];
958        }
959      }
960    }
961    temp3 = temp1;
962    temp1 = temp2;
963    temp2 = temp3;
964    for (j=0;j<=rows;j++)
965    {
966    //  if (((*result)[i+1+j*cols]!=0) && (j>*regularity)) *regularity = j;
967      if ((IMATELEM((*result),j+1,i+2)!=0) && (j>*regularity)) *regularity = j;
968    }
969    if ((i==0) && (weights!=NULL)) pSetModDeg(NULL);
970  }
971  // Print("nach minim:\n"); result->show(); PrintLn();
972  /*------ clean up --------------*/
973  omFreeSize((ADDRESS)tocancel,(rows+1)*sizeof(int));
974  omFreeSize((ADDRESS)temp1,(l+1)*sizeof(int));
975  omFreeSize((ADDRESS)temp2,(l+1)*sizeof(int));
976  if ((tomin) && (mr<0))  // deletes the first (zero) line
977  {
978    for (j=1;j<=rows+mr+1;j++)
979    {
980      for (k=1;k<=cols;k++)
981      {
982        IMATELEM((*result),j,k) = IMATELEM((*result),j-mr,k);
983      }
984    }
985    for (j=rows+mr+1;j<=rows+1;j++)
986    {
987      for (k=1;k<=cols;k++)
988      {
989        IMATELEM((*result),j,k) = 0;
990      }
991    }
992  }
993  j = 0;
994  k = 0;
995  for (i=1;i<=result->rows();i++)
996  {
997    for(l=1;l<=result->cols();l++)
998    if (IMATELEM((*result),i,l) != 0)
999    {
1000      j = si_max(j, i-1);
1001      k = si_max(k, l-1);
1002    }
1003  }
1004  intvec * exactresult=new intvec(j+1,k+1,0);
1005  for (i=0;i<exactresult->rows();i++)
1006  {
1007    for (j=0;j<exactresult->cols();j++)
1008    {
1009      IMATELEM(*exactresult,i+1,j+1) = IMATELEM(*result,i+1,j+1);
1010    }
1011  }
1012  if (row_shift!=NULL) *row_shift = mr;
1013  delete result;
1014  return exactresult;
1015}
1016
1017/*2
1018* minbare via syzygies
1019*/
1020ideal syMinBase(ideal arg)
1021{
1022  intvec ** weights=NULL;
1023  int leng;
1024  if (idIs0(arg)) return idInit(1,arg->rank);
1025  resolvente res=syResolvente(arg,1,&leng,&weights,TRUE);
1026  ideal result=res[0];
1027  omFreeSize((ADDRESS)res,leng*sizeof(ideal));
1028  if (weights!=NULL)
1029  {
1030    if (*weights!=NULL)
1031    {
1032      delete (*weights);
1033      *weights=NULL;
1034    }
1035    if ((leng>=1) && (*(weights+1)!=NULL))
1036    {
1037      delete *(weights+1);
1038      *(weights+1)=NULL;
1039    }
1040  }
1041  idSkipZeroes(result);
1042  return result;
1043}
1044
1045#if 0  /* currently used: syBetti */
1046/*2
1047* computes Betti-numbers from a resolvente of
1048* (non-)homogeneous objects
1049* the numbers of entrees !=NULL in res and weights must be equal
1050* and < length
1051*/
1052intvec * syNewBetti(resolvente res, intvec ** weights, int length)
1053{
1054  intvec * result,*tocancel;
1055  int i,j,k,rsmin=0,rsmax=0,rs=0;
1056  BOOLEAN homog=TRUE;
1057
1058  if (weights!=NULL)           //---homogeneous Betti numbers
1059  {
1060/*--------------computes size of the field----------------------*/
1061    for (i=1;i<length;i++)
1062    {
1063      if (weights[i] != NULL)
1064      {
1065        for (j=1;j<(weights[i])->length();j++)
1066        {
1067          if ((*(weights[i]))[j]-i<rsmin) rsmin = (*(weights[i]))[j]-i;
1068          if ((*(weights[i]))[j]-i>rsmax) rsmax = (*(weights[i]))[j]-i;
1069        }
1070      }
1071    }
1072    i = 0;
1073    while (weights[i] != NULL) i++;
1074    i--;
1075    for (j=0;j<IDELEMS(res[i]);j++)
1076    {
1077      if (res[i]->m[j]!=NULL)
1078      {
1079        k = pFDeg(res[i]->m[j],currRing)+(*(weights[i]))[pGetComp(res[i]->m[j])]-i-1;
1080        if (k>rsmax) rsmax = k;
1081        if (k<rsmin) rsmin = k;
1082      }
1083    }
1084    for (j=1;j<(weights[0])->length();j++)
1085    {
1086      if ((*weights[0])[j]>rsmax) rsmax = (*weights[0])[j];
1087      if ((*weights[0])[j]<rsmin) rsmin = (*weights[0])[j];
1088    }
1089//Print("rsmax = %d\n",rsmax);
1090//Print("rsmin = %d\n",rsmin);
1091    rs = rsmax-rsmin+1;
1092    result = new intvec(rs,i+2,0);
1093    tocancel = new intvec(rs);
1094/*-----------enter the Betti numbers-------------------------------*/
1095    if (/*idRankFreeModule(res[0])*/ res[0]->rank==0)
1096    {
1097      IMATELEM(*result,1-rsmin,1)=1;
1098    }
1099    else
1100    {
1101      for (i=1;i<(weights[0])->length();i++)
1102        IMATELEM(*result,(*weights[0])[i]+1-rsmin,1)++;
1103    }
1104    i = 1;
1105    while (weights[i]!=NULL)
1106    {
1107      for (j=1;j<(weights[i])->length();j++)
1108      {
1109        IMATELEM(*result,(*(weights[i]))[j]-i+1-rsmin,i+1)++;
1110      }
1111      i++;
1112    }
1113    i--;
1114    for (j=0;j<IDELEMS(res[i]);j++)
1115    {
1116      if (res[i]->m[j]!=NULL)
1117      {
1118        k = pFDeg(res[i]->m[j],currRing)+(*(weights[i]))[pGetComp(res[i]->m[j])]-i;
1119        IMATELEM(*result,k-rsmin,i+2)++;
1120      }
1121    }
1122  }
1123  else                //-----the non-homgeneous case
1124  {
1125    homog = FALSE;
1126    tocancel = new intvec(1);
1127    k = length;
1128    while ((k>0) && (idIs0(res[k-1]))) k--;
1129    result = new intvec(1,k+1,0);
1130    (*result)[0] = res[0]->rank;
1131    for (i=0;i<length;i++)
1132    {
1133      if (res[i]!=NULL)
1134      {
1135        for (j=0;j<IDELEMS(res[i]);j++)
1136        {
1137          if (res[i]->m[j]!=NULL) (*result)[i+1]++;
1138        }
1139      }
1140    }
1141  }
1142/*--------computes the Betti numbers for the minimized reolvente----*/
1143
1144  i = 1;
1145  while ((res[i]!=NULL) && (weights[i]!=NULL))
1146  {
1147    syDetect(res[i],i,rsmin,homog,weights[i],tocancel);
1148    if (homog)
1149    {
1150      for (j=0;j<rs-1;j++)
1151      {
1152        IMATELEM((*result),j+1,i+1) -= (*tocancel)[j];
1153        IMATELEM((*result),j+1,i+2) -= (*tocancel)[j+1];
1154      }
1155      IMATELEM((*result),rs,i+1) -= (*tocancel)[rs-1];
1156    }
1157    else
1158    {
1159      (*result)[i+1] -= (*tocancel)[0];
1160      (*result)[i+2] -= (*tocancel)[0];
1161    }
1162    i++;
1163  }
1164
1165/*--------print Betti numbers for control---------------------------*/
1166  for(i=rsmin;i<=rsmax;i++)
1167  {
1168    Print("%2d:",i);
1169    for(j=1;j<=result->cols();j++)
1170    {
1171      Print(" %5d",IMATELEM(*result,i-rsmin+1,j));
1172    }
1173    PrintLn();
1174  }
1175  return result;
1176}
1177#endif
1178
Note: See TracBrowser for help on using the repository browser.