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

spielwiese
Last change on this file since 824241 was 212d09, checked in by Hans Schönemann <hannes@…>, 25 years ago
*hannes: system("random") -> system("--random") git-svn-id: file:///usr/local/Singular/svn/trunk@3636 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 28.0 KB
Line 
1// $Id: general.lib,v 1.19 1999-09-21 17:32:15 Singular Exp $
2//GMG, last modified 18.6.99
3///////////////////////////////////////////////////////////////////////////////
4
5version="$Id: general.lib,v 1.19 1999-09-21 17:32:15 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///////////////////////////////////////////////////////////////////////////////
689
690proc ringweights (r)
691"USAGE:   ringweights(r); r ring
692RETURN:  intvec of weights of ring variables. If, say, x(1),...,x(n) are the
693         variables of the ring r, in this order, the resulting intvec is
694         deg(x(1)),...,deg(x(n)) where deg denotes the weighted degree if
695         the monomial ordering of r has only one block of type ws,Ws,wp or Wp.
696NOTE:    In all other cases, in particular if there is more than one block,
697         the resulting intvec is 1,...,1
698EXAMPLE: example ringweights; shows an example
699"
700{
701   int i; intvec v; setring r;
702   for (i=1; i<=nvars(basering); i=i+1) { v[i] = deg(var(i)); }
703   return(v);
704}
705example
706{ "EXAMPLE:"; echo = 2;
707   ring r1=32003,(x,y,z),wp(1,2,3);
708   ring r2=32003,(x,y,z),Ws(1,2,3);
709   ring r=0,(x,y,u,v),lp;
710   intvec vr=ringweights(r1); vr;
711   ringweights(r2);
712   ringweights(r);
713}
714///////////////////////////////////////////////////////////////////////////////
715
716proc sort (id, list #)
717"USAGE:   sort(id[v,o,n]); id=ideal/module/intvec/list (of intvec's or int's)
718         sort may be called with 1, 2 or 3 arguments in the following way:
719         sort(id[v,n]); v=intvec of positive integers, n=integer,
720         sort(id[o,n]); o=string (any allowed ordstr of a ring), n=integer
721RETURN:  a list of two elements:
722         [1]: object of same type as input but sorted in the following manner:
723           - if id=ideal/module: generators of id are sorted w.r.t. intvec v
724             (id[v[1]] becomes 1-st, id[v[2]]  2-nd element, etc.). If no v is
725             present, id is sorted w.r.t. ordering o (if o is given) or w.r.t.
726             actual monomial ordering (if no o is given):
727                    generators with smaller leading term come first
728             (e.g. sort(id); sorts w.r.t actual monomial ordering)
729           - if id=list of intvec's or int's: consider a list element, say
730             id[1]=3,2,5, as exponent vector of the monomial x^3*y^2*z^5;
731             the corresponding monomials are ordered w.r.t. intvec v (s.a.).
732             If no v is present, the monomials are sorted w.r.t. ordering o
733             (if o is given) or w.r.t. lexicographical ordering (if no o is
734             given). The corresponding ordered list of exponent vectors is
735             returned.
736             (e.g. sort(id); sorts lexicographically, smaller int's come first)
737             WARNING: Since negative exponents create the 0 polynomial in
738             Singular, id should not contain negative integers: the result
739             might not be as expected
740           - if id=intvec: id is treated as list of integers
741           - if n!=0 the ordering is inverse, i.e. w.r.t. v(size(v)..1)
742             default: n=0
743         [2]: intvec, describing the permutation of the input (hence [2]=v if
744             v is given (with positive integers)
745NOTE:    If v is given id may be any simply indexed object (e.g. any list or
746         string); if v[i]<0 and i<=size(id) v[i] is set internally to i;
747         entries of v must be pairwise distinct to get a permutation if id.
748         Zero generators of ideal/module are deleted
749EXAMPLE: example sort; shows an example
750"
751{  int ii,jj,s,n = 0,0,1,0;
752   intvec v;
753   if ( defined(basering) ) { def P = basering; }
754   if ( size(#)==0 and (typeof(id)=="ideal" or typeof(id)=="module") )
755   {
756      id = simplify(id,2);
757      for ( ii=1; ii<size(id); ii++ )
758      {
759         if ( id[ii]!=id[ii+1] ) { break;}
760      }
761      if ( ii != size(id) ) { v = sortvec(id); }
762      else  { v = size(id)..1; }
763   }
764   if ( size(#)>=1 and (typeof(id)=="ideal" or typeof(id)=="module") )
765   {
766      if ( typeof(#[1])=="string" )
767      {
768         execute "ring r1 =("+charstr(P)+"),("+varstr(P)+"),("+#[1]+");";
769         def i = imap(P,id);
770         v = sortvec(i);
771         setring P;
772         n=2;
773      }
774   }
775   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==0 )
776   {
777      string o;
778      if ( size(#)==0 ) { o = "lp"; n=1; }
779      if ( size(#)>=1 )
780      {
781         if ( typeof(#[1])=="string" ) { o = #[1]; n=1; }
782      }
783   }
784   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==1 )
785   {
786      if ( typeof(id)=="list" )
787      {
788         for (ii=1; ii<=size(id); ii++)
789         {
790            if (typeof(id[ii]) != "intvec" and typeof(id[ii]) != "int")
791               { "// list elements must be intvec/int"; return(); }
792            else
793               { s=size(id[ii])*(s < size(id[ii])) + s*(s >= size(id[ii])); }
794         }
795      }
796      execute "ring r=0,x(1..s),("+o+");";
797      ideal i;
798      poly f;
799      for (ii=1; ii<=size(id); ii++)
800      {
801         f=1;
802         for (jj=1; jj<=size(id[ii]); jj++)
803         {
804            f=f*x(jj)^(id[ii])[jj];
805         }
806         i[ii]=f;
807      }
808      v = sort(i)[2];
809   }
810   if ( size(#)!=0 and n==0 ) { v = #[1]; }
811   if( size(#)==2 )
812   {
813      if ( #[2] != 0 ) { v = v[size(v)..1]; }
814   }
815   s = size(v);
816   if( size(id) < s ) { s = size(id); }
817   def m = id;
818   if ( size(m) != 0 )
819   {
820      for ( jj=1; jj<=s; jj=jj+1)
821      {
822         if ( v[jj]<=0 ) { v[jj]=jj; }
823         m[jj] = id[v[jj]];
824      }
825   }
826   if ( v == 0 ) { v = 1; }
827   list L=m,v;
828   return(L);
829}
830example
831{  "EXAMPLE:"; echo = 2;
832   ring r0 = 0,(x,y,z,t),lp;
833   ideal i = x3,z3,xyz;
834   sort(i);                // sort w.r.t. lex ordering
835   sort(i,3..1);
836   sort(i,"ls")[1];        // sort w.r.t. negative lex ordering
837   list L =1,8..5,3..10;
838   sort(L)[1];             // sort L lexicographically
839   sort(L,"Dp",1)[1];      // sort L w.r.t (total sum, reverse lex)
840}
841///////////////////////////////////////////////////////////////////////////////
842
843proc sum (id, list #)
844"USAGE:    sum(id[,v]); id=ideal/vector/module/matrix resp. id=intvec/intmat,
845                       v=intvec (e.g. v=1..n, n=integer)
846RETURN:   poly resp. int which is the sum of all entries of id, with index
847          given by v (default: v=1..number of entries of id)
848NOTE:     id is treated as a list of polys resp. integers. A module m is
849          identified with corresponding matrix M (columns of M generate m)
850EXAMPLE:  example sum; shows an example
851"
852{
853   if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
854       or typeof(id)=="module" or typeof(id)=="matrix" )
855   {
856      ideal i = ideal(matrix(id));
857      if( size(#)!=0 ) { i = i[#[1]]; }
858      matrix Z = matrix(i);
859   }
860   if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
861   {
862      if ( typeof(id) == "int" ) { intmat S =id; }
863      else { intmat S = intmat(id); }
864      intvec i = S[1..nrows(S),1..ncols(S)];
865      if( size(#)!=0 ) { i = i[#[1]]; }
866      intmat Z=transpose(i);
867   }
868   intvec v; v[ncols(Z)]=0; v=v+1;
869   return((Z*v)[1,1]);
870 }
871example
872{  "EXAMPLE:"; echo = 2;
873   ring r= 0,(x,y,z),dp;
874   vector pv = [xy,xz,yz,x2,y2,z2];
875   sum(pv);
876   sum(pv,2..5);
877   matrix M[2][3] = 1,x,2,y,3,z;
878   intvec w=2,4,6;
879   sum(M,w);
880   intvec iv = 1,2,3,4,5,6,7,8,9;
881   sum(iv,2..4);
882}
883///////////////////////////////////////////////////////////////////////////////
884
885proc which (command)
886"USAGE:    which(command); command = string expression
887RETURN:   Absolute pathname of command, if found in search path.
888          Empty string, otherwise.
889NOTE:     Based on the Unix command 'which'.
890EXAMPLE:  example which; shows an example
891"
892{
893   int rs;
894   int i;
895   string fn = "/tmp/which_" + string(system("pid"));
896   string pn;
897   if( typeof(command) != "string")
898   {
899     return (pn);
900   }
901   i = system("sh", "which " + command + " > " + fn);
902   pn = read(fn);
903   pn[size(pn)] = "";
904   i = 1;
905   while ((pn[i] != " ") and (pn[i] != ""))
906   {
907     i = i+1;
908   }
909   if (pn[i] == " ") {pn[i] = "";}
910   rs = system("sh", "ls " + pn + " > " + fn + " 2>&1 ");
911   i = system("sh", "rm " + fn);
912   if (rs == 0) {return (pn);}
913   else
914   {
915     print (command + " not found ");
916     return ("");
917   }
918}
919example
920{  "EXAMPLE:"; echo = 2;
921    which("Singular");
922}
923///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.