[d6db1f2] | 1 | // $Id: factor.lib,v 1.3 1997-05-05 12:02:44 Singular Exp $ |
---|
[3d124a7] | 2 | //(RS) |
---|
| 3 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 4 | |
---|
[d6db1f2] | 5 | LIBRARY: factor.lib PROCEDURES FOR CALLING THE REDUCE FACTORIZER |
---|
[3d124a7] | 6 | |
---|
| 7 | delete_dollar(s) return the string s with '$' deleted |
---|
| 8 | reduce_factor(f) list of factors of f, calling REDUCE (UNIX only) |
---|
| 9 | |
---|
| 10 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 11 | |
---|
| 12 | proc delete_dollar ( string s ) |
---|
| 13 | USAGE: delete_dollar(s); s = string |
---|
| 14 | RETURN: string, with '$' replaced by ' ' |
---|
| 15 | EXAMPLE: example delete_dollar; shows an example |
---|
| 16 | { |
---|
| 17 | while( s[size(s)]=="$" and size(s)!=1 ) { s=s[1,size(s)-1]; } |
---|
| 18 | while( s[1]=="$" and size(s)!=1 ) { s=s[2,size(s)-1]; } |
---|
| 19 | if( size(s)==1 and s[1]=="$" ) { return(""); } |
---|
| 20 | int i=find(s,"$"); |
---|
| 21 | while( i!=0 ) |
---|
| 22 | { |
---|
| 23 | s=s[1,i-1]+s[i+1,size(s)-i]; |
---|
| 24 | i=find(s,"$"); |
---|
| 25 | } |
---|
| 26 | return(s); |
---|
| 27 | } |
---|
| 28 | example |
---|
| 29 | { "EXAMPLE:"; echo = 2; |
---|
| 30 | string s = "123$456$"; |
---|
| 31 | delete_dollar(s); |
---|
| 32 | delete_dollar(s)+"789"; |
---|
| 33 | } |
---|
| 34 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 35 | |
---|
| 36 | proc reduce_factor ( poly f ) |
---|
| 37 | USAGE: reduce_factor(f); f = poly |
---|
| 38 | RETURN: list, the factors of f |
---|
| 39 | NOTE: due to a limitation of REDUCE, multivariate polynomials can only |
---|
| 40 | be factorized in characteristic 0 |
---|
| 41 | This proc runs under UNIX only |
---|
| 42 | EXAMPLE: example reduce_factor; shows an example |
---|
| 43 | { |
---|
| 44 | string pid = string( system( "pid" ) ); |
---|
| 45 | string outname = "/tmp/singred." + pid + ".out"; |
---|
| 46 | string scriptname = "/tmp/singred." + pid + ".sh"; |
---|
| 47 | int n = char( basering ); |
---|
| 48 | int shortOutput = short; |
---|
| 49 | short = 0; |
---|
[d6db1f2] | 50 | write (scriptname,"#!/bin/sh |
---|
[3d124a7] | 51 | $reduce/reduce >/dev/null <<EOF |
---|
| 52 | off nat$ off echo$ setmod " + string( n ) + "$ |
---|
| 53 | erg:=factorize(",f,")$ |
---|
| 54 | out \"" + outname + "\"$ |
---|
| 55 | write(\"list result;\")$ |
---|
| 56 | counter:=1$ |
---|
| 57 | for each a in erg do begin |
---|
| 58 | write(\"result[\",counter,\"]=\",a,\";\"); |
---|
| 59 | counter:=counter+1; |
---|
| 60 | end; |
---|
| 61 | shut \"" + outname + "\"; |
---|
| 62 | quit$ |
---|
[d6db1f2] | 63 | EOF"); |
---|
[3d124a7] | 64 | system( "sh", "chmod 700 " + scriptname ); |
---|
| 65 | system( "sh", scriptname ); |
---|
| 66 | string resultstring = read( outname ); |
---|
| 67 | if ( resultstring <> "" ) |
---|
| 68 | { |
---|
| 69 | resultstring = delete_dollar( resultstring ); |
---|
| 70 | execute resultstring; |
---|
| 71 | } |
---|
| 72 | // remove tmp-files after usage */ |
---|
| 73 | system( "sh", "/bin/rm " + outname ); |
---|
| 74 | system( "sh", "/bin/rm " + scriptname ); |
---|
| 75 | short = shortOutput; |
---|
| 76 | return( result ); |
---|
| 77 | } |
---|
| 78 | example |
---|
| 79 | { "EXAMPLE:"; echo = 2; |
---|
| 80 | ring r=0,(x,y,z),lp; |
---|
| 81 | poly f=(x+y)*(y+z)^2*(z+x); |
---|
| 82 | f; |
---|
| 83 | list L=reduce_factor(f); |
---|
| 84 | L; |
---|
| 85 | } |
---|