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

spielwiese
Last change on this file since 176954 was 298d0a, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes: namespace fixes(killall) git-svn-id: file:///usr/local/Singular/svn/trunk@7399 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 43.3 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.44 2004-08-13 14:09:21 Singular 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") and
498              ((typeof(`@marie[@joni]`)=="package") or
499              (typeof(`@marie[@joni]`)=="proc")))
500          {
501            if (defined(`@marie[@joni]`)) {kill `@marie[@joni]`;}
502          }
503          if (!defined(@joni)) break;
504        }
505        if ((system("with","Namespaces")) && defined(General))
506        {
507         
508          @marie=names(General);
509          for ( @joni=size(@marie); @joni>0; @joni-- )
510          {
511            if(@marie[@joni]!="killall"
512            and typeof(`@marie[@joni]`)=="proc")
513            { kill General::`@marie[@joni]`; }
514          }
515          kill General::killall;
516        }
517      }
518      else
519      {
520        // other types
521        for ( @joni=size(@marie); @joni>2; @joni-- )
522        {
523          if(typeof(`@marie[@joni]`)==#[1] and @marie[@joni]!="LIB"
524             and typeof(`@marie[@joni]`)!="proc")
525          { kill `@marie[@joni]`; }
526        }
527      }
528    }
529    else
530    {
531      // kills all user-defined variables whose name or type is not #i
532      for ( @joni=size(@marie); @joni>2; @joni-- )
533      {
534        if ( @marie[@joni] != "LIB" && @marie[@joni] != "Top"
535             && typeof(`@marie[@joni]`) != "proc")
536        {
537          no_kill = 0;
538          for (j=2; j<= size(#); j++)
539          {
540            if (typeof(`@marie[@joni]`)==#[j] or @marie[@joni] == #[j])
541            {
542              no_kill = 1;
543              break;
544            }
545          }
546          if (! no_kill)
547          {
548            kill `@marie[@joni]`;
549          }
550        }
551        if (!defined(@joni)) break;
552      }
553    }
554  }
555}
556example
557{ "EXAMPLE:"; echo = 2;
558   ring rtest; ideal i=x,y,z; string str="hi"; int j = 3;
559   export rtest,i,str,j;       //this makes the local variables global
560   listvar();
561   killall("ring");            // kills all rings
562   listvar();
563   killall("not", "int");      // kills all variables except int's (and procs)
564   listvar();
565   killall();                  // kills all vars except loaded procs
566   listvar();
567}
568///////////////////////////////////////////////////////////////////////////////
569
570proc number_e (int n)
571"USAGE:   number_e(n);  n integer
572RETURN:  Euler number e=exp(1) up to n decimal digits (no rounding)
573@*       - of type string if no basering of char 0 is defined
574@*       - of type number if a basering of char 0 is defined
575DISPLAY: decimal format of e if printlevel > 0 (default:printlevel=0 )
576NOTE:    procedure uses algorithm of A.H.J. Sale
577EXAMPLE: example number_e; shows an example
578"
579{
580   int i,m,s,t;
581   intvec u,e;
582   u[n+2]=0; e[n+1]=0; e=e+1;
583   if( defined(basering) )
584   {
585      if( char(basering)==0 ) { number r=2; t=1; }
586   }
587   string result = "2.";
588   for( i=1; i<=n+1; i=i+1 )
589   {
590      e = e*10;
591      for( m=n+1; m>=1; m=m-1 )
592      {
593         s    = e[m]+u[m+1];
594         u[m] = s div (m+1);
595         e[m] = s%(m+1);
596      }
597      result = result+string(u[1]);
598      if( t==1 ) { r = r+number(u[1])/number(10)^i; }
599   }
600   if( t==1 )
601   { dbprint(printlevel-voice+2,"// "+result[1,n+1]);
602     return(r);
603   }
604   return(result[1,n+1]);
605}
606example
607{ "EXAMPLE:"; echo = 2;
608   number_e(30);"";
609   ring R = 0,t,lp;
610   number e = number_e(30);
611   e;
612}
613///////////////////////////////////////////////////////////////////////////////
614
615proc number_pi (int n)
616"USAGE:   number_pi(n);  n positive integer
617RETURN:  pi (area of unit circle) up to n decimal digits (no rounding)
618@*       - of type string if no basering of char 0 is defined,
619@*       - of type number, if a basering of char 0 is defined
620DISPLAY: decimal format of pi if printlevel > 0 (default:printlevel=0 )
621NOTE:    procedure uses algorithm of S. Rabinowitz
622EXAMPLE: example number_pi; shows an example
623"
624{
625   int i,m,t,e,q,N;
626   intvec r,p,B,Prelim;
627   string result,prelim;
628   N = (10*n) div 3 + 2;
629   p[N+1]=0; p=p+2; r=p;
630   for( i=1; i<=N+1; i=i+1 ) { B[i]=2*i-1; }
631   if( defined(basering) )
632   {
633      if( char(basering)==0 ) { number pi; number pri; t=1; }
634   }
635   for( i=0; i<=n; i=i+1 )
636   {
637      p = r*10;
638      e = p[N+1];
639      for( m=N+1; m>=2; m=m-1 )
640      {
641         r[m] = e%B[m];
642         q    = e div B[m];
643         e    = q*(m-1)+p[m-1];
644      }
645      r[1] = e%10;
646      q    = e div 10;
647      if( q!=10 and q!=9 )
648      {
649         result = result+prelim;
650         Prelim = q;
651         prelim = string(q);
652      }
653      if( q==9 )
654      {
655         Prelim = Prelim,9;
656         prelim = prelim+"9";
657      }
658      if( q==10 )
659      {
660         Prelim = (Prelim+1)-((Prelim+1) div 10)*10;
661         for( m=size(Prelim); m>0; m=m-1)
662         {
663            prelim[m] = string(Prelim[m]);
664         }
665         result = result+prelim;
666         if( t==1 ) { pi=pi+pri; }
667         Prelim = 0;
668         prelim = "0";
669      }
670      if( t==1 ) { pi=pi+number(q)/number(10)^i; }
671   }
672   result = result,prelim[1];
673   result = "3."+result[2,n-1];
674   if( t==1 )
675   { dbprint(printlevel-voice+2,"// "+result);
676     return(pi);
677   }
678   return(result);
679}
680example
681{ "EXAMPLE:"; echo = 2;
682   number_pi(11);"";
683   ring r = (real,10),t,dp;
684   number pi = number_pi(11); pi;
685}
686///////////////////////////////////////////////////////////////////////////////
687
688proc primes (int n, int m)
689"USAGE:   primes(n,m);  n,m integers
690RETURN:  intvec, consisting of all primes p, prime(n)<=p<=m, in increasing
691         order if n<=m, resp. prime(m)<=p<=n, in decreasing order if m<n.
692NOTE:    prime(n); returns the biggest prime number <= min(n,32003)
693         if n>=2, else 2
694EXAMPLE: example primes; shows an example
695"
696{  int change;
697   if ( n>m ) { change=n; n=m ; m=change; change=1; }
698   int q,p = prime(m),prime(n); intvec v = q; q = q-1;
699   while ( q>=p ) { q = prime(q); v = q,v; q = q-1; }
700   if ( change==1 ) { v = v[size(v)..1]; }
701   return(v);
702}
703example
704{  "EXAMPLE:"; echo = 2;
705    primes(50,100);"";
706    intvec v = primes(37,1); v;
707}
708///////////////////////////////////////////////////////////////////////////////
709
710proc product (id, list #)
711"USAGE:    product(id[,v]); id ideal/vector/module/matrix/intvec/intmat/list,
712          v intvec  (default: v=1..number of entries of id)
713ASSUME:   list members can be multiplied.
714RETURN:   The product of all entries of id [with index given by v] of type
715          depending on the entries of id.
716NOTE:     If id is not a list, id is treated as a list of polys resp. integers.
717          A module m is identified with the corresponding matrix M (columns
718          of M generate m).
719@*        If v is outside the range of id, we have the empty product and the
720          result will be 1 (of type int).
721EXAMPLE:  example product; shows an example
722"
723{
724//-------------------- initialization and special feature ---------------------
725   int n,j,tt;
726   string ty;                                //will become type of id
727   list l;
728
729// We wish to allow something like product(x(1..10)) if x(1),...,x(10) are
730// variables. x(1..10) is a list of polys and enters the procedure with
731// id=x(1) and # a list with 9 polys, #[1]= x(2),...,#[9]= x(10). Hence, in
732// this case # is never empty. If an additional intvec v is given,
733// it is added to #, so we have to separate it first and make
734// the rest a list which has to be multiplied.
735
736   int s = size(#);
737   if( s!=0 )
738   {  if ( typeof(#[s])=="intvec" or typeof(#[s])=="int")
739      {
740         intvec v = #[s];
741         tt=1;
742         s=s-1;
743         if ( s>0 ) { # = #[1..s]; }
744      }
745   }
746   if ( s>0 )
747   {
748      l = list(id)+#;
749      kill id;
750      list id = l;                                    //case: id = list
751      ty = "list";
752      n = size(id);
753   }
754   else
755   {
756      ty = typeof(id);
757      if( ty == "list" )
758      { n = size(id); }
759   }
760//------------------------------ reduce to 3 cases ---------------------------
761   if( ty=="poly" or ty=="ideal" or ty=="vector"
762       or ty=="module" or ty=="matrix" )
763   {
764      ideal i = ideal(matrix(id));
765      kill id;
766      ideal id = i;                                   //case: id = ideal
767      n = ncols(id);
768   }
769   if( ty=="int" or ty=="intvec" or ty=="intmat" )
770   {
771      if ( ty == "int" ) { intmat S =id; }
772      else { intmat S = intmat(id); }
773      intvec i = S[1..nrows(S),1..ncols(S)];
774      kill id;
775      intvec id = i;                                  //case: id = intvec
776      n = size(id);
777   }
778//--------------- consider intvec v and empty product  -----------------------
779   if( tt!=0 )
780   {
781      for (j=1; j<=size(v); j++)
782      {
783         if ( v[j] <= 0 or v[j] > n )                 //v outside range of id
784         {
785            return(1);                                //empty product is 1
786         }
787      }
788      id = id[v];                                     //consider part of id
789   }                                                  //corresponding to v
790//--------------------- special case: one factor is zero  ---------------------
791   if ( typeof(id) == "ideal")
792   {
793      if( size(id) < ncols(id) )
794      {
795          poly f; return(f);
796      }
797   }
798//-------------------------- finally, multiply objects  -----------------------
799   n = size(id);
800   def f(1) = id[1];
801   for( j=2; j<=n; j=j+1 ) { def f(j)=f(j-1)*id[j]; }
802   return(f(n));
803}
804example
805{  "EXAMPLE:"; echo = 2;
806   ring r= 0,(x,y,z),dp;
807   ideal m = maxideal(1);
808   product(m);
809   product(m[2..3]);
810   matrix M[2][3] = 1,x,2,y,3,z;
811   product(M);
812   intvec v=2,4,6;
813   product(M,v);
814   intvec iv = 1,2,3,4,5,6,7,8,9;
815   v=1..5,7,9;
816   product(iv,v);
817   intmat A[2][3] = 1,1,1,2,2,2;
818   product(A,3..5);
819}
820///////////////////////////////////////////////////////////////////////////////
821
822proc sort (id, list #)
823"USAGE:   sort(id[v,o,n]); id = ideal/module/intvec/list(of intvec's or int's)
824@*       sort may be called with 1, 2 or 3 arguments in the following way:
825@*       sort(id[v,n]); v=intvec of positive integers, n=integer,
826@*       sort(id[o,n]); o=string (any allowed ordstr of a ring), n=integer
827RETURN:  a list l of two elements:
828@format
829        l[1]: object of same type as input but sorted in the following way:
830           - if id=ideal/module: generators of id are sorted w.r.t. intvec v
831             (id[v[1]] becomes 1-st, id[v[2]]  2-nd element, etc.). If no v is
832             present, id is sorted w.r.t. ordering o (if o is given) or w.r.t.
833             actual monomial ordering (if no o is given):
834             NOTE: generators with SMALLER(!) leading term come FIRST
835             (e.g. sort(id); sorts backwards to actual monomial ordering)
836           - if id=list of intvec's or int's: consider a list element, say
837             id[1]=3,2,5, as exponent vector of the monomial x^3*y^2*z^5;
838             the corresponding monomials are ordered w.r.t. intvec v (s.a.).
839             If no v is present, the monomials are sorted w.r.t. ordering o
840             (if o is given) or w.r.t. lexicographical ordering (if no o is
841             given). The corresponding ordered list of exponent vectors is
842             returned.
843             (e.g. sort(id); sorts lexicographically, smaller int's come first)
844             WARNING: Since negative exponents create the 0 polynomial in
845             Singular, id should not contain negative integers: the result
846             might not be as expected
847           - if id=intvec: id is treated as list of integers
848           - if n!=0 the ordering is inverse, i.e. w.r.t. v(size(v)..1)
849             default: n=0
850         l[2]: intvec, describing the permutation of the input (hence l[2]=v
851             if v is given (with positive integers))
852@end format
853NOTE:    If v is given id may be any simply indexed object (e.g. any list or
854         string); if v[i]<0 and i<=size(id) v[i] is set internally to i;
855         entries of v must be pairwise distinct to get a permutation if id.
856         Zero generators of ideal/module are deleted
857EXAMPLE: example sort; shows an example
858"
859{  int ii,jj,s,n = 0,0,1,0;
860   intvec v;
861   if ( defined(basering) ) { def P = basering; }
862   if ( size(#)==0 and (typeof(id)=="ideal" or typeof(id)=="module"
863                        or typeof(id)=="matrix"))
864   {
865      id = simplify(id,2);
866      for ( ii=1; ii<size(id); ii++ )
867      {
868         if ( id[ii]!=id[ii+1] ) { break;}
869      }
870      if ( ii != size(id) ) { v = sortvec(id); }
871      else  { v = size(id)..1; }
872   }
873   if ( size(#)>=1 and (typeof(id)=="ideal" or typeof(id)=="module"
874                        or typeof(id)=="matrix") )
875   {
876      if ( typeof(#[1])=="string" )
877      {
878         execute("ring r1 =("+charstr(P)+"),("+varstr(P)+"),("+#[1]+");");
879         def i = imap(P,id);
880         v = sortvec(i);
881         setring P;
882         n=2;
883      }
884   }
885   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==0 )
886   {
887      string o;
888      if ( size(#)==0 ) { o = "lp"; n=1; }
889      if ( size(#)>=1 )
890      {
891         if ( typeof(#[1])=="string" ) { o = #[1]; n=1; }
892      }
893   }
894   if ( typeof(id)=="intvec" or typeof(id)=="list" and n==1 )
895   {
896      if ( typeof(id)=="list" )
897      {
898         for (ii=1; ii<=size(id); ii++)
899         {
900            if (typeof(id[ii]) != "intvec" and typeof(id[ii]) != "int")
901               { "// list elements must be intvec/int"; return(); }
902            else
903               { s=size(id[ii])*(s < size(id[ii])) + s*(s >= size(id[ii])); }
904         }
905      }
906      execute("ring r=0,x(1..s),("+o+");");
907      ideal i;
908      poly f;
909      for (ii=1; ii<=size(id); ii++)
910      {
911         f=1;
912         for (jj=1; jj<=size(id[ii]); jj++)
913         {
914            f=f*x(jj)^(id[ii])[jj];
915         }
916         i[ii]=f;
917      }
918      v = sort(i)[2];
919   }
920   if ( size(#)!=0 and n==0 ) { v = #[1]; }
921   if( size(#)==2 )
922   {
923      if ( #[2] != 0 ) { v = v[size(v)..1]; }
924   }
925   s = size(v);
926   if( size(id) < s ) { s = size(id); }
927   def m = id;
928   if ( size(m) != 0 )
929   {
930      for ( jj=1; jj<=s; jj=jj+1)
931      {
932         if ( v[jj]<=0 ) { v[jj]=jj; }
933         m[jj] = id[v[jj]];
934      }
935   }
936   if ( v == 0 ) { v = 1; }
937   list L=m,v;
938   return(L);
939}
940example
941{  "EXAMPLE:"; echo = 2;
942   ring r0 = 0,(x,y,z,t),lp;
943   ideal i = x3,z3,xyz;
944   sort(i);            //sorts using lex ordering, smaller polys come first
945
946   sort(i,3..1);
947
948   sort(i,"ls")[1];     //sort w.r.t. negative lex ordering
949
950   intvec v =1,10..5,2..4;v;
951   sort(v)[1];          // sort v lexicographically
952
953   sort(v,"Dp",1)[1];   // sort v w.r.t (total sum, reverse lex)
954}
955///////////////////////////////////////////////////////////////////////////////
956proc sum (id, list #)
957"USAGE:    sum(id[,v]); id ideal/vector/module/matrix/intvec/intmat/list,
958          v intvec  (default: v=1..number of entries of id)
959ASSUME:   list members can be added.
960RETURN:   The sum of all entries of id [with index given by v] of type
961          depending on the entries of id.
962NOTE:     If id is not a list, id is treated as a list of polys resp. integers.
963          A module m is identified with the corresponding matrix M (columns
964          of M generate m).
965@*        If v is outside the range of id, we have the empty sum and the
966          result will be 0 (of type int).
967EXAMPLE:  example sum; shows an example
968"
969{
970//-------------------- initialization and special feature ---------------------
971   int n,j,tt;
972   string ty;                                  // will become type of id
973   list l;
974
975// We wish to allow something like sum(x(1..10)) if x(1),...,x(10) are
976// variables. x(1..10) is a list of polys and enters the procedure with
977// id=x(1) and # a list with 9 polys, #[1]= x(2),...,#[9]= x(10). Hence, in
978// this case # is never empty. If an additional intvec v is given,
979// it is added to #, so we have to separate it first and make
980// the rest a list which has to be added.
981
982   int s = size(#);
983   if( s!=0 )
984   {  if ( typeof(#[s])=="intvec" or typeof(#[s])=="int")
985      {  intvec v = #[s];
986         tt=1;
987         s=s-1;
988         if ( s>0 ) { # = #[1..s]; }
989      }
990   }
991   if ( s>0 )
992   {
993      l = list(id)+#;
994      kill id;
995      list id = l;                                    //case: id = list
996      ty = "list";
997   }
998   else
999   {
1000      ty = typeof(id);
1001   }
1002//------------------------------ reduce to 3 cases ---------------------------
1003   if( ty=="poly" or ty=="ideal" or ty=="vector"
1004       or ty=="module" or ty=="matrix" )
1005   {                                                 //case: id = ideal
1006      ideal i = ideal(matrix(id));
1007      kill id;
1008      ideal id = simplify(i,2);                      //delete 0 entries
1009   }
1010   if( ty=="int" or ty=="intvec" or ty=="intmat" )
1011   {
1012      if ( ty == "int" ) { intmat S =id; }
1013      else { intmat S = intmat(id); }
1014      intvec i = S[1..nrows(S),1..ncols(S)];
1015      kill id;
1016      intvec id = i;                                 //case: id = intvec
1017   }
1018//------------------- consider intvec v and empty sum  -----------------------
1019   if( tt!=0 )
1020   {
1021      for (j=1; j<=size(v); j++)
1022      {
1023         if ( v[j] <= 0 or v[j] > size(id) )         //v outside range of id
1024         {
1025            return(0);                               //empty sum is 0
1026         }
1027      }
1028      id = id[v];                                    //consider part of id
1029   }                                                 //corresponding to v
1030
1031//-------------------------- finally, add objects  ---------------------------
1032   n = size(id);
1033   def f(1) = id[1];
1034   for( j=2; j<=n; j=j+1 ) { def f(j)=f(j-1)+id[j]; }
1035   return(f(n));   int n,j,tt;
1036}
1037example
1038{  "EXAMPLE:"; echo = 2;
1039   ring r= 0,(x,y,z),dp;
1040   vector pv = [xy,xz,yz,x2,y2,z2];
1041   sum(pv);
1042   sum(pv,2..5);
1043   matrix M[2][3] = 1,x,2,y,3,z;
1044   intvec w=2,4,6;
1045   sum(M,w);
1046   intvec iv = 1,2,3,4,5,6,7,8,9;
1047   sum(iv,2..4);
1048}
1049///////////////////////////////////////////////////////////////////////////////
1050
1051proc which (command)
1052"USAGE:    which(command); command = string expression
1053RETURN:   Absolute pathname of command, if found in search path.
1054          Empty string, otherwise.
1055NOTE:     Based on the Unix command 'which'.
1056EXAMPLE:  example which; shows an example
1057"
1058{
1059   int rs;
1060   int i;
1061   string fn = "which_" + string(system("pid"));
1062   string pn;
1063   string cmd;
1064   if( typeof(command) != "string")
1065   {
1066     return (pn);
1067   }
1068   if (system("uname") != "ix86-Win")
1069   {
1070     cmd = "which ";
1071   }
1072   else
1073   {
1074     // unfortunately, it does not take -path
1075     cmd = "type ";
1076   }
1077   i = system("sh", cmd + command + " > " + fn);
1078   pn = read(fn);
1079   if (system("uname") != "ix86-Win")
1080   {
1081     // TBC: Hmm... should parse output to get rid of 'command is '
1082     pn[size(pn)] = "";
1083     i = 1;
1084     while ((pn[i] != " ") and (pn[i] != ""))
1085     {
1086       i = i+1;
1087     }
1088     if (pn[i] == " ") {pn[i] = "";}
1089     rs = system("sh", "ls " + pn + " > " + fn + " 2>&1 ");
1090   }
1091   else
1092   {
1093     rs = 0;
1094   }
1095   i = system("sh", "rm " + fn);
1096   if (rs == 0) {return (pn);}
1097   else
1098   {
1099     print (command + " not found ");
1100     return ("");
1101   }
1102}
1103example
1104{  "EXAMPLE:"; echo = 2;
1105    which("sh");
1106}
1107///////////////////////////////////////////////////////////////////////////////
1108
1109proc watchdog(int i, string cmd)
1110"USAGE:   watchdog(i,cmd); i integer; cmd string
1111RETURN:  Result of cmd, if the result can be computed in i seconds.
1112         Otherwise the computation is interrupted after i seconds,
1113         the string "Killed" is returned and the global variable
1114         'watchdog_interrupt' is defined.
1115NOTE:    * the MP package must be enabled
1116         * the current basering should not be watchdog_rneu, since
1117           watchdog_rneu will be killed
1118         * if there are variable names of the structure x(i) all
1119           polynomials have to be put into eval(...) in order to be
1120           interpreted correctly
1121         * a second Singular process is started by this procedure
1122EXAMPLE: example watchdog; shows an example
1123"
1124{
1125  string rname=nameof(basering);
1126  if (defined(watchdog_rneu))
1127  {
1128    kill watchdog_rneu;
1129  }
1130// If we do not have MP-links, watchdog cannot be used
1131  if (system("with","MP"))
1132  {
1133    if ( i > 0 )
1134    {
1135      int j=10;
1136      int k=999999;
1137// fork, get the pid of the child and send it the command
1138      link l_fork="MPtcp:fork";
1139      open(l_fork);
1140      write(l_fork,quote(system("pid")));
1141      int pid=read(l_fork);
1142      execute("write(l_fork,quote(" + cmd + "));");
1143
1144
1145// sleep in small, but growing intervals for appr. 1 second
1146      while(j < k)
1147      {
1148        if (status(l_fork, "read", "ready", j)) {break;}
1149        j = j + j;
1150      }
1151
1152// sleep in intervals of one second
1153      j = 1;
1154      if (!status(l_fork,"read","ready"))
1155      {
1156        while (j < i)
1157        {
1158          if (status(l_fork, "read", "ready", k)) {break;}
1159          j = j + 1;
1160        }
1161      }
1162// check, whether we have a result, and return it
1163      if (status(l_fork, "read", "ready"))
1164      {
1165        def result = read(l_fork);
1166        if (nameof(basering)!=rname)
1167        {
1168          def watchdog_rneu=basering;
1169        }
1170        if(defined(watchdog_interrupt))
1171        {
1172          kill (watchdog_interrupt);
1173        }
1174        close(l_fork);
1175      }
1176      else
1177      {
1178        string result="Killed";
1179        if(!defined(watchdog_interrupt))
1180        {
1181          int watchdog_interrupt=1;
1182          export watchdog_interrupt;
1183        }
1184        close(l_fork);
1185        j = system("sh","kill " + string(pid));
1186      }
1187      if (defined(watchdog_rneu))
1188      {
1189        keepring watchdog_rneu;
1190      }
1191      return(result);
1192    }
1193    else
1194    {
1195      ERROR("First argument of watchdog has to be a positive integer.");
1196    }
1197  }
1198  else
1199  {
1200    ERROR("MP-support is not enabled in this version of Singular.");
1201  }
1202}
1203example
1204{ "EXAMPLE:"; echo=2;
1205  ring r=0,(x,y,z),dp;
1206  poly f=x^30+y^30;
1207  watchdog(1,"factorize(eval("+string(f)+"))");
1208  watchdog(100,"factorize(eval("+string(f)+"))");
1209}
1210///////////////////////////////////////////////////////////////////////////////
1211
1212proc deleteSublist(intvec v,list l)
1213"USAGE:   deleteSublist(v,l); intvec v; list l
1214         where the entries of the integer vector v correspond to the
1215         positions of the elements to be deleted
1216RETURN:  list without the deleted elements
1217EXAMPLE: example deleteSublist; shows an example"
1218{
1219  list k;
1220  int i,j,skip;
1221  j=1;
1222  skip=0;
1223  intvec vs=sort(v)[1];
1224  for ( i=1 ; i <=size(vs) ; i++)
1225  {
1226    while ((j+skip) < vs[i])
1227    {
1228      k[j] = l[j+skip];
1229      j++;
1230    }
1231    skip++;
1232  }
1233  if(vs[size(vs)]<size(l))
1234  {
1235    k=k+list(l[(vs[size(vs)]+1)..size(l)]);
1236  }
1237  return(k);
1238}
1239example
1240{ "EXAMPLE:"; echo=2;
1241   list l=1,2,3,4,5;
1242   intvec v=1,3,4;
1243   l=deleteSublist(v,l);
1244   l;
1245}
1246///////////////////////////////////////////////////////////////////////////////
1247proc primefactors (n, list #)
1248"USAGE:   primefactors(n [,p]); n = int or number, p = integer
1249COMPUTE: primefactors <= min(p,32003) of n (default p = 32003)
1250RETURN:  a list, say l,
1251         l[1] : primefactors <= min(p,32003) of n
1252         l[2] : l[2][i] = multiplicity of l[1][i]
1253         l[3] : remaining factor ( n=product{ (l[1][i]^l[2][i])*l[3]} )
1254                type(l[3])=typeof(n)
1255NOTE:    If n is a long integer (of type number) then the procedure
1256         finds primefactors <= min(p,32003) but n may be larger as
1257         2147483647 (max. integer representation)
1258WARNING: the procedure works for small integers only, just by testing all
1259         primes (not to be considerd as serious prime factorization!)
1260EXAMPLE: example primefactors; shows an example
1261"
1262{
1263   int ii,jj,z,p,num,w3,q;
1264   intvec w1,w2,v;
1265   list l;
1266   if (size(#) == 0)
1267   {
1268      p=32003;
1269   }
1270   else
1271   {
1272     if( typeof(#[1]) != "int")
1273     {
1274       ERROR("2nd parameter must be of type int"+newline);
1275     }
1276     p=#[1];
1277   }
1278   if( n<0) { n=-n;};
1279
1280// ----------------- case: 1st parameter is a number --------------------
1281   if (typeof(n) =="number")
1282   {
1283     kill w3;
1284     number w3;
1285     if( n > 2147483647 )           //2147483647 max. integer representation
1286     {
1287        v = primes(2,p);
1288        number m;
1289        for( ii=1; ii<=size(v); ii++)
1290        {
1291          jj=0;
1292          while(1)
1293          {
1294            q  = v[ii];
1295            jj = jj+1;
1296            m  = n/q;                  //divide n as often as possible
1297            if (denominator(m)!=1) { break;  }
1298            n=m;
1299          }
1300          if( jj>1 )
1301          {
1302             w1 = w1,v[ii];          //primes
1303             w2 = w2,jj-1;           //powers
1304          }
1305          if( n <= 2147483647 ) { break;  }
1306        }
1307     }
1308
1309     if( n >  2147483647 )            //n is still too big
1310     {
1311       if( size(w1) >1 )               //at least 1 primefactor was found
1312       {
1313         w1 = w1[2..size(w1)];
1314         w2 = w2[2..size(w2)];
1315       }
1316       else                           //no primefactor was found
1317       {
1318         w1 = 1; w2 = 1;
1319       }
1320       l  = w1,w2,n;
1321       return(l);
1322     }
1323
1324     if( n <= 2147483647 )          //n is in inter range
1325     {
1326       num = int(n);
1327       kill n;
1328       int n = num;
1329     }
1330   }
1331
1332// --------------------------- trivial cases --------------------
1333   if( n==0 )
1334   {
1335     w1=1; w2=1; w3=0; l=w1,w2,w3;
1336     return(l);
1337   }
1338
1339   if( n==1 )
1340   {
1341       w3=1;
1342       if( size(w1) >1 )               //at least 1 primefactor was found
1343       {
1344         w1 = w1[2..size(w1)];
1345         w2 = w2[2..size(w2)];
1346       }
1347       else                           //no primefactor was found
1348       {
1349         w1 = 1; w2 = 1;
1350       }
1351      l=w1,w2,w3;
1352      return(l);
1353   }
1354   if ( prime(n)==n )         //note: prime(n) <= 32003 in Singular
1355   {                          //case n is a prime
1356      if (p > n)
1357      {
1358        w1=w1,n; w2=w2,1; w3=1;
1359        w1 = w1[2..size(w1)];
1360        w2 = w2[2..size(w2)];
1361        l=w1,w2,w3;
1362        return(l);
1363      }
1364      else
1365      {
1366        w3=n;
1367        if( size(w1) >1 )               //at least 1 primefactor was found
1368        {
1369           w1 = w1[2..size(w1)];
1370           w2 = w2[2..size(w2)];
1371         }
1372         else                           //no primefactor was found
1373         {
1374           w1 = 1; w2 = 1;
1375         }
1376         l=w1,w2,w3;
1377        return(l);
1378      }
1379   }
1380   else
1381   {
1382      if ( p >= n)
1383      {
1384         v = primes(q,n div 2 + 1);
1385      }
1386      else
1387      {
1388         v = primes(q,p);
1389      }
1390//------------- search for primfactors <= last entry of v ------------
1391      for(ii=1; ii<=size(v); ii++)
1392      {
1393         z=0;
1394         while( (n mod v[ii]) == 0 )
1395         {
1396            z=z+1;
1397            n = n div v[ii];
1398         }
1399         if (z!=0)
1400         {
1401            w1 = w1,v[ii];        //primes
1402            w2 = w2,z;            //multiplicities
1403         }
1404      }
1405   }
1406//--------------- case:at least 1 primefactor was found ---------------
1407   if( size(w1) >1 )               //at least 1 primefactor was found
1408   {
1409      w1 = w1[2..size(w1)];
1410      w2 = w2[2..size(w2)];
1411   }
1412   else                           //no primefactor was found
1413   {
1414     w1 = 1; w2 = 1;
1415   }
1416   w3 = n;
1417   l  = w1,w2,w3;
1418   return(l);
1419}
1420example
1421{ "EXAMPLE:"; echo = 2;
1422   primefactors(7*8*121);
1423   ring r = 0,x,dp;
1424   primefactors(123456789100);
1425}
1426
1427///////////////////////////////////////////////////////////////////////////////
1428proc primecoeffs(J, list #)
1429"USAGE:   primecoeffs(J[,q]); J any type which can be converted to a matrix
1430         e.g. ideal, matrix, vector, module, int, intvec
1431         q = intger
1432COMPUTE: primefactors <= min(p,32003) of coeffs of J (default p = 32003)
1433RETURN:  a list, say l, of two intvectors:
1434         l[1] : the different primefactors of all coefficients of J
1435         l[2] : the different remaining factors
1436NOTE:    the procedure works for small integers only, just by testing all
1437         primes (not to be considerd as serious prime factorization!)
1438EXAMPLE: example primecoeffs; shows an example
1439"
1440{
1441   int q,ii,n,mark;;
1442   if (size(#) == 0)
1443   {
1444      q=32003;
1445   }
1446   else
1447   {
1448     if( typeof(#[1]) != "int")
1449     {
1450       ERROR("2nd parameter must be of type int"+newline);
1451     }
1452     q=#[1];
1453   }
1454
1455   if (defined(basering) == 0)
1456   {
1457     mark=1;
1458     ring r = 0,x,dp;
1459   }
1460   def I = ideal(matrix(J));
1461   poly p = product(maxideal(1));
1462   matrix Coef=coef(I[1],p);
1463   ideal id, jd, rest;
1464   intvec v,re;
1465   list result,l;
1466   for(ii=2; ii<=ncols(I); ii++)
1467   {
1468     Coef=concat(Coef,coef(I[ii],p));
1469   }
1470   id = Coef[2,1..ncols(Coef)];
1471   id = simplify(id,6);
1472   for (ii=1; ii<=size(id); ii++)
1473   {
1474     l = primefactors(number(id[ii]),q);
1475     jd = jd,l[1];
1476     rest = rest,l[3];
1477   }
1478   jd = simplify(jd,6);
1479   for (ii=1; ii<=size(jd); ii++)
1480   {
1481     v[ii]=int(jd[ii]);
1482   }
1483   v = sort(v)[1];
1484   rest = simplify(rest,6);
1485   id = sort(id)[1];
1486   if (mark)
1487   {
1488     for (ii=1; ii<=size(rest); ii++)
1489     {
1490       re[ii] = int(rest[ii]);
1491     }
1492     result = v,re;
1493   }
1494   else
1495   {
1496      result = v,rest;
1497   }
1498   return(result);
1499}
1500example
1501{ "EXAMPLE:"; echo = 2;
1502   primecoeffs(intvec(7*8*121,7*8));"";
1503   ring r = 0,(b,c,t),dp;
1504   ideal I = -13b6c3t+4b5c4t,-10b4c2t-5b4ct2;
1505   primecoeffs(I);
1506}
1507///////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the repository browser.