source: git/Singular/syz.cc @ 2c694a2

spielwiese
Last change on this file since 2c694a2 was 551fd7, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* small bug fixes git-svn-id: file:///usr/local/Singular/svn/trunk@3830 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 25.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: syz.cc,v 1.29 1999-11-16 12:39:31 obachman 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 "stairc.h"
19#include "cntrlc.h"
20#include "ipid.h"
21#include "intvec.h"
22#include "ipshell.h"
23#include "numbers.h"
24#include "ideals.h"
25#include "intvec.h"
26#include "ring.h"
27#include "syz.h"
28#include "prCopy.h"
29
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  Free((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 = NewIntvec1(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]);
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 = NewIntvec1(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(pCopy(actWith),Unit2));
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 for one element
233*/
234void syGaussForOne(ideal syz, int elnum, int ModComp,int from,int till)
235{
236  int k,j,i,lu;
237  poly unit1,unit2;
238  poly actWith=syz->m[elnum];
239
240  if (from<0) from = 0;
241  if ((till<=0) || (till>IDELEMS(syz))) till = IDELEMS(syz);
242  syz->m[elnum] = NULL;
243  if (!rField_has_simple_inverse()) pCleardenom(actWith);
244/*--makes Gauss alg. for the column ModComp--*/
245  pTakeOutComp(&(actWith), ModComp, &unit1, &lu);
246  while (from<till)
247  {
248    if (syz->m[from]!=NULL)
249    {
250      pTakeOutComp(&(syz->m[from]), ModComp, &unit2, &lu);
251      syz->m[from] = pMult(syz->m[from],pCopy(unit1));
252      syz->m[from] = pSub(syz->m[from],
253        pMult(pCopy(actWith),unit2));
254    }
255    from++;
256  }
257  pDelete(&actWith);
258  pDelete(&unit1);
259}
260static void syDeleteAbove1(ideal up, int k)
261{
262  poly p,pp;
263  if (up!=NULL)
264  {
265    for (int i=0;i<IDELEMS(up);i++)
266    {
267      p = up->m[i];
268      while ((p!=NULL) && (pGetComp(p)==k))
269      {
270        pp = pNext(p);
271        pNext(p) = NULL;
272        pDelete(&p);
273        p = pp;
274      }
275      up->m[i] = p;
276      if (p!=NULL)
277      {
278        while (pNext(p)!=NULL)
279        {
280          if (pGetComp(pNext(p))==k)
281          {
282            pp = pNext(pNext(p));
283            pNext(pNext(p)) = NULL;
284            pDelete(&pNext(p));
285            pNext(p) = pp;
286          }
287          else
288            pIter(p);
289        }
290      }
291    }
292  }
293}
294/*2
295*minimizes the resolution res
296*assumes homogeneous or local case
297*/
298static void syMinStep1(resolvente res, int length)
299{
300  int i,j,k,l,index=0;
301  poly p;
302  ideal deg0=NULL,reddeg0=NULL;
303  intvec *have_del=NULL,*to_del=NULL;
304
305  while ((index<length) && (res[index]!=NULL))
306  {
307/*---we take out dependend elements from syz---------------------*/
308    if (res[index+1]!=NULL)
309    {
310      deg0 = idJet(res[index+1],0);
311      reddeg0 = kInterRed(deg0);
312      idDelete(&deg0);
313      have_del = NewIntvec1(IDELEMS(res[index]));
314      for (i=0;i<IDELEMS(reddeg0);i++)
315      {
316        if (reddeg0->m[i]!=NULL)
317        {
318          j = pGetComp(reddeg0->m[i]);
319          pDelete(&(res[index]->m[j-1]));
320          res[index]->m[j-1] = NULL;
321          (*have_del)[j-1] = 1;
322        }
323      }
324      idDelete(&reddeg0);
325    }
326    if (index>0)
327    {
328/*--- we search for units and perform Gaussian elimination------*/
329      j = to_del->length();
330      while (j>0)
331      {
332        if ((*to_del)[j-1]==1)
333        {
334          k = 0;
335          while (k<IDELEMS(res[index]))
336          {
337            p = res[index]->m[k];
338            while ((p!=NULL) && ((!pIsConstantComp(p)) || (pGetComp(p)!=j)))
339              pIter(p);
340            if ((p!=NULL) && (pIsConstantComp(p)) && (pGetComp(p)==j)) break;
341            k++;
342          }
343          if (k>=IDELEMS(res[index]))
344          {
345            PrintS("out of range\n");
346          }
347          syGaussForOne(res[index],k,j);
348          if (res[index+1]!=NULL)
349            syDeleteAbove1(res[index+1],k+1);
350          (*to_del)[j-1] = 0;
351        }
352        j--;
353      }
354    }
355    if (to_del!=NULL) delete to_del;
356    to_del = have_del;
357    have_del = NULL;
358    index++;
359  }
360  if (TEST_OPT_PROT) PrintLn();
361  syKillEmptyEntres(res,length);
362}
363
364void syMinimizeResolvente(resolvente res, int length, int first)
365{
366  int syzIndex=first;
367  intvec *dummy;
368
369  if (syzIndex<1) syzIndex=1;
370  if ((syzIndex==1) && (idHomModule(res[0],currQuotient,&dummy)))
371  {
372    syMinStep1(res,length);
373    delete dummy;
374    return;
375  }
376  while ((syzIndex<length-1) && (res[syzIndex]!=NULL) && (res[syzIndex+1]!=NULL))
377  {
378    syMinStep(res[syzIndex-1],res[syzIndex],FALSE,res[syzIndex+1]);
379    syzIndex++;
380  }
381  if (res[syzIndex]!=NULL)
382    syMinStep(res[syzIndex-1],res[syzIndex]);
383  if (!idIs0(res[0]))
384    idMinEmbedding(res[0]);
385}
386
387/*2
388* resolution of ideal/module arg, <=maxlength steps, (r[0..maxlength])
389*   no limitation in length if maxlength==0
390* input:arg
391*       minim: TRUE means mres cmd, FALSE res cmd.
392*       if *len!=0: modul weights: weights[0]
393*          (and weights is defined:weights[0..len-1]
394*
395* output:resolvente r[0..length-1],
396*        modul weights: weights[0..length-1]
397*/
398resolvente syResolvente(ideal arg, int maxlength, int * length,
399                        intvec *** weights, BOOLEAN minim)
400{
401  resolvente res;
402  resolvente newres;
403  tHomog hom=isNotHomog;
404  ideal temp=NULL;
405  intvec *w = NULL,**tempW;
406  int i,k,syzIndex = 0,j,rk_arg=max(1,idRankFreeModule(arg));
407  int Kstd1_OldDeg=Kstd1_deg;
408  BOOLEAN completeMinim;
409  BOOLEAN oldDegBound=TEST_OPT_DEGBOUND;
410  int wlength=*length;
411
412  if (maxlength!=-1) *length = maxlength+1;
413  else              *length = 5;
414  if ((wlength!=0) && (*length!=wlength))
415  {
416    intvec **wtmp = (intvec**)Alloc0((*length)*sizeof(intvec*));
417    wtmp[0]=(*weights)[0];
418    Free((ADDRESS)*weights,wlength*sizeof(intvec*));
419    *weights=wtmp;
420  }
421  res = (resolvente)Alloc0((*length)*sizeof(ideal));
422
423/*--- initialize the syzygy-ring -----------------------------*/
424  ring origR = currRing;
425  ring syz_ring = rCurrRingAssureSyzComp();
426  rSetSyzComp(rk_arg);
427 
428  if (syz_ring != origR)
429  {
430    res[0] = idrCopyR_NoSort(arg, origR);
431  }
432  else
433  {
434    res[0] = idCopy(arg);
435  }
436 
437/*--- creating weights for the module components ---------------*/
438  if ((weights==NULL) || (*weights==NULL) || ((*weights)[0]==NULL))
439  {
440    hom=(tHomog)idHomModule(res[0],currQuotient,&w);
441    if (hom==isHomog)
442    {
443      *weights = (intvec**)Alloc0((*length)*sizeof(intvec*));
444      if (w!=NULL) (*weights)[0] = ivCopy(w);
445    }
446  }
447  else
448  {
449    if ((weights!=NULL) && (*weights!=NULL)&& ((*weights)[0]!=NULL))
450    {
451      w = ivCopy((*weights)[0]);
452      hom = isHomog;
453    }
454  }
455  if (hom==isHomog)
456  {
457    intvec *w1 = syPrepareModComp(res[0],&w);
458    if (w!=NULL) { delete w;w=NULL; }
459    w = w1;
460  }
461   
462/*--- the main loop --------------------------------------*/
463  while ((!idIs0(res[syzIndex])) &&
464         ((maxlength==-1) || (syzIndex<=maxlength)))
465   // (syzIndex<maxlength+(int)minim)))
466/*--- compute one step more for minimizing-----------------*/
467  {
468    if (Kstd1_deg!=0) Kstd1_deg++;
469    if (syzIndex+1==*length)
470    {
471      newres = (resolvente)Alloc0((*length+5)*sizeof(ideal));
472      tempW = (intvec**)Alloc0((*length+5)*sizeof(intvec*));
473      for (j=0;j<*length;j++)
474      {
475        newres[j] = res[j];
476        if (*weights!=NULL) tempW[j] = (*weights)[j];
477        /*else              tempW[j] = NULL;*/
478      }
479      Free((ADDRESS)res,*length*sizeof(ideal));
480      Free((ADDRESS)*weights,*length*sizeof(intvec*));
481      *length += 5;
482      res=newres;
483      *weights = tempW;
484    }
485/*--- interreducing first -----------------------------------*/
486    if (minim || (syzIndex!=0))
487    {
488      if (syzIndex>0)
489      {
490        int rkI=idRankFreeModule(res[syzIndex]);
491        rSetSyzComp(rkI);
492      }
493      temp = kInterRed(res[syzIndex],currQuotient);
494      idDelete(&res[syzIndex]);
495      idSkipZeroes(temp);
496      res[syzIndex] = temp;
497    }
498    temp = NULL;
499/*--- computing the syzygy modules --------------------------------*/
500    if ((currQuotient==NULL)&&(syzIndex==0)&& (!TEST_OPT_DEGBOUND))
501    {
502      res[/*syzIndex+*/1] = idSyzygies(res[0/*syzIndex*/],hom,&w,FALSE,TRUE,&Kstd1_deg);
503      if ((!TEST_OPT_NOTREGULARITY) && (Kstd1_deg>0)) test |= Sy_bit(OPT_DEGBOUND);
504    }
505    else
506    {
507        res[syzIndex+1] = idSyzygies(res[syzIndex],hom,&w,FALSE);
508    }
509    completeMinim=(syzIndex!=maxlength) || (maxlength ==-1) || (hom!=isHomog);
510    syzIndex++;
511    if (TEST_OPT_PROT) Print("[%d]\n",syzIndex);
512    if ((minim)||(syzIndex>1))
513      syMinStep(res[syzIndex-1],res[syzIndex],!completeMinim,NULL,hom);
514    if (!completeMinim)
515    /*minim is TRUE, we are in the module: maxlength, maxlength <>0*/
516    {
517      idDelete(&res[syzIndex]);
518    }
519/*---creating the iterated weights for module components ---------*/
520    if ((hom == isHomog) && (!idIs0(res[syzIndex])))
521    {
522//Print("die %d Modulegewichte sind:\n",w1->length());
523//w1->show();
524//PrintLn();
525      int max_comp = idRankFreeModule(res[syzIndex]);
526      k = max_comp - rGetCurrSyzLimit();
527      assume(w != NULL);
528      if (w != NULL)
529        w->resize(max_comp+IDELEMS(res[syzIndex]));
530      else
531        w = NewIntvec1(max_comp+IDELEMS(res[syzIndex]));
532      (*weights)[syzIndex] = NewIntvec1(k);
533      for (i=0;i<k;i++)
534      {
535        if (res[syzIndex-1]->m[i]!=NULL) // hs
536        {
537          (*w)[i + rGetCurrSyzLimit()] = pFDeg(res[syzIndex-1]->m[i]);
538          if (pGetComp(res[syzIndex-1]->m[i])>0)
539            (*w)[i + rGetCurrSyzLimit()] 
540              += (*w)[pGetComp(res[syzIndex-1]->m[i])-1];
541          (*((*weights)[syzIndex]))[i] = (*w)[i+rGetCurrSyzLimit()];
542        }
543      }
544      for (i=k;i<k+IDELEMS(res[syzIndex]);i++)
545      {
546        if (res[syzIndex]->m[i-k]!=NULL)
547          (*w)[i+rGetCurrSyzLimit()] = pFDeg(res[syzIndex]->m[i-k])
548                    +(*w)[pGetComp(res[syzIndex]->m[i-k])-1];
549      }
550    }
551  }
552/*--- end of the main loop --------------------------------------*/
553/*--- deleting the temporare data structures --------------------*/
554  if ((syzIndex!=0) && (res[syzIndex]!=NULL) && (idIs0(res[syzIndex])))
555    idDelete(&res[syzIndex]);
556  if (w !=NULL) delete w;
557
558  Kstd1_deg=Kstd1_OldDeg;
559  if (!oldDegBound)
560    test &= ~Sy_bit(OPT_DEGBOUND);
561
562  for (i=1; i<=syzIndex; i++)
563  {
564    if (! idIs0(res[i]))
565    {
566      for (j=0; j<IDELEMS(res[i]); j++)
567      {
568        pShift(&res[i]->m[j], -rGetMaxSyzComp(i));
569      }
570    }
571  }
572/*--- going back to the original ring -------------------------*/
573  if (origR != syz_ring)
574  {
575    rChangeCurrRing(origR, TRUE);
576    for (i=0; i<=syzIndex; i++)
577    {
578      res[i] = idrMoveR_NoSort(res[i], syz_ring);
579    }
580  }
581  return res;
582}
583
584syStrategy syResolution(ideal arg, int maxlength,intvec * w, BOOLEAN minim)
585{
586  int typ0;
587  syStrategy result=(syStrategy)Alloc0SizeOf(ssyStrategy);
588
589  if (w!=NULL)
590  {
591    result->weights = (intvec**)Alloc0SizeOf(void_ptr);
592    (result->weights)[0] = ivCopy(w);
593    result->length = 1;
594  }
595  resolvente fr = syResolvente(arg,maxlength,&(result->length),&(result->weights),minim),fr1;
596  if (minim)
597  {
598    result->minres = (resolvente)Alloc0((result->length+1)*sizeof(ideal));
599    fr1 =  result->minres;
600  }
601  else
602  {
603    result->fullres = (resolvente)Alloc0((result->length+1)*sizeof(ideal));
604    fr1 =  result->fullres;
605  }
606  for (int i=result->length-1;i>=0;i--)
607  {
608    if (fr[i]!=NULL)
609      fr1[i] = fr[i];
610    fr[i] = NULL;
611  }
612  Free((ADDRESS)fr,(result->length)*sizeof(ideal));
613  return result;
614}
615
616static poly sypCopyConstant(poly inp)
617{
618  poly outp=NULL,q;
619
620  while (inp!=NULL)
621  {
622    if (pIsConstantComp(inp))
623    {
624      if (outp==NULL)
625      {
626        q = outp = pHead(inp);
627      }
628      else
629      {
630        pNext(q) = pHead(inp);
631        pIter(q);
632      }
633    }
634    pIter(inp);
635  }
636  return outp;
637}
638int syDetect(ideal id,int index,BOOLEAN homog,int * degrees,int * tocancel)
639{
640  int i, j, k, ModComp,subFromRank=0, lu;
641  poly p, q, qq, Unit1, Unit2;
642  ideal temp;
643
644  if (idIs0(id)) return 0;
645  temp = idInit(IDELEMS(id),id->rank);
646  for (i=0;i<IDELEMS(id);i++)
647  {
648    temp->m[i] = sypCopyConstant(id->m[i]);
649  }
650  i = IDELEMS(id);
651  while ((i>0) && (temp->m[i-1]==NULL)) i--;
652  if (i==0)
653  {
654    idDelete(&temp);
655    return 0;
656  }
657  j = 0;
658  p = NULL;
659  while ((j<i) && (temp->m[j]==NULL)) j++;
660  while (j<i)
661  {
662    if (homog)
663    {
664      k = pFDeg(id->m[j])+degrees[pGetComp(id->m[j])];
665      if (k>=index) tocancel[k-index]++;
666      if ((k>=0) && (index==0)) subFromRank++;
667    }
668    else
669    {
670      tocancel[0]--;
671    }
672    syGaussForOne(temp,j,pGetComp(temp->m[j]),j+1,i);
673    j++;
674    while ((j<i) && (temp->m[j]==NULL)) j++;
675  }
676  idDelete(&temp);
677  return subFromRank;
678}
679
680void syDetect(ideal id,int index,int rsmin, BOOLEAN homog,
681              intvec * degrees,intvec * tocancel)
682{
683  int * deg=NULL;
684  int * tocan=(int*) Alloc0(tocancel->length()*sizeof(int));
685  int i;
686
687  if (homog)
688  {
689    deg = (int*) Alloc0(degrees->length()*sizeof(int));
690    for (i=degrees->length();i>0;i--)
691      deg[i-1] = (*degrees)[i-1]-rsmin;
692  }
693  int dummy=syDetect(id,index,homog,deg,tocan);
694  for (i=tocancel->length();i>0;i--)
695    (*tocancel)[i-1] = tocan[i-1];
696  if (homog)
697    Free((ADDRESS)deg,degrees->length()*sizeof(int));
698  Free((ADDRESS)tocan,tocancel->length()*sizeof(int));
699}
700
701/*2
702* computes the betti numbers from a given resolution
703* of length 'length' (0..length-1), not necessairily minimal,
704* (if weights are given, they are used)
705* returns the int matrix of betti numbers
706* and the regularity
707*/
708intvec * syBetti(resolvente res,int length, int * regularity,
709                 intvec* weights,BOOLEAN tomin)
710{
711  //tomin = FALSE;
712  int i,j=0,k=0,l,rows,cols,mr;
713  int *temp1,*temp2,*temp3;/*used to compute degrees*/
714  int *tocancel; /*(BOOLEAN)tocancel[i]=element is superfluous*/
715
716  /*------ compute size --------------*/
717  *regularity = -1;
718  cols = length;
719  while ((cols>0)
720  && ((res[cols-1]==NULL)
721    || (idIs0(res[cols-1]))))
722  {
723    cols--;
724  }
725  intvec * result;
726  if (idIs0(res[0]))
727  {
728    if (res[0]==NULL)
729      result = NewIntvec3(1,1,1);
730    else
731      result = NewIntvec3(1,1,res[0]->rank);
732    return result;
733  }
734  intvec *w=NULL;
735  if (idHomModule(res[0],currQuotient,&w)!=isHomog)
736  {
737    Warn("betti-command: Input is not homogeneous!");
738  }
739  delete w;
740  int rkl=l = max(idRankFreeModule(res[0]),res[0]->rank);
741  i = 0;
742  while ((i<length) && (res[i]!=NULL))
743  {
744    if (IDELEMS(res[i])>l) l = IDELEMS(res[i]);
745    i++;
746  }
747  temp1 = (int*)Alloc0((l+1)*sizeof(int));
748  temp2 = (int*)Alloc((l+1)*sizeof(int));
749  rows = 1;
750  mr = 1;
751  cols++;
752  for (i=0;i<cols-1;i++)
753  {
754    if ((i==0) && (weights!=NULL)) pSetModDeg(weights);
755    memset(temp2,0,(l+1)*sizeof(int));
756    for (j=0;j<IDELEMS(res[i]);j++)
757    {
758      if (res[i]->m[j]!=NULL)
759     {
760        if ((pGetComp(res[i]->m[j])>l)
761        || ((i>1) && (res[i-1]->m[pGetComp(res[i]->m[j])-1]==NULL)))
762        {
763          WerrorS("input not a resolvent");
764          Free((ADDRESS)temp1,(l+1)*sizeof(int));
765          Free((ADDRESS)temp2,(l+1)*sizeof(int));
766          return NULL;
767        }
768        temp2[j+1] = pFDeg(res[i]->m[j])+temp1[pGetComp(res[i]->m[j])];
769        if (temp2[j+1]-i>rows) rows = temp2[j+1]-i;
770        if (temp2[j+1]-i<mr) mr = temp2[j+1]-i;
771      }
772    }
773    if ((i==0) && (weights!=NULL)) pSetModDeg(NULL);
774    temp3 = temp1;
775    temp1 = temp2;
776    temp2 = temp3;
777  }
778  mr--;
779  /*------ computation betti numbers --------------*/
780  rows -= mr;
781  result = NewIntvec3(rows,cols,0);
782  (*result)[(-mr)*cols] = /*idRankFreeModule(res[0])*/ rkl;
783  if ((!idIs0(res[0])) && ((*result)[(-mr)*cols]==0))
784    (*result)[(-mr)*cols] = 1;
785  tocancel = (int*)Alloc0((rows+1)*sizeof(int));
786  memset(temp2,0,l*sizeof(int));
787  memset(temp1,0,(l+1)*sizeof(int));
788  int dummy = syDetect(res[0],0,TRUE,temp2,tocancel);
789  if (tomin)
790    (*result)[(-mr)*cols] -= dummy;
791  for (i=0;i<cols-1;i++)
792  {
793    if ((i==0) && (weights!=NULL)) pSetModDeg(weights);
794    memset(temp2,0,l*sizeof(int));
795    for (j=0;j<IDELEMS(res[i]);j++)
796    {
797      if (res[i]->m[j]!=NULL)
798      {
799        temp2[j+1] = pFDeg(res[i]->m[j])+temp1[pGetComp(res[i]->m[j])];
800        //(*result)[i+1+(temp2[j+1]-i-1)*cols]++;
801        //if (temp2[j+1]>i) IMATELEM((*result),temp2[j+1]-i-mr,i+2)++;
802        IMATELEM((*result),temp2[j+1]-i-mr,i+2)++;
803      }
804    }
805  /*------ computation betti numbers, if res not minimal --------------*/
806    if (tomin)
807    {
808      for (j=mr;j<rows+mr;j++)
809      {
810        //(*result)[i+1+j*cols] -= tocancel[j+1];
811        IMATELEM((*result),j+1-mr,i+2) -= tocancel[j+1];
812      }
813      if ((i<length-1) && (res[i+1]!=NULL))
814      {
815        memset(tocancel,0,(rows+1)*sizeof(int));
816        dummy = syDetect(res[i+1],i+1,TRUE,temp2,tocancel);
817        for (j=0;j<rows;j++)
818        {
819          //(*result)[i+1+j*cols] -= tocancel[j];
820          IMATELEM((*result),j+1,i+2) -= tocancel[j];
821        }
822      }
823    }
824    temp3 = temp1;
825    temp1 = temp2;
826    temp2 = temp3;
827    for (j=0;j<rows;j++)
828    {
829    //  if (((*result)[i+1+j*cols]!=0) && (j>*regularity)) *regularity = j;
830      if ((IMATELEM((*result),j+1,i+2)!=0) && (j>*regularity)) *regularity = j;
831    }
832    if ((i==0) && (weights!=NULL)) pSetModDeg(NULL);
833  }
834  /*------ clean up --------------*/
835  Free((ADDRESS)tocancel,(rows+1)*sizeof(int));
836  Free((ADDRESS)temp1,(l+1)*sizeof(int));
837  Free((ADDRESS)temp2,(l+1)*sizeof(int));
838  if ((tomin) && (mr<0))  // deletes the first (zero) line
839  {
840    for (j=1;j<=rows+mr;j++)
841    {
842      for (k=1;k<=cols;k++)
843      {
844        IMATELEM((*result),j,k) = IMATELEM((*result),j-mr,k);
845      }
846    }
847    for (j=rows+mr+1;j<=rows;j++)
848    {
849      for (k=1;k<=cols;k++)
850      {
851        IMATELEM((*result),j,k) = 0;
852      }
853    }
854  }
855  j = 0;
856  k = 0;
857  for (i=0;i<rows*cols;i++)
858  {
859    if ((*result)[i] != 0)
860    {
861      if (i/cols>j) j = i/cols;
862      if (i>k+(i/cols)*cols) k = i-(i/cols)*cols;
863    }
864  }
865  intvec * exactresult=NewIntvec3(j+1,k+1,0);
866  for (i=0;i<exactresult->rows();i++)
867  {
868    for (j=0;j<exactresult->cols();j++)
869    {
870      IMATELEM(*exactresult,i+1,j+1) = IMATELEM(*result,i+1,j+1);
871    }
872  }
873  delete result;
874  return exactresult;
875}
876
877/*2
878* minbare via syzygies
879*/
880ideal syMinBase(ideal arg)
881{
882  intvec ** weights=NULL;
883  int leng;
884  if (idIs0(arg)) return idInit(1,arg->rank);
885  resolvente res=syResolvente(arg,1,&leng,&weights,TRUE);
886  ideal result=res[0];
887  Free((ADDRESS)res,leng*sizeof(ideal));
888  if (weights!=NULL)
889  {
890    if (*weights!=NULL)
891    {
892      delete (*weights);
893      *weights=NULL;
894    }
895    if ((leng>=1) && (*(weights+1)!=NULL))
896    {
897      delete *(weights+1);
898      *(weights+1)=NULL;
899    }
900  }
901  idSkipZeroes(result);
902  return result;
903}
904
905/*2
906* computes Betti-numbers from a resolvente of
907* (non-)homogeneous objects
908* the numbers of entrees !=NULL in res and weights must be equal
909* and < length
910*/
911intvec * syNewBetti(resolvente res, intvec ** weights, int length)
912{
913  intvec * result,*tocancel;
914  int i,j,k,rsmin=0,rsmax=0,rs=0;
915  BOOLEAN homog=TRUE;
916
917  if (weights!=NULL)           //---homogeneous Betti numbers
918  {
919/*--------------computes size of the field----------------------*/
920    for (i=1;i<length;i++)
921    {
922      if (weights[i] != NULL)
923      {
924        for (j=1;j<(weights[i])->length();j++)
925        {
926          if ((*(weights[i]))[j]-i<rsmin) rsmin = (*(weights[i]))[j]-i;
927          if ((*(weights[i]))[j]-i>rsmax) rsmax = (*(weights[i]))[j]-i;
928        }
929      }
930    }
931    i = 0;
932    while (weights[i] != NULL) i++;
933    i--;
934    for (j=0;j<IDELEMS(res[i]);j++)
935    {
936      if (res[i]->m[j]!=NULL)
937      {
938        k = pFDeg(res[i]->m[j])+(*(weights[i]))[pGetComp(res[i]->m[j])]-i-1;
939        if (k>rsmax) rsmax = k;
940        if (k<rsmin) rsmin = k;
941      }
942    }
943    for (j=1;j<(weights[0])->length();j++)
944    {
945      if ((*weights[0])[j]>rsmax) rsmax = (*weights[0])[j];
946      if ((*weights[0])[j]<rsmin) rsmin = (*weights[0])[j];
947    }
948//Print("rsmax = %d\n",rsmax);
949//Print("rsmin = %d\n",rsmin);
950    rs = rsmax-rsmin+1;
951    result = NewIntvec3(rs,i+2,0);
952    tocancel = NewIntvec1(rs);
953/*-----------enter the Betti numbers-------------------------------*/
954    if (/*idRankFreeModule(res[0])*/ res[0]->rank==0)
955    {
956      IMATELEM(*result,1-rsmin,1)=1;
957    }
958    else
959    {
960      for (i=1;i<(weights[0])->length();i++)
961        IMATELEM(*result,(*weights[0])[i]+1-rsmin,1)++;
962    }
963    i = 1;
964    while (weights[i]!=NULL)
965    {
966      for (j=1;j<(weights[i])->length();j++)
967      {
968        IMATELEM(*result,(*(weights[i]))[j]-i+1-rsmin,i+1)++;
969      }
970      i++;
971    }
972    i--;
973    for (j=0;j<IDELEMS(res[i]);j++)
974    {
975      if (res[i]->m[j]!=NULL)
976      {
977        k = pFDeg(res[i]->m[j])+(*(weights[i]))[pGetComp(res[i]->m[j])]-i;
978        IMATELEM(*result,k-rsmin,i+2)++;
979      }
980    }
981  }
982  else                //-----the non-homgeneous case
983  {
984    homog = FALSE;
985    tocancel = NewIntvec1(1);
986    k = length;
987    while ((k>0) && (idIs0(res[k-1]))) k--;
988    result = NewIntvec3(1,k+1,0);
989    (*result)[0] = res[0]->rank;
990    for (i=0;i<length;i++)
991    {
992      if (res[i]!=NULL)
993      {
994        for (j=0;j<IDELEMS(res[i]);j++)
995        {
996          if (res[i]->m[j]!=NULL) (*result)[i+1]++;
997        }
998      }
999    }
1000  }
1001/*--------computes the Betti numbers for the minimized reolvente----*/
1002
1003  i = 1;
1004  while ((res[i]!=NULL) && (weights[i]!=NULL))
1005  {
1006    syDetect(res[i],i,rsmin,homog,weights[i],tocancel);
1007    if (homog)
1008    {
1009      for (j=0;j<rs-1;j++)
1010      {
1011        IMATELEM((*result),j+1,i+1) -= (*tocancel)[j];
1012        IMATELEM((*result),j+1,i+2) -= (*tocancel)[j+1];
1013      }
1014      IMATELEM((*result),rs,i+1) -= (*tocancel)[rs-1];
1015    }
1016    else
1017    {
1018      (*result)[i+1] -= (*tocancel)[0];
1019      (*result)[i+2] -= (*tocancel)[0];
1020    }
1021    i++;
1022  }
1023
1024/*--------print Betti numbers for control---------------------------*/
1025  for(i=rsmin;i<=rsmax;i++)
1026  {
1027    Print("%2d:",i);
1028    for(j=1;j<=result->cols();j++)
1029    {
1030      Print(" %5d",IMATELEM(*result,i-rsmin+1,j));
1031    }
1032    PrintLn();
1033  }
1034  return result;
1035}
1036
1037/*2
1038* is looking for the minimal minimized module of a resolvente
1039* i.e. returns 0 if res comes from a mres-command and 1 in the
1040* case of res-commands
1041*/
1042int syIsMinimizedFrom(resolvente res,int length)
1043{
1044  poly p;
1045  int i,j=length;
1046
1047  while ((j>0) && (res[j-1]==NULL)) j--;
1048  while (j>0)
1049  {
1050    for (i=0;i<IDELEMS(res[j-1]);i++)
1051    {
1052      p = res[j-1]->m[i];
1053      while (p!=NULL)
1054      {
1055        if (pIsConstantComp(p)) return j;
1056        p = pNext(p);
1057      }
1058    }
1059    j--;
1060  }
1061  return j;
1062}
Note: See TracBrowser for help on using the repository browser.