source: git/Singular/LIB/general.lib @ 3d124a7

spielwiese
Last change on this file since 3d124a7 was 3d124a7, checked in by Olaf Bachmann <obachman@…>, 27 years ago
This commit was generated by cvs2svn to compensate for changes in r191, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@192 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 18.8 KB
RevLine 
[3d124a7]1// $Id: general.lib,v 1.1.1.1 1997-04-25 15:13:26 obachman Exp $
2//system("random",787422842);
3//(GMG, last modified 22.06.96)
4///////////////////////////////////////////////////////////////////////////////
5
6LIBRARY:  general.lib   PROCEDURES OF GENERAL TYPE
7
8 A_Z("a",n);            string a,b,... of n comma seperated letters
9 binomial(n,m[,../..]); n choose m (type int), [type string/type number]
10 factorial(n[,../..]);  n factorial (=n!) (type int), [type string/number]
11 fibonacci(n[,p]);      nth Fibonacci number [char p]
12 kmemory();             int = active memory (kilobyte)
13 killall();             kill all user-defined variables
14 number_e(n);           compute exp(1) up to n decimal digits
15 number_pi(n);          compute pi (area of unit circle) up to n digits
16 primes(n,m);           intvec of primes p, n<=p<=m
17 product(../..[,v]);    multiply components of vector/ideal/...[indices v]
18 ringweights(r);        intvec of weights of ring variables of ring r
19 sort(ideal/module);    sort generators according to monomial ordering
20 sum(vector/id/..[,v]); add components of vector/ideal/...[with indices v]
21           (parameters in square brackets [] are optional)
22
23LIB "inout.lib";
24///////////////////////////////////////////////////////////////////////////////
25
26proc A_Z (string s,int n)
27USAGE:   A_Z("a",n);  a any letter, n integer (-26<= n <=26, !=0)
28RETURN:  string of n small (if a is small) or capital (if a is capital)
29         letters, comma seperated, beginning with a, in alphabetical
30         order (or revers alphabetical order if n<0)
31EXAMPLE: example A_Z; shows an example
32{
33  if ( n>=-26 and n<=26 and n!=0 )
34  {
35    string alpha =
36    "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,"+
37    "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,"+
38    "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,"+
39    "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";
40    int ii; int aa;
41    for(ii=1; ii<=51; ii=ii+2)
42    {
43       if( alpha[ii]==s ) { aa=ii; }
44    }
45    if ( aa==0)
46    {
47      for(ii=105; ii<=155; ii=ii+2)
48      {
49        if( alpha[ii]==s ) { aa=ii; }
50      }
51    }
52    if( aa!=0 )
53    {
54      string out;
55      if (n > 0) { out = alpha[aa,2*(n)-1];  return (out); }
56      if (n < 0)
57      {
58        string beta =
59        "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,"+
60        "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,"+
61        "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,"+
62        "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";
63        if ( aa < 52 ) { aa=52-aa; }
64        if ( aa > 104 ) { aa=260-aa; }
65        out = beta[aa,2*(-n)-1]; return (out);
66      }
67    }
68  }
69}
70example
71{ "EXAMPLE:"; echo = 2;
72   A_Z("c",5);
73   A_Z("Z",-5);
74   string sR = "ring R = (0,"+A_Z("A",6)+"),("+A_Z("a",10)+"),dp;";
75   sR;
76   execute sR;
77   R;
78}
79///////////////////////////////////////////////////////////////////////////////
80
81proc binomial (int n, int k, list #)
82USAGE:   binomial(n,k[,p/s]); n,k,p integers, s string
83RETURN:  binomial(n,k);    binomial coefficient n choose k of type int
84                           (machine integer, limited size! )
85         binomial(n,k,p);  n choose k in char p of type string
86         binomial(n,k,s);  n choose k of type number (s any string), computed
87                           in char of basering if a basering is defined
88EXAMPLE: example binomial; shows an example
89{
90   if ( size(#)==0 ) { int rr=1; }
91   if ( typeof(#[1])=="int") { ring bin = #[1],x,dp; number rr=1; }
92   if ( typeof(#[1])=="string") { number rr=1; }
93   if ( size(#)==0 or typeof(#[1])=="int" or typeof(#[1])=="string" )
94   {
95      def r = rr;
96      if ( k<=0 or k>n ) { return((k==0)*r); }
97      if ( k>n-k ) { k = n-k; }
98      int l;
99      for (l=1; l<=k; l=l+1 )
100      {
101         r=r*(n+1-l)/l;
102      }
103      if ( typeof(#[1])=="int" ) { return(string(r)); }
104      return(r);
105   }
106}
107example
108{ "EXAMPLE:"; echo = 2;
109   int b1 = binomial(10,7); b1;
110   binomial(37,17,0);
111   ring t = 31,x,dp;
112   number b2 = binomial(37,17,""); b2;
113}
114///////////////////////////////////////////////////////////////////////////////
115
116proc factorial (int n, list #)
117USAGE:   factorial(n[,string]);  n integer
118RETURN:  factorial(n); string of n! in char 0
119         factorial(n,s);  n! of type number (s any string), computed in char of
120         basering if a basering is defined
121EXAMPLE: example factorial; shows an example
122{
123   if ( size(#)==0 ) { ring R = 0,x,dp; poly r=1; }
124   if ( typeof(#[1])=="string" ) { number r=1; }
125   if ( size(#)==0 or typeof(#[1])=="string" )
126   {
127      int l;
128      for (l=2; l<=n; l=l+1)
129      {
130         r=r*l;
131      }
132      if ( size(#)==0 ) { return(string(r)); }
133      return(r);
134   }
135}
136example
137{ "EXAMPLE:"; echo = 2;
138   factorial(37);
139   ring r1 = 32003,(x,y,z),ds;
140   number p = factorial(37,""); p;
141}
142///////////////////////////////////////////////////////////////////////////////
143
144proc fibonacci (int n, list #)
145USAGE:   fibonacci(n[,string]);  (n integer)
146RETURN:  fibonacci(n); string of nth Fibonacci number,
147            f(0)=f(1)=1, f(i+1)=f(i-1)+f(i)
148         fibonacci(n,s);  nth Fibonacci number of type number (s any string),
149         computed in characteristic of basering if a basering is defined
150EXAMPLE: example fibonacci; shows an example
151{
152   if ( size(#)==0 ) { ring fibo = 0,x,dp; number f=1; }
153   if ( typeof(#[1])=="string" ) { number f=1; }
154   if ( size(#)==0 or typeof(#[1])=="string" )
155   {
156      number g,h = 1,1; int ii;
157      for (ii=3; ii<=n; ii=ii+1)
158      {
159         h = f+g; f = g; g = h;
160      }
161      if ( size(#)==0 ) { return(string(h)); }
162      return(h);
163   }
164}
165example
166{ "EXAMPLE:"; echo = 2;
167   fibonacci(37);
168   ring r = 17,x,dp;
169   number b = fibonacci(37,""); b;
170}
171///////////////////////////////////////////////////////////////////////////////
172
173proc kmemory ()
174USAGE:   kmemory();
175RETURN:  memory used by active variables, of type int (in kilobyte)
176EXAMPLE: example kmemory; shows an example
177{
178  if ( voice==2 ) { "// memory used by active variables (kilobyte):"; }
179   return ((memory(0)+1023)/1024);
180}
181example
182{ "EXAMPLE:"; echo = 2;
183   kmemory();
184}
185///////////////////////////////////////////////////////////////////////////////
186
187proc killall
188USAGE:   killall(); (no parameter)
189         killall("proc");
190COMPUTE: killall(); kills all user-defined variables but not loaded procedures
191         killall("proc"); kills all loaded procedures
192RETURN:  no return value
193NOTE:    killall should never be used inside a procedure
194EXAMPLE: example killall; shows an example AND KILLS ALL YOUR VARIABLES
195{
196   list L=names(); int joni=size(L);
197   if( size(#)==0 )
198   {
199      for ( ; joni>0; joni-- )
200      {
201         if( L[joni]!="LIB" and typeof(`L[joni]`)!="proc" ) { kill `L[joni]`; }
202      }
203      return();
204   }
205   if( #[1] == "proc" )
206   {
207      for ( joni=size(L); joni>0; joni-- )
208      {
209         if( L[joni]=="LIB" or typeof(`L[joni]`)=="proc" ) { kill `L[joni]`; }
210      }
211   }
212}
213example
214{ "EXAMPLE:"; echo = 2;
215   ring rtest; ideal i=x,y,z; number n=37; string str="hi";
216   export rtest,i,n,str;     //this makes the local variables global
217   listvar(all);
218   killall();
219   listvar(all);
220}
221///////////////////////////////////////////////////////////////////////////////
222
223proc number_e (int n)
224USAGE:   number_e(n);  n integer
225COMPUTE: exp(1) up to n decimal digits (no rounding)
226         by A.H.J. Sale's algorithm
227RETURN:  - string of exp(1) if no basering of char 0 is defined;
228         - exp(1), of type number, if a basering of char 0 is defined and
229         display its decimal format
230EXAMPLE: example number_e; shows an example
231{
232   int i,m,s,t;
233   intvec u,e;
234   u[n+2]=0; e[n+1]=0; e=e+1;
235   if( defined(basering) )
236   {
237      if( char(basering)==0 ) { number r=2; t=1; }
238   }
239   string result = "2.";
240   for( i=1; i<=n+1; i=i+1 )
241   {
242      e = e*10;
243      for( m=n+1; m>=1; m=m-1 )
244      {
245         s    = e[m]+u[m+1];
246         u[m] = s/(m+1);
247         e[m] = s%(m+1);
248      }
249      result = result+string(u[1]);
250      if( t==1 ) { r = r+number(u[1])/number(10)^i; }
251   }
252   if( t==1 ) { "//",result[1,n+1]; return(r); }
253   return(result[1,n+1]);
254}
255example
256{ "EXAMPLE:"; echo = 2;
257   number_e(15);
258   ring R = 0,t,lp;
259   number e = number_e(10);
260   e;
261}
262///////////////////////////////////////////////////////////////////////////////
263
264proc number_pi (int n)
265USAGE:   number_pi(n);  n positive integer
266COMPUTE: pi (area of unit circle) up to n decimal digits (no rounding)
267         by algorithm of S. Rabinowitz
268RETURN:  - string of pi if no basering of char 0 is defined,
269         - pi, of type number, if a basering of char 0 is defined and display
270         its decimal format
271EXAMPLE: example number_pi; shows an example
272{
273   int i,m,t,e,q,N;
274   intvec r,p,B,Prelim;
275   string result,prelim;
276   N = (10*n)/3 + 2;
277   p[N+1]=0; p=p+2; r=p;
278   for( i=1; i<=N+1; i=i+1 ) { B[i]=2*i-1; }
279   if( defined(basering) )
280   {
281      if( char(basering)==0 ) { number pi; number pri; t=1; }
282   }
283   for( i=0; i<=n; i=i+1 )
284   {
285      p = r*10;
286      e = p[N+1];
287      for( m=N+1; m>=2; m=m-1 )
288      {
289         r[m] = e%B[m];
290         q    = e/B[m];
291         e    = q*(m-1)+p[m-1];
292      }
293      r[1] = e%10;
294      q    = e/10;
295      if( q!=10 and q!=9 )
296      {
297         result = result+prelim;
298         Prelim = q;
299         prelim = string(q);
300      }
301      if( q==9 )
302      {
303         Prelim = Prelim,9;
304         prelim = prelim+"9";
305      }
306      if( q==10 )
307      {
308         Prelim = (Prelim+1)-((Prelim+1)/10)*10;
309         for( m=size(Prelim); m>0; m=m-1)
310         {
311            prelim[m] = string(Prelim[m]);
312         }
313         result = result+prelim;
314         if( t==1 ) { pi=pi+pri; }
315         Prelim = 0;
316         prelim = "0";
317      }
318      if( t==1 ) { pi=pi+number(q)/number(10)^i; }
319   }
320   result = result,prelim[1];
321   result = "3."+result[2,n-1];
322   if( t==1 ) { "//",result; return(pi); }
323   return(result);
324}
325example
326{ "EXAMPLE:"; echo = 2;
327   number_pi(5);
328   ring r = 0,t,lp;
329   number pi = number_pi(6);
330   pi;
331}
332///////////////////////////////////////////////////////////////////////////////
333
334proc primes (int n, int m)
335USAGE:   primes(n,m);  n,m integers
336RETURN:  intvec, consisting of all primes p, prime(n)<=p<=m, in increasing
337         order if n<=m, resp. prime(m)<=p<=n, in decreasing order if m<n
338NOTE:    prime(n); returns the biggest prime number <= n (if n>=2, else 2)
339EXAMPLE: example primes; shows an example
340{  int change;
341   if ( n>m ) { change=n; n=m ; m=change; change=1; }
342   int q,p = prime(m),prime(n); intvec v = q; q = q-1;
343   while ( q>=p ) { q = prime(q); v = q,v; q = q-1; }
344   if ( change==1 ) { v = v[size(v)..1]; }
345   return(v);
346}
347example
348{  "EXAMPLE:"; echo = 2;
349   primes(50,100);
350   intvec v = primes(37,1); v;
351}
352///////////////////////////////////////////////////////////////////////////////
353
354proc product (id, list #)
355USAGE:    product(id[,v]); id=ideal/vector/module/matrix
356          resp.id=intvec/intmat, v=intvec (e.g. v=1..n, n=integer)
357RETURN:   poly resp. int which is the product of all entries of id, with index
358          given by v (default: v=1..number of entries of id)
359NOTE:     id is treated as a list of polys resp. integers. A module m is
360          identified with corresponding matrix M (columns of M generate m)
361EXAMPLE:  example product; shows an example
362{
363   int n,j;
364   if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
365       or typeof(id)=="module" or typeof(id)=="matrix" )
366   {
367      ideal i = ideal(matrix(id));
368      if( size(#)!=0 ) { i = i[#[1]]; }
369      n = ncols(i); poly f=1;
370   }
371   if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
372   {
373      if ( typeof(id) == "int" ) { intmat S =id; }
374      else { intmat S = intmat(id); }
375      intvec i = S[1..nrows(S),1..ncols(S)];
376      if( size(#)!=0 ) { i = i[#[1]]; }
377      n = size(i); int f=1;
378   }
379   for( j=1; j<=n; j=j+1 ) { f=f*i[j]; }
380   return(f);
381}
382example
383{  "EXAMPLE:"; echo = 2;
384   ring r= 0,(x,y,z),dp;
385   ideal m = maxideal(1);
386   product(m);
387   matrix M[2][3] = 1,x,2,y,3,z;
388   product(M);
389   intvec v=2,4,6;
390   product(M,v);
391   intvec iv = 1,2,3,4,5,6,7,8,9;
392   v=1..5,7,9;
393   product(iv,v);
394   intmat A[2][3] = 1,1,1,2,2,2;
395   product(A,3..5);
396}
397///////////////////////////////////////////////////////////////////////////////
398
399proc ringweights (r)
400USAGE:   ringweights(r); r ring
401RETURN:  intvec of weights of ring variables. If, say, x(1),...,x(n) are the
402         variables of the ring r, in this order, the resulting intvec is
403         deg(x(1)),...,deg(x(n)) where deg denotes the weighted degree if
404         the monomial ordering of r has only one block of type ws,Ws,wp or Wp.
405NOTE:    In all other cases, in particular if there is more than one block,
406         the resulting intvec is 1,...,1
407EXAMPLE: example ringweights; shows an example
408{
409   int i; intvec v; setring r;
410   for (i=1; i<=nvars(basering); i=i+1) { v[i] = deg(var(i)); }
411   return(v);
412}
413example
414{ "EXAMPLE:"; echo = 2;
415   ring r1=32003,(x,y,z),wp(1,2,3);
416   ring r2=32003,(x,y,z),Ws(1,2,3);
417   ring r=0,(x,y,u,v),lp;
418   intvec vr=ringweights(r1); vr;
419   ringweights(r2);
420   ringweights(r);
421}
422///////////////////////////////////////////////////////////////////////////////
423
424proc sort (id, list #)
425USAGE:   sort(id[v,o,n]); id=ideal/module/intvec/list (of intvec's or int's)
426         sort may be called with 1, 2 or 3 arguments in the following way:
427         -  sort(id[v,n]); v=intvec, n=integer,
428         -  sort(id[o,n]); o=string (any allowed ordstr of a ring), n=integer
429RETURN:  a list of two elements:
430         [1]: object of same type as input but sorted in the following manner:
431           - if id=ideal/module: generators of id are sorted w.r.t. intvec v
432             (id[v[1]] becomes 1-st, id[v[2]]  2-nd element, etc.). If no v is
433             present, id is sorted w.r.t. ordering o (if o is given) or w.r.t.
434             actual monomial ordering (if no o is given):
435                    generators with smaller leading term come first
436             (e.g. sort(id); sorts w.r.t actual monomial ordering)
437           - if id=list of intvec's or int's: consider a list element, say
438             id[1]=3,2,5, as exponent vector of the monomial x^3*y^2*z^5;
439             the corresponding monomials are ordered w.r.t. intvec v (s.a.).
440             If no v is present, the monomials are sorted w.r.t. ordering o
441             (if o is given) or w.r.t. lexicographical ordering (if no o is
442             given). The corresponding ordered list of exponent vectors is
443             returned.
444             (e.g. sort(id); sorts lexicographically, smaller int's come first)
445             WARNING: Since negative exponents create the 0 plynomial in
446             Singular, id should not contain negative integers: the result might
447             not be as exspected
448           - if id=intvec: id is treated as list of integers
449           - if n!=0 the ordering is inverse, i.e. w.r.t. v(size(v)..1)
450             default: n=0
451         [2]: intvec, describing the permutation of the input (hence [2]=v if
452             v is given)
453NOTE:    If v is given, id may be any simply indexed object (e.g. any list);
454         entries of v must be pairwise distinct to get a permutation if id.
455         Zero generators of ideal/module are deleted
456EXAMPLE: example sort; shows an example
457{
458   int ii,jj,s,n = 0,0,1,0;
459   intvec v;
460   if ( defined(basering) ) { def P = basering; }
461   if ( size(#)==0 and (typeof(id)=="ideal" or typeof(id)=="module") )
462   {
463      id = simplify(id,2);
464      for ( ii=1; ii<size(id); ii++ )
465      {
466         if ( id[ii]!=id[ii+1] ) { break;}
467      }
468      if ( ii != size(id) ) { v = sortvec(id); }
469      else  { v = size(id)..1; }
470      if ( v == 0 ) { v = 1; }
471   }
472   if ( size(#)>=1 and (typeof(id)=="ideal" or typeof(id)=="module") )
473   {
474      if ( typeof(#[1])=="string" )
475      {
476         execute "ring r1 =("+charstr(P)+"),("+varstr(P)+"),("+#[1]+");";
477         def i = imap(P,id);
478         v = sortvec(i);
479         setring P;
480         n=2;
481      }
482   }
483   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==0 )
484   {
485      string o;
486      if ( size(#)==0 ) { o = "lp"; n=1; }
487      if ( size(#)>=1 )
488      {
489         if ( typeof(#[1])=="string" ) { o = #[1]; n=1; }
490      }
491   }
492   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==1 )
493   {
494      if ( typeof(id)=="list" )
495      {
496         for (ii=1; ii<=size(id); ii++)
497         {
498            if (typeof(id[ii]) != "intvec" and typeof(id[ii]) != "int")
499               { "// list elements must be intvec/int"; return(); }
500            else
501               { s=size(id[ii])*(s < size(id[ii])) + s*(s >= size(id[ii])); }
502         }
503      }
504      execute "ring r=0,x(1..s),("+o+");";
505      ideal i;
506      poly f;
507      for (ii=1; ii<=size(id); ii++)
508      {
509         f=1;
510         for (jj=1; jj<=size(id[ii]); jj++)
511         {
512            f=f*x(jj)^(id[ii])[jj];
513         }
514         i[ii]=f;
515      }
516      v = sort(i)[2];
517   }
518   if ( size(#)!=0 and n==0 ) { v = #[1]; }
519   if( size(#)==2 )
520   {
521      if ( #[2] != 0 ) { v = v[size(v)..1]; }
522   }
523   s = size(v);
524   def m = id;
525   for ( jj=1; jj<=s; jj=jj+1) { m[jj] = id[v[jj]]; }
526   list L=m,v;
527   return(L);
528}
529example
530{  "EXAMPLE:"; echo = 2;
531   ring r0 = 0,(x,y,z),lp;
532   ideal i = x3,y3,z3,x2z,x2y,y2z,y2x,z2y,z2x,xyz;
533   show(sort(i));"";
534   show(sort(i,"wp(1,2,3)"));"";
535   intvec v=10..1;
536   show(sort(i,v));"";
537   show(sort(i,v,1));"";   // should be the identity
538   ring r1  = 0,t,ls;
539   ideal j = t14,t6,t28,t20,t12,t34,t26,t18,t40,t32,t24,t38,t30,t36;
540   show(sort(j)[1]);"";
541   show(sort(j,"lp")[1]);"";
542   list L =1,5..8,10,2,8..5,8,3..10;
543   sort(L)[1];"";          // sort L lexicographically
544   sort(L,"Dp",1)[1];      // sort L w.r.t (total sum, reverse lex)
545}
546///////////////////////////////////////////////////////////////////////////////
547
548proc sum (id, list #)
549USAGE:    sum(id[,v]); id=ideal/vector/module/matrix resp. id=intvec/intmat,
550                       v=intvec (e.g. v=1..n, n=integer)
551RETURN:   poly resp. int which is the sum of all entries of id, with index
552          given by v (default: v=1..number of entries of id)
553NOTE:     id is treated as a list of polys resp. integers. A module m is
554          identified with corresponding matrix M (columns of M generate m)
555EXAMPLE:  example sum; shows an example
556{
557   if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
558       or typeof(id)=="module" or typeof(id)=="matrix" )
559   {
560      ideal i = ideal(matrix(id));
561      if( size(#)!=0 ) { i = i[#[1]]; }
562      matrix Z = matrix(i);
563   }
564   if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
565   {
566      if ( typeof(id) == "int" ) { intmat S =id; }
567      else { intmat S = intmat(id); }
568      intvec i = S[1..nrows(S),1..ncols(S)];
569      if( size(#)!=0 ) { i = i[#[1]]; }
570      intmat Z=transpose(i);
571   }
572   intvec v; v[ncols(Z)]=0; v=v+1;
573   return((Z*v)[1,1]);
574}
575example
576{  "EXAMPLE:"; echo = 2;
577   ring r= 0,(x,y,z),dp;
578   vector pv = [xy,xz,yz,x2,y2,z2];
579   sum(pv);
580   //sum(pv,2..5);
581   //matrix M[2][3] = 1,x,2,y,3,z;
582   //sum(M);
583   //intvec w=2,4,6;
584   //sum(M,w);
585   //intvec iv = 1,2,3,4,5,6,7,8,9;
586   //w=1..5,7,9;
587   //sum(iv,w);
588   //intmat m[2][3] = 1,1,1,2,2,2;
589   //sum(m,3..4);
590}
591///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.