source: git/Singular/LIB/poly.lib @ 927ed62

spielwiese
Last change on this file since 927ed62 was 927ed62, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: new function content in poly.lib lcm moved from primdec.lib to poly.lib git-svn-id: file:///usr/local/Singular/svn/trunk@600 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 19.0 KB
Line 
1// $Id: poly.lib,v 1.6 1997-08-04 15:14:19 Singular Exp $
2//system("random",787422842);
3//(GMG, last modified 22.06.96)
4///////////////////////////////////////////////////////////////////////////////
5
6LIBRARY:  poly.lib      PROCEDURES FOR MANIPULATING POLYS, IDEALS, MODULES
7
8 cyclic(int);           ideal of cyclic n-roots
9 freerank(poly/...)     rank of coker(input) if coker is free else -1
10 is_homog(poly/...);    int, =1 resp. =0 if input is homogeneous resp. not
11 is_zero(poly/...);     int, =1 resp. =0 if coker(input) is 0 resp. not
12 lcm(ideal);            lcm of given generators of ideal
13 maxcoef(poly/...);     maximal length of coefficient occuring in poly/...
14 maxdeg(poly/...);      int/intmat = degree/s of terms of maximal order
15 maxdeg1(poly/...);     int = [weighted] maximal degree of input
16 mindeg(poly/...);      int/intmat = degree/s of terms of minimal order
17 mindeg1(poly/...);     int = [weighted] minimal degree of input
18 normalize(poly/...);   normalize poly/... such that leading coefficient is 1
19 rad_con(p,I);          check radical containment of poly p in ideal I
20           (parameters in square brackets [] are optional)
21
22
23LIB "general.lib";
24///////////////////////////////////////////////////////////////////////////////
25
26proc cyclic (int n)
27USAGE:   cyclic(n);  n integer
28RETURN:  ideal of cyclic n-roots from 1-st n variables of basering
29EXAMPLE: example cyclic; shows examples
30{
31//----------------------------- procedure body --------------------------------
32   ideal m = maxideal(1);
33   m = m[1..n],m[1..n];
34   int i,j;
35   ideal s; poly t;
36   for ( j=0; j<=n-2; j=j+1 )
37   {
38      t=0;
39      for( i=1;i<=n;i=i+1 ) { t=t+product(m,i..i+j); }
40      s=s+t;
41   }
42   s=s,product(m,1..n)-1;
43   return (s);
44}
45//-------------------------------- examples -----------------------------------
46example
47{ "EXAMPLE:"; echo = 2;
48   ring r=0,(u,v,w,x,y,z),lp;
49   cyclic(nvars(basering));
50   homog(cyclic(5),z);
51}
52///////////////////////////////////////////////////////////////////////////////
53
54proc freerank
55USAGE:   freerank(M[,any]);  M=poly/ideal/vector/module/matrix
56COMPUTE: rank of module presented by M in case it is free. By definition this
57         is vdim(coker(M)/m*coker(M)) if coker(M) is free, where m = maximal
58         ideal of basering and M is considered as matrix (the 0-module is
59         free of rank 0)
60RETURN:  rank of coker(M) if coker(M) is free and -1 else;
61         in case of a second argument return a list:
62                L[1] = rank of coker(M) or -1
63                L[2] = minbase(M)
64NOTE:    freerank(syz(M)); computes the rank of M if M is free (and -1 else)
65         //* Zur Zeit noch ein Bug, da erste Bettizahl falsch berechnet wird:
66         //betti(0) ist -1 statt 0
67EXAMPLE: example freerank; shows examples
68{
69  int rk;
70  def M = simplify(#[1],10);
71  list mre = mres(M,2);
72  intmat B = betti(mre);
73  if ( ncols(B)>1 ) { rk = -1; }
74  else { rk = sum(B[1..nrows(B),1]); }
75  if (size(#) == 2) { list L=rk,mre[1]; return(L);}
76  return(rk);
77}
78example
79{"EXAMPLE";   echo=2;
80  ring r;
81  ideal i=x;
82  module M=[x,0,1],[-x,0,-1];
83  freerank(M);           // should be -1, coker(M) is not free
84                         // [1] should be 1, coker(syz(M))=M is free of rank 1
85  freerank(syz (M),"");  // [2] should be gen(2)+gen(1) (minimal relation of M)
86  freerank(i);
87  freerank(syz(i));      //* bug, should be 1, coker(syz(i))=i is free of rank 1
88}
89///////////////////////////////////////////////////////////////////////////////
90
91proc is_homog (id)
92USAGE:   is_homog(id);  id  poly/ideal/vector/module/matrix
93RETURN:  integer which is 1 if input is homogeneous (resp. weighted homogeneous
94         if the monomial ordering consists of one block of type ws,Ws,wp or Wp,
95         assuming that all weights are positive) and 0 otherwise
96NOTE:    A vector is homogeneous, if the components are homogeneous of same
97         degree, a module/matrix is homogeneous if all column vectors are
98         homogeneous
99         //*** ergaenzen, wenn Matrizen-Spalten Gewichte haben
100EXAMPLE: example is_homog; shows examples
101{
102//----------------------------- procedure body --------------------------------
103   module M = module(matrix(id));
104   M = simplify(M,2);                        // remove 0-columns
105   intvec v = ringweights(basering);
106   int i,j=1,1;
107   for (i=1; i<=ncols(M); i=i+1)
108   {
109      if( M[i]!=jet(M[i],deg(lead(M[i])),v)-jet(M[i],deg(lead(M[i]))-1,v))
110      { return(0); }
111   }
112   return(1);
113}
114//-------------------------------- examples -----------------------------------
115example
116{ "EXAMPLE:"; echo = 2;
117   ring r = 0,(x,y,z),wp(1,2,3);
118   is_homog(x5-yz+y3);
119   ideal i = x6+y3+z2, x9-z3;
120   is_homog(i);
121   ring s = 0,(a,b,c),ds;
122   vector v = [a2,0,ac+bc];
123   vector w = [a3,b3,c4];
124   is_homog(v);
125   is_homog(w);
126}
127///////////////////////////////////////////////////////////////////////////////
128
129proc is_zero
130USAGE:   is_zero(M[,any]); M=poly/ideal/vector/module/matrix
131RETURN:  integer, 1 if coker(M)=0 resp. 0 if coker(M)!=0, where M is considered
132         as matrix
133         if a second argument is given, return a list:
134                L[1] = 1 if coker(M)=0 resp. 0 if coker(M)!=0
135                L[2] = dim(M)
136EXAMPLE: example is_zero; shows examples
137{
138  int d=dim(std(#[1]));
139  int a = ( d==-1 );
140  if( size(#) >1 ) { list L=a,d; return(L); }
141  return(a);
142}
143example
144{ "EXAMPLE:";   echo=2;
145  ring r;
146  module m = [x],[y],[1,z];
147  is_zero(m,1);
148  qring q = std(ideal(x2+y3+z2));
149  ideal j = x2+y3+z2-37;
150  is_zero(j);
151}
152////////////////////////////////////////////////////////////////////////////////
153
154proc maxcoef (f)
155USAGE:   maxcoef(f);  f  poly/ideal/vector/module/matrix
156RETURN:  maximal length of coefficient of f of type int (by counting the
157         length of the string of each coefficient)
158EXAMPLE: example maxcoef; shows examples
159{
160//----------------------------- procedure body --------------------------------
161   int max,s,ii,jj; string t;
162   ideal i = ideal(matrix(f));
163   i = simplify(i,6);            // delete 0's and keep first of equal elements
164   poly m = var(1); matrix C;
165   for (ii=2;ii<=nvars(basering);ii=ii+1) { m = m*var(ii); }
166   for (ii=1; ii<=size(i); ii=ii+1)
167   {
168      C = coef(i[ii],m);
169      for (jj=1; jj<=ncols(C); jj=jj+1)
170      {
171         t = string(C[2,jj]);  s = size(t);
172         if ( t[1] == "-" ) { s = s - 1; }
173         if ( s > max ) { max = s; }
174      }
175   }
176   return(max);
177}
178//-------------------------------- examples -----------------------------------
179example
180{ "EXAMPLE:"; echo = 2;
181   ring r= 0,(x,y,z),ds;
182   poly g = 345x2-1234567890y+7/4z;
183   maxcoef(g);
184   ideal i = g,10/1234567890;
185   maxcoef(i);
186   // since i[2]=1/123456789
187}
188///////////////////////////////////////////////////////////////////////////////
189
190proc maxdeg (id)
191USAGE:   maxdeg(id);  id  poly/ideal/vector/module/matrix
192RETURN:  int/intmat, each component equals maximal degree of monomials in the
193         corresponding component of id, independent of ring ordering
194         (maxdeg of each var is 1)
195         of type int if id is of type poly, of type intmat else
196NOTE:    proc maxdeg1 returns 1 integer, the absolut maximum; moreover, it has
197         an option for computing weighted degrees
198EXAMPLE: example maxdeg; shows examples
199{
200   //-------- subprocedure to find maximal degree of given component ----------
201   proc findmaxdeg
202   {
203      poly c = #[1];
204      if (c==0) { return(-1); }
205   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
206      int d = (deg(c)>=0)*deg(c)-(deg(c)<0)*deg(c);
207      int i = d;
208      while ( c-jet(c,i) != 0 ) { i = 2*(i+1); }
209      int o = i-1;
210      int u = (d != i)*((i/ 2)-1);
211   //----------------------- "quick search" for maxdeg ------------------------
212      while ( (c-jet(c,i)==0)*(c-jet(c,i-1)!=0) == 0)
213      {
214         i = (o+1+u)/ 2;
215         if (c-jet(c,i)!=0) { u = i+1; }
216         else { o = i-1; }
217      }
218      return(i);
219   }
220//------------------------------ main program ---------------------------------
221   matrix M = matrix(id);
222   int r,c = nrows(M), ncols(M); int i,j;
223   intmat m[r][c];
224   for (i=r; i>0; i=i-1)
225   {
226      for (j=c; j>0; j=j-1) { m[i,j] = findmaxdeg(M[i,j]); }
227   }
228   if (typeof(id)=="poly") { return(m[1,1]); }
229   return(m);
230}
231//-------------------------------- examples -----------------------------------
232example
233{ "EXAMPLE:"; echo = 2;
234   ring r = 0,(x,y,z),wp(-1,-2,-3);
235   poly f = x+y2+z3;
236   deg(f);               //deg; returns weighted degree (in case of 1 block)!
237   maxdeg(f);
238   matrix m[2][2]=f+x10,1,0,f^2;
239   maxdeg(m);
240}
241///////////////////////////////////////////////////////////////////////////////
242
243proc maxdeg1 (id,list #)
244USAGE:   maxdeg1(id[,v]);  id=poly/ideal/vector/module/matrix, v=intvec
245RETURN:  integer, maximal [weighted] degree of monomials of id independent of
246         ring ordering, maxdeg1 of i-th variable is v[i] (default: v=1..1).
247NOTE:    This proc returns one integer while maxdeg returns, in general,
248         a matrix of integers. For one polynomial and if no intvec v is given
249         maxdeg is faster
250EXAMPLE: example maxdeg1; shows examples
251{
252   //-------- subprocedure to find maximal degree of given component ----------
253   proc findmaxdeg
254   {
255      poly c = #[1];
256      if (c==0) { return(-1); }
257      intvec v = #[2];
258   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
259      int d = (deg(c)>=0)*deg(c)-(deg(c)<0)*deg(c);
260      int i = d;
261      if ( c == jet(c,-1,v))      //case: maxdeg is negative
262      {
263         i = -d;
264         while ( c == jet(c,i,v) ) { i = 2*(i-1); }
265         int o = (d != -i)*((i/ 2)+2) - 1;
266         int u = i+1;
267         int e = -1;
268      }
269      else                        //case: maxdeg is nonnegative
270      {
271         while ( c != jet(c,i,v) ) { i = 2*(i+1); }
272         int o = i-1;
273         int u = (d != i)*((i/ 2)-1);
274         int e = 1;
275      }
276   //----------------------- "quick search" for maxdeg ------------------------
277      while ( ( c==jet(c,i,v) )*( c!=jet(c,i-1,v) ) == 0 )
278      {
279         i = (o+e+u)/ 2;
280         if ( c!=jet(c,i,v) ) { u = i+1; }
281         else { o = i-1; }
282      }
283      return(i);
284   }
285//------------------------------ main program ---------------------------------
286   ideal M = simplify(ideal(matrix(id)),8);   //delete scalar multiples from id
287   int c = ncols(M);
288   int i,n;
289   if( size(#)==0 )
290   {
291      int m = maxdeg(M[c]);
292      for (i=c-1; i>0; i=i-1)
293      {
294          n = maxdeg(M[i]);
295          m = (m>=n)*m + (m<n)*n;             //let m be the maximum of m and n
296      }
297   }
298   else
299   {
300      intvec v=#[1];                          //weight vector for the variables
301      int m = findmaxdeg(M[c],v);
302      for (i=c-1; i>0; i--)
303      {
304         n = findmaxdeg(M[i],v);
305         if( n>m ) { m=n; }
306      }
307   }
308   return(m);
309}
310//-------------------------------- examples -----------------------------------
311example
312{ "EXAMPLE:"; echo = 2;
313   ring r = 0,(x,y,z),wp(-1,-2,-3);
314   poly f = x+y2+z3;
315   deg(f);                  //deg returns weighted degree (in case of 1 block)!
316   maxdeg1(f);
317   intvec v = ringweights(r);
318   maxdeg1(f,v);                             //weighted maximal degree
319   matrix m[2][2]=f+x10,1,0,f^2;
320   maxdeg1(m,v);                             //absolut weighted maximal degree
321}
322///////////////////////////////////////////////////////////////////////////////
323
324proc mindeg (id)
325USAGE:   mindeg(id);  id  poly/ideal/vector/module/matrix
326RETURN:  minimal degree/s of monomials of id, independent of ring ordering
327         (mindeg of each variable is 1) of type int if id of type poly, else
328         of type intmat.
329NOTE:    proc mindeg1 returns one integer, the absolut minimum; moreover it
330         has an option for computing weighted degrees.
331EXAMPLE: example mindeg; shows examples
332{
333   //--------- subprocedure to find minimal degree of given component ---------
334   proc findmindeg
335   {
336      poly c = #[1];
337      if (c==0) { return(-1); }
338   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
339      int d = (ord(c)>=0)*ord(c)-(ord(c)<0)*ord(c);
340      int i = d;
341      while ( jet(c,i) == 0 ) { i = 2*(i+1); }
342      int o = i-1;
343      int u = (d != i)*((i/ 2)-1);
344   //----------------------- "quick search" for mindeg ------------------------
345      while ( (jet(c,u)==0)*(jet(c,o)!=0) )
346      {
347         i = (o+u)/ 2;
348         if (jet(c,i)==0) { u = i+1; }
349         else { o = i-1; }
350      }
351      if (jet(c,u)!=0) { return(u); }
352      else { return(o+1); }
353   }
354//------------------------------ main program ---------------------------------
355   matrix M = matrix(id);
356   int r,c = nrows(M), ncols(M); int i,j;
357   intmat m[r][c];
358   for (i=r; i>0; i=i-1)
359   {
360      for (j=c; j>0; j=j-1) { m[i,j] = findmindeg(M[i,j]); }
361   }
362   if (typeof(id)=="poly") { return(m[1,1]); }
363   return(m);
364}
365//-------------------------------- examples -----------------------------------
366example
367{ "EXAMPLE:"; echo = 2;
368   ring r = 0,(x,y,z),ls;
369   poly f = x5+y2+z3;
370   ord(f);                      // ord returns weighted order of leading term!
371   mindeg(f);                   // computes minimal degree
372   matrix m[2][2]=x10,1,0,f^2;
373   mindeg(m);                   // computes matrix of minimum degrees
374}
375///////////////////////////////////////////////////////////////////////////////
376
377proc mindeg1 (id, list #)
378USAGE:   mindeg1(id[,v]);  id=poly/ideal/vector/module/matrix, v=intvec
379RETURN:  integer, minimal [weighted] degree of monomials of id independent of
380         ring ordering, mindeg1 of i-th variable is v[i] (default v=1..1).
381NOTE:    This proc returns one integer while mindeg returns, in general,
382         a matrix of integers. For one polynomial and if no intvec v is given
383         mindeg is faster.
384EXAMPLE: example mindeg1; shows examples
385{
386   //--------- subprocedure to find minimal degree of given component ---------
387   proc findmindeg
388   {
389      poly c = #[1];
390      intvec v = #[2];
391      if (c==0) { return(-1); }
392   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
393      int d = (ord(c)>=0)*ord(c)-(ord(c)<0)*ord(c);
394      int i = d;
395      if ( jet(c,-1,v) !=0 )      //case: mindeg is negative
396      {
397         i = -d;
398         while ( jet(c,i,v) != 0 ) { i = 2*(i-1); }
399         int o = (d != -i)*((i/ 2)+2) - 1;
400         int u = i+1;
401         int e = -1; i=u;
402      }
403      else                        //case: inded is nonnegative
404      {
405         while ( jet(c,i,v) == 0 ) { i = 2*(i+1); }
406         int o = i-1;
407         int u = (d != i)*((i/ 2)-1);
408         int e = 1; i=u;
409      }
410   //----------------------- "quick search" for mindeg ------------------------
411      while ( (jet(c,i-1,v)==0)*(jet(c,i,v)!=0) == 0 )
412      {
413         i = (o+e+u)/ 2;
414         if (jet(c,i,v)==0) { u = i+1; }
415         else { o = i-1; }
416      }
417      return(i);
418   }
419//------------------------------ main program ---------------------------------
420   ideal M = simplify(ideal(matrix(id)),8);   //delete scalar multiples from id
421   int c = ncols(M);
422   int i,n;
423   if( size(#)==0 )
424   {
425      int m = mindeg(M[c]);
426      for (i=c-1; i>0; i=i-1)
427      {
428          n = mindeg(M[i]);
429          m = (m<=n)*m + (m>n)*n;             //let m be the maximum of m and n
430      }
431   }
432   else
433   {
434      intvec v=#[1];                          //weight vector for the variables
435      int m = findmindeg(M[c],v);
436      for (i=c-1; i>0; i=i-1)
437      {
438         n = findmindeg(M[i],v);
439         m = (m<=n)*m + (m>n)*n;              //let m be the maximum of m and n
440      }
441   }
442   return(m);
443}
444//-------------------------------- examples -----------------------------------
445example
446{ "EXAMPLE:"; echo = 2;
447   ring r = 0,(x,y,z),ls;
448   poly f = x5+y2+z3;
449   ord(f);                      // ord returns weighted order of leading term!
450   intvec v = 1,-3,2;
451   mindeg1(f,v);                // computes minimal weighted degree
452   matrix m[2][2]=x10,1,0,f^2;
453   mindeg1(m,1..3);             // computes absolut minimum of weighted degrees
454}
455///////////////////////////////////////////////////////////////////////////////
456
457proc normalize (id)
458USAGE:   normalize(id);  id=poly/vector/ideal/module
459RETURN:  object of same type with leading coefficient equal to 1
460EXAMPLE: example normalize; shows an example
461{
462   return(simplify(id,1));
463}
464//-------------------------------- examples -----------------------------------
465example
466{  "EXAMPLE:"; echo = 2;
467   ring r = 0,(x,y,z),ls;
468   poly f = 2x5+3y2+4z3;
469   normalize(f);
470   module m=[9xy,0,3z3],[4z,6y,2x];
471   normalize(m);
472   ring s = 0,(x,y,z),(c,ls);
473   module m=[9xy,0,3z3],[4z,6y,2x];
474   normalize(m);
475   normalize(matrix(m));             // by automatic type conversion to module!
476}
477///////////////////////////////////////////////////////////////////////////////
478
479////////////////////////////////////////////////////////////////////////////////
480// Input: <ideal>=<f1,f2,...,fm> and <polynomial> g
481// Question: Does g lie in the radical of <ideal>?
482// Solution: Compute a standard basis G for <f1,f2,...,fm,gz-1> where z is a new
483//           variable. Then g is contained in the radical of <ideal> <=> 1 is
484//           generator in G.
485////////////////////////////////////////////////////////////////////////////////
486proc rad_con (poly g,ideal I)
487  USAGE:   rad_con(<poly>,<ideal>);
488  RETURNS: 1 (TRUE) (type <int>) if <poly> is contained in the radical of
489           <ideal>, 0 (FALSE) (type <int>) otherwise
490  EXAMPLE: example rad_con; shows an example
491{ def br=basering;
492  int n=nvars(br);
493  int dB=degBound;
494  degBound=0;
495  string mp=string(minpoly);
496  execute "ring R=("+charstr(br)+"),(x(1..n),z),dp;";
497  execute "minpoly=number("+mp+");";
498  ideal irrel=x(1..n);
499  map f=br,irrel;
500  poly p=f(g);
501  ideal J=f(I)+ideal(p*z-1);
502  J=std(J);
503  degBound=dB;
504  if (J[1]==1)
505  { return(1);
506  }
507  else
508  { return(0);
509  }
510}
511example
512{ "  EXAMPLE: Sturmfels: Algorithms in Invariant Theory 2.3.7.";
513  echo=2;
514           ring R=0,(x,y,z),dp;
515           ideal I=x2+y2,z2;
516           poly f=x4+y4;
517           rad_con(f,I);
518           ideal J=x2+y2,z2,x4+y4;
519           poly g=z;
520           rad_con(g,I);
521}
522
523///////////////////////////////////////////////////////////////////////////////
524
525proc lcm (ideal i)
526USAGE:   lcm(i); i ideal
527RETURN:  poly = lcm(i[1],...,i[size(i)])
528NOTE:   
529EXAMPLE: example lcm; shows an example
530{
531  int k,j;
532   poly p,q;
533  i=simplify(i,10);
534  for(j=1;j<=size(i);j++)
535  {
536    if(deg(i[j])>0)
537    {
538      p=i[j];
539      break;
540    }
541  }
542  if(deg(p)==-1)
543  {
544    return(1);
545  }
546  for (k=j+1;k<=size(i);k++)
547  {
548     if(deg(i[k])!=0)
549     {
550        q=GCD(p,i[k]);
551        if(deg(q)==0)
552        {
553           p=p*i[k];
554        }
555        else
556        {
557           p=p/q;
558           p=p*i[k];
559        }
560     }
561   }
562  return(p);
563}
564example
565{ "EXAMPLE:"; echo = 2;
566   ring  r = 0,(x,y,z),lp;
567   poly  p = (x+y)*(y+z);
568   poly  q = (z4+2)*(y+z);
569   ideal l=p,q;
570   poly  pr= lcm(l);
571   pr;
572   l=1,-1,p,1,-1,q,1;
573   pr=lcm(l);
574   pr;
575}
576
577///////////////////////////////////////////////////////////////////////////////
578
579proc content(f)
580USAGE:   content(f); f polynomial/vector
581RETURN:  number, the content (greatest common factor of coefficients)
582         of the polynomial/vector f
583EXAMPLE: example content; shows an example
584{
585  return(leadcoef(f)/leadcoef(cleardenom(f)));
586
587example
588{ "EXAMPLE:"; echo = 2;
589   ring r=0,(x,y,z),(c,lp);
590   content(3x2+18xy-27xyz);
591   vector v=[3x2+18xy-27xyz,15x2+12y4,3];
592   content(v);
593}
594
Note: See TracBrowser for help on using the repository browser.