source: git/Singular/LIB/poly.lib @ 667ba1

fieker-DuValspielwiese
Last change on this file since 667ba1 was 1cbb1f4, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
(Trac Ticket #389) transfer denominator() and numerator() from poly.lib to the Singular kernel ADD: new kernel functions denominator& numerator: number -> number CHG: removed denominator& numerator from poly.lib ADD: added documentation for these new kernel functions FIX: updated tests (Manual/listvar.tst)
  • Property mode set to 100644
File size: 33.4 KB
Line 
1///////////////////////////////////////////////////////////////////////////////
2version="$Id$";
3category="General purpose";
4info="
5LIBRARY:  poly.lib      Procedures for Manipulating Polys, Ideals, Modules
6AUTHORS:  O. Bachmann, G.-M. Greuel, A. Fruehbis
7
8PROCEDURES:
9 cyclic(int);           ideal of cyclic n-roots
10 elemSymmId(int);       ideal of elementary symmetric polynomials
11 katsura([i]);          katsura [i] ideal
12 freerank(poly/...)     rank of coker(input) if coker is free else -1
13 is_zero(poly/...);     int, =1 resp. =0 if coker(input) is 0 resp. not
14 lcm(ideal);            lcm of given generators of ideal
15 maxcoef(poly/...);     maximal length of coefficient occurring in poly/...
16 maxdeg(poly/...);      int/intmat = degree/s of terms of maximal order
17 maxdeg1(poly/...);     int = [weighted] maximal degree of input
18 mindeg(poly/...);      int/intmat = degree/s of terms of minimal order
19 mindeg1(poly/...);     int = [weighted] minimal degree of input
20 normalize(poly/...);   normalize poly/... such that leading coefficient is 1
21 rad_con(p,I);          check radical containment of polynomial p in ideal I
22 content(f);            content of polynomial/vector f
23 mod2id(M,iv);          conversion of a module M to an ideal
24 id2mod(i,iv);          conversion inverse to mod2id
25 substitute(I,...)      substitute in I variables by polynomials
26 subrInterred(i1,i2,iv);interred w.r.t. a subset of variables
27 newtonDiag(f);         Newton diagram of a polynomial
28 hilbPoly(I);           Hilbert polynomial of basering/I
29          (parameters in square brackets [] are optional)
30
31";
32
33LIB "general.lib";
34LIB "ring.lib";
35///////////////////////////////////////////////////////////////////////////////
36static proc bino(int a, int b)
37{
38//computes binomial var(1)+a over b
39   int i;
40   if(b==0){return(1);}
41   poly p=(var(1)+a)/b;
42   for(i=1;i<=b-1;i++)
43   {
44      p=p*(var(1)+a-i)/i;
45   }
46   return(p);
47}
48
49proc hilbPoly(ideal I)
50"USAGE: hilbPoly(I); I a homogeneous ideal
51RETURN: the Hilbert polynomial of basering/I as an intvec v=v_0,...,v_r
52        such that the Hilbert polynomial is (v_0+v_1*t+...v_r*t^r)/r!
53EXAMPLE: example hilbPoly; shows an example
54"
55{
56   def R=basering;
57   if(!attrib(I,"isSB")){I=std(I);}
58   intvec v=hilb(I,2);
59   int s=dim(I);
60   intvec hp;
61   if(s==0){return(hp);}
62   int d=size(v)-2;
63   ring S=0,t,dp;
64   poly p=v[1+d]*bino(s-1-d,s-1);
65   int i;
66   for(i=1;i<=d;i++)
67   {
68      p=p+v[d-i+1]*bino(s-1-d+i,s-1);
69   }
70   int n=1;
71   for(i=2;i<=s-1;i++){n=n*i;}
72   p=n*p;
73   for(i=1;i<=s;i++)
74   {
75      hp[i]=int(leadcoef(p[s-i+1]));
76   }
77   setring R;
78   return(hp);
79}
80example
81{ "EXAMPLE:"; echo = 2;
82   ring r = 0,(b,c,t,h),dp;
83   ideal I=
84   bct-t2h+2th2+h3,
85   bt3-ct3-t4+b2th+c2th-2bt2h+2ct2h+2t3h-bch2-2bth2+2cth2+2th3,
86   b2c2+bt2h-ct2h-t3h+b2h2+2bch2+c2h2-2bth2+2cth2+t2h2-2bh3+2ch3+2th3+3h4,
87   c2t3+ct4-c3th-2c2t2h-2ct3h-t4h+bc2h2-2c2th2-bt2h2+4t3h2+2bth3-2cth3-t2h3
88   +bh4-6th4-2h5;
89   hilbPoly(I);
90}
91
92///////////////////////////////////////////////////////////////////////////////
93proc substitute (I,list #)
94"USAGE:  - case 1: typeof(#[1])==poly:
95           substitute (I,v,f[,v1,f1,v2,f2,...]); I object of basering which
96           can be mapped, v,v1,v2,.. ring variables, f,f1,f2,... poly
97@*       - case 2: typeof(#[1])==ideal:
98           substitute (I,v,f); I object of basering which can be mapped,
99           v ideal of ring variables, f ideal
100RETURN:  object of same type as I,
101@*       - case 1: ring variable v,v1,v2,... substituted by polynomials
102           f,f1,f2,..., in this order
103@*       - case 2: ring variables in v substituted by polynomials in f:
104           v[i] is substituted by f[i], i=1,...,i=min(size(v),ncols(f))
105NOTE:    this procedure extends the built-in command subst via maps
106EXAMPLE: example substitute; shows an example
107"
108{
109  def bas = basering;
110  ideal m = maxideal(1);
111  int i,ii;
112  if(typeof(#[1])=="poly")
113  {
114    poly v = #[1];
115    poly f = #[2];
116    map phi = bas,m;
117    def J = I;
118    for (ii=1; ii<=size(#) - 1; ii=ii+2)
119    {
120      m = maxideal(1);
121      i=rvar(#[ii]);
122      m[i] = #[ii+1];
123      phi = bas,m;
124      J = phi(J);
125    }
126    return(J);
127  }
128  if(typeof(#[1])=="ideal")
129  {
130    ideal v = #[1];
131    ideal f = #[2];
132    int mi = ncols(v);
133    if(ncols(f)<mi)
134    {
135      mi = ncols(f);
136    }
137    def J = I;
138    for (ii=1; ii<=mi; ii++)
139    {
140      m[rvar(v[ii])]=f[ii];
141    }
142    map phi = bas,m;
143    J = phi(I);
144    return(J);
145  }
146}
147example
148{ "EXAMPLE:"; echo = 2;
149   ring r = 0,(b,c,t),dp;
150   ideal I = -bc+4b2c2t,bc2t-5b2c;
151   substitute(I,c,b+c,t,0,b,b-1);
152   ideal v = c,t,b;
153   ideal f = b+c,0,b-1;
154   substitute(I,v,f);
155}
156
157///////////////////////////////////////////////////////////////////////////////
158proc cyclic (int n)
159"USAGE:   cyclic(n);  n integer
160RETURN:  ideal of cyclic n-roots from 1-st n variables of basering
161EXAMPLE: example cyclic; shows examples
162"
163{
164//----------------------------- procedure body --------------------------------
165   ideal m = maxideal(1);
166   m = m[1..n],m[1..n];
167   int i,j;
168   ideal s; poly t;
169   for ( j=0; j<=n-2; j++ )
170   {
171      t=0;
172      for( i=1;i<=n;i++ ) { t=t+product(m,i..i+j); }
173      s=s+t;
174   }
175   s=s,product(m,1..n)-1;
176   return (s);
177}
178//-------------------------------- examples -----------------------------------
179example
180{ "EXAMPLE:"; echo = 2;
181   ring r=0,(u,v,w,x,y,z),lp;
182   cyclic(nvars(basering));
183   homog(cyclic(5),z);
184}
185
186///////////////////////////////////////////////////////////////////////////////
187proc elemSymmPoly(int d, int lindex, int hindex)
188"USAGE:  elemSymmPoly(d,lindex,hindex); d,lindex,hindex integers
189RETURN:  elementary symmetric polynomial of degree d for variables
190@*       var(lindex),...,var(hindex) of basering
191EXAMPLE: example elemSymmPoly; shows an example
192"
193{
194 if(hindex - lindex + 1 < d)
195 {
196   int i = hindex - lindex + 1;
197   "========================================================";
198   "There is no elementary symmetric polynomial of degree "+string(d);
199   "for just "+string(i)+" variables.";
200   "========================================================";
201   return(poly(0));
202 }
203 if(d == 0)
204 {
205   return(poly(1));
206 }
207 else
208 {
209   int i;
210   poly p;
211   for (i = lindex; i <=  hindex - d + 1; i++)
212   {
213     p = p + var(i) * elemSymmPoly(d - 1, i + 1, hindex);
214   }
215   return(p);
216 }
217}
218example
219{ "EXAMPLE:"; echo = 2;
220  ring R = 0, (u,v,w,x,y,z), lp;
221  elemSymmPoly(3,2,5);
222  elemSymmPoly(6,1,6);
223}
224
225//////////////////////////////////////////////////////////////////////////
226proc elemSymmId(int n)
227"USAGE:  elemSymmId(n); n integer
228RETURN:  ideal of elementary symmetric polynomials for 1-st n
229@*       variables of basering
230EXAMPLE: example elemSymmId; shows an example
231{
232 int i;
233 ideal symm;
234 for(i = 1; i <= n; i++)
235 {
236   symm = symm + elemSymmPoly(i, 1, n);
237 }
238 return(symm);
239}
240example
241{ "EXAMPLE:"; echo = 2;
242  ring R = 0, (v,w,x,y,z), lp;
243  elemSymmId(3);
244  elemSymmId(nvars(basering));
245}
246
247//////////////////////////////////////////////////////////////////////////
248proc katsura
249"USAGE: katsura([n]); n integer
250RETURN: katsura(n) : n-th katsura ideal of
251         (1) newly created and set ring (32003, x(0..n), dp), if
252             nvars(basering) < n
253         (2) basering, if nvars(basering) >= n
254        katsura()  : katsura ideal of basering
255EXAMPLE: example katsura; shows examples
256"
257{
258  int n;
259  if ( size(#) == 1 && typeof(#[1]) == "int")
260  {
261    n = #[1] - 1;
262    while (1)
263    {
264      if (defined(basering))
265      {
266        if (nvars(basering) >= #[1]) {break;}
267      }
268      ring katsura_ring = 32003, x(0..#[1]), dp;
269      keepring katsura_ring;
270      break;
271    }
272  }
273  else
274  {
275    n = nvars(basering) -1;
276  }
277
278  ideal s;
279  int i, j;
280  poly p;
281
282  p = -1;
283  for (i = -n; i <= n; i++)
284  {
285    p = p + kat_var(i, n);
286  }
287  s[1] = p;
288
289  for (i = 0; i < n; i++)
290  {
291    p = -1 * kat_var(i,n);
292    for (j = -n; j <= n; j++)
293    {
294      p = p + kat_var(j,n) * kat_var(i-j, n);
295    }
296    s = s,p;
297  }
298  return (s);
299}
300//-------------------------------- examples -----------------------------------
301example
302{
303  "EXAMPLE:"; echo = 2;
304  ring r; basering;
305  katsura();
306  katsura(4); basering;
307}
308
309proc kat_var(int i, int n)
310{
311  poly p;
312  if (i < 0)  { i = -i;}
313  if (i <= n) { p = var(i+1); }
314  return (p);
315}
316///////////////////////////////////////////////////////////////////////////////
317
318proc freerank
319"USAGE:   freerank(M[,any]);  M=poly/ideal/vector/module/matrix
320COMPUTE: rank of module presented by M in case it is free.
321         By definition this is vdim(coker(M)/m*coker(M)) if coker(M)
322         is free, where m is the maximal ideal of the variables of the
323         basering and M is considered to be a matrix.
324         (the 0-module is free of rank 0)
325RETURN:  rank of coker(M) if coker(M) is free and -1 else;
326         in case of a second argument return a list:
327                L[1] = rank of coker(M) or -1
328                L[2] = minbase(M)
329NOTE:    freerank(syz(M)); computes the rank of M if M is free (and -1 else)
330EXAMPLE: example freerank; shows examples
331"
332{
333  int rk;
334  def M = simplify(#[1],10);
335  resolution mre = res(M,2);
336  intmat B = betti(mre);
337  if ( ncols(B)>1 ) { rk = -1; }
338  else { rk = sum(B[1..nrows(B),1]); }
339  if (size(#) == 2) { list L=rk,mre[1]; return(L);}
340  return(rk);
341}
342example
343{"EXAMPLE";   echo=2;
344  ring r;
345  ideal i=x;
346  module M=[x,0,1],[-x,0,-1];
347  freerank(M);          // should be 2, coker(M) is not free
348  freerank(syz (M),"");
349  // [1] should be 1, coker(syz(M))=M is free of rank 1
350  // [2] should be gen(2)+gen(1) (minimal relation of M)
351  freerank(i);
352  freerank(syz(i));     // should be 1, coker(syz(i))=i is free of rank 1
353}
354///////////////////////////////////////////////////////////////////////////////
355
356proc is_zero
357"USAGE:   is_zero(M[,any]); M=poly/ideal/vector/module/matrix
358RETURN:  integer, 1 if coker(M)=0 resp. 0 if coker(M)!=0, where M is
359         considered as matrix.
360         If a second argument is given, return a list:
361                L[1] = 1 if coker(M)=0 resp. 0 if coker(M)!=0
362                L[2] = dim(M)
363EXAMPLE: example is_zero; shows examples
364"
365{
366  int d=dim(std(#[1]));
367  int a = ( d==-1 );
368  if( size(#) >1 ) { return(list(a,d)); }
369  return(a);
370}
371example
372{ "EXAMPLE:";   echo=2;
373  ring r;
374  module m = [x],[y],[1,z];
375  is_zero(m,1);
376  qring q = std(ideal(x2+y3+z2));
377  ideal j = x2+y3+z2-37;
378  is_zero(j);
379}
380///////////////////////////////////////////////////////////////////////////////
381
382proc maxcoef (f)
383"USAGE:   maxcoef(f);  f  poly/ideal/vector/module/matrix
384RETURN:  maximal length of coefficient of f of type int (by measuring the
385         length of the string of each coefficient)
386EXAMPLE: example maxcoef; shows examples
387"
388{
389//----------------------------- procedure body --------------------------------
390   int max,s,ii,jj; string t;
391   ideal i = ideal(matrix(f));
392   i = simplify(i,6);            // delete 0's and keep first of equal elements
393   poly m = var(1); matrix C;
394   for (ii=2;ii<=nvars(basering);ii++) { m = m*var(ii); }
395   for (ii=1; ii<=size(i); ii++)
396   {
397      C = coef(i[ii],m);
398      for (jj=1; jj<=ncols(C); jj++)
399      {
400         t = string(C[2,jj]);  s = size(t);
401         if ( t[1] == "-" ) { s = s - 1; }
402         if ( s > max ) { max = s; }
403      }
404   }
405   return(max);
406}
407//-------------------------------- examples -----------------------------------
408example
409{ "EXAMPLE:"; echo = 2;
410   ring r= 0,(x,y,z),ds;
411   poly g = 345x2-1234567890y+7/4z;
412   maxcoef(g);
413   ideal i = g,10/1234567890;
414   maxcoef(i);
415   // since i[2]=1/123456789
416}
417///////////////////////////////////////////////////////////////////////////////
418
419proc maxdeg (id)
420"USAGE:   maxdeg(id);  id  poly/ideal/vector/module/matrix
421RETURN:  int/intmat, each component equals maximal degree of monomials in the
422         corresponding component of id, independent of ring ordering
423         (maxdeg of each var is 1).
424         Of type int, if id is of type poly; of type intmat otherwise
425SEE ALSO: maxdeg1
426EXAMPLE: example maxdeg; shows examples
427"
428{
429   //-------- subprocedure to find maximal degree of given component ----------
430   proc findmaxdeg
431   {
432      poly c = #[1];
433      if (c==0) { return(-1); }
434   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
435      int d = (deg(c)>=0)*deg(c)-(deg(c)<0)*deg(c);
436      int i = d;
437      while ( c-jet(c,i) != 0 ) { i = 2*(i+1); }
438      int o = i-1;
439      int u = (d != i)*((i div  2)-1);
440   //----------------------- "quick search" for maxdeg ------------------------
441      while ( (c-jet(c,i)==0)*(c-jet(c,i-1)!=0) == 0)
442      {
443         i = (o+1+u) div  2;
444         if (c-jet(c,i)!=0) { u = i+1; }
445         else { o = i-1; }
446      }
447      return(i);
448   }
449//------------------------------ main program ---------------------------------
450   matrix M = matrix(id);
451   int r,c = nrows(M), ncols(M); int i,j;
452   intmat m[r][c];
453   for (i=r; i>0; i--)
454   {
455      for (j=c; j>0; j--) { m[i,j] = findmaxdeg(M[i,j]); }
456   }
457   if (typeof(id)=="poly") { return(m[1,1]); }
458   return(m);
459}
460//-------------------------------- examples -----------------------------------
461example
462{ "EXAMPLE:"; echo = 2;
463   ring r = 0,(x,y,z),wp(1,2,3);
464   poly f = x+y2+z3;
465   deg(f);             //deg; returns weighted degree (in case of 1 block)!
466   maxdeg(f);
467   matrix m[2][2]=f+x10,1,0,f^2;
468   maxdeg(m);
469}
470///////////////////////////////////////////////////////////////////////////////
471
472proc maxdeg1 (id,list #)
473"USAGE:   maxdeg1(id[,v]);  id=poly/ideal/vector/module/matrix, v=intvec
474RETURN:  integer, maximal [weighted] degree of monomials of id independent of
475         ring ordering, maxdeg1 of i-th variable is v[i] (default: v=1..1).
476NOTE:    This proc returns one integer while maxdeg returns, in general,
477         a matrix of integers. For one polynomial and if no intvec v is given
478         maxdeg is faster
479EXAMPLE: example maxdeg1; shows examples
480"
481{
482   //-------- subprocedure to find maximal degree of given component ----------
483   proc findmaxdeg
484   {
485      poly c = #[1];
486      if (c==0) { return(-1); }
487      intvec v = #[2];
488   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
489      int d = (deg(c)>=0)*deg(c)-(deg(c)<0)*deg(c);
490      int i = d;
491      if ( c == jet(c,-1,v))      //case: maxdeg is negative
492      {
493         i = -d;
494         while ( c == jet(c,i,v) ) { i = 2*(i-1); }
495         int o = (d != -i)*((i div  2)+2) - 1;
496         int u = i+1;
497         int e = -1;
498      }
499      else                        //case: maxdeg is nonnegative
500      {
501         while ( c != jet(c,i,v) ) { i = 2*(i+1); }
502         int o = i-1;
503         int u = (d != i)*((i div  2)-1);
504         int e = 1;
505      }
506   //----------------------- "quick search" for maxdeg ------------------------
507      while ( ( c==jet(c,i,v) )*( c!=jet(c,i-1,v) ) == 0 )
508      {
509         i = (o+e+u) div  2;
510         if ( c!=jet(c,i,v) ) { u = i+1; }
511         else { o = i-1; }
512      }
513      return(i);
514   }
515//------------------------------ main program ---------------------------------
516   ideal M = simplify(ideal(matrix(id)),8);   //delete scalar multiples from id
517   int c = ncols(M);
518   int i,n;
519   if( size(#)==0 )
520   {
521      int m = maxdeg(M[c]);
522      for (i=c-1; i>0; i--)
523      {
524          n = maxdeg(M[i]);
525          m = (m>=n)*m + (m<n)*n;             //let m be the maximum of m and n
526      }
527   }
528   else
529   {
530      intvec v=#[1];                          //weight vector for the variables
531      int m = findmaxdeg(M[c],v);
532      for (i=c-1; i>0; i--)
533      {
534         n = findmaxdeg(M[i],v);
535         if( n>m ) { m=n; }
536      }
537   }
538   return(m);
539}
540//-------------------------------- examples -----------------------------------
541example
542{ "EXAMPLE:"; echo = 2;
543   ring r = 0,(x,y,z),wp(1,2,3);
544   poly f = x+y2+z3;
545   deg(f);            //deg returns weighted degree (in case of 1 block)!
546   maxdeg1(f);
547   intvec v = ringweights(r);
548   maxdeg1(f,v);                        //weighted maximal degree
549   matrix m[2][2]=f+x10,1,0,f^2;
550   maxdeg1(m,v);                        //absolute weighted maximal degree
551}
552///////////////////////////////////////////////////////////////////////////////
553
554proc mindeg (id)
555"USAGE:   mindeg(id);  id  poly/ideal/vector/module/matrix
556RETURN:  minimal degree/s of monomials of id, independent of ring ordering
557         (mindeg of each variable is 1) of type int if id of type poly, else
558         of type intmat.
559SEE ALSO: mindeg1
560EXAMPLE: example mindeg; shows examples
561"
562{
563   //--------- subprocedure to find minimal degree of given component ---------
564   proc findmindeg
565   {
566      poly c = #[1];
567      if (c==0) { return(-1); }
568   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
569      int d = (ord(c)>=0)*ord(c)-(ord(c)<0)*ord(c);
570      int i = d;
571      while ( jet(c,i) == 0 ) { i = 2*(i+1); }
572      int o = i-1;
573      int u = (d != i)*((i div  2)-1);
574   //----------------------- "quick search" for mindeg ------------------------
575      while ( (jet(c,u)==0)*(jet(c,o)!=0) )
576      {
577         i = (o+u) div 2;
578         if (jet(c,i)==0) { u = i+1; }
579         else { o = i-1; }
580      }
581      if (jet(c,u)!=0) { return(u); }
582      else { return(o+1); }
583   }
584//------------------------------ main program ---------------------------------
585   matrix M = matrix(id);
586   int r,c = nrows(M), ncols(M); int i,j;
587   intmat m[r][c];
588   for (i=r; i>0; i--)
589   {
590      for (j=c; j>0; j--) { m[i,j] = findmindeg(M[i,j]); }
591   }
592   if (typeof(id)=="poly") { return(m[1,1]); }
593   return(m);
594}
595//-------------------------------- examples -----------------------------------
596example
597{ "EXAMPLE:"; echo = 2;
598   ring r = 0,(x,y,z),ls;
599   poly f = x5+y2+z3;
600   ord(f);                  // ord returns weighted order of leading term!
601   mindeg(f);               // computes minimal degree
602   matrix m[2][2]=x10,1,0,f^2;
603   mindeg(m);               // computes matrix of minimum degrees
604}
605///////////////////////////////////////////////////////////////////////////////
606
607proc mindeg1 (id, list #)
608"USAGE:   mindeg1(id[,v]);  id=poly/ideal/vector/module/matrix, v=intvec
609RETURN:  integer, minimal [weighted] degree of monomials of id independent of
610         ring ordering, mindeg1 of i-th variable is v[i] (default v=1..1).
611NOTE:    This proc returns one integer while mindeg returns, in general,
612         a matrix of integers. For one polynomial and if no intvec v is given
613         mindeg is faster.
614EXAMPLE: example mindeg1; shows examples
615"
616{
617   //--------- subprocedure to find minimal degree of given component ---------
618   proc findmindeg
619   {
620      poly c = #[1];
621      intvec v = #[2];
622      if (c==0) { return(-1); }
623   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
624      int d = (ord(c)>=0)*ord(c)-(ord(c)<0)*ord(c);
625      int i = d;
626      if ( jet(c,-1,v) !=0 )      //case: mindeg is negative
627      {
628         i = -d;
629         while ( jet(c,i,v) != 0 ) { i = 2*(i-1); }
630         int o = (d != -i)*((i div  2)+2) - 1;
631         int u = i+1;
632         int e = -1; i=u;
633      }
634      else                        //case: inded is nonnegative
635      {
636         while ( jet(c,i,v) == 0 ) { i = 2*(i+1); }
637         int o = i-1;
638         int u = (d != i)*((i div  2)-1);
639         int e = 1; i=u;
640      }
641   //----------------------- "quick search" for mindeg ------------------------
642      while ( (jet(c,i-1,v)==0)*(jet(c,i,v)!=0) == 0 )
643      {
644         i = (o+e+u) div  2;
645         if (jet(c,i,v)==0) { u = i+1; }
646         else { o = i-1; }
647      }
648      return(i);
649   }
650//------------------------------ main program ---------------------------------
651   ideal M = simplify(ideal(matrix(id)),8);   //delete scalar multiples from id
652   int c = ncols(M);
653   int i,n;
654   if( size(#)==0 )
655   {
656      int m = mindeg(M[c]);
657      for (i=c-1; i>0; i--)
658      {
659          n = mindeg(M[i]);
660          m = (m<=n)*m + (m>n)*n;             //let m be the maximum of m and n
661      }
662   }
663   else
664   {
665      intvec v=#[1];                          //weight vector for the variables
666      int m = findmindeg(M[c],v);
667      for (i=c-1; i>0; i--)
668      {
669         n = findmindeg(M[i],v);
670         m = (m<=n)*m + (m>n)*n;              //let m be the maximum of m and n
671      }
672   }
673   return(m);
674}
675//-------------------------------- examples -----------------------------------
676example
677{ "EXAMPLE:"; echo = 2;
678   ring r = 0,(x,y,z),ls;
679   poly f = x5+y2+z3;
680   ord(f);                  // ord returns weighted order of leading term!
681   intvec v = 1,-3,2;
682   mindeg1(f,v);            // computes minimal weighted degree
683   matrix m[2][2]=x10,1,0,f^2;
684   mindeg1(m,1..3);         // computes absolute minimum of weighted degrees
685}
686///////////////////////////////////////////////////////////////////////////////
687
688proc normalize (id)
689"USAGE:   normalize(id);  id=poly/vector/ideal/module
690RETURN:  object of same type
691         each element is normalized with leading coefficient equal to 1
692EXAMPLE: example normalize; shows an example
693"
694{
695   return(simplify(id,1));
696}
697//-------------------------------- examples -----------------------------------
698example
699{ "EXAMPLE:"; echo = 2;
700   ring r = 0,(x,y,z),ls;
701   poly f = 2x5+3y2+4z3;
702   normalize(f);
703   module m=[9xy,0,3z3],[4z,6y,2x];
704   normalize(m);
705   ring s = 0,(x,y,z),(c,ls);
706   module m=[9xy,0,3z3],[4z,6y,2x];
707   normalize(m);
708}
709///////////////////////////////////////////////////////////////////////////////
710
711///////////////////////////////////////////////////////////////////////////////
712// Input: <ideal>=<f1,f2,...,fm> and <polynomial> g
713// Question: Does g lie in the radical of <ideal>?
714// Solution: Compute a standard basis G for <f1,f2,...,fm,gz-1> where z is a
715//           new variable. Then g is contained in the radical of <ideal> <=>
716//           1 is generator in G.
717///////////////////////////////////////////////////////////////////////////////
718proc rad_con (poly g,ideal I)
719"USAGE:   rad_con(g,I); g polynomial, I ideal
720RETURN:  1 (TRUE) (type int) if g is contained in the radical of I
721@*       0 (FALSE) (type int) otherwise
722EXAMPLE: example rad_con; shows an example
723"
724{ def br=basering;
725  int n=nvars(br);
726  int dB=degBound;
727  degBound=0;
728  string mp=string(minpoly);
729  if (attrib(br,"global")==1)
730  {
731    execute("ring R=("+charstr(br)+"),(@x(1..n),@z),dp;");
732  }
733  else
734  {
735    execute("ring R=("+charstr(br)+"),(@z,@x(1..n)),(dp(1),"+ordstr(br)+");");
736  }
737  execute("minpoly=number("+mp+");");
738  ideal irrel=@x(1..n);
739  map f=br,irrel;
740  poly p=f(g);
741  ideal J=f(I),ideal(p*@z-1);
742  J=std(J);
743  degBound=dB;
744  if (J[1]==1)
745  {
746     setring br;
747     return(1);
748  }
749  else
750  {
751     setring br;
752     return(0);
753  }
754}
755example
756{ "EXAMPLE:"; echo=2;
757   ring R=0,(x,y,z),dp;
758   ideal I=x2+y2,z2;
759   poly f=x4+y4;
760   rad_con(f,I);
761   ideal J=x2+y2,z2,x4+y4;
762   poly g=z;
763   rad_con(g,I);
764}
765///////////////////////////////////////////////////////////////////////////////
766
767proc lcm (id, list #)
768"USAGE:   lcm(p[,q]); p int/intvec q a list of integers or
769          p poly/ideal q a list of polynomials
770RETURN:  the least common multiple of p and q:
771@*         - of type int if p is an int/intvec
772@*         - of type poly if p is a poly/ideal
773EXAMPLE: example lcm; shows an example
774"
775{
776  int k,j;
777//------------------------------ integer case --------------------------------
778  if( typeof(id) == "int" or typeof(id) == "intvec" )
779  {
780     intvec i = id;
781     if (size(#)!=0)
782     {
783        for (j = 1; j<=size(#); j++)
784        {
785           if (typeof(#[j]) !="int" and typeof(#[j]) !="intvec")
786           { ERROR("// ** list element must be an integer");}
787           else
788           { i = i,#[j]; }
789        }
790     }
791     int p,q;
792     if( i == 0 )
793     {
794        return(0);
795     }
796     for(j=1;j<=size(i);j++)
797     {
798       if( i[j] != 0 )
799       {
800         p=i[j];
801         break;
802       }
803     }
804     for (k=j+1;k<=size(i);k++)
805     {
806        if( i[k] !=0)
807        {
808           q=gcd(p,i[k]);
809           p=p div q;
810           p=p*i[k];
811        }
812     }
813     if(p <0 )
814     {return(-p);}
815     return(p);
816   }
817
818//----------------------------- polynomial case ------------------------------
819  if( typeof(id) == "poly" or typeof(id) == "ideal" )
820  {
821     ideal i = id;
822     if (size(#)!=0)
823     {
824        for (j = 1; j<=size(#); j++)
825        {
826           if (typeof(#[j]) !="poly" and typeof(#[j]) !="ideal"
827              and typeof(#[j]) !="int" and typeof(#[j]) !="intvec")
828           { ERROR("// ** list element must be a polynomial");}
829           else
830           { i = i,#[j]; }
831        }
832     }
833     poly p,q;
834     i=simplify(i,10);
835     if(size(i) == 0)
836     {
837        return(0);
838     }
839     for(j=1;j<=size(i);j++)
840     {
841       if(maxdeg(i[j])!= 0)
842       {
843         p=i[j];
844         break;
845       }
846     }
847     if(deg(p)==-1)
848     {
849       return(1);
850     }
851     for (k=j+1;k<=size(i);k++)
852     {
853        if(maxdeg(i[k])!=0)
854        {
855           q=gcd(p,i[k]);
856           if(maxdeg(q)==0)
857           {
858                 p=p*i[k];
859           }
860           else
861           {
862              p=p/q;
863              p=p*i[k];
864           }
865        }
866     }
867     return(p);
868   }
869}
870example
871{ "EXAMPLE:"; echo = 2;
872   ring  r = 0,(x,y,z),lp;
873   poly  p = (x+y)*(y+z);
874   poly  q = (z4+2)*(y+z);
875   lcm(p,q);
876   ideal i=p,q,y+z;
877   lcm(i,p);
878   lcm(2,3,6);
879   lcm(2..6);
880}
881
882///////////////////////////////////////////////////////////////////////////////
883
884proc content(f)
885"USAGE:   content(f); f polynomial/vector
886RETURN:  number, the content (greatest common factor of coefficients)
887         of the polynomial/vector f
888SEE ALSO: cleardenom
889EXAMPLE: example content; shows an example
890"
891{
892  if (f==0) { return(number(1)); }
893  return(leadcoef(f)/leadcoef(cleardenom(f)));
894}
895example
896{ "EXAMPLE:"; echo = 2;
897   ring r=0,(x,y,z),(c,lp);
898   content(3x2+18xy-27xyz);
899   vector v=[3x2+18xy-27xyz,15x2+12y4,3];
900   content(v);
901}
902///////////////////////////////////////////////////////////////////////////////
903
904////////////////////////////////////////////////////////////////////////
905// The idea of the procedures mod2id, id2mod and subrInterred is, to
906// perform standard basis computation or interreduction of a submodule
907// of a free module with generators gen(1),...,gen(n) over a ring R
908// in a ring R[t1,...,tn]/<ti*tj> with gen(i) maped to ti
909////////////////////////////////////////////////////////////////////////
910
911proc mod2id(matrix M,intvec vpos)
912"USAGE:   mod2id(M,vpos); M matrix, vpos intvec
913ASSUME:  vpos is an integer vector such that gen(i) corresponds
914         to var(vpos[i]).
915         The basering contains variables var(vpos[i]) which do not occur
916         in M.
917RETURN:  ideal I in which each gen(i) from the module is replaced by
918         var(vpos[i]) and all monomials var(vpos[i])*var(vpos[j]) have
919         been added to the generating set of I.
920NOTE:    This procedure should be used in the following situation:
921         one wants to pass to a ring with new variables, say e(1),..,e(s),
922         which correspond to the components gen(1),..,gen(s) of the
923         module M such that e(i)*e(j)=0 for all i,j.
924         The new ring should already exist and be the current ring
925EXAMPLE: example mod2id; shows an example"
926{
927  int p = printlevel-voice+3;  // p=printlevel+1 (default: p=1)
928//----------------------------------------------------------------------
929// define the ideal generated by the var(vpos[i]) and set up the matrix
930// for the multiplication
931//----------------------------------------------------------------------
932  ideal vars=var(vpos[1]);
933  for(int i=2;i<=size(vpos);i++)
934  {
935    vars=vars,var(vpos[i]);
936  }
937  matrix varm[1][size(vpos)]=vars;
938  if (size(vpos) > nrows(M))
939  {
940    matrix Mt[size(vpos)][ncols(M)];
941    Mt[1..nrows(M),1..ncols(M)]=M;
942    kill M;
943    matrix M=Mt;
944  }
945//----------------------------------------------------------------------
946// define the desired ideal
947//----------------------------------------------------------------------
948  ideal ret=vars^2,varm*M;
949  return(ret);
950}
951example
952{ "EXAMPLE:"; echo=2;
953  ring r=0,(e(1),e(2),x,y,z),(dp(2),ds(3));
954  module mo=x*gen(1)+y*gen(2);
955  intvec iv=2,1;
956  mod2id(mo,iv);
957}
958////////////////////////////////////////////////////////////////////////
959
960proc id2mod(ideal i,intvec vpos)
961"USAGE:   id2mod(I,vpos); I ideal, vpos intvec
962RETURN:  module corresponding to the ideal by replacing var(vpos[i]) by
963         gen(i) and omitting all generators var(vpos[i])*var(vpos[j])
964NOTE:    * This procedure only makes sense if the ideal contains
965           all var(vpos[i])*var(vpos[j]) as monomial generators and
966           all other generators of I are linear combinations of the
967           var(vpos[i]) over the ring in the other variables.
968         * This is the inverse procedure to mod2id and should be applied
969           only to ideals created by mod2id using the same intvec vpos
970           (possibly after a standard basis computation)
971EXAMPLE: example id2mod; shows an example"
972{
973  int p = printlevel-voice+3;  // p=printlevel+1 (default: p=1)
974//---------------------------------------------------------------------
975// Initialization
976//---------------------------------------------------------------------
977  int n=size(i);
978  int v=size(vpos);
979  matrix tempmat;
980  matrix mm[v][n];
981//---------------------------------------------------------------------
982// Conversion
983//---------------------------------------------------------------------
984  for(int j=1;j<=v;j++)
985  {
986    tempmat=coeffs(i,var(vpos[j]));
987    mm[j,1..n]=tempmat[2,1..n];
988  }
989  for(j=1;j<=v;j++)
990  {
991    mm=subst(mm,var(vpos[j]),0);
992  }
993  module ret=simplify(mm,10);
994  return(ret);
995}
996example
997{ "EXAMPLE:"; echo=2;
998  ring r=0,(e(1),e(2),x,y,z),(dp(2),ds(3));
999  ideal i=e(2)^2,e(1)*e(2),e(1)^2,e(1)*y+e(2)*x;
1000  intvec iv=2,1;
1001  id2mod(i,iv);
1002}
1003///////////////////////////////////////////////////////////////////////
1004
1005proc subrInterred(ideal mon, ideal sm, intvec iv)
1006"USAGE:   subrInterred(mon,sm,iv);
1007         sm:   ideal in a ring r with n + s variables,
1008               e.g. x_1,..,x_n and t_1,..,t_s
1009         mon:  ideal with monomial generators (not divisible by
1010               any of the t_i) such that sm is contained in the module
1011               k[t_1,..,t_s]*mon[1]+..+k[t_1,..,t_s]*mon[size(mon)]
1012         iv:   intvec listing the variables which are supposed to be used
1013               as x_i
1014RETURN:  list l:
1015          l[1]=the monomials from mon in the order used
1016          l[2]=their coefficients after interreduction
1017          l[3]=l[1]*l[2]
1018PURPOSE:  Do interred only w.r.t. a subset of variables.
1019         The procedure returns an interreduced system of generators of
1020         sm considered as a k[t_1,..,t_s]-submodule of the free module
1021         k[t_1,..,t_s]*mon[1]+..+k[t_1,..,t_s]*mon[size(mon)]).
1022EXAMPLE: example subrInterred; shows an example
1023"
1024{
1025  int p = printlevel-voice+3;  // p=printlevel+1 (default: p=1)
1026//-----------------------------------------------------------------------
1027// check that mon is really generated by monomials
1028// and sort its generators with respect to the monomial ordering
1029//-----------------------------------------------------------------------
1030  int err;
1031  for(int i=1;i<=ncols(mon);i++)
1032  {
1033    if ( size(mon[i]) > 1 )
1034    {
1035      err=1;
1036    }
1037  }
1038  if (err==1)
1039  {
1040    ERROR("mon has to be generated by monomials");
1041  }
1042  intvec sv=sortvec(mon);
1043  ideal mons;
1044  for(i=1;i<=size(sv);i++)
1045  {
1046    mons[i]=mon[sv[i]];
1047  }
1048  ideal itemp=maxideal(1);
1049  for(i=1;i<=size(iv);i++)
1050  {
1051    itemp=subst(itemp,var(iv[i]),0);
1052  }
1053  itemp=simplify(itemp,10);
1054  def r=basering;
1055  string tempstr="ring rtemp=" + charstr(basering) + ",(" + string(itemp)
1056                               + "),(C,dp);";
1057//-----------------------------------------------------------------------
1058// express m in terms of the generators of mon and check whether m
1059// can be considered as a submodule of k[t_1,..,t_n]*mon
1060//-----------------------------------------------------------------------
1061  module motemp;
1062  motemp[ncols(sm)]=0;
1063  poly ptemp;
1064  int j;
1065  for(i=1;i<=ncols(mons);i++)
1066  {
1067    for(j=1;j<=ncols(sm);j++)
1068    {
1069      ptemp=sm[j]/mons[i];
1070      motemp[j]=motemp[j]+ptemp*gen(i);
1071    }
1072  }
1073  for(i=1;i<=size(iv);i++)
1074  {
1075    motemp=subst(motemp,var(iv[i]),0);
1076  }
1077  matrix monmat[1][ncols(mons)]=mons;
1078  ideal dummy=monmat*motemp;
1079  for(i=1;i<=size(sm);i++)
1080  {
1081    if(sm[i]-dummy[i]!=0)
1082    {
1083      ERROR("the second argument is not a submodule of the assumed structure");
1084    }
1085  }
1086//----------------------------------------------------------------------
1087// do the interreduction and convert back
1088//----------------------------------------------------------------------
1089  execute(tempstr);
1090  def motemp=imap(r,motemp);
1091  intvec save=option(get);
1092  option(redSB);
1093  motemp=interred(motemp);
1094  option(set,save);
1095  setring r;
1096  kill motemp;
1097  def motemp=imap(rtemp,motemp);
1098  //list ret=monmat,motemp,monmat*motemp;
1099  module motemp2=motemp;
1100  for(i=1;i<=ncols(motemp2);i++)
1101  {
1102    motemp2[i]=cleardenom(motemp2[i]);
1103  }
1104  module motemp3=monmat*motemp;
1105  for(i=1;i<=ncols(motemp3);i++)
1106  {
1107    motemp3[i]=cleardenom(motemp3[i]);
1108  }
1109  list ret=monmat,motemp2,matrix(motemp3);
1110  return(ret);
1111}
1112example
1113{ "EXAMPLE:"; echo=2;
1114  ring r=0,(x,y,z),dp;
1115  ideal i=x^2+x*y^2,x*y+x^2*y,z;
1116  ideal j=x^2+x*y^2,x*y,z;
1117  ideal mon=x^2,z,x*y;
1118  intvec iv=1,3;
1119  subrInterred(mon,i,iv);
1120  subrInterred(mon,j,iv);
1121}
1122
1123///////////////////////////////////////////////////////////////////////////////
1124// moved here from nctools.lib
1125// This procedure calculates the Newton diagram of the polynomial f
1126// The output is a intmat M, each row of M is the exp of a monomial in f
1127////////////////////////////////////////////////////////////////////////
1128proc newtonDiag(poly f)
1129"USAGE:  newtonDiag(f); f a poly
1130RETURN:  intmat
1131PURPOSE: compute the Newton diagram of f
1132NOTE:    each row is the exponent of a monomial of f
1133EXAMPLE: example newtonDiag; shows examples
1134"{
1135  int n = nvars(basering);
1136  intvec N=0;
1137  if ( f != 0 )
1138  {
1139    while ( f != 0 )
1140    {
1141      N = N, leadexp(f);
1142      f = f-lead(f);
1143    }
1144  }
1145  else
1146  {
1147    N=N, leadexp(f);
1148  }
1149  N = N[2..size(N)]; // Deletes the zero added in the definition of T
1150  intmat M=intmat(N,(size(N) div n),n); // Conversion from vector to matrix
1151  return (M);
1152}
1153example
1154{
1155  "EXAMPLE:";echo=2;
1156  ring r = 0,(x,y,z),lp;
1157  poly f = x2y+3xz-5y+3;
1158  newtonDiag(f);
1159}
1160
1161////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.