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