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