source: git/Singular/syz.cc @ c4bbf1f

spielwiese
Last change on this file since c4bbf1f was ca7a56, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes: * changes for TEST_MAC_ORDER, part 3 (binom.cc, binom.h, spSpolyLoop.cc, algmap.cc polys-impl.cc polys-impl.h polys.h syz.cc syz0.cc syz1.cc) git-svn-id: file:///usr/local/Singular/svn/trunk@1038 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 24.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: syz.cc,v 1.8 1998-01-17 18:08:00 Singular Exp $ */
5
6/*
7* ABSTRACT: resolutions
8*/
9
10
11#include "mod2.h"
12#include "tok.h"
13#include "mmemory.h"
14#include "polys.h"
15#include "febase.h"
16#include "kstd1.h"
17#include "kutil.h"
18#include "spolys.h"
19#include "stairc.h"
20#include "ipid.h"
21#include "cntrlc.h"
22#include "ipid.h"
23#include "intvec.h"
24#include "ipshell.h"
25#include "numbers.h"
26#include "ideals.h"
27#include "intvec.h"
28#include "ring.h"
29#include "syz.h"
30
31static intvec * syPrepareModComp(ideal arg,intvec ** w)
32{
33  intvec *w1 = NULL;
34  int i;
35  BOOLEAN isIdeal=FALSE;
36
37  if ((w==NULL) || (*w==NULL)) return w1;
38  int maxxx = (*w)->length();
39  if (maxxx==1)
40  {
41    maxxx = 2;
42    isIdeal = TRUE;
43  }
44  w1 = new intvec(maxxx+IDELEMS(arg));
45  if (!isIdeal)
46  {
47    for (i=0;i<maxxx;i++)
48    {
49      (*w1)[i] = (**w)[i];
50    }
51  }
52  for (i=maxxx;i<maxxx+IDELEMS(arg);i++)
53  {
54    if (arg->m[i-maxxx]!=NULL)
55    {
56      (*w1)[i] = pFDeg(arg->m[i-maxxx]);
57      if (pGetComp(arg->m[i-maxxx])!=0)
58      {
59        (*w1)[i]+=(**w)[pGetComp(arg->m[i-maxxx])-1];
60      }
61    }
62  }
63  delete (*w);
64  *w = new intvec(IDELEMS(arg)+1);
65  for (i=0;i<IDELEMS(arg);i++)
66  {
67     (**w)[i+1] = (*w1)[i+maxxx];
68  }
69  return w1;
70}
71
72static void syDeleteAbove(ideal up, int k)
73{
74  if (up!=NULL)
75  {
76    for (int i=0;i<IDELEMS(up);i++)
77    {
78      if (up->m[i]!=NULL)
79        pDeleteComp(&(up->m[i]),k+1);
80    }
81  }
82}
83
84/*2
85*minimizes the module mod and cancel superfluous syzygies
86*from syz
87*/
88static void syMinStep(ideal mod,ideal syz,BOOLEAN final=FALSE,ideal up=NULL,
89                      tHomog h=isNotHomog)
90{
91  ideal deg0=NULL;
92  poly Unit1,Unit2,actWith;
93  int len,i,j,ModComp,m,k,l;
94  BOOLEAN searchUnit,existsUnit;
95
96  if (TEST_OPT_PROT) Print("m");
97  if ((final) && (h==isHomog))
98  /*minim is TRUE, we are in the module: maxlength, maxlength <>0*/
99  {
100    deg0=idJet(syz,0);
101    idSkipZeroes(deg0);
102    syz=deg0;
103  }
104/*--cancels empty entees and their related components above--*/
105  j = IDELEMS(syz);
106  while ((j>0) && (!syz->m[j-1])) j--;
107  k = 0;
108  while (k<j)
109  {
110    if (syz->m[k]!=NULL)
111      k++;
112    else
113    {
114      if (TEST_OPT_PROT) Print(".");
115      for (l=k;l<j-1;l++) syz->m[l] = syz->m[l+1];
116      syz->m[j-1] = NULL;
117      syDeleteAbove(up,k);
118      j--;
119    }
120  }
121/*--searches for syzygies coming from superfluous elements
122* in the module below--*/
123  searchUnit = TRUE;
124  while (searchUnit)
125  {
126    i=0;
127    j=IDELEMS(syz);
128    while ((j>0) && (syz->m[j-1]==NULL)) j--;
129    existsUnit = FALSE;
130    if (currRing->OrdSgn == 1)
131    {
132      while ((i<j) && (!existsUnit))
133      {
134        existsUnit = pVectorHasUnitB(syz->m[i],&ModComp);
135        i++;
136      }
137    }
138    else
139    {
140      int I=0;
141      l = 0;
142      len=0;
143      for (i=0;i<IDELEMS(syz);i++)
144      {
145        if (syz->m[i]!=NULL)
146        {
147          pVectorHasUnit(syz->m[i],&m, &l);
148          if ((len==0) ||((l>0) && (l<len)))
149          {
150            len = l;
151            ModComp = m;
152            I = i;
153          }
154        }
155      }
156//Print("Laenge ist: %d\n",len);
157      if (len>0) existsUnit = TRUE;
158      i = I+1;
159    }
160    if (existsUnit)
161    {
162      i--;
163//--takes out the founded syzygy--
164      if (TEST_OPT_PROT) Print("f");
165      actWith = syz->m[i];
166      if (currRing->ch<2) pCleardenom(actWith);
167//Print("actWith: ");pWrite(actWith);
168      syz->m[i] = NULL;
169      for (k=i;k<j-1;k++)  syz->m[k] = syz->m[k+1];
170      syz->m[j-1] = NULL;
171      syDeleteAbove(up,i);
172      j--;
173//--makes Gauss alg. for the column ModComp--
174      Unit1 = pTakeOutComp(&(actWith), ModComp);
175//Print("actWith now: ");pWrite(actWith);
176//Print("Unit1: ");pWrite(Unit1);
177      k=0;
178//Print("j= %d",j);
179      while (k<j)
180      {
181        if (syz->m[k]!=NULL)
182        {
183          Unit2 = pTakeOutComp(&(syz->m[k]), ModComp);
184//Print("element %d: ",k);pWrite(syz->m[k]);
185//Print("Unit2: ");pWrite(Unit2);
186          syz->m[k] = pMult(syz->m[k],pCopy(Unit1));
187          syz->m[k] = pSub(syz->m[k],
188            pMult(pCopy(actWith),Unit2));
189          if (syz->m[k]==NULL)
190          {
191            for (l=k;l<j-1;l++)
192              syz->m[l] = syz->m[l+1];
193            syz->m[j-1] = NULL;
194            j--;
195            syDeleteAbove(up,k);
196            k--;
197          }
198        }
199        k++;
200      }
201      pDelete(&actWith);
202      pDelete(&Unit1);
203//--deletes superfluous elements from the module below---
204      pDelete(&(mod->m[ModComp-1]));
205      for (k=ModComp-1;k<IDELEMS(mod)-1;k++)
206        mod->m[k] = mod->m[k+1];
207      mod->m[IDELEMS(mod)-1] = NULL;
208    }
209    else
210      searchUnit = FALSE;
211  }
212  if (TEST_OPT_PROT) Print("\n");
213  idSkipZeroes(mod);
214  idSkipZeroes(syz);
215  if (deg0!=NULL)
216    idDelete(&deg0);
217}
218
219void syMinimizeResolvente(resolvente res, int length, int first)
220{
221  int syzIndex=first;
222
223  if (syzIndex<1) syzIndex=1;
224  while ((syzIndex<length-1) && (res[syzIndex]!=NULL) && (res[syzIndex+1]!=NULL))
225  {
226    syMinStep(res[syzIndex-1],res[syzIndex],FALSE,res[syzIndex+1]);
227    syzIndex++;
228  }
229  if (res[syzIndex]!=NULL)
230    syMinStep(res[syzIndex-1],res[syzIndex]);
231}
232
233/*2
234* resolution of ideal/module arg, <=maxlength steps, (r[0..maxlength])
235*   no limitation in length if maxlength==0
236* input:arg
237*       minim: TRUE means mres cmd, FALSE res cmd.
238*       if *len!=0: modul weights: weights[0]
239*          (and weights is defined:weights[0..len-1]
240*
241* output:resolvente r[0..length-1],
242*        modul weights: weights[0..length-1]
243*/
244resolvente syResolvente(ideal arg, int maxlength, int * length,
245                        intvec *** weights, BOOLEAN minim)
246{
247  resolvente res;
248  resolvente newres;
249  tHomog hom=isNotHomog;
250  ideal temp=NULL;
251  intvec *w,*w1=NULL,**tempW;
252  int i,k,syzIndex = 0,j;
253  int Kstd1_OldDeg=Kstd1_deg;
254  BOOLEAN completeMinim;
255  BOOLEAN oldDegBound=TEST_OPT_DEGBOUND;
256  int wlength=*length;
257
258  if (maxlength!=-1) *length = maxlength+1;
259  else              *length = 5;
260  if ((wlength!=0) && (*length!=wlength))
261  {
262    intvec **wtmp = (intvec**)Alloc0((*length)*sizeof(intvec*));
263    wtmp[0]=(*weights)[0];
264    Free((ADDRESS)*weights,wlength*sizeof(intvec*));
265    *weights=wtmp;
266  }
267  res = (resolvente)Alloc0((*length)*sizeof(ideal));
268  res[0] = idCopy(arg);
269  if ((weights==NULL) || (*weights==NULL) || ((*weights)[0]==NULL))
270  {
271    hom=(tHomog)idHomModule(res[0],currQuotient,&w);
272    if (hom==isHomog)
273    {
274      *weights = (intvec**)Alloc0((*length)*sizeof(intvec*));
275      if (w!=NULL) (*weights)[0] = ivCopy(w);
276//Print("die %d Modulegewichte sind:\n",w->length());
277//w->show();
278//Print("\n");
279    }
280  }
281  else
282  {
283    if ((weights!=NULL) && (*weights!=NULL)&& ((*weights)[0]!=NULL))
284    {
285      w = ivCopy((*weights)[0]);
286      hom = isHomog;
287    }
288  }
289  if (hom==isHomog)
290  {
291    w1 = syPrepareModComp(res[0],&w);
292    if (w!=NULL) { delete w;w=NULL; }
293  }
294  while ((!idIs0(res[syzIndex])) &&
295         ((maxlength==-1) || (syzIndex<=maxlength)))
296   // (syzIndex<maxlength+(int)minim)))
297  /*compute one step more for minimizing*/
298  {
299    if (Kstd1_deg!=0) Kstd1_deg++;
300    if (syzIndex+1==*length)
301    {
302      newres = (resolvente)Alloc0((*length+5)*sizeof(ideal));
303      tempW = (intvec**)Alloc0((*length+5)*sizeof(intvec*));
304      for (j=0;j<*length;j++)
305      {
306        newres[j] = res[j];
307        if (*weights!=NULL) tempW[j] = (*weights)[j];
308        /*else              tempW[j] = NULL;*/
309      }
310      Free((ADDRESS)res,*length*sizeof(ideal));
311      Free((ADDRESS)*weights,*length*sizeof(intvec*));
312      *length += 5;
313      res=newres;
314      *weights = tempW;
315    }
316    if (minim || (syzIndex!=0))
317    {
318      temp = kInterRed(res[syzIndex],currQuotient);
319      idDelete(&res[syzIndex]);
320      idSkipZeroes(temp);
321      res[syzIndex] = temp;
322    }
323    temp = NULL;
324    if ((currQuotient==NULL)&&(syzIndex==0)&& (!TEST_OPT_DEGBOUND))
325    {
326      res[/*syzIndex+*/1] = idSyzygies(res[0/*syzIndex*/],
327        NULL/*currQuotient*/,hom,&w1,TRUE,Kstd1_deg);
328      if ((!TEST_OPT_NOTREGULARITY) && (Kstd1_deg>0)) test |= Sy_bit(OPT_DEGBOUND);
329    }
330    else
331      res[syzIndex+1] = idSyzygies(res[syzIndex],currQuotient,hom,&w1);
332    completeMinim=(syzIndex!=maxlength) || (maxlength ==-1) || (hom!=isHomog);
333    syzIndex++;
334    if (TEST_OPT_PROT) Print("[%d]\n",syzIndex);
335    if ((minim)||(syzIndex>1))
336      syMinStep(res[syzIndex-1],res[syzIndex],!completeMinim,NULL,hom);
337    if (!completeMinim)
338    /*minim is TRUE, we are in the module: maxlength, maxlength <>0*/
339    {
340      idDelete(&res[syzIndex]);
341    }
342    if ((hom == isHomog) && (!idIs0(res[syzIndex])))
343    {
344//Print("die %d Modulegewichte sind:\n",w1->length());
345//w1->show();
346//Print("\n");
347      k = idRankFreeModule(res[syzIndex]);
348      w = new intvec(k+IDELEMS(res[syzIndex]));
349      (*weights)[syzIndex] = new intvec(k);
350      for (i=0;i<k;i++)
351      {
352        if (res[syzIndex-1]->m[i]!=NULL) // hs
353        {
354          (*w)[i] = pFDeg(res[syzIndex-1]->m[i]);
355          if (pGetComp(res[syzIndex-1]->m[i])>0)
356            (*w)[i] += (*w1)[pGetComp(res[syzIndex-1]->m[i])-1];
357          (*((*weights)[syzIndex]))[i] = (*w)[i];
358        }
359      }
360      for (i=k;i<k+IDELEMS(res[syzIndex]);i++)
361      {
362        if (res[syzIndex]->m[i-k]!=NULL)
363          (*w)[i] = pFDeg(res[syzIndex]->m[i-k])
364                    +(*w)[pGetComp(res[syzIndex]->m[i-k])-1];
365      }
366      delete w1;
367      w1 = w;
368      w = NULL;
369    }
370  }
371  if ((syzIndex!=0) && (res[syzIndex]!=NULL) && (idIs0(res[syzIndex])))
372    idDelete(&res[syzIndex]);
373  if (w1!=NULL) delete w1;
374  //if ((maxlength!=-1) && (!minim))
375  //{
376  //  while (syzIndex<=maxlength)
377  //  {
378  //    res[syzIndex]=idInit(1,1);
379  //    syzIndex++;
380  //    if (syzIndex<=maxlength)
381  //    {
382  //      res[syzIndex]=idFreeModule(1);
383  //      syzIndex++;
384  //    }
385  //  }
386  //}
387  Kstd1_deg=Kstd1_OldDeg;
388  if (!oldDegBound)
389    test &= ~Sy_bit(OPT_DEGBOUND);
390  return res;
391}
392
393resolvente syMinRes(ideal arg, int maxlength, int * length, BOOLEAN minim)
394{
395  resolvente res;
396  resolvente newres;
397  tHomog hom;
398  ideal temp=NULL;
399  intvec *w,*w1=NULL;
400  int i,k,syzIndex = 0,j;
401  int Kstd1_OldDeg=Kstd1_deg;
402  BOOLEAN isIdeal=FALSE,oldDegBound=TEST_OPT_DEGBOUND;
403
404  if (maxlength!=-1) *length = maxlength+1;
405  else              *length = 4;
406  res = (resolvente)Alloc0((*length)*sizeof(ideal));
407  res[0] = idCopy(arg);
408  hom=(tHomog)idHomModule(res[0],currQuotient,&w);
409//Print("die %d Modulegewichte sind:\n",w->length());
410//w->show();
411//PrintLn();
412  if (hom==isHomog)
413  {
414    //if (w!=NULL)
415    //{
416    //  w->show();
417    //  PrintLn();
418    //  if (res[0]!=NULL) jjPRINT_MA0(idModule2Matrix(idCopy(res[0])),NULL);
419    //}
420    int maxxx = 0;
421    if (w!=NULL) maxxx = w->length();
422    if (maxxx==1)
423    {
424      maxxx = 2;
425      isIdeal = TRUE;
426    }
427    w1 = new intvec(maxxx+IDELEMS(arg));
428    if (!isIdeal)
429    {
430      for (i=0;i<maxxx;i++)
431      {
432        (*w1)[i] = (*w)[i];
433      }
434    }
435    for (i=maxxx;i<maxxx+IDELEMS(arg);i++)
436    {
437      if (res[0]->m[i-maxxx]!=0)
438        (*w1)[i] = pFDeg(res[0]->m[i-maxxx])+(*w)[pGetComp(res[0]->m[i-maxxx])];
439    }
440    if (w!=NULL) {delete w;w=NULL;}
441  }
442  while ((!idIs0(res[syzIndex]))
443  && ((maxlength==-1)
444    || (syzIndex<maxlength)))
445   // (syzIndex<maxlength+(int)minim)))
446  /*compute one step more for minimizing*/
447  {
448    if (Kstd1_deg!=0) Kstd1_deg++;
449    if (syzIndex+1==*length)
450    {
451      newres = (resolvente)Alloc0((*length+4)*sizeof(ideal));
452      for (j=0;j<*length;j++) newres[j] = res[j];
453      Free((ADDRESS)res,*length*sizeof(ideal));
454      *length += 4;
455      res=newres;
456    }
457    if (minim || (syzIndex!=0))
458    {
459      temp = kInterRed(res[syzIndex],currQuotient);
460      idDelete(&res[syzIndex]);
461      res[syzIndex] = temp;
462    }
463    temp = NULL;
464    if ((hom==isHomog) && (currRing->OrdSgn==1))
465    {
466//PrintS("Input: ");
467//for (i=0;i<IDELEMS(res[syzIndex]);i++)
468//{
469//Print("Element %d: ",i);pWrite(res[syzIndex]->m[i]);
470//}
471      if ((currQuotient==NULL)&&(syzIndex==0)&& (!TEST_OPT_DEGBOUND))
472      {
473        res[/*syzIndex+*/1] = idSyzMin(res[0/*syzIndex*/],
474          NULL/*currQuotient*/,hom,&w1,TRUE,Kstd1_deg);
475        if ((!TEST_OPT_NOTREGULARITY) && (Kstd1_deg>0)) test |= Sy_bit(OPT_DEGBOUND);
476//Print("Regularity is %d \n",Kstd1_deg);
477      }
478      else
479        res[syzIndex+1] = idSyzMin(res[syzIndex],
480          currQuotient,hom,&w1,FALSE,Kstd1_deg);
481//PrintS("Input: ");
482//for (i=0;i<IDELEMS(res[syzIndex+1]);i++)
483//{
484//Print("Element %d: ",i);pWrite(res[syzIndex+1]->m[i]);
485//}
486    }
487    else
488      res[syzIndex+1] = idSyzygies(res[syzIndex],currQuotient,hom,&w1);
489//Print("im %d-ten Syzygienmodul: \n",syzIndex+1);
490//for (i=0;i<IDELEMS(res[syzIndex+1]);i++)
491//{
492//Print("El %d: ",i);pWrite(res[syzIndex+1]->m[i]);
493//}
494    syzIndex++;
495    if ((maxlength!=-1) && (syzIndex==maxlength)) idDelete(&res[syzIndex]);
496    if (TEST_OPT_PROT) Print("[%d]\n",syzIndex);
497    if  ((hom==isHomog) && (res[syzIndex]!=NULL))
498    {
499//Print("die %d Modulegewichte sind:\n",w1->length());
500//w1->show();
501//PrintLn();
502      k = idRankFreeModule(res[syzIndex]);
503      w = new intvec(k+IDELEMS(res[syzIndex])+1);
504      for (i=1;i<=k;i++)
505      {
506        (*w)[i] = pFDeg(res[syzIndex-1]->m[i-1])
507                  +(*w1)[pGetComp(res[syzIndex-1]->m[i-1])];
508      }
509      for (i=k+1;i<k+1+IDELEMS(res[syzIndex]);i++)
510      {
511        if (res[syzIndex]->m[i-k-1]!=NULL)
512          (*w)[i] = pFDeg(res[syzIndex]->m[i-k-1])
513                    +(*w)[pGetComp(res[syzIndex]->m[i-k-1])];
514      }
515      delete w1;
516      w1 = w;
517      w = NULL;
518    }
519  }
520  if (w1!=NULL) delete w1;
521  if (maxlength!=-1)
522  {
523    if ((syzIndex<maxlength-1) && (res[syzIndex]!=NULL)) syzIndex++;
524    while (syzIndex<maxlength)
525    {
526      res[syzIndex]=idInit(1,1);
527      syzIndex++;
528    }
529  }
530  Kstd1_deg=Kstd1_OldDeg;
531  if (!oldDegBound)
532    test &= ~Sy_bit(OPT_DEGBOUND);
533  return res;
534}
535
536void syDetect(ideal id,int index,BOOLEAN homog,int * degrees,int * tocancel)
537{
538  int i, j, k, ModComp;
539  poly p, q, qq, Unit1, Unit2;
540  ideal temp;
541
542  if (idIs0(id)) return;
543  temp = idInit(IDELEMS(id),id->rank);
544  for (i=0;i<IDELEMS(id);i++)
545  {
546    p = id->m[i];
547    while (p!=NULL)
548    {
549      if (pIsConstantComp(p))
550      {
551        if (temp->m[i]==NULL)
552        {
553          temp->m[i] = pHead(p);
554          q = temp->m[i];
555        }
556        else
557        {
558          pNext(q) = pHead(p);
559          pIter(q);
560        }
561      }
562      pIter(p);
563    }
564  }
565  i = IDELEMS(id);
566  while ((i>0) && (temp->m[i-1]==NULL)) i--;
567  if (i==0)
568  {
569    idDelete(&temp);
570    return;
571  }
572  j = 0;
573  while ((j<i) && (temp->m[j]==NULL)) j++;
574  if (j<i)
575  {
576    p = temp->m[j];
577    temp->m[j] = NULL;
578  }
579  else
580  {
581    p = NULL;
582  }
583  while (p!=NULL)
584  {
585    if (homog)
586    {
587      k = pFDeg(id->m[j])+degrees[pGetComp(id->m[j])];
588      if (k>=index) tocancel[k-index]++;
589    }
590    else
591    {
592      tocancel[0]--;
593    }
594    ModComp = pGetComp(p);
595//Print("Element %d: ",j);pWrite(p);
596    Unit1 = pTakeOutComp1(&p,ModComp);
597    pSetComp(Unit1,0);
598    for (k=j+1;k<i;k++)
599    {
600      if (temp->m[k]!=NULL)
601      {
602//Print("erst %d: ",k);pWrite(temp->m[k]);
603        Unit2 = pTakeOutComp1(&(temp->m[k]),ModComp);
604        if (Unit2!=NULL)
605        {
606          number tmp=nCopy(pGetCoeff(Unit2));
607          tmp=nNeg(tmp);
608          qq = pCopy(p);
609          pMultN(qq,tmp);
610          nDelete(&tmp);
611          pMultN(temp->m[k],pGetCoeff(Unit1));
612          temp->m[k] = pAdd(temp->m[k],qq);
613//Print("dann %d: ",k);pWrite(temp->m[k]);
614          pDelete(&Unit2);
615        }
616      }
617    }
618//PrintLn();
619    pDelete(&Unit1);
620    pDelete(&p);
621    j++;
622    while ((j<i) && (temp->m[j]==NULL)) j++;
623    if (j>=i)
624    {
625      idDelete(&temp);
626      return;
627    }
628    p = temp->m[j];
629    temp->m[j] = NULL;
630  }
631  idDelete(&temp);
632}
633
634void syDetect(ideal id,int index,int rsmin, BOOLEAN homog,
635              intvec * degrees,intvec * tocancel)
636{
637  int * deg=NULL;
638  int * tocan=(int*) Alloc0(tocancel->length()*sizeof(int));
639  int i;
640
641  if (homog)
642  {
643    deg = (int*) Alloc0(degrees->length()*sizeof(int));
644    for (i=degrees->length();i>0;i--)
645      deg[i-1] = (*degrees)[i-1]-rsmin;
646  }
647  syDetect(id,index,homog,deg,tocan);
648  for (i=tocancel->length();i>0;i--)
649    (*tocancel)[i-1] = tocan[i-1];
650  if (homog)
651    Free((ADDRESS)deg,degrees->length()*sizeof(int));
652  Free((ADDRESS)tocan,tocancel->length()*sizeof(int));
653}
654
655/*2
656* computes the betti numbers from a given resolution
657* of length 'length' (0..length-1), not necessairily minimal,
658* (if weights are given, they are used)
659* returns the int matrix of betti numbers
660* and the regularity
661*/
662intvec * syBetti(resolvente res,int length, int * regularity,
663                 intvec* weights)
664{
665  int i,j=0,k=0,l,rows,cols;
666  int *temp1,*temp2,*temp3;/*used to compute degrees*/
667  int *tocancel; /*(BOOLEAN)tocancel[i]=element is superfluous*/
668
669  /*------ compute size --------------*/
670  *regularity = -1;
671  cols = length;
672  while ((cols>0)
673  && ((res[cols-1]==NULL)
674    || (idIs0(res[cols-1]))))
675  {
676    cols--;
677  }
678  l = max(idRankFreeModule(res[0]),res[0]->rank);
679  i = 0;
680  while ((i<length) && (res[i]!=NULL))
681  {
682    if (IDELEMS(res[i])>l) l = IDELEMS(res[i]);
683    i++;
684  }
685  temp1 = (int*)Alloc0((l+1)*sizeof(int));
686  temp2 = (int*)Alloc((l+1)*sizeof(int));
687  rows = 1;
688  cols++;
689  for (i=0;i<cols-1;i++)
690  {
691    if ((i==0) && (weights!=NULL)) pSetModDeg(weights);
692    memset(temp2,0,(l+1)*sizeof(int));
693    for (j=0;j<IDELEMS(res[i]);j++)
694    {
695      if (res[i]->m[j]!=NULL)
696     {
697        if ((pGetComp(res[i]->m[j])>l)
698        || ((i>1) && (res[i-1]->m[pGetComp(res[i]->m[j])-1]==NULL)))
699        {
700          WerrorS("input not a resolvent");
701          Free((ADDRESS)temp1,(l+1)*sizeof(int));
702          Free((ADDRESS)temp2,(l+1)*sizeof(int));
703          return NULL;
704        }
705        temp2[j+1] = pFDeg(res[i]->m[j])+temp1[pGetComp(res[i]->m[j])];
706        if (temp2[j+1]-i>rows) rows = temp2[j+1]-i;
707      }
708    }
709    if ((i==0) && (weights!=NULL)) pSetModDeg(NULL);
710    temp3 = temp1;
711    temp1 = temp2;
712    temp2 = temp3;
713  }
714  /*------ computation betti numbers --------------*/
715  intvec * result = new intvec(rows,cols,0);
716  (*result)[0] = /*idRankFreeModule(res[0])*/ res[0]->rank;
717  if ((!idIs0(res[0])) && ((*result)[0]==0)) (*result)[0] = 1;
718  tocancel = (int*)Alloc0((rows+1)*sizeof(int));
719  memset(temp2,0,l*sizeof(int));
720  memset(temp1,0,(l+1)*sizeof(int));
721  syDetect(res[0],0,TRUE,temp2,tocancel);
722  (*result)[0] -= tocancel[0];
723  for (i=0;i<cols-1;i++)
724  {
725    if ((i==0) && (weights!=NULL)) pSetModDeg(weights);
726    memset(temp2,0,l*sizeof(int));
727    for (j=0;j<IDELEMS(res[i]);j++)
728    {
729      if (res[i]->m[j]!=NULL)
730      {
731        temp2[j+1] = pFDeg(res[i]->m[j])+temp1[pGetComp(res[i]->m[j])];
732        //(*result)[i+1+(temp2[j+1]-i-1)*cols]++;
733        if (temp2[j+1]>i) IMATELEM((*result),temp2[j+1]-i,i+2)++;
734      }
735    }
736  /*------ computation betti numbers, if res not minimal --------------*/
737    for (j=0;j<rows-1;j++)
738    {
739      //(*result)[i+1+j*cols] -= tocancel[j+1];
740      IMATELEM((*result),j+1,i+2) -= tocancel[j+1];
741    }
742    if ((i<length-1) && (res[i+1]!=NULL))
743    {
744      memset(tocancel,0,(rows+1)*sizeof(int));
745      syDetect(res[i+1],i+1,TRUE,temp2,tocancel);
746      for (j=0;j<rows;j++)
747      {
748        //(*result)[i+1+j*cols] -= tocancel[j];
749        IMATELEM((*result),j+1,i+2) -= tocancel[j];
750      }
751    }
752    temp3 = temp1;
753    temp1 = temp2;
754    temp2 = temp3;
755    for (j=0;j<rows;j++)
756    {
757    //  if (((*result)[i+1+j*cols]!=0) && (j>*regularity)) *regularity = j;
758      if ((IMATELEM((*result),j+1,i+2)!=0) && (j>*regularity)) *regularity = j;
759    }
760    if ((i==0) && (weights!=NULL)) pSetModDeg(NULL);
761  }
762  /*------ clean up --------------*/
763  Free((ADDRESS)tocancel,(rows+1)*sizeof(int));
764  Free((ADDRESS)temp1,(l+1)*sizeof(int));
765  Free((ADDRESS)temp2,(l+1)*sizeof(int));
766  j = 0;
767  k = 0;
768  for (i=0;i<rows*cols;i++)
769  {
770    if ((*result)[i] != 0)
771    {
772      j = i/cols;
773      if (i>k+j*cols) k = i-j*cols;
774    }
775  }
776  intvec * exactresult=new intvec(j+1,k+1,0);
777  for (i=0;i<exactresult->rows();i++)
778  {
779    for (j=0;j<exactresult->cols();j++)
780    {
781      IMATELEM(*exactresult,i+1,j+1) = IMATELEM(*result,i+1,j+1);
782    }
783  }
784  delete result;
785  return exactresult;
786}
787
788/*2
789* minbare via syzygies
790*/
791ideal syMinBase(ideal arg)
792{
793  intvec ** weights=NULL;
794  int leng;
795  if (idIs0(arg)) return idInit(1,arg->rank);
796  resolvente res=syResolvente(arg,1,&leng,&weights,TRUE);
797  ideal result=res[0];
798  Free((ADDRESS)res,leng*sizeof(ideal));
799  if (weights!=NULL)
800  {
801    if (*weights!=NULL)
802    {
803      delete (*weights);
804      *weights=NULL;
805    }
806    if ((leng>=1) && (*(weights+1)!=NULL))
807    {
808      delete *(weights+1);
809      *(weights+1)=NULL;
810    }
811  }
812  idSkipZeroes(result);
813  return result;
814}
815
816/*2
817* computes Betti-numbers from a resolvente of
818* (non-)homogeneous objects
819* the numbers of entrees !=NULL in res and weights must be equal
820* and < length
821*/
822intvec * syNewBetti(resolvente res, intvec ** weights, int length)
823{
824  intvec * result,*tocancel;
825  int i,j,k,rsmin=0,rsmax=0,rs=0;
826  BOOLEAN homog=TRUE;
827
828  if (weights!=NULL)           //---homogeneous Betti numbers
829  {
830/*--------------computes size of the field----------------------*/
831    for (i=1;i<length;i++)
832    {
833      if (weights[i] != NULL)
834      {
835        for (j=1;j<(weights[i])->length();j++)
836        {
837          if ((*(weights[i]))[j]-i<rsmin) rsmin = (*(weights[i]))[j]-i;
838          if ((*(weights[i]))[j]-i>rsmax) rsmax = (*(weights[i]))[j]-i;
839        }
840      }
841    }
842    i = 0;
843    while (weights[i] != NULL) i++;
844    i--;
845    for (j=0;j<IDELEMS(res[i]);j++)
846    {
847      if (res[i]->m[j]!=NULL)
848      {
849        k = pFDeg(res[i]->m[j])+(*(weights[i]))[pGetComp(res[i]->m[j])]-i-1;
850        if (k>rsmax) rsmax = k;
851        if (k<rsmin) rsmin = k;
852      }
853    }
854    for (j=1;j<(weights[0])->length();j++)
855    {
856      if ((*weights[0])[j]>rsmax) rsmax = (*weights[0])[j];
857      if ((*weights[0])[j]<rsmin) rsmin = (*weights[0])[j];
858    }
859//Print("rsmax = %d\n",rsmax);
860//Print("rsmin = %d\n",rsmin);
861    rs = rsmax-rsmin+1;
862    result = new intvec(rs,i+2,0);
863    tocancel = new intvec(rs);
864/*-----------enter the Betti numbers-------------------------------*/
865    if (/*idRankFreeModule(res[0])*/ res[0]->rank==0)
866    {
867      IMATELEM(*result,1-rsmin,1)=1;
868    }
869    else
870    {
871      for (i=1;i<(weights[0])->length();i++)
872        IMATELEM(*result,(*weights[0])[i]+1-rsmin,1)++;
873    }
874    i = 1;
875    while (weights[i]!=NULL)
876    {
877      for (j=1;j<(weights[i])->length();j++)
878      {
879        IMATELEM(*result,(*(weights[i]))[j]-i+1-rsmin,i+1)++;
880      }
881      i++;
882    }
883    i--;
884    for (j=0;j<IDELEMS(res[i]);j++)
885    {
886      if (res[i]->m[j]!=NULL)
887      {
888        k = pFDeg(res[i]->m[j])+(*(weights[i]))[pGetComp(res[i]->m[j])]-i;
889        IMATELEM(*result,k-rsmin,i+2)++;
890      }
891    }
892  }
893  else                //-----the non-homgeneous case
894  {
895    homog = FALSE;
896    tocancel = new intvec(1);
897    k = length;
898    while ((k>0) && (idIs0(res[k-1]))) k--;
899    result = new intvec (1,k+1,0);
900    (*result)[0] = res[0]->rank;
901    for (i=0;i<length;i++)
902    {
903      if (res[i]!=NULL)
904      {
905        for (j=0;j<IDELEMS(res[i]);j++)
906        {
907          if (res[i]->m[j]!=NULL) (*result)[i+1]++;
908        }
909      }
910    }
911  }
912/*--------computes the Betti numbers for the minimized reolvente----*/
913
914  i = 1;
915  while ((res[i]!=NULL) && (weights[i]!=NULL))
916  {
917    syDetect(res[i],i,rsmin,homog,weights[i],tocancel);
918    if (homog)
919    {
920      for (j=0;j<rs-1;j++)
921      {
922        IMATELEM((*result),j+1,i+1) -= (*tocancel)[j];
923        IMATELEM((*result),j+1,i+2) -= (*tocancel)[j+1];
924      }
925      IMATELEM((*result),rs,i+1) -= (*tocancel)[rs-1];
926    }
927    else
928    {
929      (*result)[i+1] -= (*tocancel)[0];
930      (*result)[i+2] -= (*tocancel)[0];
931    }
932    i++;
933  }
934
935/*--------print Betti numbers for control---------------------------*/
936  for(i=rsmin;i<=rsmax;i++)
937  {
938    Print("%2d:",i);
939    for(j=1;j<=result->cols();j++)
940    {
941      Print(" %5d",IMATELEM(*result,i-rsmin+1,j));
942    }
943    PrintLn();
944  }
945  return result;
946}
947
948/*2
949* is looking for the minimal minimized module of a resolvente
950* i.e. returns 0 if res comes from a mres-command and 1 in the
951* case of res-commands
952*/
953int syIsMinimizedFrom(resolvente res,int length)
954{
955  poly p;
956  int i,j=length;
957
958  while ((j>0) && (res[j-1]==NULL)) j--;
959  while (j>0)
960  {
961    for (i=0;i<IDELEMS(res[j-1]);i++)
962    {
963      p = res[j-1]->m[i];
964      while (p!=NULL)
965      {
966        if (pIsConstantComp(p)) return j;
967        p = pNext(p);
968      }
969    }
970    j--;
971  }
972  return j;
973}
Note: See TracBrowser for help on using the repository browser.