source: git/Singular/LIB/poly.lib @ 07b0a56

fieker-DuValspielwiese
Last change on this file since 07b0a56 was 6f7f1a, checked in by Hans Schönemann <hannes@…>, 17 years ago
*hannes: fixed typo git-svn-id: file:///usr/local/Singular/svn/trunk@10172 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 32.9 KB
Line 
1///////////////////////////////////////////////////////////////////////////////
2version="$Id: poly.lib,v 1.45 2007-07-05 12:32:12 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  if (attrib(br,"global")==1)
675  {
676    execute("ring R=("+charstr(br)+"),(@x(1..n),@z),dp;");
677  }
678  else
679  {
680    execute("ring R=("+charstr(br)+"),(@z,@x(1..n)),(dp(1),"+ordstr(br)+");");
681  }
682  execute("minpoly=number("+mp+");");
683  ideal irrel=@x(1..n);
684  map f=br,irrel;
685  poly p=f(g);
686  ideal J=f(I),ideal(p*@z-1);
687  J=std(J);
688  degBound=dB;
689  if (J[1]==1)
690  {
691     setring br;
692     return(1);
693  }
694  else
695  {
696     setring br;
697     return(0);
698  }
699}
700example
701{ "EXAMPLE:"; echo=2;
702   ring R=0,(x,y,z),dp;
703   ideal I=x2+y2,z2;
704   poly f=x4+y4;
705   rad_con(f,I);
706   ideal J=x2+y2,z2,x4+y4;
707   poly g=z;
708   rad_con(g,I);
709}
710///////////////////////////////////////////////////////////////////////////////
711
712proc lcm (id, list #)
713"USAGE:   lcm(p[,q]); p int/intvec q a list of integers or
714          p poly/ideal q a list of polynomials
715RETURN:  the least common multiple of p and q:
716@*         - of type int if p is an int/intvec
717@*         - of type poly if p is a poly/ideal
718EXAMPLE: example lcm; shows an example
719"
720{
721  int k,j;
722//------------------------------ integer case --------------------------------
723  if( typeof(id) == "int" or typeof(id) == "intvec" )
724  {
725     intvec i = id;
726     if (size(#)!=0)
727     {
728        for (j = 1; j<=size(#); j++)
729        {
730           if (typeof(#[j]) !="int" and typeof(#[j]) !="intvec")
731           { ERROR("// ** list element must be an integer");}
732           else
733           { i = i,#[j]; }
734        }
735     }
736     int p,q;
737     if( i == 0 )
738     {
739        return(0);
740     }
741     for(j=1;j<=size(i);j++)
742     {
743       if( i[j] != 0 )
744       {
745         p=i[j];
746         break;
747       }
748     }
749     for (k=j+1;k<=size(i);k++)
750     {
751        if( i[k] !=0)
752        {
753           q=gcd(p,i[k]);
754           p=p/q;
755           p=p*i[k];
756        }
757     }
758     if(p <0 )
759     {return(-p);}
760     return(p);
761   }
762
763//----------------------------- polynomial case ------------------------------
764  if( typeof(id) == "poly" or typeof(id) == "ideal" )
765  {
766     ideal i = id;
767     if (size(#)!=0)
768     {
769        for (j = 1; j<=size(#); j++)
770        {
771           if (typeof(#[j]) !="poly" and typeof(#[j]) !="ideal"
772              and typeof(#[j]) !="int" and typeof(#[j]) !="intvec")
773           { ERROR("// ** list element must be a polynomial");}
774           else
775           { i = i,#[j]; }
776        }
777     }
778     poly p,q;
779     i=simplify(i,10);
780     if(size(i) == 0)
781     {
782        return(0);
783     }
784     for(j=1;j<=size(i);j++)
785     {
786       if(maxdeg(i[j])!= 0)
787       {
788         p=i[j];
789         break;
790       }
791     }
792     if(deg(p)==-1)
793     {
794       return(1);
795     }
796     for (k=j+1;k<=size(i);k++)
797     {
798        if(maxdeg(i[k])!=0)
799        {
800           q=gcd(p,i[k]);
801           if(maxdeg(q)==0)
802           {
803                 p=p*i[k];
804           }
805           else
806           {
807              p=p/q;
808              p=p*i[k];
809           }
810        }
811     }
812     return(p);
813   }
814}
815example
816{ "EXAMPLE:"; echo = 2;
817   ring  r = 0,(x,y,z),lp;
818   poly  p = (x+y)*(y+z);
819   poly  q = (z4+2)*(y+z);
820   lcm(p,q);
821   ideal i=p,q,y+z;
822   lcm(i,p);
823   lcm(2,3,6);
824   lcm(2..6);
825}
826
827///////////////////////////////////////////////////////////////////////////////
828
829proc content(f)
830"USAGE:   content(f); f polynomial/vector
831RETURN:  number, the content (greatest common factor of coefficients)
832         of the polynomial/vector f
833EXAMPLE: example content; shows an example
834"
835{
836  return(leadcoef(f)/leadcoef(cleardenom(f)));
837}
838example
839{ "EXAMPLE:"; echo = 2;
840   ring r=0,(x,y,z),(c,lp);
841   content(3x2+18xy-27xyz);
842   vector v=[3x2+18xy-27xyz,15x2+12y4,3];
843   content(v);
844}
845///////////////////////////////////////////////////////////////////////////////
846
847proc numerator(number n)
848"USAGE:    numerator(n); n number
849RETURN:   number, the numerator of n
850SEE ALSO: denominator, content, cleardenom
851EXAMPLE:  example numerator; shows an example
852"
853{
854  poly p = cleardenom(n+var(1));
855  return (number(coeffs(p,var(1))[1,1]));
856}
857example
858{
859  "EXAMPLE:"; echo = 2;
860  ring r = 0,x, dp;
861  number n = 3/2;
862  numerator(n);
863}
864///////////////////////////////////////////////////////////////////////////////
865
866proc denominator(number n)
867"USAGE:    denominator(n); n number
868RETURN:   number, the denominator of n
869SEE ALSO: denominator, content, cleardenom
870EXAMPLE:  example denominator; shows an example
871"
872{
873  poly p = cleardenom(n+var(1));
874  return (number(coeffs(p,var(1))[2,1]));
875}
876example
877{
878  "EXAMPLE:"; echo = 2;
879  ring r = 0,x, dp;
880  number n = 3/2;
881  denominator(n);
882}
883////////////////////////////////////////////////////////////////////////
884
885////////////////////////////////////////////////////////////////////////
886// The idea of the procedures mod2id, id2mod and subrInterred is, to
887// perform standard basis computation or interreduction of a submodule
888// of a free module with generators gen(1),...,gen(n) over a ring R
889// in a ring R[t1,...,tn]/<ti*tj> with gen(i) maped to ti
890////////////////////////////////////////////////////////////////////////
891
892proc mod2id(matrix M,intvec vpos)
893"USAGE:   mod2id(M,vpos); M matrix, vpos intvec
894ASSUME:  vpos is an integer vector such that gen(i) corresponds
895         to var(vpos[i]).
896         The basering contains variables var(vpos[i]) which do not occur
897         in M.
898RETURN:  ideal I in which each gen(i) from the module is replaced by
899         var(vpos[i]) and all monomials var(vpos[i])*var(vpos[j]) have
900         been added to the generating set of I.
901NOTE:    This procedure should be used in the following situation:
902         one wants to pass to a ring with new variables, say e(1),..,e(s),
903         which correspond to the components gen(1),..,gen(s) of the
904         module M such that e(i)*e(j)=0 for all i,j.
905         The new ring should already exist and be the current ring
906EXAMPLE: example mod2id; shows an example"
907{
908  int p = printlevel-voice+3;  // p=printlevel+1 (default: p=1)
909//----------------------------------------------------------------------
910// define the ideal generated by the var(vpos[i]) and set up the matrix
911// for the multiplication
912//----------------------------------------------------------------------
913  ideal vars=var(vpos[1]);
914  for(int i=2;i<=size(vpos);i++)
915  {
916    vars=vars,var(vpos[i]);
917  }
918  matrix varm[1][size(vpos)]=vars;
919  if (size(vpos) > nrows(M))
920  {
921    matrix Mt[size(vpos)][ncols(M)];
922    Mt[1..nrows(M),1..ncols(M)]=M;
923    kill M;
924    matrix M=Mt;
925  }
926//----------------------------------------------------------------------
927// define the desired ideal
928//----------------------------------------------------------------------
929  ideal ret=vars^2,varm*M;
930  return(ret);
931}
932example
933{ "EXAMPLE:"; echo=2;
934  ring r=0,(e(1),e(2),x,y,z),(dp(2),ds(3));
935  module mo=x*gen(1)+y*gen(2);
936  intvec iv=2,1;
937  mod2id(mo,iv);
938}
939////////////////////////////////////////////////////////////////////////
940
941proc id2mod(ideal i,intvec vpos)
942"USAGE:   id2mod(I,vpos); I ideal, vpos intvec
943RETURN:  module corresponding to the ideal by replacing var(vpos[i]) by
944         gen(i) and omitting all generators var(vpos[i])*var(vpos[j])
945NOTE:    * This procedure only makes sense if the ideal contains
946           all var(vpos[i])*var(vpos[j]) as monomial generators and
947           all other generators of I are linear combinations of the
948           var(vpos[i]) over the ring in the other variables.
949         * This is the inverse procedure to mod2id and should be applied
950           only to ideals created by mod2id using the same intvec vpos
951           (possibly after a standard basis computation)
952EXAMPLE: example id2mod; shows an example"
953{
954  int p = printlevel-voice+3;  // p=printlevel+1 (default: p=1)
955//---------------------------------------------------------------------
956// Initialization
957//---------------------------------------------------------------------
958  int n=size(i);
959  int v=size(vpos);
960  matrix tempmat;
961  matrix mm[v][n];
962//---------------------------------------------------------------------
963// Conversion
964//---------------------------------------------------------------------
965  for(int j=1;j<=v;j++)
966  {
967    tempmat=coeffs(i,var(vpos[j]));
968    mm[j,1..n]=tempmat[2,1..n];
969  }
970  for(j=1;j<=v;j++)
971  {
972    mm=subst(mm,var(vpos[j]),0);
973  }
974  module ret=simplify(mm,10);
975  return(ret);
976}
977example
978{ "EXAMPLE:"; echo=2;
979  ring r=0,(e(1),e(2),x,y,z),(dp(2),ds(3));
980  ideal i=e(2)^2,e(1)*e(2),e(1)^2,e(1)*y+e(2)*x;
981  intvec iv=2,1;
982  id2mod(i,iv);
983}
984///////////////////////////////////////////////////////////////////////
985
986proc subrInterred(ideal mon, ideal sm, intvec iv)
987"USAGE:   subrInterred(mon,sm,iv);
988         sm:   ideal in a ring r with n + s variables,
989               e.g. x_1,..,x_n and t_1,..,t_s
990         mon:  ideal with monomial generators (not divisible by
991               any of the t_i) such that sm is contained in the module
992               k[t_1,..,t_s]*mon[1]+..+k[t_1,..,t_s]*mon[size(mon)]
993         iv:   intvec listing the variables which are supposed to be used
994               as x_i
995RETURN:  list l:
996          l[1]=the monomials from mon in the order used
997          l[2]=their coefficients after interreduction
998          l[3]=l[1]*l[2]
999PURPOSE:  Do interred only w.r.t. a subset of variables.
1000         The procedure returns an interreduced system of generators of
1001         sm considered as a k[t_1,..,t_s]-submodule of the free module
1002         k[t_1,..,t_s]*mon[1]+..+k[t_1,..,t_s]*mon[size(mon)]).
1003EXAMPLE: example subrInterred; shows an example
1004"
1005{
1006  int p = printlevel-voice+3;  // p=printlevel+1 (default: p=1)
1007//-----------------------------------------------------------------------
1008// check that mon is really generated by monomials
1009// and sort its generators with respect to the monomial ordering
1010//-----------------------------------------------------------------------
1011  int err;
1012  for(int i=1;i<=ncols(mon);i++)
1013  {
1014    if ( size(mon[i]) > 1 )
1015    {
1016      err=1;
1017    }
1018  }
1019  if (err==1)
1020  {
1021    ERROR("mon has to be generated by monomials");
1022  }
1023  intvec sv=sortvec(mon);
1024  ideal mons;
1025  for(i=1;i<=size(sv);i++)
1026  {
1027    mons[i]=mon[sv[i]];
1028  }
1029  ideal itemp=maxideal(1);
1030  for(i=1;i<=size(iv);i++)
1031  {
1032    itemp=subst(itemp,var(iv[i]),0);
1033  }
1034  itemp=simplify(itemp,10);
1035  def r=basering;
1036  string tempstr="ring rtemp=" + charstr(basering) + ",(" + string(itemp)
1037                               + "),(C,dp);";
1038//-----------------------------------------------------------------------
1039// express m in terms of the generators of mon and check whether m
1040// can be considered as a submodule of k[t_1,..,t_n]*mon
1041//-----------------------------------------------------------------------
1042  module motemp;
1043  motemp[ncols(sm)]=0;
1044  poly ptemp;
1045  int j;
1046  for(i=1;i<=ncols(mons);i++)
1047  {
1048    for(j=1;j<=ncols(sm);j++)
1049    {
1050      ptemp=sm[j]/mons[i];
1051      motemp[j]=motemp[j]+ptemp*gen(i);
1052    }
1053  }
1054  for(i=1;i<=size(iv);i++)
1055  {
1056    motemp=subst(motemp,var(iv[i]),0);
1057  }
1058  matrix monmat[1][ncols(mons)]=mons;
1059  ideal dummy=monmat*motemp;
1060  for(i=1;i<=size(sm);i++)
1061  {
1062    if(sm[i]-dummy[i]!=0)
1063    {
1064      ERROR("the second argument is not a submodule of the assumed structure");
1065    }
1066  }
1067//----------------------------------------------------------------------
1068// do the interreduction and convert back
1069//----------------------------------------------------------------------
1070  execute(tempstr);
1071  def motemp=imap(r,motemp);
1072  motemp=interred(motemp);
1073  setring r;
1074  kill motemp;
1075  def motemp=imap(rtemp,motemp);
1076  list ret=monmat,motemp,monmat*motemp;
1077  for(i=1;i<=ncols(ret[2]);i++)
1078  {
1079    ret[2][i]=cleardenom(ret[2][i]);
1080  }
1081  for(i=1;i<=ncols(ret[3]);i++)
1082  {
1083    ret[3][i]=cleardenom(ret[3][i]);
1084  }
1085  return(ret);
1086}
1087example
1088{ "EXAMPLE:"; echo=2;
1089  ring r=0,(x,y,z),dp;
1090  ideal i=x^2+x*y^2,x*y+x^2*y,z;
1091  ideal j=x^2+x*y^2,x*y,z;
1092  ideal mon=x^2,z,x*y;
1093  intvec iv=1,3;
1094  subrInterred(mon,i,iv);
1095  subrInterred(mon,j,iv);
1096}
1097
1098///////////////////////////////////////////////////////////////////////////////
1099// moved here from nctools.lib
1100// This procedure calculates the Newton diagram of the polynomial f
1101// The output is a intmat M, each row of M is the exp of a monomial in f
1102////////////////////////////////////////////////////////////////////////
1103proc newtonDiag(poly f)
1104"USAGE:  newtonDiag(f); f a poly
1105RETURN:  intmat
1106PURPOSE: compute the Newton diagram of f
1107NOTE:    each row is the exponent of a monomial of f
1108EXAMPLE: example newtonDiag; shows examples
1109"{
1110  int n = nvars(basering);
1111  intvec N=0;
1112  if ( f != 0 )
1113  {
1114    while ( f != 0 )
1115    {
1116      N = N, leadexp(f);
1117      f = f-lead(f);
1118    }
1119  }
1120  else
1121  {
1122    N=N, leadexp(f);
1123  }
1124  N = N[2..size(N)]; // Deletes the zero added in the definition of T
1125  intmat M=intmat(N,(size(N)/n),n); // Conversion from vector to matrix
1126  return (M);
1127}
1128example
1129{
1130  "EXAMPLE:";echo=2;
1131  ring r = 0,(x,y,z),lp;
1132  poly f = x2y+3xz-5y+3;
1133  newtonDiag(f);
1134}
1135
1136////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.