source: git/kernel/syz.cc @ 88e5d0

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