source: git/Singular/LIB/poly.lib @ 1134b5

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