source: git/Singular/LIB/poly.lib @ 4508b59

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