source: git/Singular/LIB/poly.lib @ 73b186

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