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