source: git/Singular/LIB/poly.lib @ 400884

spielwiese
Last change on this file since 400884 was 6f2edc, checked in by Olaf Bachmann <obachman@…>, 27 years ago
Mon Apr 28 21:00:07 1997 Olaf Bachmann <obachman@ratchwum.mathematik.uni-kl.de (Olaf Bachmann)> * dunno why I am committing these libs -- have not modified any of them git-svn-id: file:///usr/local/Singular/svn/trunk@205 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.2 KB
Line 
1// $Id: poly.lib,v 1.2 1997-04-28 19:27:22 obachman 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 maxcoef(poly/...);     maximal length of coefficient occuring in poly/...
13 maxdeg(poly/...);      int/intmat = degree/s of terms of maximal order
14 maxdeg1(poly/...);     int = [weighted] maximal degree of input
15 mindeg(poly/...);      int/intmat = degree/s of terms of minimal order
16 mindeg1(poly/...);     int = [weighted] minimal degree of input
17 normalize(poly/...);   normalize poly/... such that leading coefficient is 1
18           (parameters in square brackets [] are optional)
19
20LIB "general.lib";
21///////////////////////////////////////////////////////////////////////////////
22
23proc cyclic (int n)
24USAGE:   cyclic(n);  n integer
25RETURN:  ideal of cyclic n-roots from 1-st n variables of basering
26EXAMPLE: example cyclic; shows examples
27{
28//----------------------------- procedure body --------------------------------
29   ideal m = maxideal(1);
30   m = m[1..n],m[1..n];
31   int i,j;
32   ideal s; poly t;
33   for ( j=0; j<=n-2; j=j+1 )
34   {
35      t=0;
36      for( i=1;i<=n;i=i+1 ) { t=t+product(m,i..i+j); }
37      s=s+t;
38   }
39   s=s,product(m,1..n)-1;
40   return (s);
41}
42//-------------------------------- examples -----------------------------------
43example
44{ "EXAMPLE:"; echo = 2;
45   ring r=0,(u,v,w,x,y,z),lp;
46   cyclic(nvars(basering));
47   homog(cyclic(5),z);
48}
49///////////////////////////////////////////////////////////////////////////////
50
51proc freerank
52USAGE:   freerank(M[,any]);  M=poly/ideal/vector/module/matrix
53COMPUTE: rank of module presented by M in case it is free. By definition this
54         is vdim(coker(M)/m*coker(M)) if coker(M) is free, where m = maximal
55         ideal of basering and M is considered as matrix (the 0-module is
56         free of rank 0)
57RETURN:  rank of coker(M) if coker(M) is free and -1 else;
58         in case of a second argument return a list:
59                L[1] = rank of coker(M) or -1
60                L[2] = minbase(M)
61NOTE:    freerank(syz(M)); computes the rank of M if M is free (and -1 else)
62         //* Zur Zeit noch ein Bug, da erste Bettizahl falsch berechnet wird:
63         //betti(0) ist -1 statt 0
64EXAMPLE: example freerank; shows examples
65{
66  int rk;
67  def M = simplify(#[1],10);
68  list mre = mres(M,2);
69  intmat B = betti(mre);
70  if ( ncols(B)>1 ) { rk = -1; }
71  else { rk = sum(B[1..nrows(B),1]); }
72  if (size(#) == 2) { list L=rk,mre[1]; return(L);}
73  return(rk);
74}
75example
76{"EXAMPLE";   echo=2;
77  ring r;
78  ideal i=x;
79  module M=[x,0,1],[-x,0,-1];
80  freerank(M);           // should be -1, coker(M) is not free
81                         // [1] should be 1, coker(syz(M))=M is free of rank 1
82  freerank(syz (M),"");  // [2] should be gen(2)+gen(1) (minimal relation of M)
83  freerank(i);
84  freerank(syz(i));      //* bug, should be 1, coker(syz(i))=i is free of rank 1
85}
86///////////////////////////////////////////////////////////////////////////////
87
88proc is_homog (id)
89USAGE:   is_homog(id);  id  poly/ideal/vector/module/matrix
90RETURN:  integer which is 1 if input is homogeneous (resp. weighted homogeneous
91         if the monomial ordering consists of one block of type ws,Ws,wp or Wp,
92         assuming that all weights are positive) and 0 otherwise
93NOTE:    A vector is homogeneous, if the components are homogeneous of same
94         degree, a module/matrix is homogeneous if all column vectors are
95         homogeneous
96         //*** ergaenzen, wenn Matrizen-Spalten Gewichte haben
97EXAMPLE: example is_homog; shows examples
98{
99//----------------------------- procedure body --------------------------------
100   module M = module(matrix(id));
101   M = simplify(M,2);                        // remove 0-columns
102   intvec v = ringweights(basering);
103   int i,j=1,1;
104   for (i=1; i<=ncols(M); i=i+1)
105   {
106      if( M[i]!=jet(M[i],deg(lead(M[i])),v)-jet(M[i],deg(lead(M[i]))-1,v))
107      { return(0); }
108   }
109   return(1);
110}
111//-------------------------------- examples -----------------------------------
112example
113{ "EXAMPLE:"; echo = 2;
114   ring r = 0,(x,y,z),wp(1,2,3);
115   is_homog(x5-yz+y3);
116   ideal i = x6+y3+z2, x9-z3;
117   is_homog(i);
118   ring s = 0,(a,b,c),ds;
119   vector v = [a2,0,ac+bc];
120   vector w = [a3,b3,c4];
121   is_homog(v);
122   is_homog(w);
123}
124///////////////////////////////////////////////////////////////////////////////
125
126proc is_zero
127USAGE:   is_zero(M[,any]); M=poly/ideal/vector/module/matrix
128RETURN:  integer, 1 if coker(M)=0 resp. 0 if coker(M)!=0, where M is considered
129         as matrix
130         if a second argument is given, return a list:
131                L[1] = 1 if coker(M)=0 resp. 0 if coker(M)!=0
132                L[2] = dim(M)
133EXAMPLE: example is_zero; shows examples
134{
135  int d=dim(std(#[1]));
136  int a = ( d==-1 );
137  if( size(#) >1 ) { list L=a,d; return(L); }
138  return(a);
139}
140example
141{ "EXAMPLE:";   echo=2;
142  ring r;
143  module m = [x],[y],[1,z];
144  is_zero(m,1);
145  qring q = std(ideal(x2+y3+z2));
146  ideal j = x2+y3+z2-37;
147  is_zero(j);
148}
149////////////////////////////////////////////////////////////////////////////////
150
151proc maxcoef (f)
152USAGE:   maxcoef(f);  f  poly/ideal/vector/module/matrix
153RETURN:  maximal length of coefficient of f of type int (by counting the
154         length of the string of each coefficient)
155EXAMPLE: example maxcoef; shows examples
156{
157//----------------------------- procedure body --------------------------------
158   int max,s,ii,jj; string t;
159   ideal i = ideal(matrix(f));
160   i = simplify(i,6);            // delete 0's and keep first of equal elements
161   poly m = var(1); matrix C;
162   for (ii=2;ii<=nvars(basering);ii=ii+1) { m = m*var(ii); }
163   for (ii=1; ii<=size(i); ii=ii+1)
164   {
165      C = coef(i[ii],m);
166      for (jj=1; jj<=ncols(C); jj=jj+1)
167      {
168         t = string(C[2,jj]);  s = size(t);
169         if ( t[1] == "-" ) { s = s - 1; }
170         if ( s > max ) { max = s; }
171      }
172   }
173   return(max);
174}
175//-------------------------------- examples -----------------------------------
176example
177{ "EXAMPLE:"; echo = 2;
178   ring r= 0,(x,y,z),ds;
179   poly g = 345x2-1234567890y+7/4z;
180   maxcoef(g);
181   ideal i = g,10/1234567890;
182   maxcoef(i);
183   // since i[2]=1/123456789
184}
185///////////////////////////////////////////////////////////////////////////////
186
187proc maxdeg (id)
188USAGE:   maxdeg(id);  id  poly/ideal/vector/module/matrix
189RETURN:  int/intmat, each component equals maximal degree of monomials in the
190         corresponding component of id, independent of ring ordering
191         (maxdeg of each var is 1)
192         of type int if id is of type poly, of type intmat else
193NOTE:    proc maxdeg1 returns 1 integer, the absolut maximum; moreover, it has
194         an option for computing weighted degrees
195EXAMPLE: example maxdeg; shows examples
196{
197   //-------- subprocedure to find maximal degree of given component ----------
198   proc findmaxdeg
199   {
200      poly c = #[1];
201      if (c==0) { return(-1); }
202   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
203      int d = (deg(c)>=0)*deg(c)-(deg(c)<0)*deg(c);
204      int i = d;
205      while ( c-jet(c,i) != 0 ) { i = 2*(i+1); }
206      int o = i-1;
207      int u = (d != i)*((i/ 2)-1);
208   //----------------------- "quick search" for maxdeg ------------------------
209      while ( (c-jet(c,i)==0)*(c-jet(c,i-1)!=0) == 0)
210      {
211         i = (o+1+u)/ 2;
212         if (c-jet(c,i)!=0) { u = i+1; }
213         else { o = i-1; }
214      }
215      return(i);
216   }
217//------------------------------ main program ---------------------------------
218   matrix M = matrix(id);
219   int r,c = nrows(M), ncols(M); int i,j;
220   intmat m[r][c];
221   for (i=r; i>0; i=i-1)
222   {
223      for (j=c; j>0; j=j-1) { m[i,j] = findmaxdeg(M[i,j]); }
224   }
225   if (typeof(id)=="poly") { return(m[1,1]); }
226   return(m);
227}
228//-------------------------------- examples -----------------------------------
229example
230{ "EXAMPLE:"; echo = 2;
231   ring r = 0,(x,y,z),wp(-1,-2,-3);
232   poly f = x+y2+z3;
233   deg(f);               //deg; returns weighted degree (in case of 1 block)!
234   maxdeg(f);
235   matrix m[2][2]=f+x10,1,0,f^2;
236   maxdeg(m);
237}
238///////////////////////////////////////////////////////////////////////////////
239
240proc maxdeg1 (id,list #)
241USAGE:   maxdeg1(id[,v]);  id=poly/ideal/vector/module/matrix, v=intvec
242RETURN:  integer, maximal [weighted] degree of monomials of id independent of
243         ring ordering, maxdeg1 of i-th variable is v[i] (default: v=1..1).
244NOTE:    This proc returns one integer while maxdeg returns, in general,
245         a matrix of integers. For one polynomial and if no intvec v is given
246         maxdeg is faster
247EXAMPLE: example maxdeg1; shows examples
248{
249   //-------- subprocedure to find maximal degree of given component ----------
250   proc findmaxdeg
251   {
252      poly c = #[1];
253      if (c==0) { return(-1); }
254      intvec v = #[2];
255   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
256      int d = (deg(c)>=0)*deg(c)-(deg(c)<0)*deg(c);
257      int i = d;
258      if ( c == jet(c,-1,v))      //case: maxdeg is negative
259      {
260         i = -d;
261         while ( c == jet(c,i,v) ) { i = 2*(i-1); }
262         int o = (d != -i)*((i/ 2)+2) - 1;
263         int u = i+1;
264         int e = -1;
265      }
266      else                        //case: maxdeg is nonnegative
267      {
268         while ( c != jet(c,i,v) ) { i = 2*(i+1); }
269         int o = i-1;
270         int u = (d != i)*((i/ 2)-1);
271         int e = 1;
272      }
273   //----------------------- "quick search" for maxdeg ------------------------
274      while ( ( c==jet(c,i,v) )*( c!=jet(c,i-1,v) ) == 0 )
275      {
276         i = (o+e+u)/ 2;
277         if ( c!=jet(c,i,v) ) { u = i+1; }
278         else { o = i-1; }
279      }
280      return(i);
281   }
282//------------------------------ main program ---------------------------------
283   ideal M = simplify(ideal(matrix(id)),8);   //delete scalar multiples from id
284   int c = ncols(M);
285   int i,n;
286   if( size(#)==0 )
287   {
288      int m = maxdeg(M[c]);
289      for (i=c-1; i>0; i=i-1)
290      {
291          n = maxdeg(M[i]);
292          m = (m>=n)*m + (m<n)*n;             //let m be the maximum of m and n
293      }
294   }
295   else
296   {
297      intvec v=#[1];                          //weight vector for the variables
298      int m = findmaxdeg(M[c],v);
299      for (i=c-1; i>0; i--)
300      {
301         n = findmaxdeg(M[i],v);
302         if( n>m ) { m=n; }
303      }
304   }
305   return(m);
306}
307//-------------------------------- examples -----------------------------------
308example
309{ "EXAMPLE:"; echo = 2;
310   ring r = 0,(x,y,z),wp(-1,-2,-3);
311   poly f = x+y2+z3;
312   deg(f);                  //deg returns weighted degree (in case of 1 block)!
313   maxdeg1(f);
314   intvec v = ringweights(r);
315   maxdeg1(f,v);                             //weighted maximal degree
316   matrix m[2][2]=f+x10,1,0,f^2;
317   maxdeg1(m,v);                             //absolut weighted maximal degree
318}
319///////////////////////////////////////////////////////////////////////////////
320
321proc mindeg (id)
322USAGE:   mindeg(id);  id  poly/ideal/vector/module/matrix
323RETURN:  minimal degree/s of monomials of id, independent of ring ordering
324         (mindeg of each variable is 1) of type int if id of type poly, else
325         of type intmat.
326NOTE:    proc mindeg1 returns one integer, the absolut minimum; moreover it
327         has an option for computing weighted degrees.
328EXAMPLE: example mindeg; shows examples
329{
330   //--------- subprocedure to find minimal degree of given component ---------
331   proc findmindeg
332   {
333      poly c = #[1];
334      if (c==0) { return(-1); }
335   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
336      int d = (ord(c)>=0)*ord(c)-(ord(c)<0)*ord(c);
337      int i = d;
338      while ( jet(c,i) == 0 ) { i = 2*(i+1); }
339      int o = i-1;
340      int u = (d != i)*((i/ 2)-1);
341   //----------------------- "quick search" for mindeg ------------------------
342      while ( (jet(c,u)==0)*(jet(c,o)!=0) )
343      {
344         i = (o+u)/ 2;
345         if (jet(c,i)==0) { u = i+1; }
346         else { o = i-1; }
347      }
348      if (jet(c,u)!=0) { return(u); }
349      else { return(o+1); }
350   }
351//------------------------------ main program ---------------------------------
352   matrix M = matrix(id);
353   int r,c = nrows(M), ncols(M); int i,j;
354   intmat m[r][c];
355   for (i=r; i>0; i=i-1)
356   {
357      for (j=c; j>0; j=j-1) { m[i,j] = findmindeg(M[i,j]); }
358   }
359   if (typeof(id)=="poly") { return(m[1,1]); }
360   return(m);
361}
362//-------------------------------- examples -----------------------------------
363example
364{ "EXAMPLE:"; echo = 2;
365   ring r = 0,(x,y,z),ls;
366   poly f = x5+y2+z3;
367   ord(f);                      // ord returns weighted order of leading term!
368   mindeg(f);                   // computes minimal degree
369   matrix m[2][2]=x10,1,0,f^2;
370   mindeg(m);                   // computes matrix of minimum degrees
371}
372///////////////////////////////////////////////////////////////////////////////
373
374proc mindeg1 (id, list #)
375USAGE:   mindeg1(id[,v]);  id=poly/ideal/vector/module/matrix, v=intvec
376RETURN:  integer, minimal [weighted] degree of monomials of id independent of
377         ring ordering, mindeg1 of i-th variable is v[i] (default v=1..1).
378NOTE:    This proc returns one integer while mindeg returns, in general,
379         a matrix of integers. For one polynomial and if no intvec v is given
380         mindeg is faster.
381EXAMPLE: example mindeg1; shows examples
382{
383   //--------- subprocedure to find minimal degree of given component ---------
384   proc findmindeg
385   {
386      poly c = #[1];
387      intvec v = #[2];
388      if (c==0) { return(-1); }
389   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
390      int d = (ord(c)>=0)*ord(c)-(ord(c)<0)*ord(c);
391      int i = d;
392      if ( jet(c,-1,v) !=0 )      //case: mindeg is negative
393      {
394         i = -d;
395         while ( jet(c,i,v) != 0 ) { i = 2*(i-1); }
396         int o = (d != -i)*((i/ 2)+2) - 1;
397         int u = i+1;
398         int e = -1; i=u;
399      }
400      else                        //case: inded is nonnegative
401      {
402         while ( jet(c,i,v) == 0 ) { i = 2*(i+1); }
403         int o = i-1;
404         int u = (d != i)*((i/ 2)-1);
405         int e = 1; i=u;
406      }
407   //----------------------- "quick search" for mindeg ------------------------
408      while ( (jet(c,i-1,v)==0)*(jet(c,i,v)!=0) == 0 )
409      {
410         i = (o+e+u)/ 2;
411         if (jet(c,i,v)==0) { u = i+1; }
412         else { o = i-1; }
413      }
414      return(i);
415   }
416//------------------------------ main program ---------------------------------
417   ideal M = simplify(ideal(matrix(id)),8);   //delete scalar multiples from id
418   int c = ncols(M);
419   int i,n;
420   if( size(#)==0 )
421   {
422      int m = mindeg(M[c]);
423      for (i=c-1; i>0; i=i-1)
424      {
425          n = mindeg(M[i]);
426          m = (m<=n)*m + (m>n)*n;             //let m be the maximum of m and n
427      }
428   }
429   else
430   {
431      intvec v=#[1];                          //weight vector for the variables
432      int m = findmindeg(M[c],v);
433      for (i=c-1; i>0; i=i-1)
434      {
435         n = findmindeg(M[i],v);
436         m = (m<=n)*m + (m>n)*n;              //let m be the maximum of m and n
437      }
438   }
439   return(m);
440}
441//-------------------------------- examples -----------------------------------
442example
443{ "EXAMPLE:"; echo = 2;
444   ring r = 0,(x,y,z),ls;
445   poly f = x5+y2+z3;
446   ord(f);                      // ord returns weighted order of leading term!
447   intvec v = 1,-3,2;
448   mindeg1(f,v);                // computes minimal weighted degree
449   matrix m[2][2]=x10,1,0,f^2;
450   mindeg1(m,1..3);             // computes absolut minimum of weighted degrees
451}
452///////////////////////////////////////////////////////////////////////////////
453
454proc normalize (id)
455USAGE:   normalize(id);  id=poly/vector/ideal/module
456RETURN:  object of same type with leading coefficient equal to 1
457EXAMPLE: example normalize; shows an example
458{
459   return(simplify(id,1));
460}
461//-------------------------------- examples -----------------------------------
462example
463{  "EXAMPLE:"; echo = 2;
464   ring r = 0,(x,y,z),ls;
465   poly f = 2x5+3y2+4z3;
466   normalize(f);
467   module m=[9xy,0,3z3],[4z,6y,2x];
468   normalize(m);
469   ring s = 0,(x,y,z),(c,ls);
470   module m=[9xy,0,3z3],[4z,6y,2x];
471   normalize(m);
472   normalize(matrix(m));             // by automatic type conversion to module!
473}
474///////////////////////////////////////////////////////////////////////////////
475
Note: See TracBrowser for help on using the repository browser.