source: git/kernel/walkSupport.cc @ 841e96d

spielwiese
Last change on this file since 841e96d was 841e96d, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: frwalk stuff git-svn-id: file:///usr/local/Singular/svn/trunk@8047 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 34.0 KB
Line 
1#include <string.h>
2#include "mod2.h"
3#include "intvec.h"
4#include "int64vec.h"
5#include "polys.h"
6#include "ideals.h"
7#include "ring.h"
8#include "walkSupport.h"
9#include "prCopy.h"
10#include "kstd1.h"
11#include "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, currRing);
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], currRing);
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    temp64=iv64Copy(taun64);
220    (*taun64)*=inveps64;
221    for(int i=0; i<currRing->N;i++)
222    {
223      if((*temp64)[i]!=0 && (((*taun64)[i])/((*temp64)[i]))!=inveps64)
224      overflow_error=12;
225    }
226    delete temp64;
227    temp64=iv64Copy(taun64);
228    add64=getNthRow64(targm,n);
229    taun64=iv64Add(add64,taun64);
230    for(int i=0; i<currRing->N;i++)
231    {
232      if( ( ((*temp64)[i]) > 0 ) && ( ((*add64)[i]) > 0 ) )
233      {
234        if( ((*taun64)[i]) < ((*temp64)[i])  )
235          overflow_error=13;
236      }
237      if( ( ((*temp64)[i]) < 0 ) && ( ((*add64)[i]) < 0 ) )
238      {
239        if( ((*taun64)[i]) > ((*temp64)[i])  )
240          overflow_error=13;
241      }
242    }
243    delete temp64;
244  }
245
246  //lists taunlist64=makeTaunList64(taun64,inveps64);
247  //return(taunlist64);
248  *v64=taun64;
249  i64=inveps64;
250}
251
252///////////////////////////////////////////////////////////////////
253
254
255///////////////////////////////////////////////////////////////////
256//init64
257///////////////////////////////////////////////////////////////////
258//Description: returns the initial form ideal of the input ideal
259//I w.r.t. the weight vector currw64
260///////////////////////////////////////////////////////////////////
261//Uses: idealSize,getNthPolyOfId,leadExp64,pLength
262///////////////////////////////////////////////////////////////////
263
264ideal init64(ideal G,int64vec *currw64)
265{
266  int length=IDELEMS(G);
267  ideal I=idInit(length,G->rank);
268  int j;
269  int64 leadingweight,templeadingweight;
270  poly p=NULL;
271  poly leadp=NULL;
272  for (j=1; j<=length; j++)
273  {
274    p=getNthPolyOfId(G,j);
275    int64vec *tt=leadExp64(p);
276    leadingweight=scalarProduct64(currw64,tt /*leadExp64(p)*/);
277    delete tt;
278    while (p!=NULL)
279    {
280      tt=leadExp64(p);
281      templeadingweight=scalarProduct64(currw64,tt /*leadExp64(p)*/);
282      delete tt;
283      if(templeadingweight==leadingweight)
284      {
285        leadp=pAdd(leadp,pHead(p));
286      }
287      if(templeadingweight>leadingweight)
288      {
289        pDelete(&leadp);
290        leadp=pHead(p);
291        leadingweight=templeadingweight;
292      }
293      pIter(p);
294    }
295    (I->m)[j-1]=leadp;
296    p=NULL;
297    leadp=NULL;
298  }
299  return(I);
300}
301
302///////////////////////////////////////////////////////////////////
303
304
305///////////////////////////////////////////////////////////////////
306//currwOnBorder64
307///////////////////////////////////////////////////////////////////
308//Description: returns TRUE if currw64 lies on the border of the
309//groebner cone of the order w.r.t. which the reduced GB G
310//is calculated, otherwise FALSE
311///////////////////////////////////////////////////////////////////
312//Uses: init64
313///////////////////////////////////////////////////////////////////
314
315BOOLEAN currwOnBorder64(ideal G, int64vec* currw64)
316{
317  ideal J=init64(G,currw64);
318  int length=idealSize(J);
319  BOOLEAN res=FALSE;
320  for(int i=length; i>0; i--)
321  {
322    //if(pLength(getNthPolyOfId(J,i))>1)
323    poly p=getNthPolyOfId(J,i);
324    if ((p!=NULL) && (pNext(p)!=NULL))
325    {
326      res=TRUE;break;
327    }
328  }
329  idDelete(&J);
330  return res;
331}
332
333///////////////////////////////////////////////////////////////////
334
335
336///////////////////////////////////////////////////////////////////
337//noPolysWithMoreThanTwoTerms
338///////////////////////////////////////////////////////////////////
339//Description: returns TRUE if all polynomials of Gw are of length
340//<=2, otherwise FALSE
341///////////////////////////////////////////////////////////////////
342//Uses: idealSize, getNthPolyOfId
343///////////////////////////////////////////////////////////////////
344
345BOOLEAN noPolysWithMoreThanTwoTerms(ideal Gw)
346{
347  int length=idealSize(Gw);
348  for(int i=length; i>0; i--)
349  {
350    //if(pLength(getNthPolyOfId(Gw,i))>2)
351    poly p=getNthPolyOfId(Gw,i);
352    if ((p!=NULL) && (pNext(p)!=NULL) && (pNext(pNext(p))!=NULL))
353    {
354      return FALSE;
355    }
356  }
357  return TRUE;
358}
359
360///////////////////////////////////////////////////////////////////
361
362
363///////////////////////////////////////////////////////////////////
364//DIFFspy
365///////////////////////////////////////////////////////////////////
366//Description: returns the length of the list to be created in
367//DIFF
368///////////////////////////////////////////////////////////////////
369//Uses: idealSize,pLength,getNthPolyOfId
370///////////////////////////////////////////////////////////////////
371
372int DIFFspy(ideal G)
373{
374  int s=idealSize(G);
375  int j;
376  int temp;
377  int sum=0;
378  poly p;
379  for (j=1; j<=s; j++)
380  {
381    p=getNthPolyOfId(G,j);
382    if((temp=pLength(p))>0) {sum += (temp-1);}
383  }
384  return(sum);
385}
386
387///////////////////////////////////////////////////////////////////
388
389
390///////////////////////////////////////////////////////////////////
391//DIFF
392///////////////////////////////////////////////////////////////////
393//Description: returns a list of all differences of leading
394//exponents and nonleading exponents of elements of the current
395//GB (see relevant part of thesis for further details)
396///////////////////////////////////////////////////////////////////
397//Uses: DIFFspy,idealSize,getNthPolyOfId,leadExp,pLength
398///////////////////////////////////////////////////////////////////
399
400intvec* DIFF(ideal G)
401{
402  intvec  *v,*w;
403  poly p;
404  int s=idealSize(G);
405  int n=currRing->N;
406  int m=DIFFspy(G);
407  intvec* diffm=new intvec(m,n,0);
408  int j,l;
409  int inc=0;
410  for (j=1; j<=s; j++)
411  {
412    p=getNthPolyOfId(G,j);
413    v=leadExp(p);
414    pIter(p);
415    while(p!=NULL)
416    {
417      inc++;
418      intvec *lep=leadExp(p);
419      w=ivSub(v,lep /*leadExp(p)*/);
420      delete lep;
421      pIter(p);
422      for (l=1; l<=n; l++)
423      {
424        // setPosOfIM(diffm,inc,l,(*w)[l-1]);
425        IMATELEM(*diffm,inc,l) =(*w)[l-1] ;
426      }
427      delete w;
428    }
429    delete v;
430  }
431  return(diffm);
432}
433
434///////////////////////////////////////////////////////////////////
435
436
437///////////////////////////////////////////////////////////////////
438//gett64
439///////////////////////////////////////////////////////////////////
440//Description: returns the t corresponding to the vector listw
441//which contains a vector from the list returned by DIFF
442///////////////////////////////////////////////////////////////////
443//Uses: ivSize
444///////////////////////////////////////////////////////////////////
445
446void gett64(intvec* listw, int64vec* currw64, int64vec* targw64, int64 &tvec0, int64 &tvec1)
447{
448  int s=ivSize(listw);
449  int j;
450  int64 zaehler64=0;
451  int64 nenner64=0;
452  int64 temp1,temp2,temp3,temp4; //overflowstuff
453  for(j=1; j<=s; j++)
454  {
455
456    temp3=zaehler64;
457    temp1=((int64)((*listw)[j-1]));
458    temp2=((*currw64)[j-1]);
459    temp4=temp1*temp2;
460    zaehler64=temp3-temp4;
461
462    //overflow test
463    if(temp1!=0 && (temp4/temp1)!=temp2) overflow_error=3;
464
465    if( ( temp3<0 && temp4>0 ) || ( temp3>0 && temp4<0 ) )
466    {
467      int64 abs_t3=abs64(temp3);
468      if( (abs_t3+abs64(temp4))<abs_t3 ) overflow_error=4;
469    }
470
471    //overflow test
472    temp1=((*targw64)[j-1])-((*currw64)[j-1]);
473    //this subtraction can never yield an overflow since both number
474    //will always be positive
475    temp2=((int64)((*listw)[j-1]));
476    temp3=nenner64;
477    temp4=temp1*temp2;
478    nenner64=temp3+temp4;
479
480    //overflow test
481    if(temp1!=0 && ((temp1*temp2)/temp1)!=temp2) overflow_error=5;
482
483    if( (temp3>0 && temp4>0) ||
484      (temp3<0 && temp4<0)    )
485    {
486      int64 abs_t3=abs64(temp3);
487      if( (abs_t3+abs64(temp4))<abs_t3 )
488      {
489        overflow_error=6;
490      }
491    }
492  }
493
494  if (nenner64==0)
495  {
496    zaehler64=2;
497  }
498  else
499  {
500    if ( (zaehler64<=0) && (nenner64<0) )
501    {
502      zaehler64=-zaehler64;
503      nenner64=-nenner64;
504    }
505  }
506
507  int64 g=gcd64(zaehler64,nenner64);
508
509  tvec0=zaehler64/g;
510  tvec1=nenner64/g;
511
512}
513
514///////////////////////////////////////////////////////////////////
515
516
517///////////////////////////////////////////////////////////////////
518//nextt64
519///////////////////////////////////////////////////////////////////
520//Description: returns the t determining the next weight vector
521///////////////////////////////////////////////////////////////////
522//Uses:
523///////////////////////////////////////////////////////////////////
524
525void nextt64(ideal G,int64vec* currw64,int64vec* targw64, int64 & tvec0, int64 & tvec1)
526{
527  intvec* diffm=DIFF(G);
528  int s=diffm->rows();
529  tvec0=(int64)2;
530  tvec1=(int64)0;
531  intvec *tt;
532  for(int j=1; j<=s; j++)
533  {
534    tt=getNthRow(diffm,j);
535    int64 temptvec0, temptvec1;
536    gett64(tt,currw64,targw64,temptvec0, temptvec1);
537    delete tt;
538
539    //if tempt>0 both parts will be>0
540    if ( (temptvec1!=0) //that tempt is defined
541       &&
542       (temptvec0>0) && (temptvec1>0) //that tempt>0
543     )
544    {
545      if( ( (temptvec0) <= (temptvec1) ) //that tempt<=1
546        &&
547        ( ( (temptvec0) * (tvec1) ) <
548          ( (temptvec1) * (tvec0) ) )
549      )
550      {  //that tempt<t
551        tvec0=temptvec0;
552        tvec1=temptvec1;
553      }
554    }
555  }
556  delete diffm;
557  return;
558}
559
560///////////////////////////////////////////////////////////////////
561
562
563///////////////////////////////////////////////////////////////////
564//nextw64
565///////////////////////////////////////////////////////////////////
566//Uses:iv64Size,gcd,
567///////////////////////////////////////////////////////////////////
568
569int64vec* nextw64(int64vec* currw, int64vec* targw,
570                  int64 nexttvec0, int64 nexttvec1)
571{
572  //to do (targw-currw)*tvec[0]+currw*tvec[1]
573  int64vec* tempv;
574  int64vec* nextweight;
575  int64vec* a=iv64Sub(targw,currw);
576  //no overflow can occur since both are>=0
577
578  //to test overflow
579  tempv=iv64Copy(a);
580  *a *= (nexttvec0);
581  for(int i=0; i<currRing->N; i++)
582  {
583    if( (nexttvec0) !=0 &&
584        (((*a)[i])/(nexttvec0))!=((*tempv)[i]) )
585    {
586      overflow_error=7;
587      break;
588    }
589  }
590  delete tempv;
591  int64vec* b=currw;
592  tempv=iv64Copy(b);
593  *b *= (nexttvec1);
594  for(int i=0; i<currRing->N; i++)
595  {
596    if( (nexttvec1) !=0 &&
597        (((*b)[i])/(nexttvec1))!=((*tempv)[i]) )
598    {
599      overflow_error=8;
600      break;
601    }
602  }
603  delete tempv;
604  nextweight=iv64Add(a,b);
605
606  for(int i=0; i<currRing->N; i++){
607    if( (((*a)[i])>=0 && ((*b)[i])>=0) ||
608        (((*a)[i])<0 && ((*b)[i])<0) )
609    {
610      if( (abs64((*a)[i]))>abs64((*nextweight)[i]) ||
611          (abs64((*b)[i]))>abs64((*nextweight)[i])
612        )
613      {
614        overflow_error=9;
615        break;
616      }
617    }
618  }
619
620  //to reduce common factors of nextweight
621  int s=iv64Size(nextweight);
622  int64 g,temp;
623  g=(*nextweight)[0];
624  for (int i=1; i<s; i++)
625  {
626    temp=(*nextweight)[i];
627    g=gcd64(g,temp);
628    if (g==1) break;
629  }
630
631  if (g!=1) *nextweight /= g;
632
633  return(nextweight);
634}
635
636///////////////////////////////////////////////////////////////////
637
638
639
640///////////////////////////////////////////////////////////////////
641//FUNCTIONS NOT ORIGINATING FROM THE SINGULAR IMPLEMENTATION CODE
642///////////////////////////////////////////////////////////////////
643
644
645///////////////////////////////////////////////////////////////////
646//getNthPolyOfId
647///////////////////////////////////////////////////////////////////
648//Description: returns the nth poly of ideal I
649///////////////////////////////////////////////////////////////////
650poly getNthPolyOfId(ideal I,int n)
651{
652  if(0<n && n<=((int)I->ncols))
653  {
654    return (I->m)[n-1];
655  }
656  else
657  {
658    return(NULL);
659  }
660}
661
662///////////////////////////////////////////////////////////////////
663
664
665///////////////////////////////////////////////////////////////////
666//idealSize
667///////////////////////////////////////////////////////////////////
668//Description: returns the number of generator of input ideal I
669///////////////////////////////////////////////////////////////////
670//Uses: none
671///////////////////////////////////////////////////////////////////
672// #define idealSize(I) IDELEMS(I)
673///////////////////////////////////////////////////////////////////
674
675
676///////////////////////////////////////////////////////////////////
677//ivSize
678///////////////////////////////////////////////////////////////////
679//Description: returns the number of entries of v
680///////////////////////////////////////////////////////////////////
681//Uses: none
682///////////////////////////////////////////////////////////////////
683
684// inline int ivSize(intvec* v){ return((v->rows())*(v->cols())); }
685
686///////////////////////////////////////////////////////////////////
687
688
689///////////////////////////////////////////////////////////////////
690//iv64Size
691///////////////////////////////////////////////////////////////////
692//Description: returns the number of entries of v
693///////////////////////////////////////////////////////////////////
694//Uses: none
695///////////////////////////////////////////////////////////////////
696
697// int iv64Size(int64vec* v){ return((v->rows())*(v->cols())); }
698
699///////////////////////////////////////////////////////////////////
700
701
702///////////////////////////////////////////////////////////////////
703//leadExp
704///////////////////////////////////////////////////////////////////
705//Description: returns an intvec containg the exponet vector of p
706///////////////////////////////////////////////////////////////////
707//Uses: sizeof,omAlloc,omFree
708///////////////////////////////////////////////////////////////////
709
710intvec* leadExp(poly p)
711{
712  int N=currRing->N;
713  int *e=(int*)omAlloc((N+1)*sizeof(int));
714  pGetExpV(p,e);
715  intvec* iv=new intvec(N);
716  for(int i=N;i>0;i--) { (*iv)[i-1]=e[i];}
717  omFree(e);
718  return(iv);
719}
720
721///////////////////////////////////////////////////////////////////
722
723
724///////////////////////////////////////////////////////////////////
725//leadExp64
726///////////////////////////////////////////////////////////////////
727//Description: returns an int64vec containing the exponent
728//vector of p
729///////////////////////////////////////////////////////////////////
730//Uses: sizeof,omAlloc,omFree
731///////////////////////////////////////////////////////////////////
732
733int64vec* leadExp64(poly p)
734{
735  int N=currRing->N;
736  int *e=(int*)omAlloc((N+1)*sizeof(int));
737  pGetExpV(p,e);
738  int64vec* iv64=new int64vec(N);
739  for(int i=N;i>0;i--) { (*iv64)[i-1]=(int64)e[i];}
740  omFree(e);
741  return(iv64);
742}
743
744///////////////////////////////////////////////////////////////////
745
746
747///////////////////////////////////////////////////////////////////
748//setPosOfIM
749///////////////////////////////////////////////////////////////////
750//Description: sets entry i,j of im to val
751///////////////////////////////////////////////////////////////////
752//Uses: none
753///////////////////////////////////////////////////////////////////
754
755//void setPosOfIM(intvec* im,int i,int j,int val){
756//  int r=im->rows();
757//  int c=im->cols();
758//  if( (0<i && i<=r) && (0<j && j<=c) ){
759//    (*im)[(i-1)*c+j-1]=val;
760//    }
761//  return;
762//}
763
764///////////////////////////////////////////////////////////////////
765
766
767///////////////////////////////////////////////////////////////////
768//scalarProduct
769///////////////////////////////////////////////////////////////////
770//Description: returns the scalar product of intvecs a and b
771///////////////////////////////////////////////////////////////////
772//Uses: none
773///////////////////////////////////////////////////////////////////
774
775static inline long  scalarProduct(intvec* a, intvec* b)
776{
777  assume( a->length() ==  b->length());
778  int i, n = a->length();
779  long result = 0;
780  for(i=n-1; i>=0; i--)
781    result += (*a)[i] * (*b)[i];
782  return result;
783}
784
785///////////////////////////////////////////////////////////////////
786
787
788///////////////////////////////////////////////////////////////////
789//scalarProduct64
790///////////////////////////////////////////////////////////////////
791//Description: returns the scalar product of int64vecs a and b
792///////////////////////////////////////////////////////////////////
793//Assume: Overflow tests assumes input has nonnegative entries
794///////////////////////////////////////////////////////////////////
795//Uses: none
796///////////////////////////////////////////////////////////////////
797
798static inline int64  scalarProduct64(int64vec* a, int64vec* b)
799{
800  assume( a->length() ==  b->length());
801  int i, n = a->length();
802  int64 result = 0;
803  int64 temp1,temp2;
804  for(i=n-1; i>=0; i--)
805  {
806    temp1=(*a)[i] * (*b)[i];
807    if((*a)[i]!=0 && (temp1/(*a)[i])!=(*b)[i]) overflow_error=1;
808
809    temp2=result;
810    result += temp1;
811
812    //since both vectors always have nonnegative entries in init64
813    //result should be >=temp2
814    if(temp2>result) overflow_error=2;
815  }
816
817  return result;
818}
819
820///////////////////////////////////////////////////////////////////
821
822
823///////////////////////////////////////////////////////////////////
824//gcd
825///////////////////////////////////////////////////////////////////
826//Description: returns the gcd of a and b
827///////////////////////////////////////////////////////////////////
828//Uses: none
829///////////////////////////////////////////////////////////////////
830
831int gcd(int a, int b)
832{
833  int r, p0 = a, p1 = b;
834  if(p0 < 0)
835    p0 = -p0;
836
837  if(p1 < 0)
838    p1 = -p1;
839  while(p1 != 0)
840  {
841    r = p0 % p1;
842    p0 = p1;
843    p1 = r;
844  }
845  return p0;
846}
847
848///////////////////////////////////////////////////////////////////
849
850
851///////////////////////////////////////////////////////////////////
852//gcd64
853///////////////////////////////////////////////////////////////////
854//Description: returns the gcd of a and b
855///////////////////////////////////////////////////////////////////
856//Uses: none
857///////////////////////////////////////////////////////////////////
858
859int64 gcd64(int64 a, int64 b)
860{
861  int64 r, p0 = a, p1 = b;
862  if(p0 < 0)
863    p0 = -p0;
864
865  if(p1 < 0)
866    p1 = -p1;
867
868  while(p1 != ((int64)0) )
869  {
870    r = p0 % p1;
871    p0 = p1;
872    p1 = r;
873  }
874
875  return p0;
876}
877
878///////////////////////////////////////////////////////////////////
879
880
881///////////////////////////////////////////////////////////////////
882//abs64
883///////////////////////////////////////////////////////////////////
884//Description: returns the absolute value of int64 i
885///////////////////////////////////////////////////////////////////
886//Uses: none
887///////////////////////////////////////////////////////////////////
888
889//int64 abs64(int64 i)
890//{
891//  if(i>=0)
892//  return(i);
893//else
894//  return((-i));
895//}
896
897///////////////////////////////////////////////////////////////////
898
899
900///////////////////////////////////////////////////////////////////
901//makeTaunList64
902///////////////////////////////////////////////////////////////////
903//Description: makes a list of an int64vec and an int64
904///////////////////////////////////////////////////////////////////
905//Uses: omAllocBin
906///////////////////////////////////////////////////////////////////
907
908#if 0
909lists makeTaunList64(int64vec *iv64,int64 i64)
910{
911  lists l=(lists)omAllocBin(slists_bin);
912  l->Init(2);
913  l->m[0].rtyp=INTVEC_CMD;
914  l->m[1].rtyp=INT_CMD;
915  l->m[0].data=(void *)iv64;
916  l->m[1].data=(void *)i64;
917  return l;
918}
919#endif
920
921///////////////////////////////////////////////////////////////////
922
923
924///////////////////////////////////////////////////////////////////
925//idStd
926///////////////////////////////////////////////////////////////////
927//Description: returns the GB of G calculated w.r.t. the order of
928//currRing
929///////////////////////////////////////////////////////////////////
930//Uses: kStd,idSkipZeroes
931///////////////////////////////////////////////////////////////////
932
933ideal idStd(ideal G)
934{
935  ideal GG = kStd(G, NULL, testHomog, NULL);
936  idSkipZeroes(GG);
937  return GG;
938}
939
940///////////////////////////////////////////////////////////////////
941
942
943///////////////////////////////////////////////////////////////////
944//idInterRed
945///////////////////////////////////////////////////////////////////
946//Description: returns the interreduction of G
947///////////////////////////////////////////////////////////////////
948//Assumes: that the input is a GB
949///////////////////////////////////////////////////////////////////
950//Uses: kInterRed,idSkipZeroes
951///////////////////////////////////////////////////////////////////
952
953ideal idInterRed(ideal G)
954{
955  assume(G != NULL);
956
957  ideal GG = kInterRed(G, NULL);
958  idDelete(&G);
959  return GG;
960}
961
962///////////////////////////////////////////////////////////////////
963
964
965///////////////////////////////////////////////////////////////////
966//matIdLift
967///////////////////////////////////////////////////////////////////
968//Description: yields the same reslut as lift in Singular
969///////////////////////////////////////////////////////////////////
970//Uses: idLift,idModule2formatedMatrix
971///////////////////////////////////////////////////////////////////
972
973matrix matIdLift(ideal Gomega, ideal M)
974{
975  ideal Mtmp = idLift(Gomega, M, NULL, FALSE, FALSE, TRUE, NULL);
976  int rows=IDELEMS(Gomega);
977  int cols=IDELEMS(Mtmp);
978  matrix res=idModule2formatedMatrix(Mtmp,rows,cols);
979  return res;
980}
981///////////////////////////////////////////////////////////////////
982
983
984///////////////////////////////////////////////////////////////////
985//rCopyAndChangeA
986///////////////////////////////////////////////////////////////////
987//Description: changes currRing to a copy of currRing with the
988//int64vec w instead of the old one
989///////////////////////////////////////////////////////////////////
990//Assumes: that currRing is alterd as mentioned in rCopy0AndAddA
991///////////////////////////////////////////////////////////////////
992//Uses: rCopy0,rComplete,rChangeCurrRing,rSetWeightVec
993///////////////////////////////////////////////////////////////////
994
995void rCopyAndChangeA(int64vec* w)
996{
997  ring rnew=rCopy0(currRing);
998  rComplete(rnew);
999  rSetWeightVec(rnew,w->iv64GetVec());
1000  rChangeCurrRing(rnew);
1001}
1002
1003///////////////////////////////////////////////////////////////////
1004
1005
1006///////////////////////////////////////////////////////////////////
1007//rCopy0AndAddA
1008///////////////////////////////////////////////////////////////////
1009//Description: returns a copy of currRing with the
1010//int64vec wv64 added in front of the current order
1011//i.e. if old order was O, then the new one will be (A(w),O)
1012///////////////////////////////////////////////////////////////////
1013//Uses: omAllocBin,memcpy4,sizeof,nCopy,omStrDup,omMemDup,
1014//idrCopyR_NoSort
1015///////////////////////////////////////////////////////////////////
1016
1017ring rCopy0AndAddA(ring r, int64vec *wv64, BOOLEAN copy_qideal,
1018                   BOOLEAN copy_ordering)
1019{
1020
1021  if (r == NULL) return NULL;
1022  int i,j;
1023  ring res=(ring)omAllocBin(ip_sring_bin);
1024
1025  memcpy4(res,r,sizeof(ip_sring));
1026  res->VarOffset = NULL;
1027  res->ref=0;
1028  if (r->algring!=NULL)
1029    r->algring->ref++;
1030  if (r->parameter!=NULL)
1031  {
1032    res->minpoly=nCopy(r->minpoly);
1033    int l=rPar(r);
1034    res->parameter=(char **)omAlloc(l*sizeof(char_ptr));
1035    int i;
1036    for(i=0;i<rPar(r);i++)
1037    {
1038      res->parameter[i]=omStrDup(r->parameter[i]);
1039    }
1040  }
1041
1042  i=rBlocks(r);
1043  res->wvhdl   = (int **)omAlloc((i+1) * sizeof(int_ptr));
1044  res->order   = (int *) omAlloc((i+1) * sizeof(int));
1045  res->block0  = (int *) omAlloc((i+1) * sizeof(int));
1046  res->block1  = (int *) omAlloc((i+1) * sizeof(int));
1047  for (j=0; j<i; j++)
1048  {
1049    if (r->wvhdl[j]!=NULL)
1050    {
1051      res->wvhdl[j+1] = (int*) omMemDup(r->wvhdl[j]);
1052    }
1053    else
1054      res->wvhdl[j+1]=NULL;
1055  }
1056  for (j=0;j<i;j++)
1057  {
1058    res->order[j+1]=r->order[j];
1059    res->block0[j+1]=r->block0[j];
1060    res->block1[j+1]=r->block1[j];
1061  }
1062
1063 //the added A
1064  res->order[0]=ringorder_a64;
1065  int length=wv64->rows();
1066  int64 *A=(int64 *)omAlloc(length*sizeof(int64));
1067  for(j=0;j<length;j++)
1068  {
1069    A[j]=(*wv64)[j];
1070  }
1071  res->wvhdl[0]=(int *)A;
1072  res->block0[0]=1;
1073  res->block1[0]=length;
1074
1075  res->names   = (char **)omAlloc0(r->N * sizeof(char_ptr));
1076  for (i=0; i<res->N; i++)
1077  {
1078    res->names[i] = omStrDup(r->names[i]);
1079  }
1080  res->idroot = NULL;
1081  if (r->qideal!=NULL)
1082  {
1083    if (copy_qideal) res->qideal= idrCopyR_NoSort(r->qideal, r);
1084    else res->qideal = NULL;
1085  }
1086
1087  return res;
1088}
1089
1090///////////////////////////////////////////////////////////////////
1091
1092
1093///////////////////////////////////////////////////////////////////
1094//rGetGlobalOrderMatrix
1095///////////////////////////////////////////////////////////////////
1096//Description: returns a matrix corresponding to the order of r
1097///////////////////////////////////////////////////////////////////
1098//Assumes: the order of r is a combination of the orders M,lp,dp,
1099//Dp,wp,Wp,C
1100///////////////////////////////////////////////////////////////////
1101//Uses: none
1102///////////////////////////////////////////////////////////////////
1103
1104int64vec* rGetGlobalOrderMatrix(ring r)
1105{
1106  int n=r->N;
1107  int64vec* res=new int64vec(n,n,(int64)0);
1108  if (r->OrdSgn != 1) return res;
1109  int pos1=0;
1110  int pos2=0;
1111  int temp;
1112  int i=0;
1113  while(r->order[i]!=0 && pos2<n)
1114  {
1115    pos2=pos2+r->block1[i] - r->block0[i];
1116
1117    if(r->order[i]==ringorder_lp)
1118    {
1119      temp=pos1;
1120      for(int j=pos1; j<=pos2; j++)
1121        (*res)[j*n+j]=(int64)1;
1122    }
1123    else if(r->order[i]==ringorder_dp)
1124    {
1125      for(int j=pos1;j<=pos2;j++)
1126        (*res)[pos1*n+j]=(int64)1;
1127      for(int j=1;j<=(pos2-pos1);j++)
1128        (*res)[(pos1+j)*n+(pos2+1-j)]=(int64)-1;
1129    }
1130    else if(r->order[i]==ringorder_Dp)
1131    {
1132      for(int j=pos1;j<=pos2;j++)
1133        (*res)[pos1*n+j]=(int64)1;
1134      for(int j=1;j<=(pos2-pos1);j++)
1135        (*res)[(pos1+j)*n+(pos1+j-1)]=(int64)1;
1136    }
1137    else if(r->order[i]==ringorder_wp)
1138    {
1139      int* weights=r->wvhdl[i];
1140      for(int j=pos1;j<=pos2;j++)
1141        (*res)[pos1*n+j]=(int64)weights[j-pos1];
1142      for(int j=1;j<=(pos2-pos1);j++)
1143        (*res)[(pos1+j)*n+(pos2+1-j)]=(int64)-1;
1144    }
1145    else if(r->order[i]==ringorder_Wp)
1146    {
1147      int* weights=r->wvhdl[i];
1148      for(int j=pos1;j<=pos2;j++)
1149        (*res)[pos1*n+j]=(int64)weights[j-pos1];
1150      for(int j=1;j<=(pos2-pos1);j++)
1151        (*res)[(pos1+j)*n+(pos1+j-1)]=(int64)1;
1152    }
1153
1154    else if(r->order[0]==ringorder_M)
1155    {
1156      int* weights=r->wvhdl[0];
1157      for(int j=pos1;j<((pos2+1)*(pos2+1));j++)
1158        (*res)[j]=(int64)weights[j];
1159    }
1160
1161    pos1=pos2+1;
1162    pos2=pos2+1;
1163    i++;
1164  }
1165
1166  return(res);
1167}
1168
1169///////////////////////////////////////////////////////////////////
1170
1171
1172///////////////////////////////////////////////////////////////////
1173//rGetGlobalOrderWeightVec
1174///////////////////////////////////////////////////////////////////
1175//Description: returns a weight vector corresponding to the first
1176//row of a matrix corresponding to the order of r
1177///////////////////////////////////////////////////////////////////
1178//Uses: none
1179///////////////////////////////////////////////////////////////////
1180
1181int64vec* rGetGlobalOrderWeightVec(ring r)
1182{
1183  int n=r->N;
1184  int64vec* res=new int64vec(n);
1185
1186  if (r->OrdSgn != 1) return res;
1187
1188  int length;
1189
1190  if(r->order[0]==ringorder_lp)
1191  {
1192    (*res)[0]=(int64)1;
1193  }
1194  else if( (r->order[0]==ringorder_dp) || (r->order[0]==ringorder_Dp) )
1195  {
1196    length=r->block1[0] - r->block0[0];
1197    for (int j=0;j<=length;j++)
1198      (*res)[j]=(int64)1;
1199  }
1200  else if( (r->order[0]==ringorder_wp) || (r->order[0]==ringorder_Wp) ||
1201      (r->order[0]==ringorder_a)  || (r->order[0]==ringorder_M)    )
1202  {
1203    int* weights=r->wvhdl[0];
1204    length=r->block1[0] - r->block0[0];
1205    for (int j=0;j<=length;j++)
1206      (*res)[j]=(int64)weights[j];
1207  }
1208  else if( (r->order[0]==ringorder_a64) )
1209  {
1210    int64* weights=(int64*)r->wvhdl[0];
1211    length=r->block1[0] - r->block0[0];
1212    for (int j=0;j<=length;j++)
1213      (*res)[j]=weights[j];
1214  }
1215
1216  return(res);
1217}
1218
1219///////////////////////////////////////////////////////////////////
1220
1221
1222///////////////////////////////////////////////////////////////////
1223//sortRedSB
1224///////////////////////////////////////////////////////////////////
1225//Description: sorts a reduced GB of an ideal after the leading
1226//terms of the polynomials with the smallest one first
1227///////////////////////////////////////////////////////////////////
1228//Assumes: that the given input is a minimal GB
1229///////////////////////////////////////////////////////////////////
1230//Uses:idealSize,idCopy,pLmCmp
1231///////////////////////////////////////////////////////////////////
1232
1233ideal sortRedSB(ideal G)
1234{
1235  int s=idealSize(G);
1236  poly* m=G->m;
1237  poly p,q;
1238  for (int i=0; i<(s-1); i++)
1239  {
1240    for (int j=0; j<((s-1)-i); j++)
1241    {
1242      p=m[j];
1243      q=m[j+1];
1244      if (pLmCmp(p,q)==1)
1245      {
1246        m[j+1]=p;
1247        m[j]=q;
1248      }
1249    }
1250  }
1251  return(G);
1252}
1253
1254///////////////////////////////////////////////////////////////////
1255
1256
1257///////////////////////////////////////////////////////////////////
1258//int64VecToIntVec
1259///////////////////////////////////////////////////////////////////
1260//Description: converts an int64vec to an intvec
1261// deletes the input
1262///////////////////////////////////////////////////////////////////
1263//Assumes: that the int64vec contains no entries larger than 2^32
1264///////////////////////////////////////////////////////////////////
1265//Uses: none
1266///////////////////////////////////////////////////////////////////
1267
1268intvec* int64VecToIntVec(int64vec* source)
1269{
1270  int r=source->rows();
1271  int c=source->cols();
1272  intvec* res=new intvec(r,c,0);
1273  for(int i=0;i<r;i++){
1274    for(int j=0;j<c;j++){
1275      (*res)[i*c+j]=(int)(*source)[i*c+j];
1276    }
1277  }
1278  delete source;
1279  return(res);
1280}
1281
1282///////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.