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