source: git/kernel/syz.cc @ 861529

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