source: git/Singular/LIB/general.lib @ 447527

jengelh-datetimespielwiese
Last change on this file since 447527 was 447527, checked in by Hans Schönemann <hannes@…>, 24 years ago
* hannes: system(--ticks-per-second") does not return a string any longer (tst.lib) general.lib::ringweights substituted by normal.lib::extraweight git-svn-id: file:///usr/local/Singular/svn/trunk@3651 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 29.4 KB
Line 
1// $Id: general.lib,v 1.20 1999-09-23 12:13:38 Singular Exp $
2//GMG, last modified 18.6.99
3///////////////////////////////////////////////////////////////////////////////
4
5version="$Id: general.lib,v 1.20 1999-09-23 12:13:38 Singular Exp $";
6info="
7LIBRARY:  general.lib   PROCEDURES OF GENERAL TYPE
8
9PROCEDURES:
10 A_Z(\"a\",n);          string a,b,... of n comma seperated letters
11 ASCII([n,m]);          string of printable ASCII characters (number n to m)
12 binomial(n,m[,../..]); n choose m (type int), [type string/type number]
13 factorial(n[,../..]);  n factorial (=n!) (type int), [type string/number]
14 fibonacci(n[,p]);      nth Fibonacci number [char p]
15 kmemory([n[,v]]);      active [allocated] memory in kilobyte
16 killall();             kill all user-defined variables
17 number_e(n);           compute exp(1) up to n decimal digits
18 number_pi(n);          compute pi (area of unit circle) up to n digits
19 primes(n,m);           intvec of primes p, n<=p<=m
20 product(../..[,v]);    multiply components of vector/ideal/...[indices v]
21 ringweights(r);        intvec of weights of ring variables of ring r
22 sort(ideal/module);    sort generators according to monomial ordering
23 sum(vector/id/..[,v]); add components of vector/ideal/...[with indices v]
24 which(command);        search for command and return absolute path, if found
25           (parameters in square brackets [] are optional)
26";
27
28LIB "inout.lib";
29///////////////////////////////////////////////////////////////////////////////
30
31proc A_Z (string s,int n)
32"USAGE:   A_Z(\"a\",n);  a any letter, n integer (-26<= n <=26, !=0)
33RETURN:  string of n small (if a is small) or capital (if a is capital)
34         letters, comma seperated, beginning with a, in alphabetical
35         order (or revers alphabetical order if n<0)
36EXAMPLE: example A_Z; shows an example
37"
38{
39  if ( n>=-26 and n<=26 and n!=0 )
40  {
41    string alpha =
42    "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,"+
43    "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,"+
44    "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,"+
45    "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";
46    int ii; int aa;
47    for(ii=1; ii<=51; ii=ii+2)
48    {
49       if( alpha[ii]==s ) { aa=ii; }
50    }
51    if ( aa==0)
52    {
53      for(ii=105; ii<=155; ii=ii+2)
54      {
55        if( alpha[ii]==s ) { aa=ii; }
56      }
57    }
58    if( aa!=0 )
59    {
60      string out;
61      if (n > 0) { out = alpha[aa,2*(n)-1];  return (out); }
62      if (n < 0)
63      {
64        string beta =
65        "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,"+
66        "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,"+
67        "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,"+
68        "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";
69        if ( aa < 52 ) { aa=52-aa; }
70        if ( aa > 104 ) { aa=260-aa; }
71        out = beta[aa,2*(-n)-1]; return (out);
72      }
73    }
74  }
75}
76example
77{ "EXAMPLE:"; echo = 2;
78   A_Z("c",5);
79   A_Z("Z",-5);
80   string sR = "ring R = (0,"+A_Z("A",6)+"),("+A_Z("a",10)+"),dp;";
81   sR;
82   execute sR;
83   R;
84}
85///////////////////////////////////////////////////////////////////////////////
86proc ASCII (list #)
87"USAGE:   ASCII([n,m]); n,m= integers (32 <= n <= m <= 126)
88RETURN:   printable ASCII characters (no native language support)
89          ASCII():    string of  all ASCII characters with its numbers,
90                      no return value
91          ASCII(n):   string, n-th ASCII character
92          ASCII(n,m): list, n-th up to m-th ASCII character (inclusive)
93EXAMPLE: example ASCII; shows an example
94"
95{
96  string s1 =
97 "     !    \"    #    $       &    '    (    )    *    +    ,    -    .
9832   33   34   35   36   37   38   39   40   41   42   43   44   45   46
99/    0    1    2    3    4    5    6    7    8    9    :    ;    <    =
10047   48   49   50   51   52   53   54   55   56   57   58   59   60   61
101>    ?    @    A    B    C    D    E    F    G    H    I    J    K    L
10262   63   64   65   66   67   68   69   70   71   72   73   74   75   76
103M    N    O    P    Q    R    S    T    U    V    W    X    Y    Z    [
10477   78   79   80   81   82   83   84   85   86   87   88   89   90   91
105\\    ]    ^    _    `    a    b    c    d    e    f    g    h    i    j
10692   93   94   95   96   97   98   99  100  101  102  103  104  105  10
107k    l    m    n    o    p    q    r    s    t    u    v    w    x    y
108107  108  109  110  111  112  113  114  115  116  117  118  119  120  121
109z    {    |    }    ~
110122  123  124  125  126 ";
111
112   string s2 =
113 " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
114
115   if ( size(#) == 0 )
116   {
117      return(s1);
118   }
119   if ( size(#) == 1 )
120   {
121      return( s2[#[1]-31] );
122   }
123   if ( size(#) == 2 )
124   {
125      return( s2[#[1]-31,#[2]-#[1]+1] );
126   }
127}
128example
129{ "EXAMPLE:"; echo = 2;
130   ASCII();"";
131   ASCII(42);
132   ASCII(32,126);
133}
134///////////////////////////////////////////////////////////////////////////////
135
136proc binomial (int n, int k, list #)
137"USAGE:   binomial(n,k[,p]); n,k,p integers
138RETURN:  binomial(n,k); binomial coefficient n choose k,
139         - of type string (computed in characteristic 0)
140         binomial(n,k,p); n choose k, computed in characteristic prime(p)
141         - of type number if a basering is present and prime(p)=char(basering)
142         - of type string else
143NOTE:    In any characteristic, binomial(n,k) = coefficient of x^k in (1+x)^n
144EXAMPLE: example binomial; shows an example
145"
146{
147   int str,p;
148//---------------------------- initialization -------------------------------
149   if ( size(#) == 0 )
150   {  str = 1;
151      ring bin = 0,x,dp;
152      number r=1;
153   }
154   if ( size(#) > 0 )
155   {
156      p = (#[1]!=0)*prime(#[1]);
157      if ( defined(basering) )
158      {
159         if ( p == char(basering) )
160         {  number r=1;
161         }
162         else
163         {  str = 1;
164            ring bin = p,x,dp;
165            number r=1;
166         }
167      }
168      else
169      {  str = 1;
170         ring bin = p,x,dp;
171         number r=1;
172      }
173   }
174//-------------------------------- char 0 -----------------------------------
175   if ( p==0 )
176   {
177      r = binom0(n,k);
178   }
179//-------------------------------- char p -----------------------------------
180   else
181   {
182      r = binomp(n,k,p);
183   }
184//-------------------------------- return -----------------------------------
185   if ( str==1 ) { return(string(r)); }
186   else { return(r); }
187 }
188example
189{ "EXAMPLE:"; echo = 2;
190   binomial(200,100);"";                   //type string, computed in char 0
191   binomial(200,100,3);"";                 //type string, computed in char 3
192   int n,k = 200,100;
193   ring r = 0,x,dp;
194   number b1 = binomial(n,k,0);            //type number, computed in ring r
195   poly b2 = coeffs((x+1)^n,x)[k+1,1];     //coefficient of x^k in (x+1)^n
196   b1-b2;                                  //b1 and b2 should coincide
197}
198///////////////////////////////////////////////////////////////////////////////
199
200static proc binom0 (int n, int k)
201 //computes binomial coefficient n choose k in basering, assume 0<k<=n
202 //and char(basering) = 0 or n < char(basering)
203{
204   int l;
205   number r=1;
206   if ( k > n-k )
207   { k = n-k;
208   }
209   if ( k<=0 or k>n )               //trivial cases
210   { r = (k==0)*r;
211   }
212   for (l=1; l<=k; l++ )
213   {
214      r=r*(n+1-l)/l;
215   }
216   return(r);
217}
218///////////////////////////////////////////////////////////////////////////////
219
220static proc binomp (int n, int k, int p)
221 //computes binomial coefficient n choose k in basering of char p > 0
222 //binomial(n,k) = coefficient of x^k in (1+x)^n.
223 //Let n=q*p^j, gcd(q,p)=1, then (1+x)^n = (1 + x^(p^j))^q. We have
224 //binomial(n,k)=0 if k!=l*p^j and binomial(n,l*p^j) = binomial(q,l).
225 //Do this reduction first. Then, in denominator and numerator
226 //of defining formula for binomial coefficient, reduce those factors
227 //mod p which are not divisible by p and cancel common factors p. Hence,
228 //if n = h*p+r, k=l*p+s, r,s<p, binomial(n,k) = binomial(r,s)*binomial(h,l)
229{
230   int l,q,i= 1,n,1;
231   number r=1;
232   if ( k > n-k )
233   { k = n-k;
234   }
235   if ( k<=0 or k>n)               //trivial cases
236   { r = (k==0)*r;
237   }
238   else
239   {
240      while ( q mod p == 0 )
241      {  l = l*p;
242         q = q div p;
243      }                            //we have now n=q*l, l=p^j, gcd(q,p)=1;
244      if (k mod l != 0 )
245      { r = 0;
246      }
247      else
248      {  l = k div l;
249         n = q mod p;
250         k = l mod p;              //now 0<= k,n <p, use binom0 for n,k
251         q = q div p;              //recursion for q,l
252         l = l div p;              //use binomp for q,l
253         r = binom0(n,k)*binomp(q,l,p);
254      }
255   }
256   return(r);
257}
258///////////////////////////////////////////////////////////////////////////////
259
260proc factorial (int n, list #)
261"USAGE:   factorial(n[,p]);  n,p integers
262RETURN:  factorial(n):   n! (computed in characteristic 0), of type string
263         factorial(n,p): n! computed in characteristic prime(p)
264         - of type number if a basering is present and prime(p)=char(basering)
265         - of type string else
266EXAMPLE: example factorial; shows an example
267"
268{   int str,l,p;
269//---------------------------- initialization -------------------------------
270   if ( size(#) == 0 )
271   {  str = 1;
272      ring bin = 0,x,dp;
273      number r=1;
274   }
275   if ( size(#) > 0 )
276   {
277      p = (#[1]!=0)*prime(#[1]);
278      if ( defined(basering) )
279      {
280         if ( p == char(basering) )
281         {  number r=1;
282         }
283         else
284         {  str = 1;
285            ring bin = p,x,dp;
286            number r=1;
287         }
288      }
289      else
290      {  str = 1;
291         ring bin = p,x,dp;
292         number r=1;
293      }
294   }
295//------------------------------ computation --------------------------------
296   for (l=2; l<=n; l++)
297   {
298      r=r*l;
299   }
300   if ( str==1 ) { return(string(r)); }
301   else { return(r); }
302}
303example
304{ "EXAMPLE:"; echo = 2;
305   factorial(37);"";                   //37! of type string (as long integer)
306   ring r1 = 0,x,dp;
307   number p = factorial(37,0);         //37! of type number, computed in r
308   p;
309}
310///////////////////////////////////////////////////////////////////////////////
311
312proc fibonacci (int n, list #)
313"USAGE:   fibonacci(n);  n,p integers
314RETURN:  fibonacci(n): nth Fibonacci number, f(0)=f(1)=1, f(i+1)=f(i-1)+f(i)
315         - computed in characteristic 0, of type string
316         of type number computed in char(basering) if n is of type number
317         fibonacci(n,p): f(n) computed in characteristic prime(p)
318         - of type number if a basering is present and prime(p)=char(basering)
319         - of type string else
320EXAMPLE: example fibonacci; shows an example
321"
322{   int str,ii,p;
323//---------------------------- initialization -------------------------------
324   if ( size(#) == 0 )
325   {  str = 1;
326      ring bin = 0,x,dp;
327      number f,g,h=1,1,1;
328   }
329   if ( size(#) > 0 )
330   {
331      p = (#[1]!=0)*prime(#[1]);
332      if ( defined(basering) )
333      {
334         if ( p == char(basering) )
335         {  number f,g,h=1,1,1;
336         }
337         else
338         {  str = 1;
339            ring bin = p,x,dp;
340            number f,g,h=1,1,1;
341         }
342      }
343      else
344      {  str = 1;
345         ring bin = p,x,dp;
346         number f,g,h=1,1,1;
347      }
348   }
349//------------------------------ computation --------------------------------
350   for (ii=3; ii<=n; ii=ii+1)
351   {
352      h = f+g; f = g; g = h;
353    }
354   if ( str==1 ) { return(string(h)); }
355   else { return(h); }
356}
357example
358{ "EXAMPLE:"; echo = 2;
359   fibonacci(333); "";              //f(333) of type string (as long integer)
360   ring r = 17,x,dp;
361   number b = fibonacci(333,17);    //f(333) of type number, computed in r
362   b;
363}
364///////////////////////////////////////////////////////////////////////////////
365
366proc kmemory (list #)
367"USAGE:   kmemory([n,[v]]); n = int
368RETURN:  memory in kilobyte of type int
369         n=0: memory used by active variables (same as no parameters)
370         n=1: total memory allocated by Singular
371         n=2: difference between top and init memory adress (sbrk memory)
372         n!=0,1,2: 0
373DISPLAY: detailed information about allocated and used memory if v!=0
374NOTE:    kmemory uses internal function 'memory' to compute kilobyte, and
375         is the same as 'memory' for n!=0,1,2
376EXAMPLE: example kmemory; shows an example
377"
378{
379   int n;
380   int verb;
381   if (size(#) != 0)
382   {
383     n=#[1];
384     if (size(#) >1)
385     { verb=#[2]; }
386   }
387
388  if ( verb != 0)
389  {
390    if ( n==0)
391    { dbprint(printlevel-voice+3,
392      "// memory used, at the moment, by active variables (kilobyte):"); }
393    if ( n==1 )
394    { dbprint(printlevel-voice+3,
395      "// total memory allocated, at the moment, by SINGULAR (kilobyte):"); }
396   }
397   return ((memory(n)+1023)/1024);
398}
399example
400{ "EXAMPLE:"; echo = 2;
401   kmemory();
402   kmemory(1,1);
403}
404///////////////////////////////////////////////////////////////////////////////
405
406proc killall
407"USAGE:   killall(); (no parameter)
408         killall(\"type_name\");
409         killall(\"not\", \"type_name\");
410COMPUTE: killall(); kills all user-defined variables but not loaded procedures
411         killall(\"type_name\"); kills all user-defined variables,
412         of type \"type_name\"
413         killall(\"not\", \"type_name\"); kills all user-defined variables,
414         except those of type \"type_name\" and except loaded procedures
415RETURN:  no return value
416NOTE:    killall should never be used inside a procedure
417EXAMPLE: example killall; shows an example AND KILLS ALL YOUR VARIABLES
418"
419{
420   list L=names(); int joni=size(L);
421   if( size(#)==0 )
422   {
423      for ( ; joni>0; joni-- )
424      {
425         if( L[joni]!="LIB" and typeof(`L[joni]`)!="proc" ) { kill `L[joni]`; }
426      }
427   }
428   else
429   {
430     if( size(#)==1 )
431     {
432       if( #[1] == "proc" )
433       {
434          for ( joni=size(L); joni>0; joni-- )
435          {
436             if( L[joni]=="LIB" or typeof(`L[joni]`)=="proc" )
437               { kill `L[joni]`; }
438          }
439       }
440       else
441       {
442          for ( ; joni>2; joni-- )
443          {
444            if(typeof(`L[joni]`)==#[1] and L[joni]!="LIB" and typeof(`L[joni]`)!="proc") { kill `L[joni]`; }
445          }
446        }
447     }
448     else
449     {
450        for ( ; joni>2; joni-- )
451        {
452          if(typeof(`L[joni]`)!=#[2] and L[joni]!="LIB" and typeof(`L[joni]`)!="proc") { kill `L[joni]`; }
453        }
454     }
455  }
456  return();
457}
458example
459{ "EXAMPLE:"; echo = 2;
460   ring rtest; ideal i=x,y,z; string str="hi"; int j = 3;
461   export rtest,i,str,j;       //this makes the local variables global
462   listvar();
463   killall("ring");            // kills all rings
464   listvar();
465   killall("not", "int");      // kills all variables except int's (and procs)
466   listvar();
467   killall();                  // kills all vars except loaded procs
468   listvar();
469}
470///////////////////////////////////////////////////////////////////////////////
471
472proc number_e (int n)
473"USAGE:   number_e(n);  n integer
474COMPUTE: Euler number e=exp(1) up to n decimal digits (no rounding)
475         by A.H.J. Sale's algorithm
476RETURN:  - string of exp(1) if no basering of char 0 is defined;
477         - exp(1), type number, if a basering of char 0 is defined, display its
478         decimal format if printlevel >= voice (default:printlevel=voice-1 )
479EXAMPLE: example number_e; shows an example
480"
481{
482   int i,m,s,t;
483   intvec u,e;
484   u[n+2]=0; e[n+1]=0; e=e+1;
485   if( defined(basering) )
486   {
487      if( char(basering)==0 ) { number r=2; t=1; }
488   }
489   string result = "2.";
490   for( i=1; i<=n+1; i=i+1 )
491   {
492      e = e*10;
493      for( m=n+1; m>=1; m=m-1 )
494      {
495         s    = e[m]+u[m+1];
496         u[m] = s div (m+1);
497         e[m] = s%(m+1);
498      }
499      result = result+string(u[1]);
500      if( t==1 ) { r = r+number(u[1])/number(10)^i; }
501   }
502   if( t==1 )
503   { dbprint(printlevel-voice+2,"// "+result[1,n+1]);
504     return(r);
505   }
506   return(result[1,n+1]);
507}
508example
509{ "EXAMPLE:"; echo = 2;
510   number_e(30);"";
511   ring R = 0,t,lp;
512   number e = number_e(30);
513   e;
514}
515///////////////////////////////////////////////////////////////////////////////
516
517proc number_pi (int n)
518"USAGE:   number_pi(n);  n positive integer
519COMPUTE: pi (area of unit circle) up to n decimal digits (no rounding)
520         by algorithm of S. Rabinowitz
521RETURN:  - string of pi if no basering of char 0 is defined,
522         - pi, of type number, if a basering of char 0 is defined, display its
523         decimal format if printlevel >= voice (default:printlevel=voice-1 )
524EXAMPLE: example number_pi; shows an example
525"
526{
527   int i,m,t,e,q,N;
528   intvec r,p,B,Prelim;
529   string result,prelim;
530   N = (10*n) div 3 + 2;
531   p[N+1]=0; p=p+2; r=p;
532   for( i=1; i<=N+1; i=i+1 ) { B[i]=2*i-1; }
533   if( defined(basering) )
534   {
535      if( char(basering)==0 ) { number pi; number pri; t=1; }
536   }
537   for( i=0; i<=n; i=i+1 )
538   {
539      p = r*10;
540      e = p[N+1];
541      for( m=N+1; m>=2; m=m-1 )
542      {
543         r[m] = e%B[m];
544         q    = e div B[m];
545         e    = q*(m-1)+p[m-1];
546      }
547      r[1] = e%10;
548      q    = e div 10;
549      if( q!=10 and q!=9 )
550      {
551         result = result+prelim;
552         Prelim = q;
553         prelim = string(q);
554      }
555      if( q==9 )
556      {
557         Prelim = Prelim,9;
558         prelim = prelim+"9";
559      }
560      if( q==10 )
561      {
562         Prelim = (Prelim+1)-((Prelim+1) div 10)*10;
563         for( m=size(Prelim); m>0; m=m-1)
564         {
565            prelim[m] = string(Prelim[m]);
566         }
567         result = result+prelim;
568         if( t==1 ) { pi=pi+pri; }
569         Prelim = 0;
570         prelim = "0";
571      }
572      if( t==1 ) { pi=pi+number(q)/number(10)^i; }
573   }
574   result = result,prelim[1];
575   result = "3."+result[2,n-1];
576   if( t==1 )
577   { dbprint(printlevel-voice+2,"// "+result);
578     return(pi);
579   }
580   return(result);
581}
582example
583{ "EXAMPLE:"; echo = 2;
584   number_pi(11);"";
585   ring r = (real,10),t,dp;
586   number pi = number_pi(11); pi;
587}
588///////////////////////////////////////////////////////////////////////////////
589
590proc primes (int n, int m)
591"USAGE:   primes(n,m);  n,m integers
592RETURN:  intvec, consisting of all primes p, prime(n)<=p<=m, in increasing
593         order if n<=m, resp. prime(m)<=p<=n, in decreasing order if m<n
594NOTE:    prime(n); returns the biggest prime number <= n (if n>=2, else 2)
595EXAMPLE: example primes; shows an example
596"
597{  int change;
598   if ( n>m ) { change=n; n=m ; m=change; change=1; }
599   int q,p = prime(m),prime(n); intvec v = q; q = q-1;
600   while ( q>=p ) { q = prime(q); v = q,v; q = q-1; }
601   if ( change==1 ) { v = v[size(v)..1]; }
602   return(v);
603}
604example
605{  "EXAMPLE:"; echo = 2;
606    primes(50,100);"";
607    intvec v = primes(37,1); v;
608}
609///////////////////////////////////////////////////////////////////////////////
610
611proc product (id, list #)
612"USAGE:    product(id[,v]); id ideal/vector/module/matrix/intvec/intmat/list,
613          v intvec  (default: v=1.. number of entries of id)
614RETURN:   - if id is not a list: poly resp. int, the product of all entries of
615          id with index given by v.
616          id is treated as a list of polys resp. integers. A module m is
617          identified with corresponding matrix M (columns of M generate m)
618          - if id is a list: product of list entries, with index given by v.
619          Assume that list members can be multiplied
620EXAMPLE:  example product; shows an example
621"
622{
623   int n,j,tt;
624   string ty;
625   list l;
626   int s = size(#);
627   if( s!=0 )
628   {  if ( typeof(#[s])=="intvec" )
629      {  intvec v = #[s];
630         tt=1; s=s-1;
631         if ( s>0 ) { # = #[1..s]; }
632      }
633   }
634   if ( s>0 )
635   {
636     l = list(id)+#;
637     kill id;
638     list id = l;
639     ty = "list";
640   }
641   else
642   { ty = typeof(id);
643   }
644   if( ty=="list" )
645   { n = size(id);
646     def f(1) = id[1];
647     for( j=2; j<=n; j=j+1 ) { def f(j)=f(j-1)*id[j]; }
648     return(f(n));
649   }
650   if( ty=="poly" or ty=="ideal" or ty=="vector"
651       or ty=="module" or ty=="matrix" )
652   {
653      ideal i = ideal(matrix(id));
654      kill id;
655      ideal id = i;
656      if( tt!=0 ) { id = id[v]; }
657      n = ncols(id); poly f(1)=id[1];
658   }
659   if( ty=="int" or ty=="intvec" or ty=="intmat" )
660   {
661      if ( ty == "int" ) { intmat S =id; }
662      else { intmat S = intmat(id); }
663      intvec i = S[1..nrows(S),1..ncols(S)];
664      kill id;
665      intvec id = i;
666      if( tt!=0 ) { id = id[v]; }
667      n = size(id); int f(1)=id[1];
668   }
669   for( j=2; j<=n; j=j+1 ) { def f(j)=f(j-1)*id[j]; }
670   return(f(n));
671}
672example
673{  "EXAMPLE:"; echo = 2;
674   ring r= 0,(x,y,z),dp;
675   ideal m = maxideal(1);
676   product(m);
677   product(m[2..3]);
678   matrix M[2][3] = 1,x,2,y,3,z;
679   product(M);
680   intvec v=2,4,6;
681   product(M,v);
682   intvec iv = 1,2,3,4,5,6,7,8,9;
683   v=1..5,7,9;
684   product(iv,v);
685   intmat A[2][3] = 1,1,1,2,2,2;
686   product(A,3..5);
687}
688///////////////////////////////////////////////////////////////////////////////
689proc ringweights (list # )
690"USAGE:   ringweights (P); P=name of an existing ring (true name, not a string)
691RETURN:  intvec, size=nvars(P), consisting of the weights of the variables of P
692NOTE:    This is useful when enlarging P but keeping the weights of the old
693         variables
694EXAMPLE: example ringweights;  shows an example
695"
696{
697   int ii,q,fi,fo,fia;
698   intvec rw,nw;
699   string os;
700   def P = #[1];
701   string osP = ordstr(P);
702   fo  = 1;
703//------------------------- find weights in ordstr(P) -------------------------
704   fi  = find(osP,"(",fo);
705   fia = find(osP,"a",fo)+find(osP,"w",fo)+find(osP,"W",fo);
706   while ( fia )
707   {
708      os = osP[fi+1,find(osP,")",fi)-fi-1];
709      if( find(os,",") )
710      {
711         execute "nw = "+os+";";
712         if( size(nw) > ii )
713         {
714             rw = rw,nw[ii+1..size(nw)];
715         }
716         else  {  ii = ii - size(nw); }
717
718         if( find(osP[1,fi],"a",fo) ) { ii = size(nw); }
719      }
720      else
721      {
722         execute "q = "+os+";";
723         if( q > ii )
724         {
725            nw = 0; nw[q-ii] = 0;
726            nw = nw + 1;          //creates an intvec 1,...,1 of length q-ii
727            rw = rw,nw;
728         }
729         else { ii = ii - q; }
730      }
731      fo  = fi+1;
732      fi  = find(osP,"(",fo);
733      fia = find(osP,"a",fo)+find(osP,"w",fo)+find(osP,"W",fo);
734   }
735//-------------- adjust weight vector to length = nvars(P)  -------------------
736   if( fo > 1 )
737   {                                            // case when weights were found
738      rw = rw[2..size(rw)];
739      if( size(rw) > nvars(P) )
740      {
741         rw = rw[1..nvars(P)];
742      }
743      if( size(rw) < nvars(P) )
744      {
745         nw=0; nw[nvars(P)-size(rw)]=0; nw=nw+1; rw=rw,nw;
746      }
747   }
748   else
749   {                                         // case when no weights were found
750      rw[nvars(P)]= 0; rw=rw+1;
751   }
752   return(rw);
753}
754example
755{"EXAMPLE:";  echo = 2;
756  ring r0 = 0,(x,y,z),dp;
757  ringweights(r0);
758  ring r1 = 0,x(1..5),(ds(3),wp(2,3));
759  ringweights(r1);
760  ring r2 = 0,x(1..5),(a(1,2,3,0),dp);
761  ringweights(r2);
762  ring r3 = 0,x(1..10),(a(1..5),dp(5),a(10..13),Wp(5..9));
763  ringweights(r3);
764  // an example for enlarging the ring:
765  intvec v = 6,2,3,4,5;
766  ring R = 0,x(1..10),(a(ringweights(r1),v),dp);
767  ordstr(R);
768}
769
770///////////////////////////////////////////////////////////////////////////////
771
772proc sort (id, list #)
773"USAGE:   sort(id[v,o,n]); id=ideal/module/intvec/list (of intvec's or int's)
774         sort may be called with 1, 2 or 3 arguments in the following way:
775         sort(id[v,n]); v=intvec of positive integers, n=integer,
776         sort(id[o,n]); o=string (any allowed ordstr of a ring), n=integer
777RETURN:  a list of two elements:
778         [1]: object of same type as input but sorted in the following manner:
779           - if id=ideal/module: generators of id are sorted w.r.t. intvec v
780             (id[v[1]] becomes 1-st, id[v[2]]  2-nd element, etc.). If no v is
781             present, id is sorted w.r.t. ordering o (if o is given) or w.r.t.
782             actual monomial ordering (if no o is given):
783                    generators with smaller leading term come first
784             (e.g. sort(id); sorts w.r.t actual monomial ordering)
785           - if id=list of intvec's or int's: consider a list element, say
786             id[1]=3,2,5, as exponent vector of the monomial x^3*y^2*z^5;
787             the corresponding monomials are ordered w.r.t. intvec v (s.a.).
788             If no v is present, the monomials are sorted w.r.t. ordering o
789             (if o is given) or w.r.t. lexicographical ordering (if no o is
790             given). The corresponding ordered list of exponent vectors is
791             returned.
792             (e.g. sort(id); sorts lexicographically, smaller int's come first)
793             WARNING: Since negative exponents create the 0 polynomial in
794             Singular, id should not contain negative integers: the result
795             might not be as expected
796           - if id=intvec: id is treated as list of integers
797           - if n!=0 the ordering is inverse, i.e. w.r.t. v(size(v)..1)
798             default: n=0
799         [2]: intvec, describing the permutation of the input (hence [2]=v if
800             v is given (with positive integers)
801NOTE:    If v is given id may be any simply indexed object (e.g. any list or
802         string); if v[i]<0 and i<=size(id) v[i] is set internally to i;
803         entries of v must be pairwise distinct to get a permutation if id.
804         Zero generators of ideal/module are deleted
805EXAMPLE: example sort; shows an example
806"
807{  int ii,jj,s,n = 0,0,1,0;
808   intvec v;
809   if ( defined(basering) ) { def P = basering; }
810   if ( size(#)==0 and (typeof(id)=="ideal" or typeof(id)=="module") )
811   {
812      id = simplify(id,2);
813      for ( ii=1; ii<size(id); ii++ )
814      {
815         if ( id[ii]!=id[ii+1] ) { break;}
816      }
817      if ( ii != size(id) ) { v = sortvec(id); }
818      else  { v = size(id)..1; }
819   }
820   if ( size(#)>=1 and (typeof(id)=="ideal" or typeof(id)=="module") )
821   {
822      if ( typeof(#[1])=="string" )
823      {
824         execute "ring r1 =("+charstr(P)+"),("+varstr(P)+"),("+#[1]+");";
825         def i = imap(P,id);
826         v = sortvec(i);
827         setring P;
828         n=2;
829      }
830   }
831   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==0 )
832   {
833      string o;
834      if ( size(#)==0 ) { o = "lp"; n=1; }
835      if ( size(#)>=1 )
836      {
837         if ( typeof(#[1])=="string" ) { o = #[1]; n=1; }
838      }
839   }
840   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==1 )
841   {
842      if ( typeof(id)=="list" )
843      {
844         for (ii=1; ii<=size(id); ii++)
845         {
846            if (typeof(id[ii]) != "intvec" and typeof(id[ii]) != "int")
847               { "// list elements must be intvec/int"; return(); }
848            else
849               { s=size(id[ii])*(s < size(id[ii])) + s*(s >= size(id[ii])); }
850         }
851      }
852      execute "ring r=0,x(1..s),("+o+");";
853      ideal i;
854      poly f;
855      for (ii=1; ii<=size(id); ii++)
856      {
857         f=1;
858         for (jj=1; jj<=size(id[ii]); jj++)
859         {
860            f=f*x(jj)^(id[ii])[jj];
861         }
862         i[ii]=f;
863      }
864      v = sort(i)[2];
865   }
866   if ( size(#)!=0 and n==0 ) { v = #[1]; }
867   if( size(#)==2 )
868   {
869      if ( #[2] != 0 ) { v = v[size(v)..1]; }
870   }
871   s = size(v);
872   if( size(id) < s ) { s = size(id); }
873   def m = id;
874   if ( size(m) != 0 )
875   {
876      for ( jj=1; jj<=s; jj=jj+1)
877      {
878         if ( v[jj]<=0 ) { v[jj]=jj; }
879         m[jj] = id[v[jj]];
880      }
881   }
882   if ( v == 0 ) { v = 1; }
883   list L=m,v;
884   return(L);
885}
886example
887{  "EXAMPLE:"; echo = 2;
888   ring r0 = 0,(x,y,z,t),lp;
889   ideal i = x3,z3,xyz;
890   sort(i);                // sort w.r.t. lex ordering
891   sort(i,3..1);
892   sort(i,"ls")[1];        // sort w.r.t. negative lex ordering
893   list L =1,8..5,3..10;
894   sort(L)[1];             // sort L lexicographically
895   sort(L,"Dp",1)[1];      // sort L w.r.t (total sum, reverse lex)
896}
897///////////////////////////////////////////////////////////////////////////////
898
899proc sum (id, list #)
900"USAGE:    sum(id[,v]); id=ideal/vector/module/matrix resp. id=intvec/intmat,
901                       v=intvec (e.g. v=1..n, n=integer)
902RETURN:   poly resp. int which is the sum of all entries of id, with index
903          given by v (default: v=1..number of entries of id)
904NOTE:     id is treated as a list of polys resp. integers. A module m is
905          identified with corresponding matrix M (columns of M generate m)
906EXAMPLE:  example sum; shows an example
907"
908{
909   if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
910       or typeof(id)=="module" or typeof(id)=="matrix" )
911   {
912      ideal i = ideal(matrix(id));
913      if( size(#)!=0 ) { i = i[#[1]]; }
914      matrix Z = matrix(i);
915   }
916   if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
917   {
918      if ( typeof(id) == "int" ) { intmat S =id; }
919      else { intmat S = intmat(id); }
920      intvec i = S[1..nrows(S),1..ncols(S)];
921      if( size(#)!=0 ) { i = i[#[1]]; }
922      intmat Z=transpose(i);
923   }
924   intvec v; v[ncols(Z)]=0; v=v+1;
925   return((Z*v)[1,1]);
926 }
927example
928{  "EXAMPLE:"; echo = 2;
929   ring r= 0,(x,y,z),dp;
930   vector pv = [xy,xz,yz,x2,y2,z2];
931   sum(pv);
932   sum(pv,2..5);
933   matrix M[2][3] = 1,x,2,y,3,z;
934   intvec w=2,4,6;
935   sum(M,w);
936   intvec iv = 1,2,3,4,5,6,7,8,9;
937   sum(iv,2..4);
938}
939///////////////////////////////////////////////////////////////////////////////
940
941proc which (command)
942"USAGE:    which(command); command = string expression
943RETURN:   Absolute pathname of command, if found in search path.
944          Empty string, otherwise.
945NOTE:     Based on the Unix command 'which'.
946EXAMPLE:  example which; shows an example
947"
948{
949   int rs;
950   int i;
951   string fn = "/tmp/which_" + string(system("pid"));
952   string pn;
953   if( typeof(command) != "string")
954   {
955     return (pn);
956   }
957   i = system("sh", "which " + command + " > " + fn);
958   pn = read(fn);
959   pn[size(pn)] = "";
960   i = 1;
961   while ((pn[i] != " ") and (pn[i] != ""))
962   {
963     i = i+1;
964   }
965   if (pn[i] == " ") {pn[i] = "";}
966   rs = system("sh", "ls " + pn + " > " + fn + " 2>&1 ");
967   i = system("sh", "rm " + fn);
968   if (rs == 0) {return (pn);}
969   else
970   {
971     print (command + " not found ");
972     return ("");
973   }
974}
975example
976{  "EXAMPLE:"; echo = 2;
977    which("Singular");
978}
979///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.