source: git/kernel/syz.cc @ 338842d

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