source: git/Singular/LIB/general.lib @ ca41246

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