[d694de] | 1 | //GMG, last modified 18.6.99 |
---|
[ebbe4a] | 2 | //anne, added deleteSublist and watchdog 12.12.2000 |
---|
[3d124a7] | 3 | /////////////////////////////////////////////////////////////////////////////// |
---|
[50cbdc] | 4 | version="$Id: general.lib,v 1.40 2001-08-27 14:47:50 Singular Exp $"; |
---|
[49998f] | 5 | category="General purpose"; |
---|
[5480da] | 6 | info=" |
---|
[803c5a1] | 7 | LIBRARY: general.lib Elementary Computations of General Type |
---|
[3d124a7] | 8 | |
---|
[f34c37c] | 9 | PROCEDURES: |
---|
[0b59f5] | 10 | A_Z(\"a\",n); string a,b,... of n comma separated letters |
---|
[63be42] | 11 | ASCII([n,m]); string of printable ASCII characters (number n to m) |
---|
[3d124a7] | 12 | binomial(n,m[,../..]); n choose m (type int), [type string/type number] |
---|
[ebbe4a] | 13 | deleteSublist(iv,l); delete entries given by iv from list l |
---|
[3d124a7] | 14 | factorial(n[,../..]); n factorial (=n!) (type int), [type string/number] |
---|
| 15 | fibonacci(n[,p]); nth Fibonacci number [char p] |
---|
[dd2aa36] | 16 | kmemory([n[,v]]); active [allocated] memory in kilobyte |
---|
[3d124a7] | 17 | killall(); kill all user-defined variables |
---|
| 18 | number_e(n); compute exp(1) up to n decimal digits |
---|
| 19 | number_pi(n); compute pi (area of unit circle) up to n digits |
---|
| 20 | primes(n,m); intvec of primes p, n<=p<=m |
---|
| 21 | product(../..[,v]); multiply components of vector/ideal/...[indices v] |
---|
| 22 | sort(ideal/module); sort generators according to monomial ordering |
---|
| 23 | sum(vector/id/..[,v]); add components of vector/ideal/...[with indices v] |
---|
[ebbe4a] | 24 | watchdog(i,cmd); only wait for result of command cmd for i seconds |
---|
[63be42] | 25 | which(command); search for command and return absolute path, if found |
---|
[194f5e5] | 26 | (parameters in square brackets [] are optional) |
---|
[5480da] | 27 | "; |
---|
| 28 | |
---|
[3d124a7] | 29 | LIB "inout.lib"; |
---|
| 30 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 31 | |
---|
| 32 | proc A_Z (string s,int n) |
---|
[d2b2a7] | 33 | "USAGE: A_Z(\"a\",n); a any letter, n integer (-26<= n <=26, !=0) |
---|
[3d124a7] | 34 | RETURN: string of n small (if a is small) or capital (if a is capital) |
---|
[0b59f5] | 35 | letters, comma separated, beginning with a, in alphabetical |
---|
[3d124a7] | 36 | order (or revers alphabetical order if n<0) |
---|
| 37 | EXAMPLE: example A_Z; shows an example |
---|
[d2b2a7] | 38 | " |
---|
[3d124a7] | 39 | { |
---|
| 40 | if ( n>=-26 and n<=26 and n!=0 ) |
---|
| 41 | { |
---|
| 42 | string alpha = |
---|
| 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 | "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"; |
---|
| 47 | int ii; int aa; |
---|
| 48 | for(ii=1; ii<=51; ii=ii+2) |
---|
| 49 | { |
---|
| 50 | if( alpha[ii]==s ) { aa=ii; } |
---|
| 51 | } |
---|
| 52 | if ( aa==0) |
---|
| 53 | { |
---|
| 54 | for(ii=105; ii<=155; ii=ii+2) |
---|
| 55 | { |
---|
| 56 | if( alpha[ii]==s ) { aa=ii; } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | if( aa!=0 ) |
---|
| 60 | { |
---|
| 61 | string out; |
---|
| 62 | if (n > 0) { out = alpha[aa,2*(n)-1]; return (out); } |
---|
| 63 | if (n < 0) |
---|
| 64 | { |
---|
| 65 | string beta = |
---|
| 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 | "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"; |
---|
| 70 | if ( aa < 52 ) { aa=52-aa; } |
---|
| 71 | if ( aa > 104 ) { aa=260-aa; } |
---|
| 72 | out = beta[aa,2*(-n)-1]; return (out); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | example |
---|
| 78 | { "EXAMPLE:"; echo = 2; |
---|
| 79 | A_Z("c",5); |
---|
| 80 | A_Z("Z",-5); |
---|
| 81 | string sR = "ring R = (0,"+A_Z("A",6)+"),("+A_Z("a",10)+"),dp;"; |
---|
| 82 | sR; |
---|
[034ce1] | 83 | execute(sR); |
---|
[3d124a7] | 84 | R; |
---|
| 85 | } |
---|
| 86 | /////////////////////////////////////////////////////////////////////////////// |
---|
[63be42] | 87 | proc ASCII (list #) |
---|
| 88 | "USAGE: ASCII([n,m]); n,m= integers (32 <= n <= m <= 126) |
---|
[b42ab6] | 89 | RETURN: string of printable ASCII characters (no native language support) |
---|
[63be42] | 90 | ASCII(): string of all ASCII characters with its numbers, |
---|
[b42ab6] | 91 | ASCII(n): n-th ASCII character |
---|
| 92 | ASCII(n,m): n-th up to m-th ASCII character (inclusive) |
---|
[63be42] | 93 | EXAMPLE: example ASCII; shows an example |
---|
| 94 | " |
---|
| 95 | { |
---|
| 96 | string s1 = |
---|
[008846] | 97 | " ! \" # $ % & ' ( ) * + , - . |
---|
[63be42] | 98 | 32 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 : ; < = |
---|
| 100 | 47 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 |
---|
| 102 | 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
---|
| 103 | M N O P Q R S T U V W X Y Z [ |
---|
| 104 | 77 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 |
---|
| 106 | 92 93 94 95 96 97 98 99 100 101 102 103 104 105 10 |
---|
| 107 | k l m n o p q r s t u v w x y |
---|
| 108 | 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
---|
| 109 | z { | } ~ |
---|
| 110 | 122 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 | } |
---|
| 128 | example |
---|
| 129 | { "EXAMPLE:"; echo = 2; |
---|
| 130 | ASCII();""; |
---|
| 131 | ASCII(42); |
---|
| 132 | ASCII(32,126); |
---|
| 133 | } |
---|
| 134 | /////////////////////////////////////////////////////////////////////////////// |
---|
[3d124a7] | 135 | |
---|
[c860e9] | 136 | proc binomial (int n, int k, list #) |
---|
[f937e2] | 137 | "USAGE: binomial(n,k[,p]); n,k,p integers |
---|
[b42ab6] | 138 | RETURN: 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 0 or prime(p) |
---|
| 141 | @* - of type number if a basering, say R, is present and p=0=char(R) |
---|
| 142 | or if prime(p)=char(R) |
---|
| 143 | @* - of type string else |
---|
[c860e9] | 144 | NOTE: In any characteristic, binomial(n,k) = coefficient of x^k in (1+x)^n |
---|
[b42ab6] | 145 | SEE ALSO: prime |
---|
[3d124a7] | 146 | EXAMPLE: example binomial; shows an example |
---|
[d2b2a7] | 147 | " |
---|
[3d124a7] | 148 | { |
---|
[c860e9] | 149 | int str,p; |
---|
| 150 | //---------------------------- initialization ------------------------------- |
---|
| 151 | if ( size(#) == 0 ) |
---|
| 152 | { str = 1; |
---|
| 153 | ring bin = 0,x,dp; |
---|
| 154 | number r=1; |
---|
[f937e2] | 155 | } |
---|
[c860e9] | 156 | if ( size(#) > 0 ) |
---|
[3d124a7] | 157 | { |
---|
[c860e9] | 158 | p = (#[1]!=0)*prime(#[1]); |
---|
| 159 | if ( defined(basering) ) |
---|
[3d124a7] | 160 | { |
---|
[c860e9] | 161 | if ( p == char(basering) ) |
---|
| 162 | { number r=1; |
---|
| 163 | } |
---|
| 164 | else |
---|
| 165 | { str = 1; |
---|
| 166 | ring bin = p,x,dp; |
---|
| 167 | number r=1; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { str = 1; |
---|
| 172 | ring bin = p,x,dp; |
---|
| 173 | number r=1; |
---|
[3d124a7] | 174 | } |
---|
| 175 | } |
---|
[c860e9] | 176 | //-------------------------------- char 0 ----------------------------------- |
---|
| 177 | if ( p==0 ) |
---|
| 178 | { |
---|
| 179 | r = binom0(n,k); |
---|
| 180 | } |
---|
| 181 | //-------------------------------- char p ----------------------------------- |
---|
| 182 | else |
---|
| 183 | { |
---|
| 184 | r = binomp(n,k,p); |
---|
| 185 | } |
---|
| 186 | //-------------------------------- return ----------------------------------- |
---|
| 187 | if ( str==1 ) { return(string(r)); } |
---|
| 188 | else { return(r); } |
---|
| 189 | } |
---|
[3d124a7] | 190 | example |
---|
| 191 | { "EXAMPLE:"; echo = 2; |
---|
[c860e9] | 192 | binomial(200,100);""; //type string, computed in char 0 |
---|
| 193 | binomial(200,100,3);""; //type string, computed in char 3 |
---|
| 194 | int n,k = 200,100; |
---|
| 195 | ring r = 0,x,dp; |
---|
| 196 | number b1 = binomial(n,k,0); //type number, computed in ring r |
---|
| 197 | poly b2 = coeffs((x+1)^n,x)[k+1,1]; //coefficient of x^k in (x+1)^n |
---|
| 198 | b1-b2; //b1 and b2 should coincide |
---|
| 199 | } |
---|
| 200 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 201 | |
---|
| 202 | static proc binom0 (int n, int k) |
---|
| 203 | //computes binomial coefficient n choose k in basering, assume 0<k<=n |
---|
| 204 | //and char(basering) = 0 or n < char(basering) |
---|
| 205 | { |
---|
| 206 | int l; |
---|
| 207 | number r=1; |
---|
| 208 | if ( k > n-k ) |
---|
| 209 | { k = n-k; |
---|
| 210 | } |
---|
| 211 | if ( k<=0 or k>n ) //trivial cases |
---|
| 212 | { r = (k==0)*r; |
---|
| 213 | } |
---|
| 214 | for (l=1; l<=k; l++ ) |
---|
| 215 | { |
---|
| 216 | r=r*(n+1-l)/l; |
---|
| 217 | } |
---|
| 218 | return(r); |
---|
| 219 | } |
---|
| 220 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 221 | |
---|
| 222 | static proc binomp (int n, int k, int p) |
---|
| 223 | //computes binomial coefficient n choose k in basering of char p > 0 |
---|
| 224 | //binomial(n,k) = coefficient of x^k in (1+x)^n. |
---|
| 225 | //Let n=q*p^j, gcd(q,p)=1, then (1+x)^n = (1 + x^(p^j))^q. We have |
---|
| 226 | //binomial(n,k)=0 if k!=l*p^j and binomial(n,l*p^j) = binomial(q,l). |
---|
| 227 | //Do this reduction first. Then, in denominator and numerator |
---|
| 228 | //of defining formula for binomial coefficient, reduce those factors |
---|
| 229 | //mod p which are not divisible by p and cancel common factors p. Hence, |
---|
| 230 | //if n = h*p+r, k=l*p+s, r,s<p, binomial(n,k) = binomial(r,s)*binomial(h,l) |
---|
| 231 | { |
---|
| 232 | int l,q,i= 1,n,1; |
---|
| 233 | number r=1; |
---|
| 234 | if ( k > n-k ) |
---|
| 235 | { k = n-k; |
---|
| 236 | } |
---|
| 237 | if ( k<=0 or k>n) //trivial cases |
---|
| 238 | { r = (k==0)*r; |
---|
| 239 | } |
---|
| 240 | else |
---|
| 241 | { |
---|
| 242 | while ( q mod p == 0 ) |
---|
| 243 | { l = l*p; |
---|
| 244 | q = q div p; |
---|
| 245 | } //we have now n=q*l, l=p^j, gcd(q,p)=1; |
---|
| 246 | if (k mod l != 0 ) |
---|
| 247 | { r = 0; |
---|
| 248 | } |
---|
| 249 | else |
---|
| 250 | { l = k div l; |
---|
| 251 | n = q mod p; |
---|
| 252 | k = l mod p; //now 0<= k,n <p, use binom0 for n,k |
---|
| 253 | q = q div p; //recursion for q,l |
---|
| 254 | l = l div p; //use binomp for q,l |
---|
| 255 | r = binom0(n,k)*binomp(q,l,p); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | return(r); |
---|
[3d124a7] | 259 | } |
---|
| 260 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 261 | |
---|
[c860e9] | 262 | proc factorial (int n, list #) |
---|
[b42ab6] | 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 0 or prime(p) |
---|
| 266 | @* - of type number if a basering is present and 0=p=char(basering) |
---|
| 267 | or if prime(p)=char(basering) |
---|
| 268 | @* - of type string else |
---|
| 269 | SEE ALSO: prime |
---|
| 270 | EXAMPLE: example factorial; shows an example |
---|
[d2b2a7] | 271 | " |
---|
[b5726c] | 272 | { int str,l,p; |
---|
[c860e9] | 273 | //---------------------------- initialization ------------------------------- |
---|
| 274 | if ( size(#) == 0 ) |
---|
[b5726c] | 275 | { str = 1; |
---|
[c860e9] | 276 | ring bin = 0,x,dp; |
---|
| 277 | number r=1; |
---|
| 278 | } |
---|
| 279 | if ( size(#) > 0 ) |
---|
| 280 | { |
---|
| 281 | p = (#[1]!=0)*prime(#[1]); |
---|
| 282 | if ( defined(basering) ) |
---|
| 283 | { |
---|
| 284 | if ( p == char(basering) ) |
---|
[b5726c] | 285 | { number r=1; |
---|
[c860e9] | 286 | } |
---|
| 287 | else |
---|
[b5726c] | 288 | { str = 1; |
---|
[c860e9] | 289 | ring bin = p,x,dp; |
---|
| 290 | number r=1; |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | else |
---|
[b5726c] | 294 | { str = 1; |
---|
[c860e9] | 295 | ring bin = p,x,dp; |
---|
| 296 | number r=1; |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | //------------------------------ computation -------------------------------- |
---|
| 300 | for (l=2; l<=n; l++) |
---|
[3d124a7] | 301 | { |
---|
[f937e2] | 302 | r=r*l; |
---|
[3d124a7] | 303 | } |
---|
[b5726c] | 304 | if ( str==1 ) { return(string(r)); } |
---|
| 305 | else { return(r); } |
---|
[3d124a7] | 306 | } |
---|
| 307 | example |
---|
| 308 | { "EXAMPLE:"; echo = 2; |
---|
[584f84d] | 309 | factorial(37);""; //37! of type string (as long integer) |
---|
[c860e9] | 310 | ring r1 = 0,x,dp; |
---|
[584f84d] | 311 | number p = factorial(37,0); //37! of type number, computed in r |
---|
[c860e9] | 312 | p; |
---|
[3d124a7] | 313 | } |
---|
| 314 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 315 | |
---|
[c860e9] | 316 | proc fibonacci (int n, list #) |
---|
[b42ab6] | 317 | "USAGE: fibonacci(n); n,p integers |
---|
| 318 | RETURN: fibonacci(n): nth Fibonacci number, f(0)=f(1)=1, f(i+1)=f(i-1)+f(i) |
---|
| 319 | @* - computed in characteristic 0, of type string |
---|
| 320 | @* fibonacci(n,p): f(n) computed in characteristic 0 or prime(p) |
---|
| 321 | @* - of type number if a basering is present and p=0=char(basering) |
---|
| 322 | or if prime(p)=char(basering) |
---|
| 323 | @* - of type string else |
---|
| 324 | SEE ALSO: prime |
---|
[3d124a7] | 325 | EXAMPLE: example fibonacci; shows an example |
---|
[d2b2a7] | 326 | " |
---|
[c860e9] | 327 | { int str,ii,p; |
---|
| 328 | //---------------------------- initialization ------------------------------- |
---|
| 329 | if ( size(#) == 0 ) |
---|
| 330 | { str = 1; |
---|
| 331 | ring bin = 0,x,dp; |
---|
| 332 | number f,g,h=1,1,1; |
---|
| 333 | } |
---|
| 334 | if ( size(#) > 0 ) |
---|
| 335 | { |
---|
| 336 | p = (#[1]!=0)*prime(#[1]); |
---|
| 337 | if ( defined(basering) ) |
---|
| 338 | { |
---|
| 339 | if ( p == char(basering) ) |
---|
| 340 | { number f,g,h=1,1,1; |
---|
| 341 | } |
---|
| 342 | else |
---|
| 343 | { str = 1; |
---|
| 344 | ring bin = p,x,dp; |
---|
| 345 | number f,g,h=1,1,1; |
---|
| 346 | } |
---|
| 347 | } |
---|
| 348 | else |
---|
| 349 | { str = 1; |
---|
| 350 | ring bin = p,x,dp; |
---|
| 351 | number f,g,h=1,1,1; |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | //------------------------------ computation -------------------------------- |
---|
[f937e2] | 355 | for (ii=3; ii<=n; ii=ii+1) |
---|
[3d124a7] | 356 | { |
---|
[f937e2] | 357 | h = f+g; f = g; g = h; |
---|
| 358 | } |
---|
[c860e9] | 359 | if ( str==1 ) { return(string(h)); } |
---|
| 360 | else { return(h); } |
---|
[3d124a7] | 361 | } |
---|
| 362 | example |
---|
| 363 | { "EXAMPLE:"; echo = 2; |
---|
[b42ab6] | 364 | fibonacci(42); ""; //f(42) of type string (as long integer) |
---|
| 365 | ring r = 2,x,dp; |
---|
| 366 | number b = fibonacci(42,2); //f(42) of type number, computed in r |
---|
[c860e9] | 367 | b; |
---|
[3d124a7] | 368 | } |
---|
| 369 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 370 | |
---|
[d694de] | 371 | proc kmemory (list #) |
---|
[b42ab6] | 372 | "USAGE: kmemory([n,[v]]); n,v integers |
---|
[d694de] | 373 | RETURN: memory in kilobyte of type int |
---|
[b42ab6] | 374 | @* n=0: memory used by active variables (same as no parameters) |
---|
| 375 | @* n=1: total memory allocated by Singular |
---|
| 376 | @* n=2: difference between top and init memory adress (sbrk memory) |
---|
| 377 | @* n!=0,1,2: 0 |
---|
[dd2aa36] | 378 | DISPLAY: detailed information about allocated and used memory if v!=0 |
---|
[d694de] | 379 | NOTE: kmemory uses internal function 'memory' to compute kilobyte, and |
---|
| 380 | is the same as 'memory' for n!=0,1,2 |
---|
[3d124a7] | 381 | EXAMPLE: example kmemory; shows an example |
---|
[d2b2a7] | 382 | " |
---|
[917fb5] | 383 | { |
---|
[d694de] | 384 | int n; |
---|
[dd2aa36] | 385 | int verb; |
---|
[d694de] | 386 | if (size(#) != 0) |
---|
| 387 | { |
---|
| 388 | n=#[1]; |
---|
[dd2aa36] | 389 | if (size(#) >1) |
---|
| 390 | { verb=#[2]; } |
---|
[d694de] | 391 | } |
---|
[917fb5] | 392 | |
---|
[dd2aa36] | 393 | if ( verb != 0) |
---|
| 394 | { |
---|
| 395 | if ( n==0) |
---|
| 396 | { dbprint(printlevel-voice+3, |
---|
| 397 | "// memory used, at the moment, by active variables (kilobyte):"); } |
---|
| 398 | if ( n==1 ) |
---|
| 399 | { dbprint(printlevel-voice+3, |
---|
| 400 | "// total memory allocated, at the moment, by SINGULAR (kilobyte):"); } |
---|
| 401 | } |
---|
[d694de] | 402 | return ((memory(n)+1023)/1024); |
---|
[3d124a7] | 403 | } |
---|
| 404 | example |
---|
| 405 | { "EXAMPLE:"; echo = 2; |
---|
| 406 | kmemory(); |
---|
[dd2aa36] | 407 | kmemory(1,1); |
---|
[3d124a7] | 408 | } |
---|
| 409 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 410 | |
---|
| 411 | proc killall |
---|
[d2b2a7] | 412 | "USAGE: killall(); (no parameter) |
---|
| 413 | killall(\"type_name\"); |
---|
| 414 | killall(\"not\", \"type_name\"); |
---|
[b42ab6] | 415 | RETURN: killall(); kills all user-defined variables except loaded procedures, |
---|
| 416 | no return value. |
---|
| 417 | @* - killall(\"type_name\"); kills all user-defined variables, |
---|
| 418 | of type \"type_name\" |
---|
| 419 | @* - killall(\"not\", \"type_name\"); kills all user-defined variables, |
---|
| 420 | except those of type \"type_name\" and except loaded procedures |
---|
[65546eb] | 421 | @* - killall(\"not\", \"name_1\", \"name_2\", ...); |
---|
| 422 | kills all user-defined variables, except those of name \"name_i\" |
---|
[b42ab6] | 423 | and except loaded procedures |
---|
[3d124a7] | 424 | NOTE: killall should never be used inside a procedure |
---|
| 425 | EXAMPLE: example killall; shows an example AND KILLS ALL YOUR VARIABLES |
---|
[d2b2a7] | 426 | " |
---|
[3d124a7] | 427 | { |
---|
[48c165a] | 428 | if (system("with","Namespaces")) |
---|
| 429 | { |
---|
| 430 | list @marie=names(Top); |
---|
| 431 | } |
---|
| 432 | else |
---|
| 433 | { |
---|
| 434 | list @marie=names(); |
---|
| 435 | } |
---|
[09f420] | 436 | int j, no_kill, @joni; |
---|
[b42ab6] | 437 | for ( @joni=1; @joni<=size(#); @joni++) |
---|
[5c187b] | 438 | { |
---|
[b42ab6] | 439 | if (typeof(#[@joni]) != "string") |
---|
[5c187b] | 440 | { |
---|
[b42ab6] | 441 | ERROR("Need string as " + string(@joni) + "th argument"); |
---|
[5c187b] | 442 | } |
---|
| 443 | } |
---|
[65546eb] | 444 | |
---|
[5c187b] | 445 | // kills all user-defined variables but not loaded procedures |
---|
| 446 | if( size(#)==0 ) |
---|
| 447 | { |
---|
[b42ab6] | 448 | for ( @joni=size(@marie); @joni>0; @joni-- ) |
---|
[5c187b] | 449 | { |
---|
[48c165a] | 450 | if( @marie[@joni]!="LIB" and typeof(`@marie[@joni]`)!="proc" |
---|
| 451 | and typeof(`@marie[@joni]`)!="package") |
---|
[b42ab6] | 452 | { kill `@marie[@joni]`; } |
---|
[5c187b] | 453 | } |
---|
| 454 | } |
---|
| 455 | else |
---|
| 456 | { |
---|
| 457 | // kills all user-defined variables |
---|
| 458 | if( size(#)==1 ) |
---|
| 459 | { |
---|
| 460 | // of type proc |
---|
| 461 | if( #[1] == "proc" ) |
---|
[3d124a7] | 462 | { |
---|
[b42ab6] | 463 | for ( @joni=size(@marie); @joni>0; @joni-- ) |
---|
[5c187b] | 464 | { |
---|
[48c165a] | 465 | if((@marie[@joni]!="General") |
---|
| 466 | and (@marie[@joni]!="Top") |
---|
| 467 | and (@marie[@joni]!="killall") |
---|
| 468 | and (@marie[@joni]=="LIB" or |
---|
| 469 | typeof(`@marie[@joni]`)=="package" or |
---|
| 470 | typeof(`@marie[@joni]`)=="proc")) |
---|
| 471 | { kill `@marie[@joni]`; |
---|
| 472 | if (defined(`@marie[@joni]`)) {kill `@marie[@joni]`;}} |
---|
| 473 | } |
---|
| 474 | if (system("with","Namespaces")) |
---|
| 475 | { |
---|
| 476 | @marie=names(General); |
---|
| 477 | for ( @joni=size(@marie); @joni>0; @joni-- ) |
---|
| 478 | { |
---|
| 479 | if(@marie[@joni]!="killall" |
---|
| 480 | and typeof(`@marie[@joni]`)=="proc") |
---|
| 481 | { kill General::`@marie[@joni]`; } |
---|
| 482 | } |
---|
| 483 | kill General::killall; |
---|
[5c187b] | 484 | } |
---|
[3d124a7] | 485 | } |
---|
[5c187b] | 486 | else |
---|
[65546eb] | 487 | { |
---|
[5c187b] | 488 | // other types |
---|
[b42ab6] | 489 | for ( @joni=size(@marie); @joni>2; @joni-- ) |
---|
[5c187b] | 490 | { |
---|
[65546eb] | 491 | if(typeof(`@marie[@joni]`)==#[1] and @marie[@joni]!="LIB" |
---|
| 492 | and typeof(`@marie[@joni]`)!="proc") |
---|
[b42ab6] | 493 | { kill `@marie[@joni]`; } |
---|
[5c187b] | 494 | } |
---|
| 495 | } |
---|
| 496 | } |
---|
| 497 | else |
---|
| 498 | { |
---|
[65546eb] | 499 | // kills all user-defined variables whose name or type is not #i |
---|
[b42ab6] | 500 | for ( @joni=size(@marie); @joni>2; @joni-- ) |
---|
[5c187b] | 501 | { |
---|
[b42ab6] | 502 | if ( @marie[@joni] != "LIB" && typeof(`@marie[@joni]`) != "proc") |
---|
[5c187b] | 503 | { |
---|
| 504 | no_kill = 0; |
---|
[09f420] | 505 | for (j=2; j<= size(#); j++) |
---|
[6f2edc] | 506 | { |
---|
[b42ab6] | 507 | if (typeof(`@marie[@joni]`)==#[j] or @marie[@joni] == #[j]) |
---|
[5c187b] | 508 | { |
---|
| 509 | no_kill = 1; |
---|
| 510 | break; |
---|
| 511 | } |
---|
[6f2edc] | 512 | } |
---|
[5c187b] | 513 | if (! no_kill) |
---|
[6f2edc] | 514 | { |
---|
[b42ab6] | 515 | kill `@marie[@joni]`; |
---|
[6f2edc] | 516 | } |
---|
| 517 | } |
---|
[5c187b] | 518 | } |
---|
| 519 | } |
---|
[6f2edc] | 520 | } |
---|
[3d124a7] | 521 | } |
---|
| 522 | example |
---|
| 523 | { "EXAMPLE:"; echo = 2; |
---|
[c860e9] | 524 | ring rtest; ideal i=x,y,z; string str="hi"; int j = 3; |
---|
| 525 | export rtest,i,str,j; //this makes the local variables global |
---|
| 526 | listvar(); |
---|
| 527 | killall("ring"); // kills all rings |
---|
| 528 | listvar(); |
---|
| 529 | killall("not", "int"); // kills all variables except int's (and procs) |
---|
| 530 | listvar(); |
---|
| 531 | killall(); // kills all vars except loaded procs |
---|
| 532 | listvar(); |
---|
[3d124a7] | 533 | } |
---|
| 534 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 535 | |
---|
| 536 | proc number_e (int n) |
---|
[d2b2a7] | 537 | "USAGE: number_e(n); n integer |
---|
[b42ab6] | 538 | RETURN: Euler number e=exp(1) up to n decimal digits (no rounding) |
---|
| 539 | @* - of type string if no basering of char 0 is defined |
---|
| 540 | @* - of type number if a basering of char 0 is defined |
---|
| 541 | DISPLAY: decimal format of e if printlevel > 0 (default:printlevel=0 ) |
---|
| 542 | NOTE: procedure uses algorithm of A.H.J. Sale |
---|
[3d124a7] | 543 | EXAMPLE: example number_e; shows an example |
---|
[d2b2a7] | 544 | " |
---|
[3d124a7] | 545 | { |
---|
| 546 | int i,m,s,t; |
---|
| 547 | intvec u,e; |
---|
| 548 | u[n+2]=0; e[n+1]=0; e=e+1; |
---|
| 549 | if( defined(basering) ) |
---|
| 550 | { |
---|
| 551 | if( char(basering)==0 ) { number r=2; t=1; } |
---|
| 552 | } |
---|
| 553 | string result = "2."; |
---|
| 554 | for( i=1; i<=n+1; i=i+1 ) |
---|
| 555 | { |
---|
| 556 | e = e*10; |
---|
| 557 | for( m=n+1; m>=1; m=m-1 ) |
---|
| 558 | { |
---|
| 559 | s = e[m]+u[m+1]; |
---|
[18dd47] | 560 | u[m] = s div (m+1); |
---|
[3d124a7] | 561 | e[m] = s%(m+1); |
---|
| 562 | } |
---|
| 563 | result = result+string(u[1]); |
---|
| 564 | if( t==1 ) { r = r+number(u[1])/number(10)^i; } |
---|
| 565 | } |
---|
[c860e9] | 566 | if( t==1 ) |
---|
| 567 | { dbprint(printlevel-voice+2,"// "+result[1,n+1]); |
---|
| 568 | return(r); |
---|
| 569 | } |
---|
[3d124a7] | 570 | return(result[1,n+1]); |
---|
| 571 | } |
---|
| 572 | example |
---|
| 573 | { "EXAMPLE:"; echo = 2; |
---|
[c860e9] | 574 | number_e(30);""; |
---|
[3d124a7] | 575 | ring R = 0,t,lp; |
---|
[c860e9] | 576 | number e = number_e(30); |
---|
[3d124a7] | 577 | e; |
---|
| 578 | } |
---|
| 579 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 580 | |
---|
| 581 | proc number_pi (int n) |
---|
[d2b2a7] | 582 | "USAGE: number_pi(n); n positive integer |
---|
[b42ab6] | 583 | RETURN: pi (area of unit circle) up to n decimal digits (no rounding) |
---|
| 584 | @* - of type string if no basering of char 0 is defined, |
---|
| 585 | @* - of type number, if a basering of char 0 is defined |
---|
| 586 | DISPLAY: decimal format of pi if printlevel > 0 (default:printlevel=0 ) |
---|
| 587 | NOTE: procedure uses algorithm of S. Rabinowitz |
---|
[3d124a7] | 588 | EXAMPLE: example number_pi; shows an example |
---|
[d2b2a7] | 589 | " |
---|
[3d124a7] | 590 | { |
---|
| 591 | int i,m,t,e,q,N; |
---|
| 592 | intvec r,p,B,Prelim; |
---|
| 593 | string result,prelim; |
---|
[18dd47] | 594 | N = (10*n) div 3 + 2; |
---|
[3d124a7] | 595 | p[N+1]=0; p=p+2; r=p; |
---|
| 596 | for( i=1; i<=N+1; i=i+1 ) { B[i]=2*i-1; } |
---|
| 597 | if( defined(basering) ) |
---|
| 598 | { |
---|
| 599 | if( char(basering)==0 ) { number pi; number pri; t=1; } |
---|
| 600 | } |
---|
| 601 | for( i=0; i<=n; i=i+1 ) |
---|
| 602 | { |
---|
| 603 | p = r*10; |
---|
| 604 | e = p[N+1]; |
---|
| 605 | for( m=N+1; m>=2; m=m-1 ) |
---|
| 606 | { |
---|
| 607 | r[m] = e%B[m]; |
---|
[18dd47] | 608 | q = e div B[m]; |
---|
[3d124a7] | 609 | e = q*(m-1)+p[m-1]; |
---|
| 610 | } |
---|
| 611 | r[1] = e%10; |
---|
[18dd47] | 612 | q = e div 10; |
---|
[3d124a7] | 613 | if( q!=10 and q!=9 ) |
---|
| 614 | { |
---|
| 615 | result = result+prelim; |
---|
| 616 | Prelim = q; |
---|
| 617 | prelim = string(q); |
---|
| 618 | } |
---|
| 619 | if( q==9 ) |
---|
| 620 | { |
---|
| 621 | Prelim = Prelim,9; |
---|
| 622 | prelim = prelim+"9"; |
---|
| 623 | } |
---|
| 624 | if( q==10 ) |
---|
| 625 | { |
---|
[18dd47] | 626 | Prelim = (Prelim+1)-((Prelim+1) div 10)*10; |
---|
[3d124a7] | 627 | for( m=size(Prelim); m>0; m=m-1) |
---|
| 628 | { |
---|
| 629 | prelim[m] = string(Prelim[m]); |
---|
| 630 | } |
---|
| 631 | result = result+prelim; |
---|
| 632 | if( t==1 ) { pi=pi+pri; } |
---|
| 633 | Prelim = 0; |
---|
| 634 | prelim = "0"; |
---|
| 635 | } |
---|
| 636 | if( t==1 ) { pi=pi+number(q)/number(10)^i; } |
---|
| 637 | } |
---|
| 638 | result = result,prelim[1]; |
---|
| 639 | result = "3."+result[2,n-1]; |
---|
[c860e9] | 640 | if( t==1 ) |
---|
| 641 | { dbprint(printlevel-voice+2,"// "+result); |
---|
| 642 | return(pi); |
---|
| 643 | } |
---|
[3d124a7] | 644 | return(result); |
---|
| 645 | } |
---|
| 646 | example |
---|
| 647 | { "EXAMPLE:"; echo = 2; |
---|
[c860e9] | 648 | number_pi(11);""; |
---|
| 649 | ring r = (real,10),t,dp; |
---|
| 650 | number pi = number_pi(11); pi; |
---|
[3d124a7] | 651 | } |
---|
| 652 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 653 | |
---|
| 654 | proc primes (int n, int m) |
---|
[d2b2a7] | 655 | "USAGE: primes(n,m); n,m integers |
---|
[3d124a7] | 656 | RETURN: intvec, consisting of all primes p, prime(n)<=p<=m, in increasing |
---|
[b42ab6] | 657 | order if n<=m, resp. prime(m)<=p<=n, in decreasing order if m<n. |
---|
| 658 | NOTE: prime(n); returns the biggest prime number <= min(n,32003) |
---|
| 659 | if n>=2, else 2 |
---|
[3d124a7] | 660 | EXAMPLE: example primes; shows an example |
---|
[d2b2a7] | 661 | " |
---|
[3d124a7] | 662 | { int change; |
---|
| 663 | if ( n>m ) { change=n; n=m ; m=change; change=1; } |
---|
| 664 | int q,p = prime(m),prime(n); intvec v = q; q = q-1; |
---|
| 665 | while ( q>=p ) { q = prime(q); v = q,v; q = q-1; } |
---|
| 666 | if ( change==1 ) { v = v[size(v)..1]; } |
---|
| 667 | return(v); |
---|
| 668 | } |
---|
| 669 | example |
---|
| 670 | { "EXAMPLE:"; echo = 2; |
---|
[c860e9] | 671 | primes(50,100);""; |
---|
| 672 | intvec v = primes(37,1); v; |
---|
[3d124a7] | 673 | } |
---|
| 674 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 675 | |
---|
| 676 | proc product (id, list #) |
---|
[c860e9] | 677 | "USAGE: product(id[,v]); id ideal/vector/module/matrix/intvec/intmat/list, |
---|
[b42ab6] | 678 | v intvec (default: v=1..number of entries of id) |
---|
| 679 | ASSUME: list members can be multiplied. |
---|
[65546eb] | 680 | RETURN: The product of all entries of id [with index given by v] of type |
---|
[b42ab6] | 681 | depending on the entries of id. |
---|
| 682 | NOTE: If id is not a list, id is treated as a list of polys resp. integers. |
---|
| 683 | A module m is identified with the corresponding matrix M (columns |
---|
| 684 | of M generate m). |
---|
[7708934] | 685 | @* If v is outside the range of id, we have the empty product and the |
---|
| 686 | result will be 1 (of type int). |
---|
[3d124a7] | 687 | EXAMPLE: example product; shows an example |
---|
[d2b2a7] | 688 | " |
---|
[65546eb] | 689 | { |
---|
[7708934] | 690 | //-------------------- initialization and special feature --------------------- |
---|
[c860e9] | 691 | int n,j,tt; |
---|
[65546eb] | 692 | string ty; //will become type of id |
---|
[c860e9] | 693 | list l; |
---|
[7708934] | 694 | |
---|
| 695 | // We wish to allow something like product(x(1..10)) if x(1),...,x(10) are |
---|
[65546eb] | 696 | // variables. x(1..10) is a list of polys and enters the procedure with |
---|
[7708934] | 697 | // id=x(1) and # a list with 9 polys, #[1]= x(2),...,#[9]= x(10). Hence, in |
---|
| 698 | // this case # is never empty. If an additional intvec v is given, |
---|
| 699 | // it is added to #, so we have to separate it first and make |
---|
| 700 | // the rest a list which has to be multiplied. |
---|
| 701 | |
---|
[c860e9] | 702 | int s = size(#); |
---|
| 703 | if( s!=0 ) |
---|
[65546eb] | 704 | { if ( typeof(#[s])=="intvec" or typeof(#[s])=="int") |
---|
| 705 | { |
---|
[7708934] | 706 | intvec v = #[s]; |
---|
[65546eb] | 707 | tt=1; |
---|
[7708934] | 708 | s=s-1; |
---|
[c860e9] | 709 | if ( s>0 ) { # = #[1..s]; } |
---|
| 710 | } |
---|
| 711 | } |
---|
| 712 | if ( s>0 ) |
---|
| 713 | { |
---|
[7708934] | 714 | l = list(id)+#; |
---|
| 715 | kill id; |
---|
| 716 | list id = l; //case: id = list |
---|
| 717 | ty = "list"; |
---|
| 718 | n = size(id); |
---|
[c860e9] | 719 | } |
---|
| 720 | else |
---|
[65546eb] | 721 | { |
---|
[7708934] | 722 | ty = typeof(id); |
---|
[65546eb] | 723 | if( ty == "list" ) |
---|
[7708934] | 724 | { n = size(id); } |
---|
[c860e9] | 725 | } |
---|
[7708934] | 726 | //------------------------------ reduce to 3 cases --------------------------- |
---|
[c860e9] | 727 | if( ty=="poly" or ty=="ideal" or ty=="vector" |
---|
| 728 | or ty=="module" or ty=="matrix" ) |
---|
[3d124a7] | 729 | { |
---|
| 730 | ideal i = ideal(matrix(id)); |
---|
[c860e9] | 731 | kill id; |
---|
[7708934] | 732 | ideal id = i; //case: id = ideal |
---|
| 733 | n = ncols(id); |
---|
[3d124a7] | 734 | } |
---|
[c860e9] | 735 | if( ty=="int" or ty=="intvec" or ty=="intmat" ) |
---|
[3d124a7] | 736 | { |
---|
[c860e9] | 737 | if ( ty == "int" ) { intmat S =id; } |
---|
[3d124a7] | 738 | else { intmat S = intmat(id); } |
---|
| 739 | intvec i = S[1..nrows(S),1..ncols(S)]; |
---|
[c860e9] | 740 | kill id; |
---|
[7708934] | 741 | intvec id = i; //case: id = intvec |
---|
[65546eb] | 742 | n = size(id); |
---|
[7708934] | 743 | } |
---|
| 744 | //--------------- consider intvec v and empty product ----------------------- |
---|
[65546eb] | 745 | if( tt!=0 ) |
---|
[7708934] | 746 | { |
---|
| 747 | for (j=1; j<=size(v); j++) |
---|
| 748 | { |
---|
| 749 | if ( v[j] <= 0 or v[j] > n ) //v outside range of id |
---|
[65546eb] | 750 | { |
---|
[7708934] | 751 | return(1); //empty product is 1 |
---|
[65546eb] | 752 | } |
---|
[7708934] | 753 | } |
---|
| 754 | id = id[v]; //consider part of id |
---|
| 755 | } //corresponding to v |
---|
| 756 | //--------------------- special case: one factor is zero --------------------- |
---|
| 757 | if ( typeof(id) == "ideal") |
---|
| 758 | { |
---|
| 759 | if( size(id) < ncols(id) ) |
---|
| 760 | { |
---|
| 761 | poly f; return(f); |
---|
| 762 | } |
---|
[3d124a7] | 763 | } |
---|
[7708934] | 764 | //-------------------------- finally, multiply objects ----------------------- |
---|
[65546eb] | 765 | n = size(id); |
---|
[7708934] | 766 | def f(1) = id[1]; |
---|
[c860e9] | 767 | for( j=2; j<=n; j=j+1 ) { def f(j)=f(j-1)*id[j]; } |
---|
| 768 | return(f(n)); |
---|
[3d124a7] | 769 | } |
---|
| 770 | example |
---|
| 771 | { "EXAMPLE:"; echo = 2; |
---|
| 772 | ring r= 0,(x,y,z),dp; |
---|
| 773 | ideal m = maxideal(1); |
---|
| 774 | product(m); |
---|
[c860e9] | 775 | product(m[2..3]); |
---|
[3d124a7] | 776 | matrix M[2][3] = 1,x,2,y,3,z; |
---|
| 777 | product(M); |
---|
| 778 | intvec v=2,4,6; |
---|
| 779 | product(M,v); |
---|
| 780 | intvec iv = 1,2,3,4,5,6,7,8,9; |
---|
| 781 | v=1..5,7,9; |
---|
| 782 | product(iv,v); |
---|
| 783 | intmat A[2][3] = 1,1,1,2,2,2; |
---|
| 784 | product(A,3..5); |
---|
| 785 | } |
---|
| 786 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 787 | |
---|
| 788 | proc sort (id, list #) |
---|
[b5726c] | 789 | "USAGE: sort(id[v,o,n]); id = ideal/module/intvec/list(of intvec's or int's) |
---|
[b42ab6] | 790 | @* sort may be called with 1, 2 or 3 arguments in the following way: |
---|
| 791 | @* sort(id[v,n]); v=intvec of positive integers, n=integer, |
---|
| 792 | @* sort(id[o,n]); o=string (any allowed ordstr of a ring), n=integer |
---|
| 793 | RETURN: a list l of two elements: |
---|
| 794 | @format |
---|
| 795 | l[1]: object of same type as input but sorted in the following way: |
---|
[3d124a7] | 796 | - if id=ideal/module: generators of id are sorted w.r.t. intvec v |
---|
| 797 | (id[v[1]] becomes 1-st, id[v[2]] 2-nd element, etc.). If no v is |
---|
| 798 | present, id is sorted w.r.t. ordering o (if o is given) or w.r.t. |
---|
| 799 | actual monomial ordering (if no o is given): |
---|
[b42ab6] | 800 | NOTE: generators with SMALLER(!) leading term come FIRST |
---|
| 801 | (e.g. sort(id); sorts backwards to actual monomial ordering) |
---|
[3d124a7] | 802 | - if id=list of intvec's or int's: consider a list element, say |
---|
| 803 | id[1]=3,2,5, as exponent vector of the monomial x^3*y^2*z^5; |
---|
| 804 | the corresponding monomials are ordered w.r.t. intvec v (s.a.). |
---|
| 805 | If no v is present, the monomials are sorted w.r.t. ordering o |
---|
| 806 | (if o is given) or w.r.t. lexicographical ordering (if no o is |
---|
| 807 | given). The corresponding ordered list of exponent vectors is |
---|
| 808 | returned. |
---|
| 809 | (e.g. sort(id); sorts lexicographically, smaller int's come first) |
---|
[a30caa3] | 810 | WARNING: Since negative exponents create the 0 polynomial in |
---|
[63be42] | 811 | Singular, id should not contain negative integers: the result |
---|
[a30caa3] | 812 | might not be as expected |
---|
[3d124a7] | 813 | - if id=intvec: id is treated as list of integers |
---|
| 814 | - if n!=0 the ordering is inverse, i.e. w.r.t. v(size(v)..1) |
---|
| 815 | default: n=0 |
---|
[b42ab6] | 816 | l[2]: intvec, describing the permutation of the input (hence l[2]=v |
---|
| 817 | if v is given (with positive integers)) |
---|
| 818 | @end format |
---|
[63be42] | 819 | NOTE: If v is given id may be any simply indexed object (e.g. any list or |
---|
| 820 | string); if v[i]<0 and i<=size(id) v[i] is set internally to i; |
---|
[3d124a7] | 821 | entries of v must be pairwise distinct to get a permutation if id. |
---|
| 822 | Zero generators of ideal/module are deleted |
---|
| 823 | EXAMPLE: example sort; shows an example |
---|
[d2b2a7] | 824 | " |
---|
[c860e9] | 825 | { int ii,jj,s,n = 0,0,1,0; |
---|
[3d124a7] | 826 | intvec v; |
---|
| 827 | if ( defined(basering) ) { def P = basering; } |
---|
[b5726c] | 828 | if ( size(#)==0 and (typeof(id)=="ideal" or typeof(id)=="module" |
---|
| 829 | or typeof(id)=="matrix")) |
---|
[3d124a7] | 830 | { |
---|
| 831 | id = simplify(id,2); |
---|
| 832 | for ( ii=1; ii<size(id); ii++ ) |
---|
| 833 | { |
---|
| 834 | if ( id[ii]!=id[ii+1] ) { break;} |
---|
| 835 | } |
---|
| 836 | if ( ii != size(id) ) { v = sortvec(id); } |
---|
| 837 | else { v = size(id)..1; } |
---|
| 838 | } |
---|
[b5726c] | 839 | if ( size(#)>=1 and (typeof(id)=="ideal" or typeof(id)=="module" |
---|
| 840 | or typeof(id)=="matrix") ) |
---|
[3d124a7] | 841 | { |
---|
| 842 | if ( typeof(#[1])=="string" ) |
---|
| 843 | { |
---|
[034ce1] | 844 | execute("ring r1 =("+charstr(P)+"),("+varstr(P)+"),("+#[1]+");"); |
---|
[3d124a7] | 845 | def i = imap(P,id); |
---|
| 846 | v = sortvec(i); |
---|
| 847 | setring P; |
---|
| 848 | n=2; |
---|
| 849 | } |
---|
| 850 | } |
---|
| 851 | if ( typeof(id)=="intvec" or typeof(id)=="list" and n==0 ) |
---|
| 852 | { |
---|
| 853 | string o; |
---|
| 854 | if ( size(#)==0 ) { o = "lp"; n=1; } |
---|
| 855 | if ( size(#)>=1 ) |
---|
| 856 | { |
---|
| 857 | if ( typeof(#[1])=="string" ) { o = #[1]; n=1; } |
---|
| 858 | } |
---|
| 859 | } |
---|
| 860 | if ( typeof(id)=="intvec" or typeof(id)=="list" and n==1 ) |
---|
| 861 | { |
---|
| 862 | if ( typeof(id)=="list" ) |
---|
| 863 | { |
---|
| 864 | for (ii=1; ii<=size(id); ii++) |
---|
| 865 | { |
---|
| 866 | if (typeof(id[ii]) != "intvec" and typeof(id[ii]) != "int") |
---|
| 867 | { "// list elements must be intvec/int"; return(); } |
---|
| 868 | else |
---|
| 869 | { s=size(id[ii])*(s < size(id[ii])) + s*(s >= size(id[ii])); } |
---|
| 870 | } |
---|
| 871 | } |
---|
[034ce1] | 872 | execute("ring r=0,x(1..s),("+o+");"); |
---|
[3d124a7] | 873 | ideal i; |
---|
| 874 | poly f; |
---|
| 875 | for (ii=1; ii<=size(id); ii++) |
---|
| 876 | { |
---|
| 877 | f=1; |
---|
| 878 | for (jj=1; jj<=size(id[ii]); jj++) |
---|
| 879 | { |
---|
| 880 | f=f*x(jj)^(id[ii])[jj]; |
---|
| 881 | } |
---|
| 882 | i[ii]=f; |
---|
| 883 | } |
---|
| 884 | v = sort(i)[2]; |
---|
| 885 | } |
---|
| 886 | if ( size(#)!=0 and n==0 ) { v = #[1]; } |
---|
| 887 | if( size(#)==2 ) |
---|
| 888 | { |
---|
| 889 | if ( #[2] != 0 ) { v = v[size(v)..1]; } |
---|
| 890 | } |
---|
| 891 | s = size(v); |
---|
[63be42] | 892 | if( size(id) < s ) { s = size(id); } |
---|
[3d124a7] | 893 | def m = id; |
---|
[63be42] | 894 | if ( size(m) != 0 ) |
---|
| 895 | { |
---|
| 896 | for ( jj=1; jj<=s; jj=jj+1) |
---|
| 897 | { |
---|
| 898 | if ( v[jj]<=0 ) { v[jj]=jj; } |
---|
| 899 | m[jj] = id[v[jj]]; |
---|
| 900 | } |
---|
| 901 | } |
---|
| 902 | if ( v == 0 ) { v = 1; } |
---|
[3d124a7] | 903 | list L=m,v; |
---|
| 904 | return(L); |
---|
| 905 | } |
---|
| 906 | example |
---|
| 907 | { "EXAMPLE:"; echo = 2; |
---|
[c860e9] | 908 | ring r0 = 0,(x,y,z,t),lp; |
---|
| 909 | ideal i = x3,z3,xyz; |
---|
[584f84d] | 910 | sort(i); //sorts using lex ordering, smaller polys come first |
---|
[65546eb] | 911 | |
---|
[c860e9] | 912 | sort(i,3..1); |
---|
[b42ab6] | 913 | |
---|
[584f84d] | 914 | sort(i,"ls")[1]; //sort w.r.t. negative lex ordering |
---|
[b42ab6] | 915 | |
---|
| 916 | intvec v =1,10..5,2..4;v; |
---|
[584f84d] | 917 | sort(v)[1]; // sort v lexicographically |
---|
[b42ab6] | 918 | |
---|
[584f84d] | 919 | sort(v,"Dp",1)[1]; // sort v w.r.t (total sum, reverse lex) |
---|
[3d124a7] | 920 | } |
---|
| 921 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 922 | proc sum (id, list #) |
---|
[b42ab6] | 923 | "USAGE: sum(id[,v]); id ideal/vector/module/matrix/intvec/intmat/list, |
---|
| 924 | v intvec (default: v=1..number of entries of id) |
---|
| 925 | ASSUME: list members can be added. |
---|
[65546eb] | 926 | RETURN: The sum of all entries of id [with index given by v] of type |
---|
[b42ab6] | 927 | depending on the entries of id. |
---|
[7708934] | 928 | NOTE: If id is not a list, id is treated as a list of polys resp. integers. |
---|
[b42ab6] | 929 | A module m is identified with the corresponding matrix M (columns |
---|
| 930 | of M generate m). |
---|
[7708934] | 931 | @* If v is outside the range of id, we have the empty sum and the |
---|
| 932 | result will be 0 (of type int). |
---|
[3d124a7] | 933 | EXAMPLE: example sum; shows an example |
---|
[d2b2a7] | 934 | " |
---|
[3d124a7] | 935 | { |
---|
[7708934] | 936 | //-------------------- initialization and special feature --------------------- |
---|
[b42ab6] | 937 | int n,j,tt; |
---|
[7708934] | 938 | string ty; // will become type of id |
---|
[b42ab6] | 939 | list l; |
---|
[7708934] | 940 | |
---|
| 941 | // We wish to allow something like sum(x(1..10)) if x(1),...,x(10) are |
---|
[65546eb] | 942 | // variables. x(1..10) is a list of polys and enters the procedure with |
---|
[7708934] | 943 | // id=x(1) and # a list with 9 polys, #[1]= x(2),...,#[9]= x(10). Hence, in |
---|
| 944 | // this case # is never empty. If an additional intvec v is given, |
---|
| 945 | // it is added to #, so we have to separate it first and make |
---|
| 946 | // the rest a list which has to be added. |
---|
| 947 | |
---|
[b42ab6] | 948 | int s = size(#); |
---|
| 949 | if( s!=0 ) |
---|
[7708934] | 950 | { if ( typeof(#[s])=="intvec" or typeof(#[s])=="int") |
---|
[b42ab6] | 951 | { intvec v = #[s]; |
---|
[65546eb] | 952 | tt=1; |
---|
[7708934] | 953 | s=s-1; |
---|
[b42ab6] | 954 | if ( s>0 ) { # = #[1..s]; } |
---|
| 955 | } |
---|
| 956 | } |
---|
| 957 | if ( s>0 ) |
---|
| 958 | { |
---|
[7708934] | 959 | l = list(id)+#; |
---|
| 960 | kill id; |
---|
| 961 | list id = l; //case: id = list |
---|
| 962 | ty = "list"; |
---|
[b42ab6] | 963 | } |
---|
| 964 | else |
---|
[65546eb] | 965 | { |
---|
[7708934] | 966 | ty = typeof(id); |
---|
[b42ab6] | 967 | } |
---|
[7708934] | 968 | //------------------------------ reduce to 3 cases --------------------------- |
---|
[b42ab6] | 969 | if( ty=="poly" or ty=="ideal" or ty=="vector" |
---|
| 970 | or ty=="module" or ty=="matrix" ) |
---|
[7708934] | 971 | { //case: id = ideal |
---|
[3d124a7] | 972 | ideal i = ideal(matrix(id)); |
---|
[b42ab6] | 973 | kill id; |
---|
[7708934] | 974 | ideal id = simplify(i,2); //delete 0 entries |
---|
[3d124a7] | 975 | } |
---|
[b42ab6] | 976 | if( ty=="int" or ty=="intvec" or ty=="intmat" ) |
---|
[3d124a7] | 977 | { |
---|
[b42ab6] | 978 | if ( ty == "int" ) { intmat S =id; } |
---|
[3d124a7] | 979 | else { intmat S = intmat(id); } |
---|
| 980 | intvec i = S[1..nrows(S),1..ncols(S)]; |
---|
[b42ab6] | 981 | kill id; |
---|
[7708934] | 982 | intvec id = i; //case: id = intvec |
---|
[3d124a7] | 983 | } |
---|
[7708934] | 984 | //------------------- consider intvec v and empty sum ----------------------- |
---|
[65546eb] | 985 | if( tt!=0 ) |
---|
[7708934] | 986 | { |
---|
| 987 | for (j=1; j<=size(v); j++) |
---|
| 988 | { |
---|
| 989 | if ( v[j] <= 0 or v[j] > size(id) ) //v outside range of id |
---|
[65546eb] | 990 | { |
---|
[7708934] | 991 | return(0); //empty sum is 0 |
---|
[65546eb] | 992 | } |
---|
[7708934] | 993 | } |
---|
| 994 | id = id[v]; //consider part of id |
---|
| 995 | } //corresponding to v |
---|
| 996 | |
---|
| 997 | //-------------------------- finally, add objects --------------------------- |
---|
[65546eb] | 998 | n = size(id); |
---|
[7708934] | 999 | def f(1) = id[1]; |
---|
[b42ab6] | 1000 | for( j=2; j<=n; j=j+1 ) { def f(j)=f(j-1)+id[j]; } |
---|
| 1001 | return(f(n)); int n,j,tt; |
---|
| 1002 | } |
---|
[3d124a7] | 1003 | example |
---|
| 1004 | { "EXAMPLE:"; echo = 2; |
---|
| 1005 | ring r= 0,(x,y,z),dp; |
---|
| 1006 | vector pv = [xy,xz,yz,x2,y2,z2]; |
---|
| 1007 | sum(pv); |
---|
[c860e9] | 1008 | sum(pv,2..5); |
---|
| 1009 | matrix M[2][3] = 1,x,2,y,3,z; |
---|
| 1010 | intvec w=2,4,6; |
---|
| 1011 | sum(M,w); |
---|
| 1012 | intvec iv = 1,2,3,4,5,6,7,8,9; |
---|
| 1013 | sum(iv,2..4); |
---|
[3d124a7] | 1014 | } |
---|
| 1015 | /////////////////////////////////////////////////////////////////////////////// |
---|
[6f2edc] | 1016 | |
---|
| 1017 | proc which (command) |
---|
[d2b2a7] | 1018 | "USAGE: which(command); command = string expression |
---|
[6f2edc] | 1019 | RETURN: Absolute pathname of command, if found in search path. |
---|
| 1020 | Empty string, otherwise. |
---|
| 1021 | NOTE: Based on the Unix command 'which'. |
---|
| 1022 | EXAMPLE: example which; shows an example |
---|
[d2b2a7] | 1023 | " |
---|
[6f2edc] | 1024 | { |
---|
| 1025 | int rs; |
---|
| 1026 | int i; |
---|
[a70441f] | 1027 | string fn = "which_" + string(system("pid")); |
---|
[6f2edc] | 1028 | string pn; |
---|
[a70441f] | 1029 | string cmd; |
---|
[82716e] | 1030 | if( typeof(command) != "string") |
---|
[6f2edc] | 1031 | { |
---|
[82716e] | 1032 | return (pn); |
---|
[6f2edc] | 1033 | } |
---|
[a70441f] | 1034 | if (system("uname") != "ix86-Win") |
---|
| 1035 | { |
---|
| 1036 | cmd = "which "; |
---|
| 1037 | } |
---|
| 1038 | else |
---|
| 1039 | { |
---|
| 1040 | // unfortunately, it does not take -path |
---|
| 1041 | cmd = "type "; |
---|
| 1042 | } |
---|
| 1043 | i = system("sh", cmd + command + " > " + fn); |
---|
[6f2edc] | 1044 | pn = read(fn); |
---|
[a70441f] | 1045 | if (system("uname") != "ix86-Win") |
---|
| 1046 | { |
---|
| 1047 | // TBC: Hmm... should parse output to get rid of 'command is ' |
---|
| 1048 | pn[size(pn)] = ""; |
---|
| 1049 | i = 1; |
---|
| 1050 | while ((pn[i] != " ") and (pn[i] != "")) |
---|
| 1051 | { |
---|
| 1052 | i = i+1; |
---|
| 1053 | } |
---|
| 1054 | if (pn[i] == " ") {pn[i] = "";} |
---|
| 1055 | rs = system("sh", "ls " + pn + " > " + fn + " 2>&1 "); |
---|
| 1056 | } |
---|
| 1057 | else |
---|
[6f2edc] | 1058 | { |
---|
[a70441f] | 1059 | rs = 0; |
---|
[6f2edc] | 1060 | } |
---|
| 1061 | i = system("sh", "rm " + fn); |
---|
| 1062 | if (rs == 0) {return (pn);} |
---|
[82716e] | 1063 | else |
---|
[6f2edc] | 1064 | { |
---|
| 1065 | print (command + " not found "); |
---|
| 1066 | return (""); |
---|
| 1067 | } |
---|
| 1068 | } |
---|
| 1069 | example |
---|
| 1070 | { "EXAMPLE:"; echo = 2; |
---|
[a70441f] | 1071 | which("sh"); |
---|
[6f2edc] | 1072 | } |
---|
| 1073 | /////////////////////////////////////////////////////////////////////////////// |
---|
[ebbe4a] | 1074 | |
---|
| 1075 | proc watchdog(int i, string cmd) |
---|
[b42ab6] | 1076 | "USAGE: watchdog(i,cmd); i integer; cmd string |
---|
| 1077 | RETURN: Result of cmd, if the result can be computed in i seconds. |
---|
| 1078 | Otherwise the computation is interrupted after i seconds, |
---|
| 1079 | the string "Killed" is returned and the global variable |
---|
| 1080 | 'watchdog_interrupt' is defined. |
---|
[65546eb] | 1081 | NOTE: * the MP package must be enabled |
---|
| 1082 | * the current basering should not be watchdog_rneu, since |
---|
[b42ab6] | 1083 | watchdog_rneu will be killed |
---|
[ebbe4a] | 1084 | * if there are variable names of the structure x(i) all |
---|
| 1085 | polynomials have to be put into eval(...) in order to be |
---|
| 1086 | interpreted correctly |
---|
| 1087 | * a second Singular process is started by this procedure |
---|
| 1088 | EXAMPLE: example watchdog; shows an example |
---|
| 1089 | " |
---|
| 1090 | { |
---|
| 1091 | string rname=nameof(basering); |
---|
| 1092 | if (defined(watchdog_rneu)) |
---|
| 1093 | { |
---|
| 1094 | kill watchdog_rneu; |
---|
| 1095 | } |
---|
| 1096 | // If we do not have MP-links, watchdog cannot be used |
---|
| 1097 | if (system("with","MP")) |
---|
| 1098 | { |
---|
| 1099 | if ( i > 0 ) |
---|
| 1100 | { |
---|
| 1101 | int j=10; |
---|
| 1102 | int k=999999; |
---|
[65546eb] | 1103 | // fork, get the pid of the child and send it the command |
---|
[ebbe4a] | 1104 | link l_fork="MPtcp:fork"; |
---|
| 1105 | open(l_fork); |
---|
| 1106 | write(l_fork,quote(system("pid"))); |
---|
| 1107 | int pid=read(l_fork); |
---|
| 1108 | execute("write(l_fork,quote(" + cmd + "));"); |
---|
| 1109 | |
---|
| 1110 | |
---|
| 1111 | // sleep in small, but growing intervals for appr. 1 second |
---|
| 1112 | while(j < k) |
---|
| 1113 | { |
---|
| 1114 | if (status(l_fork, "read", "ready", j)) {break;} |
---|
| 1115 | j = j + j; |
---|
| 1116 | } |
---|
| 1117 | |
---|
| 1118 | // sleep in intervals of one second |
---|
| 1119 | j = 1; |
---|
| 1120 | if (!status(l_fork,"read","ready")) |
---|
| 1121 | { |
---|
| 1122 | while (j < i) |
---|
| 1123 | { |
---|
| 1124 | if (status(l_fork, "read", "ready", k)) {break;} |
---|
| 1125 | j = j + 1; |
---|
| 1126 | } |
---|
| 1127 | } |
---|
| 1128 | // check, whether we have a result, and return it |
---|
| 1129 | if (status(l_fork, "read", "ready")) |
---|
| 1130 | { |
---|
| 1131 | def result = read(l_fork); |
---|
| 1132 | if (nameof(basering)!=rname) |
---|
| 1133 | { |
---|
| 1134 | def watchdog_rneu=basering; |
---|
| 1135 | } |
---|
| 1136 | if(defined(watchdog_interrupt)) |
---|
| 1137 | { |
---|
| 1138 | kill (watchdog_interrupt); |
---|
| 1139 | } |
---|
| 1140 | close(l_fork); |
---|
| 1141 | } |
---|
| 1142 | else |
---|
| 1143 | { |
---|
| 1144 | string result="Killed"; |
---|
| 1145 | if(!defined(watchdog_interrupt)) |
---|
| 1146 | { |
---|
| 1147 | int watchdog_interrupt=1; |
---|
| 1148 | export watchdog_interrupt; |
---|
| 1149 | } |
---|
| 1150 | close(l_fork); |
---|
| 1151 | j = system("sh","kill " + string(pid)); |
---|
| 1152 | } |
---|
| 1153 | if (defined(watchdog_rneu)) |
---|
| 1154 | { |
---|
| 1155 | keepring watchdog_rneu; |
---|
| 1156 | } |
---|
| 1157 | return(result); |
---|
| 1158 | } |
---|
| 1159 | else |
---|
| 1160 | { |
---|
| 1161 | ERROR("First argument of watchdog has to be a positive integer."); |
---|
| 1162 | } |
---|
[50cbdc] | 1163 | } |
---|
| 1164 | else |
---|
| 1165 | { |
---|
[ebbe4a] | 1166 | ERROR("MP-support is not enabled in this version of Singular."); |
---|
[65546eb] | 1167 | } |
---|
[ebbe4a] | 1168 | } |
---|
| 1169 | example |
---|
| 1170 | { "EXAMPLE:"; echo=2; |
---|
| 1171 | ring r=0,(x,y,z),dp; |
---|
| 1172 | poly f=x^30+y^30; |
---|
| 1173 | watchdog(1,"factorize(eval("+string(f)+"))"); |
---|
| 1174 | watchdog(100,"factorize(eval("+string(f)+"))"); |
---|
| 1175 | } |
---|
| 1176 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 1177 | |
---|
| 1178 | proc deleteSublist(intvec v,list l) |
---|
[803c5a1] | 1179 | "USAGE: deleteSublist(v,l); intvec v; list l |
---|
[ebbe4a] | 1180 | where the entries of the integer vector v correspond to the |
---|
| 1181 | positions of the elements to be deleted |
---|
| 1182 | RETURN: list without the deleted elements |
---|
| 1183 | EXAMPLE: example deleteSublist; shows an example" |
---|
| 1184 | { |
---|
| 1185 | list k; |
---|
| 1186 | int i,j,skip; |
---|
| 1187 | j=1; |
---|
| 1188 | skip=0; |
---|
| 1189 | intvec vs=sort(v)[1]; |
---|
| 1190 | for ( i=1 ; i <=size(vs) ; i++) |
---|
| 1191 | { |
---|
| 1192 | while ((j+skip) < vs[i]) |
---|
| 1193 | { |
---|
| 1194 | k[j] = l[j+skip]; |
---|
| 1195 | j++; |
---|
| 1196 | } |
---|
| 1197 | skip++; |
---|
| 1198 | } |
---|
| 1199 | if(vs[size(vs)]<size(l)) |
---|
| 1200 | { |
---|
| 1201 | k=k+list(l[(vs[size(vs)]+1)..size(l)]); |
---|
| 1202 | } |
---|
| 1203 | return(k); |
---|
| 1204 | } |
---|
| 1205 | example |
---|
| 1206 | { "EXAMPLE:"; echo=2; |
---|
| 1207 | list l=1,2,3,4,5; |
---|
| 1208 | intvec v=1,3,4; |
---|
| 1209 | l=deleteSublist(v,l); |
---|
| 1210 | l; |
---|
| 1211 | } |
---|
| 1212 | /////////////////////////////////////////////////////////////////////////////// |
---|