source: git/Tst/Old/lib0.test @ 882ae9c

spielwiese
Last change on this file since 882ae9c was b35b93, checked in by Olaf Bachmann <obachman@…>, 26 years ago
This commit was generated by cvs2svn to compensate for changes in r1396, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@1397 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 35.8 KB
Line 
1// $Id: lib0.test,v 1.1.1.1 1998-04-17 15:06:54 obachman Exp $
2//system("random",787422842);
3//(GMG)
4///////////////////////////////////////////////////////////////////////////////
5
6LIBRARY:  lib0.lib       PROCEDURES OF GENERAL TYPE
7
8 A_Z("a",n);             string of n comma seperated letters starting with a
9 binomial(n,m[,int/str]);n choose m (type int), [type string/type number]
10 changechar(r,"c","R");  makes a copy R of ring r with new char c the basering
11 changeord(r,"ord","R"); makes a copy R of ring r with new ord the basering
12 changevar(r,"vars","R");same as copyring
13 copyring(r,"vars","R"); makes a copy R of ring r with new vars the basering
14 copyring1(r,"vars","R");string to make a copy R of ring r with new variables
15 defrings(n[,p]);        define ring Sn in n vars, char 32003 [or p], ds
16 defringp(n[,p]);        define ring Pn in n vars, char 32003 [or p], dp
17 factorial(n[,int/str]); n factorial (=n!) (type int), [type string/number]
18 fetchall(R[,str]);      fetch all objects of ring R to basering
19 fibonacci(n[,p]);       nth Fibonacci number [char p]
20 ishomog(poly/...);      int, =1 resp. =0 if input is homogeneous resp. not
21 kmemory();              int = active memory (kilobyte)
22 killall();              kill all user-defined variables
23 imapall(R [,str]);      imap all objects of ring R to basering
24 mapall(R,i[,str]);      map all objects of ring R via ideal i to basering
25 maxcoef(poly/...);      maximal length of coefficient occuring in poly/...
26 maxdeg(poly/...);       int/intmat = degree/s of terms of maximal order
27 mindeg(poly/...);       int/intmat = degree/s of terms of minimal order
28 normalize(poly/...);    normalize poly/... such that leading coefficient is 1
29 primes(n,m);            intvec of primes p, n<=p<=m
30 product(vector/..[,v]); multiply components of vector/ideal/...[indices v]
31 ringsum(s,t,..."r");    create a ring r from existing rings s,t,...
32 ringweights(r);         intvec of weights of ring variables of ring r
33 sort(ideal/module);     sort generators according to monomial ordering
34 sum(vector/id/..[,v]);  add components of vector/ideal/...[with indices v]
35           (parameters in square brackets [] are optional)
36
37LIB "inout.lib";
38///////////////////////////////////////////////////////////////////////////////
39
40proc A_Z (string s,int n)
41USAGE:   A_Z("a",n);  a any letter, n integer (-26<= n <=26, !=0)
42RETURN:  string of n small (if a is small) or capital (if a is capital)
43         letters, comma seperated, beginning with a, in alphabetical
44         order (or revers alphabetical order if n<0)
45EXAMPLE: example A_Z; shows an example
46{
47  if ( n>=-26 and n<=26 and n!=0 )
48  {
49    string alpha =
50    "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"+
51    "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"+
52    "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"+
53    "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
54    int ii; int aa;
55    for(ii=1; ii<=51; ii=ii+2)
56    {
57       if( alpha[ii]==s ) { aa=ii; }
58    }
59    if ( aa==0)
60    {
61      for(ii=105; ii<=155; ii=ii+2)
62      {
63        if( alpha[ii]==s ) { aa=ii; }
64      }
65    }
66    if( aa!=0 )
67    {
68      string out;
69      if (n > 0) { out = alpha[aa,2*(n)-1];  return (out); }
70      if (n < 0)
71      {
72        string beta =
73        "z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c,b,a,"+
74        "z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c,b,a,"+
75        "Z,Y,X,W,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A,"+
76        "Z,Y,X,W,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A";
77        if ( aa < 52 ) { aa=52-aa; }
78        if ( aa > 104 ) { aa=260-aa; }
79        out = beta[aa,2*(-n)-1]; return (out);
80      }
81    }
82  }
83}
84example
85{ "EXAMPLE:"; echo = 2;
86   A_Z("c",5);
87   A_Z("Z",-5);
88   string sR = "ring R = (0,"+A_Z("A",6)+"),("+A_Z("a",10)+"),dp;";
89   sR;
90   execute sR;
91   R;
92}
93///////////////////////////////////////////////////////////////////////////////
94
95proc binomial (int n, int k, list #)
96USAGE:   binomial(n,k[,p/s]); n,k,p integers, s string
97RETURN:  binomial(n,k);    binomial coefficient n choose k of type int
98                           (machine integer, limited size! )
99         binomial(n,k,p);  n choose k in char p of type string
100         binomial(n,k,s);  n choose k of type number (s any string), computed
101                           in char of basering if a basering is defined
102EXAMPLE: example binomial; shows an example
103{
104   if ( size(#)==0 ) { int rr=1; }
105   if ( typeof(#[1])=="int") { ring bin = #[1],x,dp; number rr=1; }
106   if ( typeof(#[1])=="string") { number rr=1; }
107   if ( size(#)==0 or typeof(#[1])=="int" or typeof(#[1])=="string" )
108   {
109      def r = rr;
110      if ( k<=0 or k>n ) { return((k==0)*r); }
111      if ( k>=n-k ) { k = n-k; }
112      int l;
113      for (l=1 ; l<=k ; l++ )
114      {
115         r=r*(n+1-l)/l;
116      }
117      if ( typeof(#[1])=="int" ) { return(string(r)); }
118      return(r);
119   }
120}
121example
122{ "EXAMPLE:"; echo = 2;
123   int b1 = binomial(10,7); b1;
124   binomial(37,17,0);
125   ring t = 31,x,dp;
126   number b2 = binomial(37,17,""); b2;
127}
128///////////////////////////////////////////////////////////////////////////////
129
130proc changechar (r, string c, string newr)
131USAGE:   changechar(r,c,newr);  r=ring/qring, c,newr=strings
132CREATE:  creates a new ring with name `newr` and makes it the basering if r is
133         an existing ring/qring.
134         The new ring differs from the old ring only in the characteristic. If,
135         say, (c,newr) = ("0,A","R") and the ring r exists, the new basering
136         will have name R characteristic 0 and one parameter A.
137RETURN:  No return value
138NOTE:    //*** Buggy for qrings
139EXAMPLE: example changechar; shows an example
140{
141   setring r;
142   ideal i = ideal(r); int q = size(i);
143   if( q!=0 )
144      { string s = "newr1"; }
145   else
146      { string s = newr; }
147   string newring = s+"=("+c+"),("+varstr(r)+"),("+ordstr(r)+");";
148   execute("ring "+newring);
149   if( q!=0 )
150   {
151      map phi = r,maxideal(1);
152      ideal i = phi(i);
153      attrib(i,"isSB",1);         //*** attrib funktioniert ?
154      execute("qring "+newr+"=i;");
155   }
156   export(`newr`);
157   keepring(`newr`);
158   return();
159}
160example
161{  "EXAMPLE:"; echo = 2;
162   ring r=0,(x,y,u,v),(dp(2),ds);
163   show(r);
164   changechar(r,"2,A","R");
165   show(R);
166   kill R;
167}
168///////////////////////////////////////////////////////////////////////////////
169
170proc changeord (r, string o, string newr)
171USAGE:   changeord(r,o,newr);  r=ring/qring, o,newr=strings
172CREATE:  creates a new ring with name `newr` and makes it the basering if r is
173         an existing ring/qring.
174         The new ring differs from the old ring only in the ordering. If, say,
175         (o,newr) = ("wp(2,3),dp","R") and the ring r exists and has >=3
176         variables, the new basering will have name R and ordering wp(2,3),dp.
177RETURN:  No return value
178EXAMPLE: example changeord; shows an example
179{
180   setring r;
181   ideal i = ideal(r); int q = size(i);
182   if( q!=0 )
183      { string s = "newr1"; }
184   else
185      { string s = newr; }
186   string newring = s+"=("+charstr(r)+"),("+varstr(r)+"),("+o+");";
187   execute("ring "+newring);
188   if( q!=0 )
189   {
190      map phi = r,maxideal(1);
191      ideal i = phi(i);
192      attrib(i,"isSB",1);         //*** attrib funktioniert ?
193      execute("qring "+newr+"=i;");
194   }
195   export(`newr`);
196   keepring(`newr`);
197   return();
198}
199example
200{  "EXAMPLE:"; echo = 2;
201   ring r=0,(x,y,u,v),(dp(2),ds);
202   changeord(r,"wp(2,3),dp","R");
203   show(R);
204   ideal i = x^2,y^2-u^3,v;
205   qring Q = std(i);
206   changeord(Q,"lp","Q'");
207   show(Q');
208   kill R,Q,Q';
209}
210///////////////////////////////////////////////////////////////////////////////
211
212proc changevar (r, string vars, string newr)
213USAGE:   changevar(r,vars,newr);  r=ring/qring, vars,newr=strings
214CREATE:  creates a new ring with name `newr` and makes it the basering if r is
215         an existing ring/qring.
216NOTE:    This procedure is the same as copyring
217EXAMPLE: example changevar; shows an example
218{
219   copyring(r,vars,newr);
220}
221example
222{  "EXAMPLE:"; echo = 2;
223   ring r=0,(x,y,u,v),(dp(2),ds);
224   changevar(r,"A()","R");
225   show(R);
226   ideal i = x^2,y^2-u^3,v;
227   qring Q = std(i);
228   changevar(Q,"a,b,c,d","Q'");
229   show(Q');
230   kill R,Q,Q';
231}
232///////////////////////////////////////////////////////////////////////////////
233
234proc copyring (r, string vars, string newr)
235USAGE:   copyring(r,vars,newr);  r ring/qring, vars, newr strings
236CREATE:  creates a new ring with name `newr` and makes it the basering if r is
237         an existing ring/qring.
238         The new ring differs from the old ring only in the variables. If, say,
239         (vars,newr) = ("t()","R") and the ring r exists and has n variables,
240         the new basering will have name R and variables t(1),...,t(n).
241         If vars = "a,b,c,d", the new ring will have the variables a,b,c,d.
242RETURN:  No return value
243NOTE:    This procedure is useful in connection with the procedure ringsum,
244         when a conflict between variable names must be avoided. See proc
245         copyring1 for an alternative
246EXAMPLE: example copyring; shows an example
247{
248   setring r;
249   ideal i = ideal(r); int q = size(i);
250   if( q!=0 )
251      { string s = "newr1"; }
252   else
253      { string s = newr; }
254   string newring = s+"=("+charstr(r)+"),(";
255   if( vars[size(vars)-1]=="(" and vars[size(vars)]==")" )
256   {
257      newring = newring+vars[1,size(vars)-2]+"(1.."+string(nvars(r))+")";
258   }
259   else { newring = newring+vars; }
260   newring = newring+"),("+ordstr(r)+");";
261   execute("ring "+newring);
262   if( q!=0 )
263   {
264      map phi = r,maxideal(1);
265      ideal i = phi(i);
266      attrib(i,"isSB",1);         //*** attrib funktioniert ?
267      execute("qring "+newr+"=i;");
268   }
269   export(`newr`);
270   keepring(`newr`);
271   return();
272}
273example
274{  "EXAMPLE:"; echo = 2;
275   ring r=0,(x,y,u,v),(dp(2),ds);
276   copyring(r,"A()","R");
277   type R;
278   ideal i = A(1)^2,A(2)^2-A(3)^3,A(4);
279   qring Q = std(i);
280   copyring(Q,"a,b,c,d","Q'");
281   type Q';
282   kill R,Q,Q';
283}
284///////////////////////////////////////////////////////////////////////////////
285
286proc copyring1 (r, string vars, string newr)
287USAGE:   copyring1(r,vars,newr);  r ring, vars, newr strings
288RETURN:  a string which can be executed to define a new ring with name equal
289         to `newr` if r is an existing ring name.
290         The new ring differs from the old ring only in the variables. If, say,
291         (vars,newr) = ("t()","R") and the ring r exists and has n variables,
292         the new basering will have name R and variables t(1),...,t(n).
293         If vars = "a,b,c,d", the new ring will have the variables a,b,c,d.
294NOTE:    This procedure differs from copyring that it returns the string to
295         create newring, but does not execute this string. Contrary to
296         copyring this procedure does not work for a qring
297EXAMPLE: example copyring1; shows an example
298{
299   string newring = "ring "+newr+"=("+charstr(r)+"),(";
300   if( vars[size(vars)-1]=="(" and vars[size(vars)]==")" )
301        { string v = vars[1,size(vars)-2]+"(1.."+string(nvars(r))+")"; }
302   else { string v = vars; }
303   return(newring+v+"),("+ordstr(r)+");");
304}
305example
306{  "EXAMPLE:"; echo = 2;
307   ring r=0,(x,y,u,v),(dp(2),ds);
308   string s=copyring1(r,"A()","R");s;
309   execute(s);
310   type R;
311   execute(copyring1(R,"a,b,c,d,e","r'"));
312   type r';
313   kill R,r';
314}
315///////////////////////////////////////////////////////////////////////////////
316
317proc defring (string s1, string s2, int n, string s3, string s4)
318USAGE:   defring(s1,s2,n,s3,s4);  s1..s4=strings, n=integer
319CREATE:  Defines a ring with name 's1', characteristic 's2', ordering 's4' and
320         n variables with names derived from s3: if s3 is a single letter, say
321         s3="a", and if n<=26 then a and the following n-1 letters from the
322         alphabeth (cyclic order) are taken as variables. If n>26 or if s3 is
323         a single letter followed by (, say s3="T(", the variables are
324         T(1),...,T(n).
325RETURN:  No return value
326EXAMPLE: example defring; shows an example
327{
328   string newring = "ring "+s1+"=("+s2+"),(";
329   if( n>26 or s3[2]=="(" ) { string v = s3[1]+"(1.."+string(n)+")"; }
330   else { string v = A_Z(s3,n); }
331   newring=newring+v+"),("+s4+");";
332   execute(newring);
333   export(basering);
334   keepring(`s1`);
335   if (voice==2) { "// basering is now:",s1; }
336   return();
337}
338example
339{ "EXAMPLE:"; echo = 2;
340   defring("r","0",5,"u","ls"); r;
341   defring("R","2,A",10,"x(","dp(3),ws(1,2,3),ds"); R;
342   kill r,R;
343}
344///////////////////////////////////////////////////////////////////////////////
345
346proc defrings (int n, list #)
347USAGE:   defrings(n,[p]);  n,p integers
348CREATE:  Defines a ring with name Sn, characteristic p, ordering ds and n
349         variables x,y,z,a,b,...if n<=26 (resp. x(1..n) if n>26) and makes it
350         the basering (default: p=32003)
351RETURN:  No return value
352EXAMPLE: example defrings; shows an example
353{
354   int p;
355   if (size(#)==0) { p=32003; }
356   else { p=#[1]; }
357   if (n >26)
358   {
359      string s="ring S"+string(n)+"="+string(p)+",x(1.."+string(n)+"),ds;";
360   }
361   else
362   {
363      string s="ring S"+string(n)+"="+string(p)+",("+A_Z("x",n)+"),ds;";
364   }
365   execute(s);
366   export basering;
367   execute("keepring S"+string(n)+";");
368   if (voice==2) { "// basering is now:",s; }
369}
370example
371{ "EXAMPLE:"; echo = 2;
372   defrings(5,0); S5;
373   defrings(30); S30;
374   kill S5, S30;
375}
376///////////////////////////////////////////////////////////////////////////////
377
378proc defringp (int n,list #)
379USAGE:   defringp(n,[p]);  n,p=integers
380CREATE:  defines a ring with name Pn, characteristic p, ordering dp and n
381         variables x,y,z,a,b,...if n<=26 (resp. x(1..n) if n>26) and makes it
382         the basering (default: p=32003)
383RETURN:  No return value
384EXAMPLE: example defringp; shows an example
385{
386   int p;
387   if (size(#)==0) { p=32003; }
388   else { p=#[1]; }
389   if (n >26)
390   {
391      string s="ring P"+string(n)+"="+string(p)+",x(1.."+string(n)+"),dp;";
392   }
393   else
394   {
395     string s="ring P"+string(n)+"="+string(p)+",("+A_Z("x",n)+"),dp;";
396   }
397   execute(s);
398   export basering;
399   execute("keepring P"+string(n)+";");
400   //the next comment is only shown if defringp is not called by another proc
401   if (voice==2) { "// basering is now:",s; }
402}
403example
404{ "EXAMPLE:"; echo = 2;
405   defringp(5,0); P5;
406   defringp(30); P30;
407   kill P5, P30;
408}
409///////////////////////////////////////////////////////////////////////////////
410
411proc factorial (int n, list #)
412USAGE:   factorial(n[,string]);  n integer
413RETURN:  factorial(n); string of n! in char 0
414         factorial(n,s);  n! of type number (s any string), computed in char of
415         basering if a basering is defined
416EXAMPLE: example factorial; shows an example
417{
418   if ( size(#)==0 ) { ring R = 0,x,dp; poly r=1; }
419   if ( typeof(#[1])=="string" ) { number r=1; }
420   if ( size(#)==0 or typeof(#[1])=="string" )
421   {
422      int l;
423      for (l=2; l<=n; l++)
424      {
425         r=r*l;
426      }
427      if ( size(#)==0 ) { return(string(r)); }
428      return(r);
429   }
430}
431example
432{ "EXAMPLE:"; echo = 2;
433   factorial(37);
434   ring r1 = 32003,(x,y,z),ds;
435   number p = factorial(37,""); p;
436}
437///////////////////////////////////////////////////////////////////////////////
438
439proc fetchall (R, list #)
440USAGE:   fetchall(R[,s]);  R=ring/qring, s=string
441CREATE:  fetch all objects of ring R (of type poly, ideal, vector, module,
442         number, matrix) into the basering.
443         If no 3rd argument is present, the names are the same as in R. If, say,
444         f is a poly in R and the 3rd argument is the string "R", then f is
445         maped to f_R etc.
446RETURN:  no return value
447NOTE:    As fetch, this procedure maps the 1st, 2nd, ... variable of R to the
448         1st, 2nd, ... variable of the basering.
449         The 3rd argument is useful in order to avoid conflicts of names, the
450         empty string is allowed
451CAUTION: fetchall does not work inside a procedure
452         //***at the moment it does not work if R contains a map
453EXAMPLE: example fetchall; shows an example
454{
455   list @L@=names(R);
456   int @ii@; string @s@;
457   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
458   for( @ii@=size(@L@); @ii@>0; @ii@=@ii@-- )
459   {
460         execute("def "+@L@[@ii@]+@s@+"=fetch(R,`@L@[@ii@]`);");
461         execute("export "+@L@[@ii@]+@s@+";");
462   }
463   return();
464}
465example
466{  "EXAMPLE:";
467"// This example is not executed since fetchall does not work in a procedure";
468"// (and hence not in the example procedure). Just try the following commands:";
469"   ring R=0,(x,y,z),dp;";
470"   ideal j=x,y2,z2;";
471"   matrix M[2][3]=1,2,3,x,y,z;";
472"   j; print(M);";
473"   ring S=0,(a,b,c),ds;";
474"   fetchall(R);           // map from R to S: x->a, y->b, z->c";
475"   names(S);";
476"   j; print(M);";
477"   fetchall(S,\"1\");       // identity map of S: copy objects, change names";
478"   names(S);";
479"   kill R,S;";
480}
481///////////////////////////////////////////////////////////////////////////////
482
483proc fibonacci (int n, list #)
484USAGE:   fibonacci(n[,string]);  (n integer)
485RETURN:  fibonacci(n); string of nth Fibonacci number,
486            f(0)=f(1)=1, f(i+1)=f(i-1)+f(i)
487         fibonacci(n,s);  nth Fibonacci number of type number (s any string),
488         computed in characteristic of basering if a basering is defined
489EXAMPLE: example fibonacci; shows an example
490{
491   if ( size(#)==0 ) { ring fibo = 0,x,dp; number f=1; }
492   if ( typeof(#[1])=="string" ) { number f=1; }
493   if ( size(#)==0 or typeof(#[1])=="string" )
494   {
495      number g,h = 1,1; int ii;
496      for (ii=3; ii<=n; ii++)
497      {
498         h = f+g; f = g; g = h;
499      }
500      if ( size(#)==0 ) { return(string(h)); }
501      return(h);
502   }
503}
504example
505{ "EXAMPLE:"; echo = 2;
506   fibonacci(37);
507   ring r = 17,x,dp;
508   number b = fibonacci(37,""); b;
509}
510///////////////////////////////////////////////////////////////////////////////
511
512proc ishomog (id)
513USAGE:   ishomog(id);  id  poly/ideal/vector/module/matrix
514RETURN:  integer which is 1 if input is homogeneous (resp. weighted homogeneous
515         if the monomial ordering consists of one block of type ws,Ws,wp or Wp,
516         assuming that all weights are positive) and 0 otherwise
517NOTE:    A vector is homogeneous, if the components are homogeneous of same
518         degree, a module/matrix is homogeneous if all column vectors are
519         homogeneous
520         //*** ergaenzen, wenn Matrizen Spalten Gewichte haben
521EXAMPLE: example ishomog; shows an example
522{
523   module M = module(matrix(id));
524   M = simplify(M,2);                        // remove 0-columns
525   intvec v = ringweights(basering);
526   int i,j=1,1;
527   for (i=1; i<=ncols(M); i++)
528   {
529      if( M[i]!=jet(M[i],deg(lead(M[i])),v)-jet(M[i],deg(lead(M[i]))-1,v))
530      { return(0); }
531   }
532   return(1);
533}
534example
535{ "EXAMPLE:"; echo = 2;
536   ring r = 0,(x,y,z),wp(1,2,3);
537   ishomog(x5-yz+y3);
538   ideal i = x6+y3+z2, x9-z3;
539   ishomog(i);
540   ring s = 0,(a,b,c),ds;
541   vector v = [a2,0,ac+bc];
542   vector w = [a3,b3,c4];
543   ishomog(v);
544   ishomog(w);
545}
546///////////////////////////////////////////////////////////////////////////////
547
548proc kmemory ()
549USAGE:   kmemory();
550RETURN:  memory used by active variables, of type int (in kilobyte)
551EXAMPLE: example kmemory; shows an example
552{
553  if ( voice==2 ) { "// memory used by active variables (kilobyte):"; }
554   return ((memory(0)+1023)/1024);
555}
556example
557{ "EXAMPLE:"; echo = 2;
558   kmemory();
559}
560///////////////////////////////////////////////////////////////////////////////
561
562proc killall ()
563USAGE:   killall(); (no parameter)
564CREATE:  kill all user-defined variables but not loaded procedures
565RETURN:  no return value
566NOTE:    killall should never be used inside a procedure
567EXAMPLE: example killall; shows an example AND KILLS ALL YOUR VARIABLES
568{
569   list L=names(); int joni=size(L);
570   for ( ; joni>0; joni-- )
571   {
572      joni,L[joni],typeof(`L[joni]`),
573      L[joni]!="LIB",
574      typeof(`L[joni]`)!="proc",
575      L[joni]!="LIB" and typeof(`L[joni]`)!="proc";
576      if( L[joni]!="LIB" and typeof(`L[joni]`)!="proc" ) { joni;"kill"+L[joni];kill `L[joni]`; }
577   }
578}
579example
580{ "EXAMPLE:"; echo = 2;
581   ring rtest; ideal i=x,y,z; number n=37; string str="hi";
582   export rtest,i,n,str;     //this makes the local variables global
583   listvar(all);
584   killall();
585   listvar(all);
586}
587///////////////////////////////////////////////////////////////////////////////
588
589proc imapall (R, list #)
590USAGE:   imapall(R[,s]);  R=ring/qring, s=string
591CREATE:  map all objects of ring R (of type poly, ideal, vector, module, number,
592         matrix) into the basering, by applying imap to all objects of R.
593         If no 3rd argument is present, the names are the same as in R. If, say,
594         f is a poly in R and the 3rd argument is the string "R", then f is
595         maped to f_R etc.
596RETURN:  no return value
597NOTE:    As imap, this procedure maps the variables of R to the variables with
598         the same name in the basering, the other variables are maped to 0.
599         The 3rd argument is useful in order to avoid conflicts of names, the
600         empty string is allowed
601CAUTION: imapall does not work inside a procedure
602         //***at the moment it does not work if R contains a map
603EXAMPLE: example imapall; shows an example
604{
605   list @L@=names(R);
606   int @ii@; string @s@;
607   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
608   for( @ii@=size(@L@); @ii@>0; @ii@=@ii@-- )
609   {
610         execute("def "+@L@[@ii@]+@s@+"=imap(R,`@L@[@ii@]`);");
611         execute("export "+@L@[@ii@]+@s@+";");
612   }
613   return();
614}
615example
616{  "EXAMPLE:";
617"// This example is not executed since imapall does not work in a procedure";
618"// (and hence not in the example procedure). Just try the following commands:";
619"   ring R=0,(x,y,z,u),dp;";
620"   ideal j=x,y,z,u2+ux+z;";
621"   matrix M[2][3]=1,2,3,x,y,uz;";
622"   j; print(M);";
623"   ring S=0,(a,b,c,x,z,y),ds;";
624"   imapall(R);           // map from R to S: x->x, y->y, z->z, u->0";
625"   names(S);";
626"   j; print(M);";
627"   imapall(S,\"1\");       // identity map of S: copy objects, change names";
628"   names(S);";
629"   kill R,S;";
630}
631///////////////////////////////////////////////////////////////////////////////
632
633proc mapall (R, ideal i, list #)
634USAGE:   mapall(R,i[,s]);  R=ring/qring, i=ideal of basering, s=string
635CREATE:  map all objects of ring R (of type poly, ideal, vector, module, number,
636         matrix, map) into the basering, by mapping the jth variable of R to
637         the jth generator of the ideal i. If no 3rd argument is present, the
638         names are the same as in R. If, say, f is a poly in R and the 3rd
639         argument is the string "R", then f is maped to f_R etc.
640RETURN:  no return value
641NOTE:    This procedure has the same effect as defining a map, say psi, by
642         map psi=R,i; and then applying psi to all objects of R. In particular,
643         maps from R to some ring S are composed with psi, creating thus a map
644         from the basering to S.
645         mapall may be combined with copyring to change vars for all objects.
646         The 3rd argument is useful in order to avoid conflicts of names, the
647         empty string is allowed
648CAUTION: mapall does not work inside a procedure
649EXAMPLE: example mapall; shows an example
650{
651   list @L@=names(R); map @psi@ = R,i;
652   int @ii@; string @s@;
653   if( size(#) > 0 ) { @s@=@s@+"_"+#[1]; }
654   for( @ii@=size(@L@); @ii@>0; @ii@=@ii@-- )
655   {
656      execute("def "+@L@[@ii@]+@s@+"=@psi@(`@L@[@ii@]`);");
657      execute("export "+@L@[@ii@]+@s@+";");
658   }
659   return();
660}
661example
662{  "EXAMPLE:";
663"// This example is not executed since mapall does not work in a procedure";
664"// (and hence not in the example procedure). Just try the following commands:";
665"   ring R=0,(x,y,z),dp;";
666"   ideal j=x,y,z;";
667"   matrix M[2][3]=1,2,3,x,y,z;";
668"   map phi=R,x2,y2,z2; ";
669"   ring S=0,(a,b,c),ds;";
670"   ideal i=c,a,b;";
671"   mapall(R,i);             // map from R to S: x->c, y->a, z->b";
672"   names(S);";
673"   j; print(M); phi;        // phi is a map from R to S: x->c2, y->a2, z->b2";
674"   ideal i1=a2,a+b,1;";
675"   mapall(R,i1,\"\");         // map from R to S: x->a2, y->a+b, z->1";
676"   names(S);";
677"   j_; print(M_); phi_;";
678"   copyring(S,\"x()\",\"T\");";
679"   mapall(R,maxideal(1));   // identity map from R to T";
680"   names(T);";
681"   j; print(M); phi;";
682"   kill R,S,T;";
683}
684///////////////////////////////////////////////////////////////////////////////
685
686proc maxcoef (f)
687USAGE:   maxcoef(f);  f  poly/ideal/vector/module/matrix
688RETURN:  maximal length of coefficient of f of type int (by counting the
689         length of the string of each coefficient)
690EXAMPLE: example maxcoef; shows an example
691{
692   int max,s,ii,jj; string t;
693   ideal i = ideal(matrix(f));
694   i = simplify(i,6);         //* delete 0's and keep first of equal elements
695   poly m = var(1); matrix C;
696   for (ii=2;ii<=nvars(basering);ii++) { m = m*var(ii); }
697   for (ii=1; ii<=size(i); ii++)
698   {
699      C = coef(i[ii],m);
700      for (jj=1; jj<=ncols(C); jj++)
701      {
702         t = string(C[2,jj]);  s = size(t);
703         if ( t[1] == "-" ) { s = s - 1; }
704         if ( s > max ) { max = s; }
705      }
706   }
707   return(max);
708}
709example
710{ "EXAMPLE:"; echo = 2;
711   ring r= 0,(x,y,z),ds;
712   poly g = 345x2-1234567890y+7/4z;
713   maxcoef(g);
714   ideal i = g,10/1234567890;
715   maxcoef(i);
716   // since i[2]=1/123456789
717}
718///////////////////////////////////////////////////////////////////////////////
719
720proc maxdeg (id)
721USAGE:   maxdeg(id);  id  poly/ideal/vector/module/matrix
722RETURN:  maximal degree/s of monomials of id independent of ring ordering
723         (maxdeg of each variable is 1)
724         of type int if id is of type poly, of type intmat else
725EXAMPLE: example maxdeg; shows an example
726{
727//------------------- find maximal degree of given component ------------------
728   proc findmaxdeg
729   {
730      poly c = #[1];
731      if (c==0) { return(-1); }
732   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
733      int d = (deg(c)>=0)*deg(c)-(deg(c)<0)*deg(c);
734      int i = d;
735      while ( c-jet(c,i) != 0 ) { i = 2*(i+1); }
736      int o = i-1;
737      int u = (d != i)*((i/ 2)-1);
738   //----------------------- "quick search" for maxdeg ------------------------
739      while ( (c-jet(c,i)==0)*(c-jet(c,i-1)!=0) == 0)
740      {
741         i = (o+1+u)/ 2;
742         if (c-jet(c,i)!=0) { u = i+1; }
743         else { o = i-1; }
744      }
745      return(i);
746   }
747//------------------------------ main program ---------------------------------
748   matrix M = matrix(id);
749   int r,c = nrows(M), ncols(M); int i,j;
750   intmat m[r][c];
751   for (i=r; i>0; i--)
752   {
753      for (j=c; j>0; j--) { m[i,j] = findmaxdeg(M[i,j]); }
754   }
755   if( typeof(id)=="poly" ) { return(m[1,1]); }
756   return(m);
757}
758example
759{ "EXAMPLE:"; echo = 2;
760   ring r = 0,(x,y,z),wp(-1,-2,-3);
761   poly f = x+y2+z3;
762   deg(f);               //deg returns weighted degree (in case of 1 block)!
763   maxdeg(f);
764   matrix m[2][2]=f+x10,1,0,f^2;
765   maxdeg(m);
766}
767///////////////////////////////////////////////////////////////////////////////
768
769proc mindeg (id)
770USAGE:   mindeg(id);  id  poly/ideal/vector/module/matrix
771RETURN:  minimal degree/s of monomials of id independent of ring ordering
772         (mindeg of each variable is 1)
773         of type int if id is of type poly, of type intmat else
774EXAMPLE: example mindeg; shows an example
775{
776//------------------- find minimal degree of given component ------------------
777   proc findmindeg
778   {
779      poly c = #[1];
780      if (c==0) { return(-1); }
781   //--- guess upper 'o' and lower 'u' bound, in case of negative weights -----
782      int d = (ord(c)>=0)*ord(c)-(ord(c)<0)*ord(c);
783      int i = d;
784      while ( jet(c,i) == 0 ) { i = 2*(i+1); }
785      int o = i-1;
786      int u = (d != i)*((i/ 2)-1);
787//----------------------- "quick search" for mindeg --------------------------
788      while ( (jet(c,u)==0)*(jet(c,o)!=0) )
789      {
790         i = (o+u)/ 2;
791         if (jet(c,i)==0) { u = i+1; }
792         else { o = i-1; }
793      }
794      if (jet(c,u)!=0) { return(u); }
795      else { return(o+1); }
796   }
797//------------------------------ main program ---------------------------------
798   matrix M = matrix(id);
799   int r,c = nrows(M), ncols(M); int i,j;
800   intmat m[r][c];
801   for (i=r; i>0; i--)
802   {
803      for (j=c; j>0; j--) { m[i,j] = findmindeg(M[i,j]); }
804   }
805   if (typeof(id)=="poly") { return(m[1,1]); }
806   return(m);
807}
808example
809{ "EXAMPLE:"; echo = 2;
810   ring r = 0,(x,y,z),ls;
811   poly f = x5+y2+z3;
812   ord(f);                // ord returns weighted order of leading term!
813   mindeg(f);
814   matrix m[2][2]=x10,1,0,f^2;
815   mindeg(m);
816}
817///////////////////////////////////////////////////////////////////////////////
818
819proc normalize (id)
820USAGE:   normalize(id);  id=poly/vector/ideal/module
821RETURN:  object of same type with leading coefficient equal to 1
822EXAMPLE: example normalize; shows an example
823{
824   return(simplify(id,1));
825}
826example
827{  "EXAMPLE:"; echo = 2;
828   ring r = 0,(x,y,z),ls;
829   poly f = 2x5+3y2+4z3;
830   normalize(f);
831   module m=[9xy,0,3z3],[4z,6y,2x];
832   show(normalize(m));
833   ring s = 0,(x,y,z),(c,ls);
834   module m=[9xy,0,3z3],[4z,6y,2x];
835   show(normalize(m));
836   normalize(matrix(m));  // by automatic type conversion to module!
837}
838///////////////////////////////////////////////////////////////////////////////
839
840proc primes (int n, int m)
841USAGE:   primes(n,m);  n,m integers
842RETURN:  intvec, consisting of all primes p, prime(n)<=p<=m, in increasing
843         order if n<=m, resp. prime(m)<=p<=n, in decreasing order if m<n
844NOTE:    prime(n); returns the biggest prime number <= n (if n>=2, else 2)
845EXAMPLE: example primes; shows an example
846{  int change;
847   if ( n>m ) { change=n; n=m ; m=change; change=1; }
848   int q,p = prime(m),prime(n); intvec v = q; q = q-1;
849   while ( q>=p ) { q = prime(q); v = q,v; q = q-1; }
850   if ( change==1 ) { v = v[size(v)..1]; }
851   return(v);
852}
853example
854{  "EXAMPLE:"; echo = 2;
855   primes(50,100);
856   intvec v = primes(37,1); v;
857}
858///////////////////////////////////////////////////////////////////////////////
859
860proc product (id, list #)
861USAGE:    product(id[,v]); id=ideal/vector/module/matrix
862          resp.id=intvec/intmat, v=intvec (e.g. v=1..n, n=integer)
863RETURN:   poly resp. int which is the product of all entries of id, with index
864          given by v (default: v=1..number of entries of id)
865NOTE:     id is treated as a list of polys resp. integers. A module m is
866          identified with corresponding matrix M (columns of M generate m)
867EXAMPLE:  example product; shows an example
868{
869   int n,j;
870   if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
871       or typeof(id)=="module" or typeof(id)=="matrix" )
872   {
873      ideal i = ideal(matrix(id));
874      if( size(#)!=0 ) { i = i[#[1]]; }
875      n = ncols(i); poly f=1;
876   }
877   if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
878   {
879      intmat S = intmat(id);
880      intvec i = S[1..nrows(S),1..ncols(S)];
881      if( size(#)!=0 ) { i = i[#[1]]; }
882      n = size(i); int f=1;
883   }
884   for( j=1; j<=n; j++ ) { f=f*i[j]; }
885   return(f);
886}
887example
888{  "EXAMPLE:"; echo = 2;
889   ring r= 0,(x,y,z),dp;
890   ideal m = maxideal(1);
891   product(m);
892   matrix M[2][3] = 1,x,2,y,3,z;
893   product(M);
894   intvec v=2,4,6;
895   product(M,v);
896   intvec iv = 1,2,3,4,5,6,7,8,9;
897   v=1..5,7,9;
898   product(iv,v);
899   intmat A[2][3] = 1,1,1,2,2,2;
900   product(A,3..5);
901}
902///////////////////////////////////////////////////////////////////////////////
903
904proc ringsum (list #)
905USAGE:   ringsum(r1,r2,...,s); r1,r2,... rings, s string (name of result ring)
906CREATE:  A new base ring with name equal to s if r1,r2,... are existing rings.
907         If, say, s = "R" and the rings r1,r2,... exist, the new ring will
908         have name R, variables from all rings r1,r2,... and as monomial
909         ordering the block (product) ordering of r1,r2,.... Mathematically, R
910         is the tensor product of the rings r1,r2,... with ordering matrix
911         equal to the direct sum of the ordering matrices of r1,r2,...
912RETURN:  no return value
913NOTE:    The characteristic of the new ring will be that of r1. The names of
914         variables in the rings r1,r2,... should differ (if a name, say x,
915         occurs in r1 and r2, then, in the new ring r, x always refers to the
916         variable with name x in r1, there is no access to x in r2).
917         The procedure works also for quotient rings.
918EXAMPLE: example ringsum; shows an example
919{
920   int ii,q;
921   int n = size(#);
922   string vars,order,oi,s;
923   for(ii=1; ii<=n-1; ii++)
924   {
925      if( ordstr(#[ii])[1]=="C" or ordstr(#[ii])[1]=="c" )
926           { oi=ordstr(#[ii])[3,size(ordstr(#[ii]))-2]; }
927      else { oi=ordstr(#[ii])[1,size(ordstr(#[ii]))-2]; }
928      vars = vars+varstr(#[ii])+",";
929      order= order+oi+",";
930      def r(ii)=#[ii];
931      setring r(ii);
932      ideal i(ii)=ideal(r(ii));
933      int q(ii)=size(i(ii));
934      q=q+q(ii);
935   }
936   if( q!=0 ) { s = "newr"; }
937   else {  s = #[size(#)]; }
938   string newring ="=("+charstr(#[1])+"),("+vars[1,size(vars)-1]+"),("
939                  +order[1,size(order)-1]+");";
940   execute("ring "+s+newring);
941   if( q!=0 )
942   {
943      ideal i;
944      for(ii=1; ii<=n-1; ii++)
945      {
946         if( q(ii)!=0 )
947         {
948            map phi = r(ii),maxideal(1);
949            i = i+phi(i(ii));
950            kill phi;
951         }
952      }
953      i=std(i);
954      execute("qring "+#[size(#)]+"=i;");
955   }
956   export(`#[size(#)]`);
957   keepring(`#[size(#)]`);
958   return();
959}
960example
961{ "EXAMPLE:"; echo = 2;
962   ring r=0,(x,y,u,v),dp;
963   ring s=32003,(a,b,c),wp(1,2,3);
964   ring t=37,x(1..5),(c,ls);
965   ringsum(r,s,t,"R");
966   type R;
967   setring s;
968   ideal i = a2+b3+c5; i=std(i);
969   qring qs =i;
970   setring s; qring qt=i;
971   ringsum(r,qs,t,qt,"Q");
972   type Q;
973   kill R,Q;
974}
975///////////////////////////////////////////////////////////////////////////////
976
977proc ringweights (r)
978USAGE:   ringweights(r); r ring
979RETURN:  intvec of weights of ring variables. If, say, x(1),...,x(n) are the
980         variables of the ring r, in this order, the resulting intvec is
981         deg(x(1)),...,deg(x(n)) where deg denotes the weighted degree if
982         the monomial ordering of r has only one block of type ws,Ws,wp or Wp.
983NOTE:    In all other cases, in particular if there is more than one block,
984         the resulting intvec is 1,...,1
985EXAMPLE: example ringweights; shows an example
986{
987   int i; intvec v; setring r;
988   for (i=1; i<=nvars(basering); i++) { v[i] = deg(var(i)); }
989   return(v);
990}
991example
992{ "EXAMPLE:"; echo = 2;
993   ring r1=32003,(x,y,z),wp(1,2,3);
994   ring r2=32003,(x,y,z),Ws(1,2,3);
995   ring r=0,(x,y,u,v),lp;
996   intvec vr=ringweights(r1); vr;
997   ringweights(r2);
998   ringweights(r);
999}
1000///////////////////////////////////////////////////////////////////////////////
1001
1002proc sort
1003USAGE:    sort(id); id ideal or module
1004RETURN:   ideal with generators of id sorted with respect to monomial ordering
1005          of the basering (generators with smaller leading term come first)
1006EXAMPLE:  example sort; shows an example
1007{
1008  intvec v = sortvec(#[1]);
1009  int s    = size(v);
1010  def m    = #[1];
1011  for (int jj=1;jj<=s;jj++) { m[jj] = #[1][v[jj]]; }
1012  return(m);
1013}
1014example
1015{  "EXAMPLE:"; echo = 2;
1016   ring r0 = 0,(x,y,z),lp;
1017   ideal i = x3,y3,z3,x2z,x2y,y2z,y2x,z2y,z2x,xyz;
1018   sort(i);
1019   ring r1  = 0,t,ls;
1020   ideal i = t47,t14,t6;
1021   ideal j = i;
1022   int ii;
1023   for (ii=1;ii<=8;ii=ii+1) { j=simplify(jet(j+i^ii,50),6); }
1024   print (matrix(j));
1025   print (matrix(sort(j)));
1026}
1027///////////////////////////////////////////////////////////////////////////////
1028
1029proc sum (id, list #)
1030USAGE:    sum(id[,v]); id=ideal/vector/module/matrix resp. id=intvec/intmat,
1031                       v=intvec (e.g. v=1..n, n=integer)
1032RETURN:   poly resp. int which is the sum of all entries of id, with index
1033          given by v (default: v=1..number of entries of id)
1034NOTE:     id is treated as a list of polys resp. integers. A module m is
1035          identified with corresponding matrix M (columns of M generate m)
1036EXAMPLE:  example sum; shows an example
1037{
1038   if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
1039       or typeof(id)=="module" or typeof(id)=="matrix" )
1040   {
1041      ideal i = ideal(matrix(id));
1042      if( size(#)!=0 ) { i = i[#[1]]; }
1043      matrix Z = matrix(i);
1044      intvec v; v[ncols(Z)]=0; v=v+1;
1045   }
1046   if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
1047   {
1048      intmat S = intmat(id);
1049      intvec v = S[1..nrows(S),1..ncols(S)];
1050      if( size(#)!=0 ) { v = v[#[1]]; }
1051      intvec z; z[size(v)]=0; z=z+1;
1052      intmat Z=transpose(z);
1053   }
1054   return((Z*v)[1,1]);
1055}
1056example
1057{  "EXAMPLE:"; echo = 2;
1058   ring r= 0,(x,y,z),dp;
1059   vector pv = [xy,xz,yz,x2,y2,z2];
1060   sum(pv);
1061   sum(pv,2..5);
1062   matrix M[2][3] = 1,x,2,y,3,z;
1063   sum(M);
1064   intvec v=2,4,6;
1065   sum(M,v);
1066   intvec iv = 1,2,3,4,5,6,7,8,9;
1067   v=1..5,7,9;
1068   sum(iv,v);
1069   intmat m[2][3] = 1,1,1,2,2,2;
1070   sum(m,3..4);
1071}
1072///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.