source: git/Singular/LIB/general.lib @ 1354bd

spielwiese
Last change on this file since 1354bd was dd2aa36, checked in by Hans Schönemann <hannes@…>, 25 years ago
* hannes: fixed verbosity of general.lib::kmemory git-svn-id: file:///usr/local/Singular/svn/trunk@3151 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 23.4 KB
Line 
1// $Id: general.lib,v 1.13 1999-06-21 17:34:15 Singular Exp $
2//system("random",787422842);
3//GMG, last modified 18.6.99
4///////////////////////////////////////////////////////////////////////////////
5
6version="$Id: general.lib,v 1.13 1999-06-21 17:34:15 Singular Exp $";
7info="
8LIBRARY:  general.lib   PROCEDURES OF GENERAL TYPE
9
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 charakter
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
100/    0    1    2    3    4    5    6    7    8    9    :    ;    <    =
10147   48   49   50   51   52   53   54   55   56   57   58   59   60   61
102
103>    ?    @    A    B    C    D    E    F    G    H    I    J    K    L
10462   63   64   65   66   67   68   69   70   71   72   73   74   75   76
105
106M    N    O    P    Q    R    S    T    U    V    W    X    Y    Z    [
10777   78   79   80   81   82   83   84   85   86   87   88   89   90   91
108
109\\    ]    ^    _    `    a    b    c    d    e    f    g    h    i    j
11092   93   94   95   96   97   98   99  100  101  102  103  104  105  10
111
112k    l    m    n    o    p    q    r    s    t    u    v    w    x    y
113107  108  109  110  111  112  113  114  115  116  117  118  119  120  121
114
115z    {    |    }    ~
116122  123  124  125  126 ";
117
118   string s2 =
119 " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
120
121   if ( size(#) == 0 )
122   {
123      return(s1);
124   }
125   if ( size(#) == 1 )
126   {
127      return( s2[#[1]-31] );
128   }
129   if ( size(#) == 2 )
130   {
131      return( s2[#[1]-31,#[2]-#[1]+1] );
132   }
133}
134example
135{ "EXAMPLE:"; echo = 2;
136   ASCII();"";
137   ASCII(42);
138   ASCII(32,126);
139}
140///////////////////////////////////////////////////////////////////////////////
141
142proc binomial (def n, int k, list #)
143"USAGE:   binomial(n,k[,p]); n,k,p integers
144RETURN:  binomial(n,k); binomial coefficient n choose k,
145         of type number if n is of type number (computed in char(basering)),
146         of type int if n is of type int (machine integer, limited size!)
147         binomial(n,k,p);  n choose k in char p, of type string
148EXAMPLE: example binomial; shows an example
149"
150{
151   if ( size(#)==0 )
152   {
153      if ( typeof(n)=="number" ) { number rr=1; }
154      else { int rr=1; }
155   }
156   if ( typeof(#[1])=="int") { ring bin = #[1],x,dp; number rr=1; }
157   if ( size(#)==0 or typeof(#[1])=="int" )
158   {
159      def r = rr;
160      if ( k<=0 or k>n ) { return((k==0)*r); }
161      if ( k>n-k ) { k = n-k; }
162      int l;
163      for (l=1; l<=k; l=l+1 )
164      {
165         r=r*(n+1-l)/l;
166      }
167      if ( typeof(#[1])=="int" ) { return(string(r)); }
168      return(r);
169   }
170}
171example
172{ "EXAMPLE:"; echo = 2;
173   int b1 = binomial(10,7); b1;
174   binomial(137,17,0);
175   ring t = 0,x,dp;
176   number b2 = binomial(number(137),17); b2;
177}
178///////////////////////////////////////////////////////////////////////////////
179
180proc factorial (def n)
181"USAGE:   factorial(n);  n = integer
182RETURN:  factorial(n); n! in char 0, of type string if n is of type int
183         n! of type number, computed in char(basering) if n is of type number
184EXAMPLE: example factorial; shows an example
185"
186{
187   int t,l;
188   if ( typeof(n)=="number" ) { number r=1; }
189   else { ring R = 0,x,dp; number r=1; t=1; }
190   for (l=2; l<=n; l=l+1)
191   {
192      r=r*l;
193   }
194   if ( t==1 ) { return(string(r)); }
195   return(r);
196}
197example
198{ "EXAMPLE:"; echo = 2;
199   factorial(37);
200   ring r1 = 32003,(x,y,z),ds;
201   number p = factorial(number(37)); p;
202}
203///////////////////////////////////////////////////////////////////////////////
204
205proc fibonacci (def n)
206"USAGE:   fibonacci(n);  (n integer)
207RETURN:  fibonacci(n); nth Fibonacci number,
208            f(0)=f(1)=1, f(i+1)=f(i-1)+f(i)
209         of type string if n is of type int
210         of type number computed in char(basering) if n is of type number
211EXAMPLE: example fibonacci; shows an example
212"
213{
214   int ii,t;
215   if ( typeof(n)=="number" ) { number f,g,h=1,1,1; }
216   else { ring fibo = 0,x,dp; number f,g,h=1,1,1; t=1; }
217   for (ii=3; ii<=n; ii=ii+1)
218   {
219      h = f+g; f = g; g = h;
220    }
221   if ( t==1 ) { return(string(h)); }
222   return(h);
223}
224example
225{ "EXAMPLE:"; echo = 2;
226   fibonacci(37);
227   ring r = 17,x,dp;
228   number b = fibonacci(number(37)); b;
229}
230///////////////////////////////////////////////////////////////////////////////
231
232proc kmemory (list #)
233"USAGE:   kmemory([n,[v]]); n = int
234RETURN:  memory in kilobyte of type int
235         n=0: memory used by active variables (same as no parameters)
236         n=1: total memory allocated by Singular
237         n=2: difference between top and init memory adress (sbrk memory)
238         n!=0,1,2: 0
239DISPLAY: detailed information about allocated and used memory if v!=0
240NOTE:    kmemory uses internal function 'memory' to compute kilobyte, and
241         is the same as 'memory' for n!=0,1,2
242EXAMPLE: example kmemory; shows an example
243"
244{
245   int n;
246   int verb;
247   if (size(#) != 0)
248   {
249     n=#[1];
250     if (size(#) >1)
251     { verb=#[2]; }
252   }
253   
254  if ( verb != 0)
255  {
256    if ( n==0)
257    { dbprint(printlevel-voice+3,
258      "// memory used, at the moment, by active variables (kilobyte):"); }
259    if ( n==1 )
260    { dbprint(printlevel-voice+3,
261      "// total memory allocated, at the moment, by SINGULAR (kilobyte):"); }
262   }
263   return ((memory(n)+1023)/1024);
264}
265example
266{ "EXAMPLE:"; echo = 2;
267   kmemory();
268   kmemory(1,1);
269}
270///////////////////////////////////////////////////////////////////////////////
271
272proc killall
273"USAGE:   killall(); (no parameter)
274         killall(\"type_name\");
275         killall(\"not\", \"type_name\");
276COMPUTE: killall(); kills all user-defined variables but not loaded procedures
277         killall(\"type_name\"); kills all user-defined variables, of type \"type_name\"
278         killall(\"not\", \"type_name\"); kills all user-defined
279         variables, except those of type \"type_name\" and except loaded procedures
280RETURN:  no return value
281NOTE:    killall should never be used inside a procedure
282EXAMPLE: example killall; shows an example AND KILLS ALL YOUR VARIABLES
283"
284{
285   list L=names(); int joni=size(L);
286   if( size(#)==0 )
287   {
288      for ( ; joni>0; joni-- )
289      {
290         if( L[joni]!="LIB" and typeof(`L[joni]`)!="proc" ) { kill `L[joni]`; }
291      }
292   }
293   else
294   {
295     if( size(#)==1 )
296     {
297       if( #[1] == "proc" )
298       {
299          for ( joni=size(L); joni>0; joni-- )
300          {
301             if( L[joni]=="LIB" or typeof(`L[joni]`)=="proc" )
302               { kill `L[joni]`; }
303          }
304       }
305       else
306       {
307          for ( ; joni>2; joni-- )
308          {
309            if(typeof(`L[joni]`)==#[1] and L[joni]!="LIB" and typeof(`L[joni]`)!="proc") { kill `L[joni]`; }
310          }
311        }
312     }
313     else
314     {
315        for ( ; joni>2; joni-- )
316        {
317          if(typeof(`L[joni]`)!=#[2] and L[joni]!="LIB" and typeof(`L[joni]`)!="proc") { kill `L[joni]`; }
318        }
319     }
320  }
321  return();
322}
323example
324{ "EXAMPLE:"; echo = 2;
325   ring rtest; ideal i=x,y,z; number n=37; string str="hi"; int j = 3;
326   export rtest,i,n,str,j;     //this makes the local variables global
327   listvar(all);
328   killall("string"); // kills all string variables
329   listvar(all);
330   killall("not", "int"); // kills all variables except int's (and procs)
331   listvar(all);
332   killall(); // kills all vars except loaded procs
333   listvar(all);
334}
335///////////////////////////////////////////////////////////////////////////////
336
337proc number_e (int n)
338"USAGE:   number_e(n);  n integer
339COMPUTE: Euler number e=exp(1) up to n decimal digits (no rounding)
340         by A.H.J. Sale's algorithm
341RETURN:  - string of exp(1) if no basering of char 0 is defined;
342         - exp(1), of type number, if a basering of char 0 is defined and
343         display its decimal format
344EXAMPLE: example number_e; shows an example
345"
346{
347   int i,m,s,t;
348   intvec u,e;
349   u[n+2]=0; e[n+1]=0; e=e+1;
350   if( defined(basering) )
351   {
352      if( char(basering)==0 ) { number r=2; t=1; }
353   }
354   string result = "2.";
355   for( i=1; i<=n+1; i=i+1 )
356   {
357      e = e*10;
358      for( m=n+1; m>=1; m=m-1 )
359      {
360         s    = e[m]+u[m+1];
361         u[m] = s div (m+1);
362         e[m] = s%(m+1);
363      }
364      result = result+string(u[1]);
365      if( t==1 ) { r = r+number(u[1])/number(10)^i; }
366   }
367   if( t==1 ) { "//",result[1,n+1]; return(r); }
368   return(result[1,n+1]);
369}
370example
371{ "EXAMPLE:"; echo = 2;
372   number_e(15);
373   ring R = 0,t,lp;
374   number e = number_e(10);
375   e;
376}
377///////////////////////////////////////////////////////////////////////////////
378
379proc number_pi (int n)
380"USAGE:   number_pi(n);  n positive integer
381COMPUTE: pi (area of unit circle) up to n decimal digits (no rounding)
382         by algorithm of S. Rabinowitz
383RETURN:  - string of pi if no basering of char 0 is defined,
384         - pi, of type number, if a basering of char 0 is defined and display
385         its decimal format
386EXAMPLE: example number_pi; shows an example
387"
388{
389   int i,m,t,e,q,N;
390   intvec r,p,B,Prelim;
391   string result,prelim;
392   N = (10*n) div 3 + 2;
393   p[N+1]=0; p=p+2; r=p;
394   for( i=1; i<=N+1; i=i+1 ) { B[i]=2*i-1; }
395   if( defined(basering) )
396   {
397      if( char(basering)==0 ) { number pi; number pri; t=1; }
398   }
399   for( i=0; i<=n; i=i+1 )
400   {
401      p = r*10;
402      e = p[N+1];
403      for( m=N+1; m>=2; m=m-1 )
404      {
405         r[m] = e%B[m];
406         q    = e div B[m];
407         e    = q*(m-1)+p[m-1];
408      }
409      r[1] = e%10;
410      q    = e div 10;
411      if( q!=10 and q!=9 )
412      {
413         result = result+prelim;
414         Prelim = q;
415         prelim = string(q);
416      }
417      if( q==9 )
418      {
419         Prelim = Prelim,9;
420         prelim = prelim+"9";
421      }
422      if( q==10 )
423      {
424         Prelim = (Prelim+1)-((Prelim+1) div 10)*10;
425         for( m=size(Prelim); m>0; m=m-1)
426         {
427            prelim[m] = string(Prelim[m]);
428         }
429         result = result+prelim;
430         if( t==1 ) { pi=pi+pri; }
431         Prelim = 0;
432         prelim = "0";
433      }
434      if( t==1 ) { pi=pi+number(q)/number(10)^i; }
435   }
436   result = result,prelim[1];
437   result = "3."+result[2,n-1];
438   if( t==1 ) { "//",result; return(pi); }
439   return(result);
440}
441example
442{ "EXAMPLE:"; echo = 2;
443   number_pi(5);
444   ring r = 0,t,lp;
445   number pi = number_pi(6);
446   pi;
447}
448///////////////////////////////////////////////////////////////////////////////
449
450proc primes (int n, int m)
451"USAGE:   primes(n,m);  n,m integers
452RETURN:  intvec, consisting of all primes p, prime(n)<=p<=m, in increasing
453         order if n<=m, resp. prime(m)<=p<=n, in decreasing order if m<n
454NOTE:    prime(n); returns the biggest prime number <= n (if n>=2, else 2)
455EXAMPLE: example primes; shows an example
456"
457{  int change;
458   if ( n>m ) { change=n; n=m ; m=change; change=1; }
459   int q,p = prime(m),prime(n); intvec v = q; q = q-1;
460   while ( q>=p ) { q = prime(q); v = q,v; q = q-1; }
461   if ( change==1 ) { v = v[size(v)..1]; }
462   return(v);
463}
464example
465{  "EXAMPLE:"; echo = 2;
466   primes(50,100);
467   intvec v = primes(37,1); v;
468}
469///////////////////////////////////////////////////////////////////////////////
470
471proc product (id, list #)
472"USAGE:    product(id[,v]); id=ideal/vector/module/matrix
473          resp.id=intvec/intmat, v=intvec (e.g. v=1..n, n=integer)
474RETURN:   poly resp. int which is the product of all entries of id, with index
475          given by v (default: v=1..number of entries of id)
476NOTE:     id is treated as a list of polys resp. integers. A module m is
477          identified with corresponding matrix M (columns of M generate m)
478EXAMPLE:  example product; shows an example
479"
480{
481   int n,j;
482   if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
483       or typeof(id)=="module" or typeof(id)=="matrix" )
484   {
485      ideal i = ideal(matrix(id));
486      if( size(#)!=0 ) { i = i[#[1]]; }
487      n = ncols(i); poly f=1;
488   }
489   if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
490   {
491      if ( typeof(id) == "int" ) { intmat S =id; }
492      else { intmat S = intmat(id); }
493      intvec i = S[1..nrows(S),1..ncols(S)];
494      if( size(#)!=0 ) { i = i[#[1]]; }
495      n = size(i); int f=1;
496   }
497   for( j=1; j<=n; j=j+1 ) { f=f*i[j]; }
498   return(f);
499}
500example
501{  "EXAMPLE:"; echo = 2;
502   ring r= 0,(x,y,z),dp;
503   ideal m = maxideal(1);
504   product(m);
505   matrix M[2][3] = 1,x,2,y,3,z;
506   product(M);
507   intvec v=2,4,6;
508   product(M,v);
509   intvec iv = 1,2,3,4,5,6,7,8,9;
510   v=1..5,7,9;
511   product(iv,v);
512   intmat A[2][3] = 1,1,1,2,2,2;
513   product(A,3..5);
514}
515///////////////////////////////////////////////////////////////////////////////
516
517proc ringweights (r)
518"USAGE:   ringweights(r); r ring
519RETURN:  intvec of weights of ring variables. If, say, x(1),...,x(n) are the
520         variables of the ring r, in this order, the resulting intvec is
521         deg(x(1)),...,deg(x(n)) where deg denotes the weighted degree if
522         the monomial ordering of r has only one block of type ws,Ws,wp or Wp.
523NOTE:    In all other cases, in particular if there is more than one block,
524         the resulting intvec is 1,...,1
525EXAMPLE: example ringweights; shows an example
526"
527{
528   int i; intvec v; setring r;
529   for (i=1; i<=nvars(basering); i=i+1) { v[i] = deg(var(i)); }
530   return(v);
531}
532example
533{ "EXAMPLE:"; echo = 2;
534   ring r1=32003,(x,y,z),wp(1,2,3);
535   ring r2=32003,(x,y,z),Ws(1,2,3);
536   ring r=0,(x,y,u,v),lp;
537   intvec vr=ringweights(r1); vr;
538   ringweights(r2);
539   ringweights(r);
540}
541///////////////////////////////////////////////////////////////////////////////
542
543proc sort (id, list #)
544"USAGE:   sort(id[v,o,n]); id=ideal/module/intvec/list (of intvec's or int's)
545         sort may be called with 1, 2 or 3 arguments in the following way:
546         -  sort(id[v,n]); v=intvec of positive integers, n=integer,
547         -  sort(id[o,n]); o=string (any allowed ordstr of a ring), n=integer
548RETURN:  a list of two elements:
549         [1]: object of same type as input but sorted in the following manner:
550           - if id=ideal/module: generators of id are sorted w.r.t. intvec v
551             (id[v[1]] becomes 1-st, id[v[2]]  2-nd element, etc.). If no v is
552             present, id is sorted w.r.t. ordering o (if o is given) or w.r.t.
553             actual monomial ordering (if no o is given):
554                    generators with smaller leading term come first
555             (e.g. sort(id); sorts w.r.t actual monomial ordering)
556           - if id=list of intvec's or int's: consider a list element, say
557             id[1]=3,2,5, as exponent vector of the monomial x^3*y^2*z^5;
558             the corresponding monomials are ordered w.r.t. intvec v (s.a.).
559             If no v is present, the monomials are sorted w.r.t. ordering o
560             (if o is given) or w.r.t. lexicographical ordering (if no o is
561             given). The corresponding ordered list of exponent vectors is
562             returned.
563             (e.g. sort(id); sorts lexicographically, smaller int's come first)
564             WARNING: Since negative exponents create the 0 polynomial in
565             Singular, id should not contain negative integers: the result
566             might not be as expected
567           - if id=intvec: id is treated as list of integers
568           - if n!=0 the ordering is inverse, i.e. w.r.t. v(size(v)..1)
569             default: n=0
570         [2]: intvec, describing the permutation of the input (hence [2]=v if
571             v is given (with positive integers)
572NOTE:    If v is given id may be any simply indexed object (e.g. any list or
573         string); if v[i]<0 and i<=size(id) v[i] is set internally to i;
574         entries of v must be pairwise distinct to get a permutation if id.
575         Zero generators of ideal/module are deleted
576EXAMPLE: example sort; shows an example
577"
578{
579   int ii,jj,s,n = 0,0,1,0;
580   intvec v;
581   if ( defined(basering) ) { def P = basering; }
582   if ( size(#)==0 and (typeof(id)=="ideal" or typeof(id)=="module") )
583   {
584      id = simplify(id,2);
585      for ( ii=1; ii<size(id); ii++ )
586      {
587         if ( id[ii]!=id[ii+1] ) { break;}
588      }
589      if ( ii != size(id) ) { v = sortvec(id); }
590      else  { v = size(id)..1; }
591   }
592   if ( size(#)>=1 and (typeof(id)=="ideal" or typeof(id)=="module") )
593   {
594      if ( typeof(#[1])=="string" )
595      {
596         execute "ring r1 =("+charstr(P)+"),("+varstr(P)+"),("+#[1]+");";
597         def i = imap(P,id);
598         v = sortvec(i);
599         setring P;
600         n=2;
601      }
602   }
603   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==0 )
604   {
605      string o;
606      if ( size(#)==0 ) { o = "lp"; n=1; }
607      if ( size(#)>=1 )
608      {
609         if ( typeof(#[1])=="string" ) { o = #[1]; n=1; }
610      }
611   }
612   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==1 )
613   {
614      if ( typeof(id)=="list" )
615      {
616         for (ii=1; ii<=size(id); ii++)
617         {
618            if (typeof(id[ii]) != "intvec" and typeof(id[ii]) != "int")
619               { "// list elements must be intvec/int"; return(); }
620            else
621               { s=size(id[ii])*(s < size(id[ii])) + s*(s >= size(id[ii])); }
622         }
623      }
624      execute "ring r=0,x(1..s),("+o+");";
625      ideal i;
626      poly f;
627      for (ii=1; ii<=size(id); ii++)
628      {
629         f=1;
630         for (jj=1; jj<=size(id[ii]); jj++)
631         {
632            f=f*x(jj)^(id[ii])[jj];
633         }
634         i[ii]=f;
635      }
636      v = sort(i)[2];
637   }
638   if ( size(#)!=0 and n==0 ) { v = #[1]; }
639   if( size(#)==2 )
640   {
641      if ( #[2] != 0 ) { v = v[size(v)..1]; }
642   }
643   s = size(v);
644   if( size(id) < s ) { s = size(id); }
645   def m = id;
646   if ( size(m) != 0 )
647   {
648      for ( jj=1; jj<=s; jj=jj+1)
649      {
650         if ( v[jj]<=0 ) { v[jj]=jj; }
651         m[jj] = id[v[jj]];
652      }
653   }
654   if ( v == 0 ) { v = 1; }
655   list L=m,v;
656   return(L);
657}
658example
659{  "EXAMPLE:"; echo = 2;
660   ring r0 = 0,(x,y,z),lp;
661   ideal i = x3,y3,z3,x2z,x2y,y2z,y2x,z2y,z2x,xyz;
662   show(sort(i));"";
663   show(sort(i,"wp(1,2,3)"));"";
664   intvec v=10..1;
665   show(sort(i,v));"";
666   show(sort(i,v,1));"";   // should be the identity
667   ring r1  = 0,t,ls;
668   ideal j = t14,t6,t28,t20,t12,t34,t26,t18,t40,t32,t24,t38,t30,t36;
669   show(sort(j)[1]);"";
670   show(sort(j,"lp")[1]);"";
671   list L =1,5..8,10,2,8..5,8,3..10;
672   sort(L)[1];"";          // sort L lexicographically
673   sort(L,"Dp",1)[1];      // sort L w.r.t (total sum, reverse lex)
674}
675///////////////////////////////////////////////////////////////////////////////
676
677proc sum (id, list #)
678"USAGE:    sum(id[,v]); id=ideal/vector/module/matrix resp. id=intvec/intmat,
679                       v=intvec (e.g. v=1..n, n=integer)
680RETURN:   poly resp. int which is the sum of all entries of id, with index
681          given by v (default: v=1..number of entries of id)
682NOTE:     id is treated as a list of polys resp. integers. A module m is
683          identified with corresponding matrix M (columns of M generate m)
684EXAMPLE:  example sum; shows an example
685"
686{
687   if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
688       or typeof(id)=="module" or typeof(id)=="matrix" )
689   {
690      ideal i = ideal(matrix(id));
691      if( size(#)!=0 ) { i = i[#[1]]; }
692      matrix Z = matrix(i);
693   }
694   if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
695   {
696      if ( typeof(id) == "int" ) { intmat S =id; }
697      else { intmat S = intmat(id); }
698      intvec i = S[1..nrows(S),1..ncols(S)];
699      if( size(#)!=0 ) { i = i[#[1]]; }
700      intmat Z=transpose(i);
701   }
702   intvec v; v[ncols(Z)]=0; v=v+1;
703   return((Z*v)[1,1]);
704}
705example
706{  "EXAMPLE:"; echo = 2;
707   ring r= 0,(x,y,z),dp;
708   vector pv = [xy,xz,yz,x2,y2,z2];
709   sum(pv);
710   //sum(pv,2..5);
711   //matrix M[2][3] = 1,x,2,y,3,z;
712   //sum(M);
713   //intvec w=2,4,6;
714   //sum(M,w);
715   //intvec iv = 1,2,3,4,5,6,7,8,9;
716   //w=1..5,7,9;
717   //sum(iv,w);
718   //intmat m[2][3] = 1,1,1,2,2,2;
719   //sum(m,3..4);
720}
721///////////////////////////////////////////////////////////////////////////////
722
723proc which (command)
724"USAGE:    which(command); command = string expression
725RETURN:   Absolute pathname of command, if found in search path.
726          Empty string, otherwise.
727NOTE:     Based on the Unix command 'which'.
728EXAMPLE:  example which; shows an example
729"
730{
731   int rs;
732   int i;
733   string fn = "/tmp/which_" + string(system("pid"));
734   string pn;
735   if( typeof(command) != "string")
736   {
737     return (pn);
738   }
739   i = system("sh", "which " + command + " > " + fn);
740   pn = read(fn);
741   pn[size(pn)] = "";
742   i = 1;
743   while ((pn[i] != " ") and (pn[i] != ""))
744   {
745     i = i+1;
746   }
747   if (pn[i] == " ") {pn[i] = "";}
748   rs = system("sh", "ls " + pn + " > " + fn + " 2>&1 ");
749   i = system("sh", "rm " + fn);
750   if (rs == 0) {return (pn);}
751   else
752   {
753     print (command + " not found ");
754     return ("");
755   }
756}
757example
758{  "EXAMPLE:"; echo = 2;
759    which("Singular");
760}
761///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.