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