Changeset b42ab6 in git


Ignore:
Timestamp:
Dec 29, 2000, 2:52:42 AM (23 years ago)
Author:
Gert-Martin Greuel <greuel@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
584f84d6e8c6f3340af1d80fff4224b3f977de76
Parents:
c52356dc5f8ba01144cc02b2004e285f29b0cc41
Message:
* GMG: Doku korrigiert, bzw. typesetting verbessert in:
*       ASCII, binomial, factorial, fibonacci, kmemory, killall,number_e,
*       number_pi, product, ringweights, sort, sum
*      Beispiel verkleinert/geaendert in: fibonacci, ringweights, sort
*      Bug behoben in: killall
*      Funktionalitaet erweitert in :sum (auf Listen)


git-svn-id: file:///usr/local/Singular/svn/trunk@4992 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/LIB/general.lib

    rc52356d rb42ab6  
    22//anne, added deleteSublist and watchdog 12.12.2000
    33///////////////////////////////////////////////////////////////////////////////
    4 version="$Id: general.lib,v 1.30 2000-12-22 13:52:31 greuel Exp $";
     4version="$Id: general.lib,v 1.31 2000-12-29 01:52:42 greuel Exp $";
    55category="General purpose";
    66info="
     
    8888proc ASCII (list #)
    8989"USAGE:   ASCII([n,m]); n,m= integers (32 <= n <= m <= 126)
    90 RETURN:   printable ASCII characters (no native language support)
     90RETURN:   string of printable ASCII characters (no native language support)
    9191          ASCII():    string of  all ASCII characters with its numbers,
    92                       no return value
    93           ASCII(n):   string, n-th ASCII character
    94           ASCII(n,m): list, n-th up to m-th ASCII character (inclusive)
     92          ASCII(n):   n-th ASCII character
     93          ASCII(n,m): n-th up to m-th ASCII character (inclusive)
    9594EXAMPLE: example ASCII; shows an example
    9695"
     
    138137proc binomial (int n, int k, list #)
    139138"USAGE:   binomial(n,k[,p]); n,k,p integers
    140 RETURN:  binomial(n,k); binomial coefficient n choose k,
    141 @*         - of type string (computed in characteristic 0)
    142          binomial(n,k,p); n choose k, computed in characteristic prime(p)
    143 @*         - of type number if a basering is present and prime(p)=char(basering)
    144 @*         - of type string else
     139RETURN:  binomial(n,k); binomial coefficient n choose k
     140@*       - of type string (computed in characteristic 0)
     141@*       binomial(n,k,p); n choose k, computed in characteristic 0 or prime(p)
     142@*       - of type number if a basering, say R, is present and p=0=char(R)
     143           or if prime(p)=char(R)
     144@*       - of type string else
    145145NOTE:    In any characteristic, binomial(n,k) = coefficient of x^k in (1+x)^n
     146SEE ALSO: prime
    146147EXAMPLE: example binomial; shows an example
    147148"
     
    261262
    262263proc factorial (int n, list #)
    263 "USAGE:   factorial(n[,p]);  n,p integers
    264 RETURN:  factorial(n):   n! (computed in characteristic 0), of type string
    265          factorial(n,p): n! computed in characteristic prime(p)
    266          - of type number if a basering is present and prime(p)=char(basering)
    267          - of type string else
    268 EXAMPLE: example factorial; shows an example
     264"USAGE:    factorial(n[,p]);  n,p integers
     265RETURN:   factorial(n):   n! (computed in characteristic 0), of type string.
     266@*        factorial(n,p): n! computed in characteristic 0 or prime(p)
     267@*        - of type number if a basering is present and 0=p=char(basering)
     268            or if prime(p)=char(basering)
     269@*        - of type string else
     270SEE ALSO: prime
     271EXAMPLE:  example factorial; shows an example
    269272"
    270273{   int str,l,p;
     
    313316
    314317proc fibonacci (int n, list #)
    315 "USAGE:   fibonacci(n);  n,p integers
    316 RETURN:  fibonacci(n): nth Fibonacci number, f(0)=f(1)=1, f(i+1)=f(i-1)+f(i)
    317          - computed in characteristic 0, of type string
    318          of type number computed in char(basering) if n is of type number
    319          fibonacci(n,p): f(n) computed in characteristic prime(p)
    320          - of type number if a basering is present and prime(p)=char(basering)
    321          - of type string else
     318"USAGE:    fibonacci(n);  n,p integers
     319RETURN:   fibonacci(n): nth Fibonacci number, f(0)=f(1)=1, f(i+1)=f(i-1)+f(i)
     320@*        - computed in characteristic 0, of type string
     321@*        fibonacci(n,p): f(n) computed in characteristic 0 or prime(p)
     322@*        - of type number if a basering is present and p=0=char(basering)
     323            or if prime(p)=char(basering)
     324@*        - of type string else
     325SEE ALSO: prime
    322326EXAMPLE: example fibonacci; shows an example
    323327"
     
    359363example
    360364{ "EXAMPLE:"; echo = 2;
    361    fibonacci(333); "";              //f(333) of type string (as long integer)
    362    ring r = 17,x,dp;
    363    number b = fibonacci(333,17);    //f(333) of type number, computed in r
     365   fibonacci(42); "";             //f(42) of type string (as long integer)
     366   ring r = 2,x,dp;
     367   number b = fibonacci(42,2);    //f(42) of type number, computed in r
    364368   b;
    365369}
     
    367371
    368372proc kmemory (list #)
    369 "USAGE:   kmemory([n,[v]]); n = int
     373"USAGE:   kmemory([n,[v]]); n,v integers
    370374RETURN:  memory in kilobyte of type int
    371          n=0: memory used by active variables (same as no parameters)
    372          n=1: total memory allocated by Singular
    373          n=2: difference between top and init memory adress (sbrk memory)
    374          n!=0,1,2: 0
     375@*       n=0: memory used by active variables (same as no parameters)
     376@*       n=1: total memory allocated by Singular
     377@*       n=2: difference between top and init memory adress (sbrk memory)
     378@*       n!=0,1,2: 0
    375379DISPLAY: detailed information about allocated and used memory if v!=0
    376380NOTE:    kmemory uses internal function 'memory' to compute kilobyte, and
     
    410414         killall(\"type_name\");
    411415         killall(\"not\", \"type_name\");
    412 COMPUTE: killall(); kills all user-defined variables but not loaded procedures
    413          killall(\"type_name\"); kills all user-defined variables,
    414          of type \"type_name\"
    415          killall(\"not\", \"type_name\"); kills all user-defined variables,
    416          except those of type \"type_name\" and except loaded procedures
    417          killall(\"not\", \"name_1\", \"name_2\", ...);
    418          kills all user-defined variables, except those of name \"name_i\"
    419          and except loaded procedures
    420 RETURN:  no return value
     416RETURN:  killall(); kills all user-defined variables except loaded procedures,
     417         no return value.
     418@*       - killall(\"type_name\"); kills all user-defined variables,
     419           of type \"type_name\"
     420@*       - killall(\"not\", \"type_name\"); kills all user-defined variables,
     421           except those of type \"type_name\" and except loaded procedures
     422@*       - killall(\"not\", \"name_1\", \"name_2\", ...);
     423           kills all user-defined variables, except those of name \"name_i\"
     424           and except loaded procedures
    421425NOTE:    killall should never be used inside a procedure
    422426EXAMPLE: example killall; shows an example AND KILLS ALL YOUR VARIABLES
    423427"
    424428{
    425   list L=names(); int joni=size(L);
    426   int no_kill, j;
    427   for (j=1; j<=size(#); j++)
     429  list @marie=names();
     430  int no_kill, @joni;
     431  for ( @joni=1; @joni<=size(#);  @joni++)
    428432  {
    429     if (typeof(#[j]) != "string")
     433    if (typeof(#[@joni]) != "string")
    430434    {
    431       ERROR("Need string as " + string(j) + "th argument");
     435      ERROR("Need string as " + string(@joni) + "th argument");
    432436    }
    433437  }
     
    436440  if( size(#)==0 )
    437441  {
    438     for ( ; joni>0; joni-- )
     442    for ( @joni=size(@marie); @joni>0; @joni-- )
    439443    {
    440       if( L[joni]!="LIB" and typeof(`L[joni]`)!="proc" ) { kill `L[joni]`; }
     444      if( @marie[@joni]!="LIB" and typeof(`@marie[@joni]`)!="proc" )
     445      { kill `@marie[@joni]`; }
    441446    }
    442447  }
     
    449454      if( #[1] == "proc" )
    450455      {
    451         for ( joni=size(L); joni>0; joni-- )
     456        for ( @joni=size(@marie); @joni>0; @joni-- )
    452457        {
    453           if((L[joni]!="killall") and (L[joni]=="LIB" or typeof(`L[joni]`)=="proc"))
    454           { kill `L[joni]`; }
     458          if((@marie[@joni]!="killall") and (@marie[@joni]=="LIB" or
     459                                       typeof(`@marie[@joni]`)=="proc"))
     460          { kill `@marie[@joni]`; }
    455461        }
    456462      }
     
    458464      { 
    459465        // other types
    460         for ( ; joni>2; joni-- )
     466        for ( @joni=size(@marie); @joni>2; @joni-- )
    461467        {
    462           if(typeof(`L[joni]`)==#[1] and L[joni]!="LIB" and typeof(`L[joni]`)!="proc") { kill `L[joni]`; }
     468          if(typeof(`@marie[@joni]`)==#[1] and @marie[@joni]!="LIB"
     469             and typeof(`@marie[@joni]`)!="proc")
     470          { kill `@marie[@joni]`; }
    463471        }
    464472      }
     
    467475    {
    468476      // kills all user-defined variables whose name or type is not #i
    469       for ( ; joni>2; joni-- )
    470       {
    471         if ( L[joni] != "LIB" && typeof(`L[joni]`) != "proc")
     477      for ( @joni=size(@marie); @joni>2; @joni-- )
     478      {
     479        if ( @marie[@joni] != "LIB" && typeof(`@marie[@joni]`) != "proc")
    472480        {
    473481          no_kill = 0;
    474482          for (j=2; j<= size(#); j++)
    475483          {
    476             if (typeof(`L[joni]`)==#[j] or L[joni] == #[j])
     484            if (typeof(`@marie[@joni]`)==#[j] or @marie[@joni] == #[j])
    477485            {
    478486              no_kill = 1;
     
    482490          if (! no_kill)
    483491          {
    484             kill `L[joni]`;
     492            kill `@marie[@joni]`;
    485493          }
    486494        }
     
    505513proc number_e (int n)
    506514"USAGE:   number_e(n);  n integer
    507 COMPUTE: Euler number e=exp(1) up to n decimal digits (no rounding)
    508          by A.H.J. Sale's algorithm
    509 RETURN:  - string of exp(1) if no basering of char 0 is defined;
    510          - exp(1), type number, if a basering of char 0 is defined, display its
    511          decimal format if printlevel >= voice (default:printlevel=voice-1 )
     515RETURN: Euler number e=exp(1) up to n decimal digits (no rounding)
     516@*       - of type string if no basering of char 0 is defined
     517@*       - of type number if a basering of char 0 is defined
     518DISPLAY: decimal format of e if printlevel > 0 (default:printlevel=0 )
     519NOTE:    procedure uses algorithm of A.H.J. Sale
    512520EXAMPLE: example number_e; shows an example
    513521"
     
    550558proc number_pi (int n)
    551559"USAGE:   number_pi(n);  n positive integer
    552 COMPUTE: pi (area of unit circle) up to n decimal digits (no rounding)
    553          by algorithm of S. Rabinowitz
    554 RETURN:  - string of pi if no basering of char 0 is defined,
    555          - pi, of type number, if a basering of char 0 is defined, display its
    556          decimal format if printlevel >= voice (default:printlevel=voice-1 )
     560RETURN: pi (area of unit circle) up to n decimal digits (no rounding)
     561@*       - of type string if no basering of char 0 is defined,
     562@*       - of type number, if a basering of char 0 is defined
     563DISPLAY: decimal format of pi if printlevel > 0 (default:printlevel=0 )
     564NOTE:    procedure uses algorithm of S. Rabinowitz
    557565EXAMPLE: example number_pi; shows an example
    558566"
     
    624632"USAGE:   primes(n,m);  n,m integers
    625633RETURN:  intvec, consisting of all primes p, prime(n)<=p<=m, in increasing
    626          order if n<=m, resp. prime(m)<=p<=n, in decreasing order if m<n
    627 NOTE:    prime(n); returns the biggest prime number <= n (if n>=2, else 2)
     634         order if n<=m, resp. prime(m)<=p<=n, in decreasing order if m<n.
     635NOTE:    prime(n); returns the biggest prime number <= min(n,32003)
     636         if n>=2, else 2
    628637EXAMPLE: example primes; shows an example
    629638"
     
    644653proc product (id, list #)
    645654"USAGE:    product(id[,v]); id ideal/vector/module/matrix/intvec/intmat/list,
    646           v intvec  (default: v=1.. number of entries of id)
    647 RETURN:   - if id is not a list: poly resp. int, the product of all entries of
    648           id with index given by v.
    649           id is treated as a list of polys resp. integers. A module m is
    650           identified with corresponding matrix M (columns of M generate m)
    651           - if id is a list: product of list entries, with index given by v.
    652           Assume that list members can be multiplied
     655          v intvec  (default: v=1..number of entries of id)
     656ASSUME:   list members can be multiplied.
     657RETURN:   The product of all entries of id [with index given by v] of type
     658          depending on the entries of id.
     659NOTE:     If id is not a list, id is treated as a list of polys resp. integers.
     660          A module m is identified with the corresponding matrix M (columns
     661          of M generate m).
    653662EXAMPLE:  example product; shows an example
    654663"
     
    721730///////////////////////////////////////////////////////////////////////////////
    722731proc ringweights (list # )
    723 "USAGE:   ringweights (P); P=name of an existing ring (true name, not a string)
    724 RETURN:  intvec, size=nvars(P), consisting of the weights of the variables of P
     732"USAGE:   ringweights(P); P=name of an existing ring (true name, not a string)
     733RETURN:  intvec consisting of the weights of the variables of P, as they
     734         appear when typing P;.
    725735NOTE:    This is useful when enlarging P but keeping the weights of the old
    726          variables
    727 EXAMPLE: example ringweights;  shows an example
     736         variables.
     737EXAMPLE: example ringweights; shows an example
    728738"
    729739{
     
    790800  ringweights(r0);
    791801  ring r1 = 0,x(1..5),(ds(3),wp(2,3));
    792   ringweights(r1);
    793   ring r2 = 0,x(1..5),(a(1,2,3,0),dp);
    794   ringweights(r2);
    795   ring r3 = 0,x(1..10),(a(1..5),dp(5),a(10..13),Wp(5..9));
    796   ringweights(r3);
    797   // an example for enlarging the ring:
    798   intvec v = 6,2,3,4,5;
    799   ring R = 0,x(1..10),(a(ringweights(r1),v),dp);
     802  ringweights(r1);"";
     803  // an example for enlarging the ring, keeping the first weights:
     804  intvec v = ringweights(r1),6,2,3,4,5;
     805  ring R = 0,x(1..10),(a(v),dp);
    800806  ordstr(R);
    801807}
     
    804810
    805811proc sort (id, list #)
    806 "USAGE:   sort(id[v,o,n]); id=ideal/module/intvec/list (of intvec's or int's)
    807          sort may be called with 1, 2 or 3 arguments in the following way:
    808          sort(id[v,n]); v=intvec of positive integers, n=integer,
    809          sort(id[o,n]); o=string (any allowed ordstr of a ring), n=integer
    810 RETURN:  a list of two elements:
    811          [1]: object of same type as input but sorted in the following manner:
     812"USAGE:   sort(id[v,o,n]); id=ideal/module/intvec/list(of intvec's or int's)
     813@*       sort may be called with 1, 2 or 3 arguments in the following way:
     814@*       sort(id[v,n]); v=intvec of positive integers, n=integer,
     815@*       sort(id[o,n]); o=string (any allowed ordstr of a ring), n=integer
     816RETURN:  a list l of two elements:
     817@format
     818        l[1]: object of same type as input but sorted in the following way:
    812819           - if id=ideal/module: generators of id are sorted w.r.t. intvec v
    813820             (id[v[1]] becomes 1-st, id[v[2]]  2-nd element, etc.). If no v is
    814821             present, id is sorted w.r.t. ordering o (if o is given) or w.r.t.
    815822             actual monomial ordering (if no o is given):
    816                     generators with smaller leading term come first
    817              (e.g. sort(id); sorts w.r.t actual monomial ordering)
     823             NOTE: generators with SMALLER(!) leading term come FIRST
     824             (e.g. sort(id); sorts backwards to actual monomial ordering)
    818825           - if id=list of intvec's or int's: consider a list element, say
    819826             id[1]=3,2,5, as exponent vector of the monomial x^3*y^2*z^5;
     
    830837           - if n!=0 the ordering is inverse, i.e. w.r.t. v(size(v)..1)
    831838             default: n=0
    832          [2]: intvec, describing the permutation of the input (hence [2]=v if
    833              v is given (with positive integers)
     839         l[2]: intvec, describing the permutation of the input (hence l[2]=v
     840             if v is given (with positive integers))
     841@end format
    834842NOTE:    If v is given id may be any simply indexed object (e.g. any list or
    835843         string); if v[i]<0 and i<=size(id) v[i] is set internally to i;
     
    921929   ring r0 = 0,(x,y,z,t),lp;
    922930   ideal i = x3,z3,xyz;
    923    sort(i);                // sort w.r.t. lex ordering
     931   sort(i);               //sorts using lex ordering,smaller polys come first
     932 
    924933   sort(i,3..1);
    925    sort(i,"ls")[1];        // sort w.r.t. negative lex ordering
    926    list L =1,8..5,3..10;
    927    sort(L)[1];             // sort L lexicographically
    928    sort(L,"Dp",1)[1];      // sort L w.r.t (total sum, reverse lex)
    929 }
    930 ///////////////////////////////////////////////////////////////////////////////
    931 
     934
     935   sort(i,"ls")[1];        //sort w.r.t. negative lex ordering
     936
     937   intvec v =1,10..5,2..4;v;
     938   sort(v)[1];             // sort v lexicographically
     939
     940   sort(v,"Dp",1)[1];      // sort v w.r.t (total sum, reverse lex)
     941}
     942///////////////////////////////////////////////////////////////////////////////
    932943proc sum (id, list #)
    933 "USAGE:    sum(id[,v]); id=ideal/vector/module/matrix resp. id=intvec/intmat,
    934                        v=intvec (e.g. v=1..n, n=integer)
    935 RETURN:   poly resp. int which is the sum of all entries of id, with index
    936           given by v (default: v=1..number of entries of id)
    937 NOTE:     id is treated as a list of polys resp. integers. A module m is
    938           identified with corresponding matrix M (columns of M generate m)
     944"USAGE:    sum(id[,v]); id ideal/vector/module/matrix/intvec/intmat/list,
     945          v intvec  (default: v=1..number of entries of id)
     946ASSUME:   list members can be added.
     947RETURN:   The sum of all entries of id [with index given by v] of type
     948          depending on the entries of id.
     949NOTE:      If id is not a list, id is treated as a list of polys resp. integers.
     950          A module m is identified with the corresponding matrix M (columns
     951          of M generate m).
    939952EXAMPLE:  example sum; shows an example
    940953"
    941954{
    942    if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector"
    943        or typeof(id)=="module" or typeof(id)=="matrix" )
     955   int n,j,tt;
     956   string ty;
     957   list l;
     958   int s = size(#);
     959   if( s!=0 )
     960   {  if ( typeof(#[s])=="intvec" )
     961      {  intvec v = #[s];
     962         tt=1; s=s-1;
     963         if ( s>0 ) { # = #[1..s]; }
     964      }
     965   }
     966   if ( s>0 )
     967   {
     968     l = list(id)+#;
     969     kill id;
     970     list id = l;
     971     ty = "list";
     972   }
     973   else
     974   { ty = typeof(id);
     975   }
     976   if( ty=="list" )
     977   { n = size(id);
     978     def f(1) = id[1];
     979     for( j=2; j<=n; j=j+1 ) { def f(j)=f(j-1)+id[j]; }
     980     return(f(n));
     981   }
     982   if( ty=="poly" or ty=="ideal" or ty=="vector"
     983       or ty=="module" or ty=="matrix" )
    944984   {
    945985      ideal i = ideal(matrix(id));
    946       if( size(#)!=0 ) { i = i[#[1]]; }
    947       matrix Z = matrix(i);
    948    }
    949    if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" )
    950    {
    951       if ( typeof(id) == "int" ) { intmat S =id; }
     986      kill id;
     987      ideal id = i;
     988      if( tt!=0 ) { id = id[v]; }
     989      n = ncols(id); poly f(1)=id[1];
     990   }
     991   if( ty=="int" or ty=="intvec" or ty=="intmat" )
     992   {
     993      if ( ty == "int" ) { intmat S =id; }
    952994      else { intmat S = intmat(id); }
    953995      intvec i = S[1..nrows(S),1..ncols(S)];
    954       if( size(#)!=0 ) { i = i[#[1]]; }
    955       intmat Z=transpose(i);
    956    }
    957    intvec v; v[ncols(Z)]=0; v=v+1;
    958    return((Z*v)[1,1]);
    959  }
     996      kill id;
     997      intvec id = i;
     998      if( tt!=0 ) { id = id[v]; }
     999      n = size(id); int f(1)=id[1];
     1000   }
     1001   for( j=2; j<=n; j=j+1 ) { def f(j)=f(j-1)+id[j]; }
     1002   return(f(n));   int n,j,tt;
     1003}
    9601004example
    9611005{  "EXAMPLE:"; echo = 2;
     
    10311075
    10321076proc watchdog(int i, string cmd)
    1033 "USAGE  : watchdog(i,cmd); i integer; cmd string
    1034 RETURNS: Result of cmd, if the result can be computed in
    1035          i seconds. Otherwise the computation is interrupted after
    1036          i seconds, the string "Killed" is returned and the global
    1037          variable 'watchdog_interrupt' is defined.
     1077"USAGE watchdog(i,cmd); i integer; cmd string
     1078RETURN:  Result of cmd, if the result can be computed in i seconds.
     1079         Otherwise the computation is interrupted after i seconds,
     1080         the string "Killed" is returned and the global variable
     1081         'watchdog_interrupt' is defined.
    10381082NOTE:    * the MP package must be enabled
    1039          * the current basering should not be watchdog_rneu
     1083         * the current basering should not be watchdog_rneu, since
     1084           watchdog_rneu will be killed
    10401085         * if there are variable names of the structure x(i) all
    10411086           polynomials have to be put into eval(...) in order to be
Note: See TracChangeset for help on using the changeset viewer.