Changeset c860e9 in git for Singular/LIB/general.lib
 Timestamp:
 Aug 23, 1999, 3:15:59 PM (24 years ago)
 Branches:
 (u'jengelhdatetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'a800fe4b3e9d37a38c5a10cc0ae9dfa0c15a4ee6')
 Children:
 36861edf6c7025f84372f1a254aefc05bb636749
 Parents:
 99a286c05af3b60f04f2443ada520b679a44e7a3
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Singular/LIB/general.lib
r99a286 rc860e9 1 // $Id: general.lib,v 1.1 6 19990709 14:06:52 obachmanExp $1 // $Id: general.lib,v 1.17 19990823 13:15:59 Singular Exp $ 2 2 //system("random",787422842); 3 3 //GMG, last modified 18.6.99 4 4 /////////////////////////////////////////////////////////////////////////////// 5 5 6 version="$Id: general.lib,v 1.1 6 19990709 14:06:52 obachmanExp $";6 version="$Id: general.lib,v 1.17 19990823 13:15:59 Singular Exp $"; 7 7 info=" 8 8 LIBRARY: general.lib PROCEDURES OF GENERAL TYPE … … 90 90 ASCII(): string of all ASCII characters with its numbers, 91 91 no return value 92 ASCII(n): string, nth ASCII chara kter92 ASCII(n): string, nth ASCII character 93 93 ASCII(n,m): list, nth up to mth ASCII character (inclusive) 94 94 EXAMPLE: example ASCII; shows an example … … 96 96 { 97 97 string s1 = 98 " ! \" # $ %& ' ( ) * + ,  .98 " ! \" # $ & ' ( ) * + ,  . 99 99 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 100 101 100 / 0 1 2 3 4 5 6 7 8 9 : ; < = 102 101 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 103 104 102 > ? @ A B C D E F G H I J K L 105 103 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 106 107 104 M N O P Q R S T U V W X Y Z [ 108 105 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 109 110 106 \\ ] ^ _ ` a b c d e f g h i j 111 107 92 93 94 95 96 97 98 99 100 101 102 103 104 105 10 112 113 108 k l m n o p q r s t u v w x y 114 109 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 115 116 110 z {  } ~ 117 111 122 123 124 125 126 "; … … 141 135 /////////////////////////////////////////////////////////////////////////////// 142 136 143 proc binomial ( defn, int k, list #)137 proc binomial (int n, int k, list #) 144 138 "USAGE: binomial(n,k[,p]); n,k,p integers 145 139 RETURN: binomial(n,k); binomial coefficient n choose k, 146 of type number if n is of type number (computed in char(basering)), 147 of type int if n is of type int (machine integer, limited size!) 148 binomial(n,k,p); n choose k in char p, of type string 140  of type string (computed in characteristic 0) 141 binomial(n,k,p); n choose k, computed in characteristic prime(p) 142  of type number if a basering is present and prime(p)=char(basering) 143  of type string else 144 NOTE: In any characteristic, binomial(n,k) = coefficient of x^k in (1+x)^n 149 145 EXAMPLE: example binomial; shows an example 150 146 " 151 147 { 152 if ( size(#)==0 ) 153 { 154 if ( typeof(n)=="number" ) { number rr=1; } 155 else { int rr=1; } 156 } 157 if ( typeof(#[1])=="int") { ring bin = #[1],x,dp; number rr=1; } 158 if ( size(#)==0 or typeof(#[1])=="int" ) 159 { 160 def r = rr; 161 if ( k<=0 or k>n ) { return((k==0)*r); } 162 if ( k>nk ) { k = nk; } 163 int l; 164 for (l=1; l<=k; l=l+1 ) 165 { 166 r=r*(n+1l)/l; 167 } 168 if ( typeof(#[1])=="int" ) { return(string(r)); } 169 return(r); 170 } 171 } 148 int str,p; 149 // initialization  150 if ( size(#) == 0 ) 151 { str = 1; 152 ring bin = 0,x,dp; 153 number r=1; 154 } 155 if ( size(#) > 0 ) 156 { 157 p = (#[1]!=0)*prime(#[1]); 158 if ( defined(basering) ) 159 { 160 if ( p == char(basering) ) 161 { number r=1; 162 } 163 else 164 { str = 1; 165 ring bin = p,x,dp; 166 number r=1; 167 } 168 } 169 else 170 { str = 1; 171 ring bin = p,x,dp; 172 number r=1; 173 } 174 } 175 // char 0  176 if ( p==0 ) 177 { 178 r = binom0(n,k); 179 } 180 // char p  181 else 182 { 183 r = binomp(n,k,p); 184 } 185 // return  186 if ( str==1 ) { return(string(r)); } 187 else { return(r); } 188 } 172 189 example 173 190 { "EXAMPLE:"; echo = 2; 174 int b1 = binomial(10,7); b1; 175 binomial(137,17,0); 176 ring t = 0,x,dp; 177 number b2 = binomial(number(137),17); b2; 178 } 179 /////////////////////////////////////////////////////////////////////////////// 180 181 proc factorial (def n) 182 "USAGE: factorial(n); n = integer 183 RETURN: factorial(n); n! in char 0, of type string if n is of type int 184 n! of type number, computed in char(basering) if n is of type number 191 binomial(200,100);""; //type string, computed in char 0 192 binomial(200,100,3);""; //type string, computed in char 3 193 int n,k = 200,100; 194 ring r = 0,x,dp; 195 number b1 = binomial(n,k,0); //type number, computed in ring r 196 poly b2 = coeffs((x+1)^n,x)[k+1,1]; //coefficient of x^k in (x+1)^n 197 b1b2; //b1 and b2 should coincide 198 } 199 /////////////////////////////////////////////////////////////////////////////// 200 201 static proc binom0 (int n, int k) 202 //computes binomial coefficient n choose k in basering, assume 0<k<=n 203 //and char(basering) = 0 or n < char(basering) 204 { 205 int l; 206 number r=1; 207 if ( k > nk ) 208 { k = nk; 209 } 210 if ( k<=0 or k>n ) //trivial cases 211 { r = (k==0)*r; 212 } 213 for (l=1; l<=k; l++ ) 214 { 215 r=r*(n+1l)/l; 216 } 217 return(r); 218 } 219 /////////////////////////////////////////////////////////////////////////////// 220 221 static proc binomp (int n, int k, int p) 222 //computes binomial coefficient n choose k in basering of char p > 0 223 //binomial(n,k) = coefficient of x^k in (1+x)^n. 224 //Let n=q*p^j, gcd(q,p)=1, then (1+x)^n = (1 + x^(p^j))^q. We have 225 //binomial(n,k)=0 if k!=l*p^j and binomial(n,l*p^j) = binomial(q,l). 226 //Do this reduction first. Then, in denominator and numerator 227 //of defining formula for binomial coefficient, reduce those factors 228 //mod p which are not divisible by p and cancel common factors p. Hence, 229 //if n = h*p+r, k=l*p+s, r,s<p, binomial(n,k) = binomial(r,s)*binomial(h,l) 230 { 231 int l,q,i= 1,n,1; 232 number r=1; 233 if ( k > nk ) 234 { k = nk; 235 } 236 if ( k<=0 or k>n) //trivial cases 237 { r = (k==0)*r; 238 } 239 else 240 { 241 while ( q mod p == 0 ) 242 { l = l*p; 243 q = q div p; 244 } //we have now n=q*l, l=p^j, gcd(q,p)=1; 245 if (k mod l != 0 ) 246 { r = 0; 247 } 248 else 249 { l = k div l; 250 n = q mod p; 251 k = l mod p; //now 0<= k,n <p, use binom0 for n,k 252 q = q div p; //recursion for q,l 253 l = l div p; //use binomp for q,l 254 r = binom0(n,k)*binomp(q,l,p); 255 } 256 } 257 return(r); 258 } 259 /////////////////////////////////////////////////////////////////////////////// 260 261 proc factorial (int n, list #) 262 "USAGE: factorial(n[,p]); n,p integers 263 RETURN: factorial(n): n! (computed in characteristic 0), of type string 264 factorial(n,p): n! computed in characteristic prime(p) 265  of type number if a basering is present and prime(p)=char(basering) 266  of type string else 185 267 EXAMPLE: example factorial; shows an example 186 268 " 187 { 188 int t,l; 189 if ( typeof(n)=="number" ) { number r=1; } 190 else { ring R = 0,x,dp; number r=1; t=1; } 191 for (l=2; l<=n; l=l+1) 269 { int str,l,p; 270 // initialization  271 if ( size(#) == 0 ) 272 { str = 1; 273 ring bin = 0,x,dp; 274 number r=1; 275 } 276 if ( size(#) > 0 ) 277 { 278 p = (#[1]!=0)*prime(#[1]); 279 if ( defined(basering) ) 280 { 281 if ( p == char(basering) ) 282 { number r=1; 283 } 284 else 285 { str = 1; 286 ring bin = p,x,dp; 287 number r=1; 288 } 289 } 290 else 291 { str = 1; 292 ring bin = p,x,dp; 293 number r=1; 294 } 295 } 296 // computation  297 for (l=2; l<=n; l++) 192 298 { 193 299 r=r*l; 194 300 } 195 if ( t==1 ) { return(string(r)); }196 return(r);301 if ( str==1 ) { return(string(r)); } 302 else { return(r); } 197 303 } 198 304 example 199 305 { "EXAMPLE:"; echo = 2; 200 factorial(37); 201 ring r1 = 32003,(x,y,z),ds;202 number p = factorial( number(37)); p;203 } 204 /////////////////////////////////////////////////////////////////////////////// 205 206 proc fibonacci (def n) 207 "USAGE: fibonacci(n); (n integer)208 RETURN: fibonacci(n); nth Fibonacci number, 209 210 of type string if n is of type int306 factorial(37);""; //37! of type string (as long integer) 307 ring r1 = 0,x,dp; 308 number p = factorial(37,0); //37! of type number, computed in r 309 p; 310 } 311 /////////////////////////////////////////////////////////////////////////////// 312 313 proc fibonacci (int n, list #) 314 "USAGE: fibonacci(n); n,p integers 315 RETURN: fibonacci(n): nth Fibonacci number, f(0)=f(1)=1, f(i+1)=f(i1)+f(i) 316  computed in characteristic 0, of type string 211 317 of type number computed in char(basering) if n is of type number 318 fibonacci(n,p): f(n) computed in characteristic prime(p) 319  of type number if a basering is present and prime(p)=char(basering) 320  of type string else 212 321 EXAMPLE: example fibonacci; shows an example 213 322 " 214 { 215 int ii,t; 216 if ( typeof(n)=="number" ) { number f,g,h=1,1,1; } 217 else { ring fibo = 0,x,dp; number f,g,h=1,1,1; t=1; } 323 { int str,ii,p; 324 // initialization  325 if ( size(#) == 0 ) 326 { str = 1; 327 ring bin = 0,x,dp; 328 number f,g,h=1,1,1; 329 } 330 if ( size(#) > 0 ) 331 { 332 p = (#[1]!=0)*prime(#[1]); 333 if ( defined(basering) ) 334 { 335 if ( p == char(basering) ) 336 { number f,g,h=1,1,1; 337 } 338 else 339 { str = 1; 340 ring bin = p,x,dp; 341 number f,g,h=1,1,1; 342 } 343 } 344 else 345 { str = 1; 346 ring bin = p,x,dp; 347 number f,g,h=1,1,1; 348 } 349 } 350 // computation  218 351 for (ii=3; ii<=n; ii=ii+1) 219 352 { 220 353 h = f+g; f = g; g = h; 221 354 } 222 if ( t==1 ) { return(string(h)); }223 return(h);355 if ( str==1 ) { return(string(h)); } 356 else { return(h); } 224 357 } 225 358 example 226 359 { "EXAMPLE:"; echo = 2; 227 fibonacci(3 7);360 fibonacci(333); ""; //f(333) of type string (as long integer) 228 361 ring r = 17,x,dp; 229 number b = fibonacci(number(37)); b; 362 number b = fibonacci(333,17); //f(333) of type number, computed in r 363 b; 230 364 } 231 365 /////////////////////////////////////////////////////////////////////////////// … … 276 410 killall(\"not\", \"type_name\"); 277 411 COMPUTE: killall(); kills all userdefined variables but not loaded procedures 278 killall(\"type_name\"); kills all userdefined variables, of type \"type_name\" 279 killall(\"not\", \"type_name\"); kills all userdefined 280 variables, except those of type \"type_name\" and except loaded procedures 412 killall(\"type_name\"); kills all userdefined variables, 413 of type \"type_name\" 414 killall(\"not\", \"type_name\"); kills all userdefined variables, 415 except those of type \"type_name\" and except loaded procedures 281 416 RETURN: no return value 282 417 NOTE: killall should never be used inside a procedure … … 324 459 example 325 460 { "EXAMPLE:"; echo = 2; 326 ring rtest; ideal i=x,y,z; number n=37;string str="hi"; int j = 3;327 export rtest,i, n,str,j;//this makes the local variables global328 listvar( all);329 killall(" string"); // kills all string variables330 listvar( all);331 killall("not", "int"); // kills all variables except int's (and procs)332 listvar( all);333 killall(); // kills all vars except loaded procs334 listvar( all);461 ring rtest; ideal i=x,y,z; string str="hi"; int j = 3; 462 export rtest,i,str,j; //this makes the local variables global 463 listvar(); 464 killall("ring"); // kills all rings 465 listvar(); 466 killall("not", "int"); // kills all variables except int's (and procs) 467 listvar(); 468 killall(); // kills all vars except loaded procs 469 listvar(); 335 470 } 336 471 /////////////////////////////////////////////////////////////////////////////// … … 341 476 by A.H.J. Sale's algorithm 342 477 RETURN:  string of exp(1) if no basering of char 0 is defined; 343  exp(1), of type number, if a basering of char 0 is defined and344 d isplay its decimal format478  exp(1), type number, if a basering of char 0 is defined, display its 479 decimal format if printlevel >= voice (default:printlevel=voice1 ) 345 480 EXAMPLE: example number_e; shows an example 346 481 " … … 366 501 if( t==1 ) { r = r+number(u[1])/number(10)^i; } 367 502 } 368 if( t==1 ) { "//",result[1,n+1]; return(r); } 503 if( t==1 ) 504 { dbprint(printlevelvoice+2,"// "+result[1,n+1]); 505 return(r); 506 } 369 507 return(result[1,n+1]); 370 508 } 371 509 example 372 510 { "EXAMPLE:"; echo = 2; 373 number_e( 15);511 number_e(30);""; 374 512 ring R = 0,t,lp; 375 number e = number_e( 10);513 number e = number_e(30); 376 514 e; 377 515 } … … 383 521 by algorithm of S. Rabinowitz 384 522 RETURN:  string of pi if no basering of char 0 is defined, 385  pi, of type number, if a basering of char 0 is defined and display386 its decimal format523  pi, of type number, if a basering of char 0 is defined, display its 524 decimal format if printlevel >= voice (default:printlevel=voice1 ) 387 525 EXAMPLE: example number_pi; shows an example 388 526 " … … 437 575 result = result,prelim[1]; 438 576 result = "3."+result[2,n1]; 439 if( t==1 ) { "//",result; return(pi); } 577 if( t==1 ) 578 { dbprint(printlevelvoice+2,"// "+result); 579 return(pi); 580 } 440 581 return(result); 441 582 } 442 583 example 443 584 { "EXAMPLE:"; echo = 2; 444 number_pi(5); 445 ring r = 0,t,lp; 446 number pi = number_pi(6); 447 pi; 585 number_pi(11);""; 586 ring r = (real,10),t,dp; 587 number pi = number_pi(11); pi; 448 588 } 449 589 /////////////////////////////////////////////////////////////////////////////// … … 465 605 example 466 606 { "EXAMPLE:"; echo = 2; 467 primes(50,100);468 intvec v = primes(37,1); v;607 primes(50,100);""; 608 intvec v = primes(37,1); v; 469 609 } 470 610 /////////////////////////////////////////////////////////////////////////////// 471 611 472 612 proc product (id, list #) 473 "USAGE: product(id[,v]); id =ideal/vector/module/matrix474 resp.id=intvec/intmat, v=intvec (e.g. v=1..n, n=integer)475 RETURN: poly resp. int which is the product of all entries of id, with index476 given by v (default: v=1..number of entries of id)477 NOTE:id is treated as a list of polys resp. integers. A module m is613 "USAGE: product(id[,v]); id ideal/vector/module/matrix/intvec/intmat/list, 614 v intvec (default: v=1.. number of entries of id) 615 RETURN:  if id is not a list: poly resp. int, the product of all entries of 616 id with index given by v. 617 id is treated as a list of polys resp. integers. A module m is 478 618 identified with corresponding matrix M (columns of M generate m) 619  if id is a list: product of list entries, with index given by v. 620 Assume that list members can be multiplied 479 621 EXAMPLE: example product; shows an example 480 622 " 481 623 { 482 int n,j; 483 if( typeof(id)=="poly" or typeof(id)=="ideal" or typeof(id)=="vector" 484 or typeof(id)=="module" or typeof(id)=="matrix" ) 624 int n,j,tt; 625 string ty; 626 list l; 627 int s = size(#); 628 if( s!=0 ) 629 { if ( typeof(#[s])=="intvec" ) 630 { intvec v = #[s]; 631 tt=1; s=s1; 632 if ( s>0 ) { # = #[1..s]; } 633 } 634 } 635 if ( s>0 ) 636 { 637 l = list(id)+#; 638 kill id; 639 list id = l; 640 ty = "list"; 641 } 642 else 643 { ty = typeof(id); 644 } 645 if( ty=="list" ) 646 { n = size(id); 647 def f(1) = id[1]; 648 for( j=2; j<=n; j=j+1 ) { def f(j)=f(j1)*id[j]; } 649 return(f(n)); 650 } 651 if( ty=="poly" or ty=="ideal" or ty=="vector" 652 or ty=="module" or ty=="matrix" ) 485 653 { 486 654 ideal i = ideal(matrix(id)); 487 if( size(#)!=0 ) { i = i[#[1]]; } 488 n = ncols(i); poly f=1; 489 } 490 if( typeof(id)=="int" or typeof(id)=="intvec" or typeof(id)=="intmat" ) 491 { 492 if ( typeof(id) == "int" ) { intmat S =id; } 655 kill id; 656 ideal id = i; 657 if( tt!=0 ) { id = id[v]; } 658 n = ncols(id); poly f(1)=id[1]; 659 } 660 if( ty=="int" or ty=="intvec" or ty=="intmat" ) 661 { 662 if ( ty == "int" ) { intmat S =id; } 493 663 else { intmat S = intmat(id); } 494 664 intvec i = S[1..nrows(S),1..ncols(S)]; 495 if( size(#)!=0 ) { i = i[#[1]]; } 496 n = size(i); int f=1; 497 } 498 for( j=1; j<=n; j=j+1 ) { f=f*i[j]; } 499 return(f); 665 kill id; 666 intvec id = i; 667 if( tt!=0 ) { id = id[v]; } 668 n = size(id); int f(1)=id[1]; 669 } 670 for( j=2; j<=n; j=j+1 ) { def f(j)=f(j1)*id[j]; } 671 return(f(n)); 500 672 } 501 673 example … … 504 676 ideal m = maxideal(1); 505 677 product(m); 678 product(m[2..3]); 506 679 matrix M[2][3] = 1,x,2,y,3,z; 507 680 product(M); … … 577 750 EXAMPLE: example sort; shows an example 578 751 " 579 { 580 int ii,jj,s,n = 0,0,1,0; 752 { int ii,jj,s,n = 0,0,1,0; 581 753 intvec v; 582 754 if ( defined(basering) ) { def P = basering; } … … 659 831 example 660 832 { "EXAMPLE:"; echo = 2; 661 ring r0 = 0,(x,y,z),lp; 662 ideal i = x3,y3,z3,x2z,x2y,y2z,y2x,z2y,z2x,xyz; 663 show(sort(i));""; 664 show(sort(i,"wp(1,2,3)"));""; 665 intvec v=10..1; 666 show(sort(i,v));""; 667 show(sort(i,v,1));""; // should be the identity 668 ring r1 = 0,t,ls; 669 ideal j = t14,t6,t28,t20,t12,t34,t26,t18,t40,t32,t24,t38,t30,t36; 670 show(sort(j)[1]);""; 671 show(sort(j,"lp")[1]);""; 672 list L =1,5..8,10,2,8..5,8,3..10; 673 sort(L)[1];""; // sort L lexicographically 833 ring r0 = 0,(x,y,z,t),lp; 834 ideal i = x3,z3,xyz; 835 sort(i); // sort w.r.t. lex ordering 836 sort(i,3..1); 837 sort(i,"ls")[1]; // sort w.r.t. negative lex ordering 838 pause ("press return key to continue"); 839 list L =1,8..5,3..10; 840 sort(L)[1]; // sort L lexicographically 674 841 sort(L,"Dp",1)[1]; // sort L w.r.t (total sum, reverse lex) 675 842 } … … 703 870 intvec v; v[ncols(Z)]=0; v=v+1; 704 871 return((Z*v)[1,1]); 705 }872 } 706 873 example 707 874 { "EXAMPLE:"; echo = 2; … … 709 876 vector pv = [xy,xz,yz,x2,y2,z2]; 710 877 sum(pv); 711 //sum(pv,2..5); 712 //matrix M[2][3] = 1,x,2,y,3,z; 713 //sum(M); 714 //intvec w=2,4,6; 715 //sum(M,w); 716 //intvec iv = 1,2,3,4,5,6,7,8,9; 717 //w=1..5,7,9; 718 //sum(iv,w); 719 //intmat m[2][3] = 1,1,1,2,2,2; 720 //sum(m,3..4); 878 sum(pv,2..5); 879 matrix M[2][3] = 1,x,2,y,3,z; 880 intvec w=2,4,6; 881 sum(M,w); 882 intvec iv = 1,2,3,4,5,6,7,8,9; 883 sum(iv,2..4); 721 884 } 722 885 ///////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset
for help on using the changeset viewer.