source: git/kernel/syz.cc @ 19370c

spielwiese
Last change on this file since 19370c was 90adb6a, checked in by Motsak Oleksandr <motsak@…>, 17 years ago
*motsak: different sca factor ideal for resolution git-svn-id: file:///usr/local/Singular/svn/trunk@9807 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 29.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: syz.cc,v 1.12 2007-02-02 15:19:46 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 (currRing->OrdSgn == 1)
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(syz->m[k],pCopy(Unit1));
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 = kInterRed(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 = kInterRed(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#ifdef HAVE_PLURAL
619  ideal idSaveCurrQuotient = currQuotient;
620  ideal idSaveCurrRingQuotient = currRing->qideal;
621  if( rIsSCA(currRing) )
622  {
623    currQuotient = currRing->nc->SCAQuotient();
624    currRing->qideal = currQuotient;
625
626    const unsigned int m_iFirstAltVar = scaFirstAltVar(currRing);
627    const unsigned int m_iLastAltVar  = scaLastAltVar(currRing);
628   
629    arg = id_KillSquares(arg, m_iFirstAltVar, m_iLastAltVar, currRing); // kill suares in input!
630  }
631#endif
632 
633  int typ0;
634  syStrategy result=(syStrategy)omAlloc0(sizeof(ssyStrategy));
635
636  if ((w!=NULL) && (!idTestHomModule(arg,currQuotient,w))) // is this right in SCA case???
637  {
638    WarnS("wrong weights given(2):");w->show();PrintLn();
639    idHomModule(arg,currQuotient,&w);
640    w->show();PrintLn();
641    w=NULL;
642  }
643  if (w!=NULL)
644  {
645    result->weights = (intvec**)omAlloc0Bin(void_ptr_bin);
646    (result->weights)[0] = ivCopy(w);
647    result->length = 1;
648  }
649  resolvente fr = syResolvente(arg,maxlength,&(result->length),&(result->weights),minim),fr1;
650  if (minim)
651  {
652    result->minres = (resolvente)omAlloc0((result->length+1)*sizeof(ideal));
653    fr1 =  result->minres;
654  }
655  else
656  {
657    result->fullres = (resolvente)omAlloc0((result->length+1)*sizeof(ideal));
658    fr1 =  result->fullres;
659  }
660  for (int i=result->length-1;i>=0;i--)
661  {
662    if (fr[i]!=NULL)
663      fr1[i] = fr[i];
664    fr[i] = NULL;
665  }
666  omFreeSize((ADDRESS)fr,(result->length)*sizeof(ideal));
667
668
669#ifdef HAVE_PLURAL
670  if( rIsSCA(currRing) )
671  {
672    currQuotient     = idSaveCurrQuotient; 
673    currRing->qideal = idSaveCurrRingQuotient;
674
675    id_Delete(&arg, currRing);
676  }
677#endif
678
679
680  return result;
681}
682
683static poly sypCopyConstant(poly inp)
684{
685  poly outp=NULL,q;
686
687  while (inp!=NULL)
688  {
689    if (pLmIsConstantComp(inp))
690    {
691      if (outp==NULL)
692      {
693        q = outp = pHead(inp);
694      }
695      else
696      {
697        pNext(q) = pHead(inp);
698        pIter(q);
699      }
700    }
701    pIter(inp);
702  }
703  return outp;
704}
705int syDetect(ideal id,int index,BOOLEAN homog,int * degrees,int * tocancel)
706{
707  int i, j, k, ModComp,subFromRank=0, lu;
708  poly p, q, qq, Unit1, Unit2;
709  ideal temp;
710
711  if (idIs0(id)) return 0;
712  temp = idInit(IDELEMS(id),id->rank);
713  for (i=0;i<IDELEMS(id);i++)
714  {
715    temp->m[i] = sypCopyConstant(id->m[i]);
716  }
717  i = IDELEMS(id);
718  while ((i>0) && (temp->m[i-1]==NULL)) i--;
719  if (i==0)
720  {
721    idDelete(&temp);
722    return 0;
723  }
724  j = 0;
725  p = NULL;
726  while ((j<i) && (temp->m[j]==NULL)) j++;
727  while (j<i)
728  {
729    if (homog)
730    {
731      if (index==0) k = pFDeg(temp->m[j],currRing)+degrees[pGetComp(temp->m[j])];
732      else          k = degrees[pGetComp(temp->m[j])];
733      if (k>=index) tocancel[k-index]++;
734      if ((k>=0) && (index==0)) subFromRank++;
735    }
736    else
737    {
738      tocancel[0]--;
739    }
740    syGaussForOne(temp,j,pGetComp(temp->m[j]),j+1,i);
741    j++;
742    while ((j<i) && (temp->m[j]==NULL)) j++;
743  }
744  idDelete(&temp);
745  return subFromRank;
746}
747
748void syDetect(ideal id,int index,int rsmin, BOOLEAN homog,
749              intvec * degrees,intvec * tocancel)
750{
751  int * deg=NULL;
752  int * tocan=(int*) omAlloc0(tocancel->length()*sizeof(int));
753  int i;
754
755  if (homog)
756  {
757    deg = (int*) omAlloc0(degrees->length()*sizeof(int));
758    for (i=degrees->length();i>0;i--)
759      deg[i-1] = (*degrees)[i-1]-rsmin;
760  }
761  int dummy=syDetect(id,index,homog,deg,tocan);
762  for (i=tocancel->length();i>0;i--)
763    (*tocancel)[i-1] = tocan[i-1];
764  if (homog)
765    omFreeSize((ADDRESS)deg,degrees->length()*sizeof(int));
766  omFreeSize((ADDRESS)tocan,tocancel->length()*sizeof(int));
767}
768
769/*2
770* computes the betti numbers from a given resolution
771* of length 'length' (0..length-1), not necessairily minimal,
772* (if weights are given, they are used)
773* returns the int matrix of betti numbers
774* and the regularity
775*/
776intvec * syBetti(resolvente res,int length, int * regularity,
777                 intvec* weights,BOOLEAN tomin,int * row_shift)
778{
779//#define BETTI_WITH_ZEROS
780  //tomin = FALSE;
781  int i,j=0,k=0,l,rows,cols,mr;
782  int *temp1,*temp2,*temp3;/*used to compute degrees*/
783  int *tocancel; /*(BOOLEAN)tocancel[i]=element is superfluous*/
784  int r0_len;
785
786  /*------ compute size --------------*/
787  *regularity = -1;
788  cols = length;
789  while ((cols>0)
790  && ((res[cols-1]==NULL)
791    || (idIs0(res[cols-1]))))
792  {
793    cols--;
794  }
795  intvec * result;
796  if (idIs0(res[0]))
797  {
798    if (res[0]==NULL)
799      result = new intvec(1,1,1);
800    else
801      result = new intvec(1,1,res[0]->rank);
802    return result;
803  }
804  intvec *w=NULL;
805  if (weights!=NULL)
806  {
807    if (!idTestHomModule(res[0],currQuotient,weights))
808    {
809      WarnS("wrong weights given(3):");weights->show();PrintLn();
810      idHomModule(res[0],currQuotient,&w);
811      if (w!=NULL) { w->show();PrintLn();}
812      weights=NULL;
813    }
814  }
815#if 0
816  if (idHomModule(res[0],currQuotient,&w)!=isHomog)
817  {
818    Warn("betti-command: Input is not homogeneous!");
819    weights=NULL;
820  }
821#endif
822  if (weights==NULL) weights=w;
823  else delete w;
824  r0_len=IDELEMS(res[0]);
825  while ((r0_len>0) && (res[0]->m[r0_len-1]==NULL)) r0_len--;
826  #ifdef SHOW_W
827  PrintS("weights:");if (weights!=NULL) weights->show(); else Print("NULL"); PrintLn();
828  #endif
829  int rkl=l = si_max(idRankFreeModule(res[0]),res[0]->rank);
830  i = 0;
831  while ((i<length) && (res[i]!=NULL))
832  {
833    if (IDELEMS(res[i])>l) l = IDELEMS(res[i]);
834    i++;
835  }
836  temp1 = (int*)omAlloc0((l+1)*sizeof(int));
837  temp2 = (int*)omAlloc((l+1)*sizeof(int));
838  rows = 1;
839  mr = 1;
840  cols++;
841  for (i=0;i<cols-1;i++)
842  {
843    if ((i==0) && (weights!=NULL)) pSetModDeg(weights);
844    memset(temp2,0,(l+1)*sizeof(int));
845    for (j=0;j<IDELEMS(res[i]);j++)
846    {
847      if (res[i]->m[j]!=NULL)
848      {
849        if ((pGetComp(res[i]->m[j])>l)
850        || ((i>1) && (res[i-1]->m[pGetComp(res[i]->m[j])-1]==NULL)))
851        {
852          WerrorS("input not a resolvent");
853          omFreeSize((ADDRESS)temp1,(l+1)*sizeof(int));
854          omFreeSize((ADDRESS)temp2,(l+1)*sizeof(int));
855          return NULL;
856        }
857        temp2[j+1] = pFDeg(res[i]->m[j],currRing)+temp1[pGetComp(res[i]->m[j])];
858        if (temp2[j+1]-i>rows) rows = temp2[j+1]-i;
859        if (temp2[j+1]-i<mr) mr = temp2[j+1]-i;
860      }
861    }
862    if ((i==0) && (weights!=NULL)) pSetModDeg(NULL);
863    temp3 = temp1;
864    temp1 = temp2;
865    temp2 = temp3;
866  }
867  mr--;
868  if (weights!=NULL)
869  {
870    for(j=0;j<weights->length();j++)
871    {
872      if (rows <(*weights)[j]+1) rows=(-mr)+(*weights)[j]+1;
873    }
874  }
875  /*------ computation betti numbers --------------*/
876  rows -= mr;
877  result = new intvec(rows+1,cols,0);
878  if (weights!=NULL)
879  {
880    for(j=0;j<weights->length();j++)
881    {
882      IMATELEM((*result),(-mr)+(*weights)[j]+1,1) ++;
883      //Print("imat(%d,%d)++ -> %d\n",(-mr)+(*weights)[j]+1, 1, IMATELEM((*result),(-mr)+(*weights)[j]+1,1));
884    }
885  }
886  else
887  {
888    (*result)[(-mr)*cols] = /*idRankFreeModule(res[0])*/ rkl;
889    if ((!idIs0(res[0])) && ((*result)[(-mr)*cols]==0))
890      (*result)[(-mr)*cols] = 1;
891  }
892  tocancel = (int*)omAlloc0((rows+1)*sizeof(int));
893  memset(temp1,0,(l+1)*sizeof(int));
894  if (weights!=NULL)
895  {
896    memset(temp2,0,l*sizeof(int));
897    pSetModDeg(weights);
898  }
899  else
900    memset(temp2,0,l*sizeof(int));
901  int dummy = syDetect(res[0],0,TRUE,temp2,tocancel);
902  if (weights!=NULL) pSetModDeg(NULL);
903  if (tomin)
904  {
905    //(*result)[(-mr)*cols] -= dummy;
906    for(j=0;j<=rows+mr;j++)
907    {
908      //Print("tocancel[%d]=%d imat(%d,%d)=%d\n",j,tocancel[j],(-mr)+j+1,1,IMATELEM((*result),(-mr)+j+1,1));
909      IMATELEM((*result),(-mr)+j+1,1) -= tocancel[j];
910    }
911  }
912  for (i=0;i<cols-1;i++)
913  {
914    if ((i==0) && (weights!=NULL)) pSetModDeg(weights);
915    memset(temp2,0,l*sizeof(int));
916    for (j=0;j<IDELEMS(res[i]);j++)
917    {
918      if (res[i]->m[j]!=NULL)
919      {
920        temp2[j+1] = pFDeg(res[i]->m[j],currRing)+temp1[pGetComp(res[i]->m[j])];
921        //(*result)[i+1+(temp2[j+1]-i-1)*cols]++;
922        //if (temp2[j+1]>i) IMATELEM((*result),temp2[j+1]-i-mr,i+2)++;
923        IMATELEM((*result),temp2[j+1]-i-mr,i+2)++;
924      }
925      else if (i==0)
926      {
927        if (j<r0_len) IMATELEM((*result),-mr,2)++;
928      }
929    }
930  /*------ computation betti numbers, if res not minimal --------------*/
931    if (tomin)
932    {
933      for (j=mr;j<rows+mr;j++)
934      {
935        //(*result)[i+1+j*cols] -= tocancel[j+1];
936        IMATELEM((*result),j+1-mr,i+2) -= tocancel[j+1];
937      }
938      if ((i<length-1) && (res[i+1]!=NULL))
939      {
940        memset(tocancel,0,(rows+1)*sizeof(int));
941        dummy = syDetect(res[i+1],i+1,TRUE,temp2,tocancel);
942        for (j=0;j<rows;j++)
943        {
944          //(*result)[i+1+j*cols] -= tocancel[j];
945          IMATELEM((*result),j+1,i+2) -= tocancel[j];
946        }
947      }
948    }
949    temp3 = temp1;
950    temp1 = temp2;
951    temp2 = temp3;
952    for (j=0;j<=rows;j++)
953    {
954    //  if (((*result)[i+1+j*cols]!=0) && (j>*regularity)) *regularity = j;
955      if ((IMATELEM((*result),j+1,i+2)!=0) && (j>*regularity)) *regularity = j;
956    }
957    if ((i==0) && (weights!=NULL)) pSetModDeg(NULL);
958  }
959  // Print("nach minim:\n"); result->show(); PrintLn();
960  /*------ clean up --------------*/
961  omFreeSize((ADDRESS)tocancel,(rows+1)*sizeof(int));
962  omFreeSize((ADDRESS)temp1,(l+1)*sizeof(int));
963  omFreeSize((ADDRESS)temp2,(l+1)*sizeof(int));
964  if ((tomin) && (mr<0))  // deletes the first (zero) line
965  {
966    for (j=1;j<=rows+mr+1;j++)
967    {
968      for (k=1;k<=cols;k++)
969      {
970        IMATELEM((*result),j,k) = IMATELEM((*result),j-mr,k);
971      }
972    }
973    for (j=rows+mr+1;j<=rows+1;j++)
974    {
975      for (k=1;k<=cols;k++)
976      {
977        IMATELEM((*result),j,k) = 0;
978      }
979    }
980  }
981  j = 0;
982  k = 0;
983  for (i=1;i<=result->rows();i++)
984  {
985    for(l=1;l<=result->cols();l++)
986    if (IMATELEM((*result),i,l) != 0)
987    {
988      j = si_max(j, i-1);
989      k = si_max(k, l-1);
990    }
991  }
992  intvec * exactresult=new intvec(j+1,k+1,0);
993  for (i=0;i<exactresult->rows();i++)
994  {
995    for (j=0;j<exactresult->cols();j++)
996    {
997      IMATELEM(*exactresult,i+1,j+1) = IMATELEM(*result,i+1,j+1);
998    }
999  }
1000  if (row_shift!=NULL) *row_shift = mr;
1001  delete result;
1002  return exactresult;
1003}
1004
1005/*2
1006* minbare via syzygies
1007*/
1008ideal syMinBase(ideal arg)
1009{
1010  intvec ** weights=NULL;
1011  int leng;
1012  if (idIs0(arg)) return idInit(1,arg->rank);
1013  resolvente res=syResolvente(arg,1,&leng,&weights,TRUE);
1014  ideal result=res[0];
1015  omFreeSize((ADDRESS)res,leng*sizeof(ideal));
1016  if (weights!=NULL)
1017  {
1018    if (*weights!=NULL)
1019    {
1020      delete (*weights);
1021      *weights=NULL;
1022    }
1023    if ((leng>=1) && (*(weights+1)!=NULL))
1024    {
1025      delete *(weights+1);
1026      *(weights+1)=NULL;
1027    }
1028  }
1029  idSkipZeroes(result);
1030  return result;
1031}
1032
1033/*2
1034* computes Betti-numbers from a resolvente of
1035* (non-)homogeneous objects
1036* the numbers of entrees !=NULL in res and weights must be equal
1037* and < length
1038*/
1039intvec * syNewBetti(resolvente res, intvec ** weights, int length)
1040{
1041  intvec * result,*tocancel;
1042  int i,j,k,rsmin=0,rsmax=0,rs=0;
1043  BOOLEAN homog=TRUE;
1044
1045  if (weights!=NULL)           //---homogeneous Betti numbers
1046  {
1047/*--------------computes size of the field----------------------*/
1048    for (i=1;i<length;i++)
1049    {
1050      if (weights[i] != NULL)
1051      {
1052        for (j=1;j<(weights[i])->length();j++)
1053        {
1054          if ((*(weights[i]))[j]-i<rsmin) rsmin = (*(weights[i]))[j]-i;
1055          if ((*(weights[i]))[j]-i>rsmax) rsmax = (*(weights[i]))[j]-i;
1056        }
1057      }
1058    }
1059    i = 0;
1060    while (weights[i] != NULL) i++;
1061    i--;
1062    for (j=0;j<IDELEMS(res[i]);j++)
1063    {
1064      if (res[i]->m[j]!=NULL)
1065      {
1066        k = pFDeg(res[i]->m[j],currRing)+(*(weights[i]))[pGetComp(res[i]->m[j])]-i-1;
1067        if (k>rsmax) rsmax = k;
1068        if (k<rsmin) rsmin = k;
1069      }
1070    }
1071    for (j=1;j<(weights[0])->length();j++)
1072    {
1073      if ((*weights[0])[j]>rsmax) rsmax = (*weights[0])[j];
1074      if ((*weights[0])[j]<rsmin) rsmin = (*weights[0])[j];
1075    }
1076//Print("rsmax = %d\n",rsmax);
1077//Print("rsmin = %d\n",rsmin);
1078    rs = rsmax-rsmin+1;
1079    result = new intvec(rs,i+2,0);
1080    tocancel = new intvec(rs);
1081/*-----------enter the Betti numbers-------------------------------*/
1082    if (/*idRankFreeModule(res[0])*/ res[0]->rank==0)
1083    {
1084      IMATELEM(*result,1-rsmin,1)=1;
1085    }
1086    else
1087    {
1088      for (i=1;i<(weights[0])->length();i++)
1089        IMATELEM(*result,(*weights[0])[i]+1-rsmin,1)++;
1090    }
1091    i = 1;
1092    while (weights[i]!=NULL)
1093    {
1094      for (j=1;j<(weights[i])->length();j++)
1095      {
1096        IMATELEM(*result,(*(weights[i]))[j]-i+1-rsmin,i+1)++;
1097      }
1098      i++;
1099    }
1100    i--;
1101    for (j=0;j<IDELEMS(res[i]);j++)
1102    {
1103      if (res[i]->m[j]!=NULL)
1104      {
1105        k = pFDeg(res[i]->m[j],currRing)+(*(weights[i]))[pGetComp(res[i]->m[j])]-i;
1106        IMATELEM(*result,k-rsmin,i+2)++;
1107      }
1108    }
1109  }
1110  else                //-----the non-homgeneous case
1111  {
1112    homog = FALSE;
1113    tocancel = new intvec(1);
1114    k = length;
1115    while ((k>0) && (idIs0(res[k-1]))) k--;
1116    result = new intvec(1,k+1,0);
1117    (*result)[0] = res[0]->rank;
1118    for (i=0;i<length;i++)
1119    {
1120      if (res[i]!=NULL)
1121      {
1122        for (j=0;j<IDELEMS(res[i]);j++)
1123        {
1124          if (res[i]->m[j]!=NULL) (*result)[i+1]++;
1125        }
1126      }
1127    }
1128  }
1129/*--------computes the Betti numbers for the minimized reolvente----*/
1130
1131  i = 1;
1132  while ((res[i]!=NULL) && (weights[i]!=NULL))
1133  {
1134    syDetect(res[i],i,rsmin,homog,weights[i],tocancel);
1135    if (homog)
1136    {
1137      for (j=0;j<rs-1;j++)
1138      {
1139        IMATELEM((*result),j+1,i+1) -= (*tocancel)[j];
1140        IMATELEM((*result),j+1,i+2) -= (*tocancel)[j+1];
1141      }
1142      IMATELEM((*result),rs,i+1) -= (*tocancel)[rs-1];
1143    }
1144    else
1145    {
1146      (*result)[i+1] -= (*tocancel)[0];
1147      (*result)[i+2] -= (*tocancel)[0];
1148    }
1149    i++;
1150  }
1151
1152/*--------print Betti numbers for control---------------------------*/
1153  for(i=rsmin;i<=rsmax;i++)
1154  {
1155    Print("%2d:",i);
1156    for(j=1;j<=result->cols();j++)
1157    {
1158      Print(" %5d",IMATELEM(*result,i-rsmin+1,j));
1159    }
1160    PrintLn();
1161  }
1162  return result;
1163}
1164
1165/*2
1166* is looking for the minimal minimized module of a resolvente
1167* i.e. returns 0 if res comes from a mres-command and 1 in the
1168* case of res-commands
1169*/
1170int syIsMinimizedFrom(resolvente res,int length)
1171{
1172  poly p;
1173  int i,j=length;
1174
1175  while ((j>0) && (res[j-1]==NULL)) j--;
1176  while (j>0)
1177  {
1178    for (i=0;i<IDELEMS(res[j-1]);i++)
1179    {
1180      p = res[j-1]->m[i];
1181      while (p!=NULL)
1182      {
1183        if (pLmIsConstantComp(p)) return j;
1184        p = pNext(p);
1185      }
1186    }
1187    j--;
1188  }
1189  return j;
1190}
Note: See TracBrowser for help on using the repository browser.