source: git/kernel/walkSupport.cc @ 737a68

spielwiese
Last change on this file since 737a68 was 737a68, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
CHG: moved libpolys/polys/polys.h to kernel/polys.h & updated includes ADD: moved (definition of) currRing/rChangeCurrRing to kernel/polys.cc!?
  • Property mode set to 100644
File size: 31.9 KB
Line 
1#include <string.h>
2#include <kernel/mod2.h>
3#include <misc/intvec.h>
4#include <misc/int64vec.h>
5#include <kernel/polys.h>
6#include <kernel/ideals.h>
7#include <polys/monomials/ring.h>
8#include <kernel/walkSupport.h>
9#include <polys/prCopy.h>
10#include <kernel/kstd1.h>
11#include <polys/matpol.h>
12#include <math.h>
13
14extern BOOLEAN overflow_error;
15
16///////////////////////////////////////////////////////////////////
17//Support functions for Groebner Walk and Fractal Walk
18///////////////////////////////////////////////////////////////////
19//v1.2 2004-06-17
20///////////////////////////////////////////////////////////////////
21//implemented by Henrik Strohmayer
22///////////////////////////////////////////////////////////////////
23
24
25
26///////////////////////////////////////////////////////////////////
27//tdeg
28///////////////////////////////////////////////////////////////////
29//Description: returns the normal degree of the input polynomial
30///////////////////////////////////////////////////////////////////
31//Uses: pTotaldegree
32///////////////////////////////////////////////////////////////////
33
34int tdeg(poly p)
35{
36  int res=0;
37  if(p!=NULL) res=pTotaldegree(p);
38  return(res);
39}
40
41///////////////////////////////////////////////////////////////////
42
43
44///////////////////////////////////////////////////////////////////
45//getMaxTdeg
46///////////////////////////////////////////////////////////////////
47//Description: returns the maximum normal degree of the
48//polynomials of the input ideal
49///////////////////////////////////////////////////////////////////
50//Uses: pTotaldegree
51///////////////////////////////////////////////////////////////////
52
53int getMaxTdeg(ideal I)
54{
55  int res=-1;
56  int length=(int)I->ncols;
57  for(int j=length-1;j>=0;j--)
58  {
59    if ((I->m)[j]!=NULL)
60    {
61      int temp=pTotaldegree((I->m)[j]);
62      if(temp>res) {res=temp;}
63    }
64  }
65  return(res);
66}
67///////////////////////////////////////////////////////////////////
68
69
70///////////////////////////////////////////////////////////////////
71//getMaxPosOfNthRow
72///////////////////////////////////////////////////////////////////
73//Description: returns the greatest integer of row n of the
74//input intvec
75///////////////////////////////////////////////////////////////////
76//Uses: none
77///////////////////////////////////////////////////////////////////
78
79int getMaxPosOfNthRow(intvec *v,int n)
80{
81  int res=0;
82  assume( (0<n) && (n<=(v->rows())) );
83  {
84    int c=v->cols();
85    int cc=(n-1)*c;
86    res=abs((*v)[0+cc /*(n-1)*c*/]);
87    for (int i=c-1;i>=0;i--)
88    {
89      int temp=abs((*v)[i+cc /*(n-1)*c*/]);
90      if(temp>res){res=temp;}
91    }
92  }
93 return(res);
94}
95
96///////////////////////////////////////////////////////////////////
97
98
99///////////////////////////////////////////////////////////////////
100//getInvEps64
101///////////////////////////////////////////////////////////////////
102//Description: returns the inverse of epsilon (see relevant
103//part of thesis for definition of epsilon)
104///////////////////////////////////////////////////////////////////
105//Uses: getMaxPosOfNthRow
106///////////////////////////////////////////////////////////////////
107
108int64 getInvEps64(ideal G,intvec *targm,int pertdeg)
109{
110  int n;
111  int64 temp64;
112  int64 sum64=0;
113//think n=2 is enough (instead of n=1)
114  for (n=pertdeg; n>1; n--)
115  {
116    temp64=getMaxPosOfNthRow(targm,n);
117    sum64 += temp64;
118  }
119  int64 inveps64=getMaxTdeg(G)*sum64+1;
120
121  //overflow test
122  if( sum64!=0 && (((inveps64-1)/sum64)!=getMaxTdeg(G)) )
123    overflow_error=11;
124
125  return(inveps64);
126}
127
128///////////////////////////////////////////////////////////////////
129
130
131///////////////////////////////////////////////////////////////////
132//invEpsOk64
133///////////////////////////////////////////////////////////////////
134//Description: checks whether the input inveps64 is the same
135//as the one you get from the current ideal I
136///////////////////////////////////////////////////////////////////
137//Uses: getInvEps64
138///////////////////////////////////////////////////////////////////
139
140int invEpsOk64(ideal I, intvec *targm, int pertdeg, int64 inveps64)
141{
142  int64 temp64=getInvEps64(I,targm,pertdeg);
143  if (inveps64>=temp64)
144  {
145    return(1);
146  }
147  else
148  {
149    return(0);
150  }
151}
152
153///////////////////////////////////////////////////////////////////
154
155
156///////////////////////////////////////////////////////////////////
157//getNthRow
158///////////////////////////////////////////////////////////////////
159//Description: returns an intvec containing the nth row of v
160///////////////////////////////////////////////////////////////////
161//Uses: none
162///////////////////////////////////////////////////////////////////
163
164intvec* getNthRow(intvec *v, int n)
165{
166  int r=v->rows();
167  int c=v->cols();
168  intvec *res=new intvec(c);
169  if((0<n) && (n<=r))
170  {
171    int cc=(n-1)*c;
172    for (int i=0; i<c; i++)
173    {
174      (*res)[i]=(*v)[i+cc /*(n-1)*c*/ ];
175    }
176  }
177  return(res);
178}
179
180int64vec* getNthRow64(intvec *v, int n)
181{
182  int r=v->rows();
183  int c=v->cols();
184  int64vec *res=new int64vec(c);
185  if((0<n) && (n<=r))
186  {
187    int cc=(n-1)*c;
188    for (int i=0; i<c; i++)
189    {
190      (*res)[i]=(int64)(*v)[i+cc /*(n-1)*c*/ ];
191    }
192  }
193  return(res);
194}
195
196///////////////////////////////////////////////////////////////////
197
198
199///////////////////////////////////////////////////////////////////
200//getTaun64
201///////////////////////////////////////////////////////////////////
202//Description: returns a list containing the nth perturbed vector
203//and the inveps64 used to calculate the vector
204///////////////////////////////////////////////////////////////////
205//Uses: getNthRow,getInvEps64
206///////////////////////////////////////////////////////////////////
207
208void getTaun64(ideal G,intvec* targm,int pertdeg, int64vec** v64, int64 & i64)
209{
210  int64vec* taun64=getNthRow64(targm,1);
211  int64vec *temp64,*add64;
212  int64 inveps64=1;
213  if (pertdeg>1) inveps64=getInvEps64(G,targm,pertdeg);
214
215  int n;
216  //temp64 is used to check for overflow:
217  for (n=2; n<=pertdeg; n++)
218  {
219    if (inveps64!=1)
220    {
221      temp64=iv64Copy(taun64);
222      (*taun64)*=inveps64;
223      for(int i=0; i<rVar(currRing);i++)
224      {
225        if((*temp64)[i]!=0 && (((*taun64)[i])/((*temp64)[i]))!=inveps64)
226        overflow_error=12;
227      }
228      delete temp64;
229    }
230    temp64=iv64Copy(taun64);
231    add64=getNthRow64(targm,n);
232    taun64=iv64Add(add64,taun64);
233    for(int i=0; i<rVar(currRing);i++)
234    {
235      if( ( ((*temp64)[i]) > 0 ) && ( ((*add64)[i]) > 0 ) )
236      {
237        if( ((*taun64)[i]) < ((*temp64)[i])  )
238          overflow_error=13;
239      }
240      if( ( ((*temp64)[i]) < 0 ) && ( ((*add64)[i]) < 0 ) )
241      {
242        if( ((*taun64)[i]) > ((*temp64)[i])  )
243          overflow_error=13;
244      }
245    }
246    delete temp64;
247  }
248
249  //lists taunlist64=makeTaunList64(taun64,inveps64);
250  //return(taunlist64);
251  *v64=taun64;
252  i64=inveps64;
253}
254
255///////////////////////////////////////////////////////////////////
256//scalarProduct64
257///////////////////////////////////////////////////////////////////
258//Description: returns the scalar product of int64vecs a and b
259///////////////////////////////////////////////////////////////////
260//Assume: Overflow tests assumes input has nonnegative entries
261///////////////////////////////////////////////////////////////////
262//Uses: none
263///////////////////////////////////////////////////////////////////
264
265static inline int64  scalarProduct64(int64vec* a, int64vec* b)
266{
267  assume( a->length() ==  b->length());
268  int i, n = a->length();
269  int64 result = 0;
270  int64 temp1,temp2;
271  for(i=n-1; i>=0; i--)
272  {
273    temp1=(*a)[i] * (*b)[i];
274    if((*a)[i]!=0 && (temp1/(*a)[i])!=(*b)[i]) overflow_error=1;
275
276    temp2=result;
277    result += temp1;
278
279    //since both vectors always have nonnegative entries in init64
280    //result should be >=temp2
281    if(temp2>result) overflow_error=2;
282  }
283
284  return result;
285}
286///////////////////////////////////////////////////////////////////
287
288
289///////////////////////////////////////////////////////////////////
290//init64
291///////////////////////////////////////////////////////////////////
292//Description: returns the initial form ideal of the input ideal
293//I w.r.t. the weight vector currw64
294///////////////////////////////////////////////////////////////////
295//Uses: idealSize,getNthPolyOfId,leadExp64,pLength
296///////////////////////////////////////////////////////////////////
297
298ideal init64(ideal G,int64vec *currw64)
299{
300  int length=IDELEMS(G);
301  ideal I=idInit(length,G->rank);
302  int j;
303  int64 leadingweight,templeadingweight;
304  poly p=NULL;
305  poly leadp=NULL;
306  for (j=1; j<=length; j++)
307  {
308    p=getNthPolyOfId(G,j);
309    int64vec *tt=leadExp64(p);
310    leadingweight=scalarProduct64(currw64,tt /*leadExp64(p)*/);
311    delete tt;
312    while (p!=NULL)
313    {
314      tt=leadExp64(p);
315      templeadingweight=scalarProduct64(currw64,tt /*leadExp64(p)*/);
316      delete tt;
317      if(templeadingweight==leadingweight)
318      {
319        leadp=pAdd(leadp,pHead(p));
320      }
321      if(templeadingweight>leadingweight)
322      {
323        pDelete(&leadp);
324        leadp=pHead(p);
325        leadingweight=templeadingweight;
326      }
327      pIter(p);
328    }
329    (I->m)[j-1]=leadp;
330    p=NULL;
331    leadp=NULL;
332  }
333  return(I);
334}
335
336///////////////////////////////////////////////////////////////////
337
338
339///////////////////////////////////////////////////////////////////
340//currwOnBorder64
341///////////////////////////////////////////////////////////////////
342//Description: returns TRUE if currw64 lies on the border of the
343//groebner cone of the order w.r.t. which the reduced GB G
344//is calculated, otherwise FALSE
345///////////////////////////////////////////////////////////////////
346//Uses: init64
347///////////////////////////////////////////////////////////////////
348
349BOOLEAN currwOnBorder64(ideal G, int64vec* currw64)
350{
351  ideal J=init64(G,currw64);
352  int length=idealSize(J);
353  BOOLEAN res=FALSE;
354  for(int i=length; i>0; i--)
355  {
356    //if(pLength(getNthPolyOfId(J,i))>1)
357    poly p=getNthPolyOfId(J,i);
358    if ((p!=NULL) && (pNext(p)!=NULL))
359    {
360      res=TRUE;break;
361    }
362  }
363  idDelete(&J);
364  return res;
365}
366
367///////////////////////////////////////////////////////////////////
368
369
370///////////////////////////////////////////////////////////////////
371//noPolysWithMoreThanTwoTerms
372///////////////////////////////////////////////////////////////////
373//Description: returns TRUE if all polynomials of Gw are of length
374//<=2, otherwise FALSE
375///////////////////////////////////////////////////////////////////
376//Uses: idealSize, getNthPolyOfId
377///////////////////////////////////////////////////////////////////
378
379BOOLEAN noPolysWithMoreThanTwoTerms(ideal Gw)
380{
381  int length=idealSize(Gw);
382  for(int i=length; i>0; i--)
383  {
384    //if(pLength(getNthPolyOfId(Gw,i))>2)
385    poly p=getNthPolyOfId(Gw,i);
386    if ((p!=NULL) && (pNext(p)!=NULL) && (pNext(pNext(p))!=NULL))
387    {
388      return FALSE;
389    }
390  }
391  return TRUE;
392}
393
394///////////////////////////////////////////////////////////////////
395
396
397///////////////////////////////////////////////////////////////////
398//DIFFspy
399///////////////////////////////////////////////////////////////////
400//Description: returns the length of the list to be created in
401//DIFF
402///////////////////////////////////////////////////////////////////
403//Uses: idealSize,pLength,getNthPolyOfId
404///////////////////////////////////////////////////////////////////
405
406int DIFFspy(ideal G)
407{
408  int s=idealSize(G);
409  int j;
410  int temp;
411  int sum=0;
412  poly p;
413  for (j=1; j<=s; j++)
414  {
415    p=getNthPolyOfId(G,j);
416    if((temp=pLength(p))>0) {sum += (temp-1);}
417  }
418  return(sum);
419}
420
421///////////////////////////////////////////////////////////////////
422
423
424///////////////////////////////////////////////////////////////////
425//DIFF
426///////////////////////////////////////////////////////////////////
427//Description: returns a list of all differences of leading
428//exponents and nonleading exponents of elements of the current
429//GB (see relevant part of thesis for further details)
430///////////////////////////////////////////////////////////////////
431//Uses: DIFFspy,idealSize,getNthPolyOfId,leadExp,pLength
432///////////////////////////////////////////////////////////////////
433
434intvec* DIFF(ideal G)
435{
436  intvec  *v,*w;
437  poly p;
438  int s=idealSize(G);
439  int n=rVar(currRing);
440  int m=DIFFspy(G);
441  intvec* diffm=new intvec(m,n,0);
442  int j,l;
443  int inc=0;
444  for (j=1; j<=s; j++)
445  {
446    p=getNthPolyOfId(G,j);
447    v=leadExp(p);
448    pIter(p);
449    while(p!=NULL)
450    {
451      inc++;
452      intvec *lep=leadExp(p);
453      w=ivSub(v,lep /*leadExp(p)*/);
454      delete lep;
455      pIter(p);
456      for (l=1; l<=n; l++)
457      {
458        // setPosOfIM(diffm,inc,l,(*w)[l-1]);
459        IMATELEM(*diffm,inc,l) =(*w)[l-1] ;
460      }
461      delete w;
462    }
463    delete v;
464  }
465  return(diffm);
466}
467
468///////////////////////////////////////////////////////////////////
469
470
471///////////////////////////////////////////////////////////////////
472//gett64
473///////////////////////////////////////////////////////////////////
474//Description: returns the t corresponding to the vector listw
475//which contains a vector from the list returned by DIFF
476///////////////////////////////////////////////////////////////////
477//Uses: ivSize
478///////////////////////////////////////////////////////////////////
479
480void gett64(intvec* listw, int64vec* currw64, int64vec* targw64, int64 &tvec0, int64 &tvec1)
481{
482  int s=ivSize(listw);
483  int j;
484  int64 zaehler64=0;
485  int64 nenner64=0;
486  int64 temp1,temp2,temp3,temp4; //overflowstuff
487  for(j=1; j<=s; j++)
488  {
489
490    temp3=zaehler64;
491    temp1=((int64)((*listw)[j-1]));
492    temp2=((*currw64)[j-1]);
493    temp4=temp1*temp2;
494    zaehler64=temp3-temp4;
495
496    //overflow test
497    if(temp1!=0 && (temp4/temp1)!=temp2) overflow_error=3;
498
499    if( ( temp3<0 && temp4>0 ) || ( temp3>0 && temp4<0 ) )
500    {
501      int64 abs_t3=abs64(temp3);
502      if( (abs_t3+abs64(temp4))<abs_t3 ) overflow_error=4;
503    }
504
505    //overflow test
506    temp1=((*targw64)[j-1])-((*currw64)[j-1]);
507    //this subtraction can never yield an overflow since both number
508    //will always be positive
509    temp2=((int64)((*listw)[j-1]));
510    temp3=nenner64;
511    temp4=temp1*temp2;
512    nenner64=temp3+temp4;
513
514    //overflow test
515    if(temp1!=0 && ((temp1*temp2)/temp1)!=temp2) overflow_error=5;
516
517    if( (temp3>0 && temp4>0) ||
518      (temp3<0 && temp4<0)    )
519    {
520      int64 abs_t3=abs64(temp3);
521      if( (abs_t3+abs64(temp4))<abs_t3 )
522      {
523        overflow_error=6;
524      }
525    }
526  }
527
528  if (nenner64==0)
529  {
530    zaehler64=2;
531  }
532  else
533  {
534    if ( (zaehler64<=0) && (nenner64<0) )
535    {
536      zaehler64=-zaehler64;
537      nenner64=-nenner64;
538    }
539  }
540
541  int64 g=gcd64(zaehler64,nenner64);
542
543  tvec0=zaehler64/g;
544  tvec1=nenner64/g;
545
546}
547
548///////////////////////////////////////////////////////////////////
549
550
551///////////////////////////////////////////////////////////////////
552//nextt64
553///////////////////////////////////////////////////////////////////
554//Description: returns the t determining the next weight vector
555///////////////////////////////////////////////////////////////////
556//Uses:
557///////////////////////////////////////////////////////////////////
558
559void nextt64(ideal G,int64vec* currw64,int64vec* targw64, int64 & tvec0, int64 & tvec1)
560{
561  intvec* diffm=DIFF(G);
562  int s=diffm->rows();
563  tvec0=(int64)2;
564  tvec1=(int64)0;
565  intvec *tt;
566  for(int j=1; j<=s; j++)
567  {
568    tt=getNthRow(diffm,j);
569    int64 temptvec0, temptvec1;
570    gett64(tt,currw64,targw64,temptvec0, temptvec1);
571    delete tt;
572
573    //if tempt>0 both parts will be>0
574    if ( (temptvec1!=0) //that tempt is defined
575       &&
576       (temptvec0>0) && (temptvec1>0) //that tempt>0
577     )
578    {
579      if( ( (temptvec0) <= (temptvec1) ) //that tempt<=1
580        &&
581        ( ( (temptvec0) * (tvec1) ) <
582          ( (temptvec1) * (tvec0) ) )
583      )
584      {  //that tempt<t
585        tvec0=temptvec0;
586        tvec1=temptvec1;
587      }
588    }
589  }
590  delete diffm;
591  return;
592}
593
594///////////////////////////////////////////////////////////////////
595
596
597///////////////////////////////////////////////////////////////////
598//nextw64
599///////////////////////////////////////////////////////////////////
600//Uses:iv64Size,gcd,
601///////////////////////////////////////////////////////////////////
602
603int64vec* nextw64(int64vec* currw, int64vec* targw,
604                  int64 nexttvec0, int64 nexttvec1)
605{
606  //to do (targw-currw)*tvec[0]+currw*tvec[1]
607  int64vec* tempv;
608  int64vec* nextweight;
609  int64vec* a=iv64Sub(targw,currw);
610  //no overflow can occur since both are>=0
611
612  //to test overflow
613  tempv=iv64Copy(a);
614  *a *= (nexttvec0);
615  for(int i=0; i<rVar(currRing); i++)
616  {
617    if( (nexttvec0) !=0 &&
618        (((*a)[i])/(nexttvec0))!=((*tempv)[i]) )
619    {
620      overflow_error=7;
621      break;
622    }
623  }
624  delete tempv;
625  int64vec* b=currw;
626  tempv=iv64Copy(b);
627  *b *= (nexttvec1);
628  for(int i=0; i<rVar(currRing); i++)
629  {
630    if( (nexttvec1) !=0 &&
631        (((*b)[i])/(nexttvec1))!=((*tempv)[i]) )
632    {
633      overflow_error=8;
634      break;
635    }
636  }
637  delete tempv;
638  nextweight=iv64Add(a,b);
639
640  for(int i=0; i<rVar(currRing); i++)
641  {
642    if( (((*a)[i])>=0 && ((*b)[i])>=0) ||
643        (((*a)[i])<0 && ((*b)[i])<0) )
644    {
645      if( (abs64((*a)[i]))>abs64((*nextweight)[i]) ||
646          (abs64((*b)[i]))>abs64((*nextweight)[i])
647        )
648      {
649        overflow_error=9;
650        break;
651      }
652    }
653  }
654
655  //to reduce common factors of nextweight
656  int s=iv64Size(nextweight);
657  int64 g,temp;
658  g=(*nextweight)[0];
659  for (int i=1; i<s; i++)
660  {
661    temp=(*nextweight)[i];
662    g=gcd64(g,temp);
663    if (g==1) break;
664  }
665
666  if (g!=1) *nextweight /= g;
667
668  return(nextweight);
669}
670
671///////////////////////////////////////////////////////////////////
672
673
674
675///////////////////////////////////////////////////////////////////
676//FUNCTIONS NOT ORIGINATING FROM THE SINGULAR IMPLEMENTATION CODE
677///////////////////////////////////////////////////////////////////
678
679
680///////////////////////////////////////////////////////////////////
681//getNthPolyOfId
682///////////////////////////////////////////////////////////////////
683//Description: returns the nth poly of ideal I
684///////////////////////////////////////////////////////////////////
685poly getNthPolyOfId(ideal I,int n)
686{
687  if(0<n && n<=((int)I->ncols))
688  {
689    return (I->m)[n-1];
690  }
691  else
692  {
693    return(NULL);
694  }
695}
696
697///////////////////////////////////////////////////////////////////
698
699
700///////////////////////////////////////////////////////////////////
701//idealSize
702///////////////////////////////////////////////////////////////////
703//Description: returns the number of generator of input ideal I
704///////////////////////////////////////////////////////////////////
705//Uses: none
706///////////////////////////////////////////////////////////////////
707// #define idealSize(I) IDELEMS(I)
708///////////////////////////////////////////////////////////////////
709
710
711///////////////////////////////////////////////////////////////////
712//ivSize
713///////////////////////////////////////////////////////////////////
714//Description: returns the number of entries of v
715///////////////////////////////////////////////////////////////////
716//Uses: none
717///////////////////////////////////////////////////////////////////
718
719// inline int ivSize(intvec* v){ return((v->rows())*(v->cols())); }
720
721///////////////////////////////////////////////////////////////////
722
723
724///////////////////////////////////////////////////////////////////
725//iv64Size
726///////////////////////////////////////////////////////////////////
727//Description: returns the number of entries of v
728///////////////////////////////////////////////////////////////////
729//Uses: none
730///////////////////////////////////////////////////////////////////
731
732// int iv64Size(int64vec* v){ return((v->rows())*(v->cols())); }
733
734///////////////////////////////////////////////////////////////////
735
736
737///////////////////////////////////////////////////////////////////
738//leadExp
739///////////////////////////////////////////////////////////////////
740//Description: returns an intvec containg the exponet vector of p
741///////////////////////////////////////////////////////////////////
742//Uses: sizeof,omAlloc,omFree
743///////////////////////////////////////////////////////////////////
744
745intvec* leadExp(poly p)
746{
747  int N=rVar(currRing);
748  int *e=(int*)omAlloc((N+1)*sizeof(int));
749  pGetExpV(p,e);
750  intvec* iv=new intvec(N);
751  for(int i=N;i>0;i--) { (*iv)[i-1]=e[i];}
752  omFree(e);
753  return(iv);
754}
755
756///////////////////////////////////////////////////////////////////
757
758
759///////////////////////////////////////////////////////////////////
760//leadExp64
761///////////////////////////////////////////////////////////////////
762//Description: returns an int64vec containing the exponent
763//vector of p
764///////////////////////////////////////////////////////////////////
765//Uses: sizeof,omAlloc,omFree
766///////////////////////////////////////////////////////////////////
767
768int64vec* leadExp64(poly p)
769{
770  int N=rVar(currRing);
771  int *e=(int*)omAlloc((N+1)*sizeof(int));
772  pGetExpV(p,e);
773  int64vec* iv64=new int64vec(N);
774  for(int i=N;i>0;i--) { (*iv64)[i-1]=(int64)e[i];}
775  omFree(e);
776  return(iv64);
777}
778
779///////////////////////////////////////////////////////////////////
780
781
782///////////////////////////////////////////////////////////////////
783//setPosOfIM
784///////////////////////////////////////////////////////////////////
785//Description: sets entry i,j of im to val
786///////////////////////////////////////////////////////////////////
787//Uses: none
788///////////////////////////////////////////////////////////////////
789
790//void setPosOfIM(intvec* im,int i,int j,int val){
791//  int r=im->rows();
792//  int c=im->cols();
793//  if( (0<i && i<=r) && (0<j && j<=c) ){
794//    (*im)[(i-1)*c+j-1]=val;
795//    }
796//  return;
797//}
798
799///////////////////////////////////////////////////////////////////
800
801
802///////////////////////////////////////////////////////////////////
803//scalarProduct
804///////////////////////////////////////////////////////////////////
805//Description: returns the scalar product of intvecs a and b
806///////////////////////////////////////////////////////////////////
807//Uses: none
808///////////////////////////////////////////////////////////////////
809
810static inline long  scalarProduct(intvec* a, intvec* b)
811{
812  assume( a->length() ==  b->length());
813  int i, n = a->length();
814  long result = 0;
815  for(i=n-1; i>=0; i--)
816    result += (*a)[i] * (*b)[i];
817  return result;
818}
819
820///////////////////////////////////////////////////////////////////
821
822
823
824///////////////////////////////////////////////////////////////////
825
826
827///////////////////////////////////////////////////////////////////
828//gcd
829///////////////////////////////////////////////////////////////////
830//Description: returns the gcd of a and b
831///////////////////////////////////////////////////////////////////
832//Uses: none
833///////////////////////////////////////////////////////////////////
834
835int gcd(int a, int b)
836{
837  int r, p0 = a, p1 = b;
838  if(p0 < 0)
839    p0 = -p0;
840
841  if(p1 < 0)
842    p1 = -p1;
843  while(p1 != 0)
844  {
845    r = p0 % p1;
846    p0 = p1;
847    p1 = r;
848  }
849  return p0;
850}
851
852///////////////////////////////////////////////////////////////////
853
854
855///////////////////////////////////////////////////////////////////
856//gcd64
857///////////////////////////////////////////////////////////////////
858//Description: returns the gcd of a and b
859///////////////////////////////////////////////////////////////////
860//Uses: none
861///////////////////////////////////////////////////////////////////
862
863int64 gcd64(int64 a, int64 b)
864{
865  int64 r, p0 = a, p1 = b;
866  if(p0 < 0)
867    p0 = -p0;
868
869  if(p1 < 0)
870    p1 = -p1;
871
872  while(p1 != ((int64)0) )
873  {
874    r = p0 % p1;
875    p0 = p1;
876    p1 = r;
877  }
878
879  return p0;
880}
881
882///////////////////////////////////////////////////////////////////
883
884
885///////////////////////////////////////////////////////////////////
886//abs64
887///////////////////////////////////////////////////////////////////
888//Description: returns the absolute value of int64 i
889///////////////////////////////////////////////////////////////////
890//Uses: none
891///////////////////////////////////////////////////////////////////
892
893//int64 abs64(int64 i)
894//{
895//  if(i>=0)
896//  return(i);
897//else
898//  return((-i));
899//}
900
901///////////////////////////////////////////////////////////////////
902
903
904///////////////////////////////////////////////////////////////////
905//makeTaunList64
906///////////////////////////////////////////////////////////////////
907//Description: makes a list of an int64vec and an int64
908///////////////////////////////////////////////////////////////////
909//Uses: omAllocBin
910///////////////////////////////////////////////////////////////////
911
912#if 0
913lists makeTaunList64(int64vec *iv64,int64 i64)
914{
915  lists l=(lists)omAllocBin(slists_bin);
916  l->Init(2);
917  l->m[0].rtyp=INTVEC_CMD;
918  l->m[1].rtyp=INT_CMD;
919  l->m[0].data=(void *)iv64;
920  l->m[1].data=(void *)i64;
921  return l;
922}
923#endif
924
925///////////////////////////////////////////////////////////////////
926
927
928///////////////////////////////////////////////////////////////////
929//idStd
930///////////////////////////////////////////////////////////////////
931//Description: returns the GB of G calculated w.r.t. the order of
932//currRing
933///////////////////////////////////////////////////////////////////
934//Uses: kStd,idSkipZeroes
935///////////////////////////////////////////////////////////////////
936
937ideal idStd(ideal G)
938{
939  ideal GG = kStd(G, NULL, testHomog, NULL);
940  idSkipZeroes(GG);
941  return GG;
942}
943
944///////////////////////////////////////////////////////////////////
945
946
947///////////////////////////////////////////////////////////////////
948//idInterRed
949///////////////////////////////////////////////////////////////////
950//Description: returns the interreduction of G
951///////////////////////////////////////////////////////////////////
952//Assumes: that the input is a GB
953///////////////////////////////////////////////////////////////////
954//Uses: kInterRed,idSkipZeroes
955///////////////////////////////////////////////////////////////////
956
957ideal idInterRed(ideal G)
958{
959  assume(G != NULL);
960
961  ideal GG = kInterRedOld(G, NULL);
962  idDelete(&G);
963  return GG;
964}
965
966///////////////////////////////////////////////////////////////////
967
968
969///////////////////////////////////////////////////////////////////
970//matIdLift
971///////////////////////////////////////////////////////////////////
972//Description: yields the same reslut as lift in Singular
973///////////////////////////////////////////////////////////////////
974//Uses: idLift,idModule2formatedMatrix
975///////////////////////////////////////////////////////////////////
976
977matrix matIdLift(ideal Gomega, ideal M)
978{
979  ideal Mtmp = idLift(Gomega, M, NULL, FALSE, FALSE, TRUE, NULL);
980  int rows=IDELEMS(Gomega);
981  int cols=IDELEMS(Mtmp);
982  matrix res=id_Module2formatedMatrix(Mtmp,rows,cols,currRing);
983  return res;
984}
985///////////////////////////////////////////////////////////////////
986
987
988///////////////////////////////////////////////////////////////////
989//rCopyAndChangeA
990///////////////////////////////////////////////////////////////////
991//Description: changes currRing to a copy of currRing with the
992//int64vec w instead of the old one
993///////////////////////////////////////////////////////////////////
994//Assumes: that currRing is alterd as mentioned in rCopy0AndAddA
995///////////////////////////////////////////////////////////////////
996//Uses: rCopy0,rComplete,rChangeCurrRing,rSetWeightVec
997///////////////////////////////////////////////////////////////////
998
999void rCopyAndChangeA(int64vec* w)
1000{
1001  ring rnew=rCopy0(currRing);
1002  rComplete(rnew);
1003  rSetWeightVec(rnew,w->iv64GetVec());
1004  rChangeCurrRing(rnew);
1005}
1006
1007///////////////////////////////////////////////////////////////////
1008//rGetGlobalOrderMatrix
1009///////////////////////////////////////////////////////////////////
1010//Description: returns a matrix corresponding to the order of r
1011///////////////////////////////////////////////////////////////////
1012//Assumes: the order of r is a combination of the orders M,lp,dp,
1013//Dp,wp,Wp,C
1014///////////////////////////////////////////////////////////////////
1015//Uses: none
1016///////////////////////////////////////////////////////////////////
1017
1018int64vec* rGetGlobalOrderMatrix(ring r)
1019{
1020  int n=rVar(r);
1021  int64vec* res=new int64vec(n,n,(int64)0);
1022  if (rHasLocalOrMixedOrdering(r)) return res;
1023  int pos1=0;
1024  int pos2=0;
1025  int temp;
1026  int i=0;
1027  while(r->order[i]!=0 && pos2<n)
1028  {
1029    pos2=pos2+r->block1[i] - r->block0[i];
1030
1031    if(r->order[i]==ringorder_lp)
1032    {
1033      temp=pos1;
1034      for(int j=pos1; j<=pos2; j++)
1035        (*res)[j*n+j]=(int64)1;
1036    }
1037    else if(r->order[i]==ringorder_dp)
1038    {
1039      for(int j=pos1;j<=pos2;j++)
1040        (*res)[pos1*n+j]=(int64)1;
1041      for(int j=1;j<=(pos2-pos1);j++)
1042        (*res)[(pos1+j)*n+(pos2+1-j)]=(int64)-1;
1043    }
1044    else if(r->order[i]==ringorder_Dp)
1045    {
1046      for(int j=pos1;j<=pos2;j++)
1047        (*res)[pos1*n+j]=(int64)1;
1048      for(int j=1;j<=(pos2-pos1);j++)
1049        (*res)[(pos1+j)*n+(pos1+j-1)]=(int64)1;
1050    }
1051    else if(r->order[i]==ringorder_wp)
1052    {
1053      int* weights=r->wvhdl[i];
1054      for(int j=pos1;j<=pos2;j++)
1055        (*res)[pos1*n+j]=(int64)weights[j-pos1];
1056      for(int j=1;j<=(pos2-pos1);j++)
1057        (*res)[(pos1+j)*n+(pos2+1-j)]=(int64)-1;
1058    }
1059    else if(r->order[i]==ringorder_Wp)
1060    {
1061      int* weights=r->wvhdl[i];
1062      for(int j=pos1;j<=pos2;j++)
1063        (*res)[pos1*n+j]=(int64)weights[j-pos1];
1064      for(int j=1;j<=(pos2-pos1);j++)
1065        (*res)[(pos1+j)*n+(pos1+j-1)]=(int64)1;
1066    }
1067
1068    else if(r->order[0]==ringorder_M)
1069    {
1070      int* weights=r->wvhdl[0];
1071      for(int j=pos1;j<((pos2+1)*(pos2+1));j++)
1072        (*res)[j]=(int64)weights[j];
1073    }
1074
1075    pos1=pos2+1;
1076    pos2=pos2+1;
1077    i++;
1078  }
1079
1080  return(res);
1081}
1082
1083///////////////////////////////////////////////////////////////////
1084
1085
1086///////////////////////////////////////////////////////////////////
1087//rGetGlobalOrderWeightVec
1088///////////////////////////////////////////////////////////////////
1089//Description: returns a weight vector corresponding to the first
1090//row of a matrix corresponding to the order of r
1091///////////////////////////////////////////////////////////////////
1092//Uses: none
1093///////////////////////////////////////////////////////////////////
1094
1095int64vec* rGetGlobalOrderWeightVec(ring r)
1096{
1097  int n=rVar(r);
1098  int64vec* res=new int64vec(n);
1099
1100  if (rHasLocalOrMixedOrdering(r)) return res;
1101
1102  int length;
1103
1104  if(r->order[0]==ringorder_lp)
1105  {
1106    (*res)[0]=(int64)1;
1107  }
1108  else if( (r->order[0]==ringorder_dp) || (r->order[0]==ringorder_Dp) )
1109  {
1110    length=r->block1[0] - r->block0[0];
1111    for (int j=0;j<=length;j++)
1112      (*res)[j]=(int64)1;
1113  }
1114  else if( (r->order[0]==ringorder_wp) || (r->order[0]==ringorder_Wp) ||
1115      (r->order[0]==ringorder_a)  || (r->order[0]==ringorder_M)    )
1116  {
1117    int* weights=r->wvhdl[0];
1118    length=r->block1[0] - r->block0[0];
1119    for (int j=0;j<=length;j++)
1120      (*res)[j]=(int64)weights[j];
1121  }
1122  else if( (r->order[0]==ringorder_a64) )
1123  {
1124    int64* weights=(int64*)r->wvhdl[0];
1125    length=r->block1[0] - r->block0[0];
1126    for (int j=0;j<=length;j++)
1127      (*res)[j]=weights[j];
1128  }
1129
1130  return(res);
1131}
1132
1133///////////////////////////////////////////////////////////////////
1134
1135
1136///////////////////////////////////////////////////////////////////
1137//sortRedSB
1138///////////////////////////////////////////////////////////////////
1139//Description: sorts a reduced GB of an ideal after the leading
1140//terms of the polynomials with the smallest one first
1141///////////////////////////////////////////////////////////////////
1142//Assumes: that the given input is a minimal GB
1143///////////////////////////////////////////////////////////////////
1144//Uses:idealSize,idCopy,pLmCmp
1145///////////////////////////////////////////////////////////////////
1146
1147ideal sortRedSB(ideal G)
1148{
1149  int s=idealSize(G);
1150  poly* m=G->m;
1151  poly p,q;
1152  for (int i=0; i<(s-1); i++)
1153  {
1154    for (int j=0; j<((s-1)-i); j++)
1155    {
1156      p=m[j];
1157      q=m[j+1];
1158      if (pLmCmp(p,q)==1)
1159      {
1160        m[j+1]=p;
1161        m[j]=q;
1162      }
1163    }
1164  }
1165  return(G);
1166}
1167
1168///////////////////////////////////////////////////////////////////
1169
1170
1171///////////////////////////////////////////////////////////////////
1172//int64VecToIntVec
1173///////////////////////////////////////////////////////////////////
1174//Description: converts an int64vec to an intvec
1175// deletes the input
1176///////////////////////////////////////////////////////////////////
1177//Assumes: that the int64vec contains no entries larger than 2^32
1178///////////////////////////////////////////////////////////////////
1179//Uses: none
1180///////////////////////////////////////////////////////////////////
1181
1182intvec* int64VecToIntVec(int64vec* source)
1183{
1184  int r=source->rows();
1185  int c=source->cols();
1186  intvec* res=new intvec(r,c,0);
1187  for(int i=0;i<r;i++){
1188    for(int j=0;j<c;j++){
1189      (*res)[i*c+j]=(int)(*source)[i*c+j];
1190    }
1191  }
1192  delete source;
1193  return(res);
1194}
1195
1196///////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.