Changeset 8b87364 in git


Ignore:
Timestamp:
Nov 5, 2001, 5:06:29 PM (22 years ago)
Author:
Gerhard Pfister <pfister@…>
Branches:
(u'spielwiese', '0d6b7fcd9813a1ca1ed4220cfa2b104b97a0a003')
Children:
16105c8f405ac4e9d787fbd2bae347a9686645db
Parents:
5b3302cc6f7c9964ee19073aca0b273ad9e606a9
Message:
neue proceduren eingefuegt


git-svn-id: file:///usr/local/Singular/svn/trunk@5668 2c84dea3-7e68-4137-9b89-c4e89433aadc
Location:
Singular/LIB
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • Singular/LIB/general.lib

    r5b3302c r8b87364  
    22//anne, added deleteSublist and watchdog 12.12.2000
    33///////////////////////////////////////////////////////////////////////////////
    4 version="$Id: general.lib,v 1.40 2001-08-27 14:47:50 Singular Exp $";
     4version="$Id: general.lib,v 1.41 2001-11-05 16:05:48 pfister Exp $";
    55category="General purpose";
    66info="
     
    2424 watchdog(i,cmd);       only wait for result of command cmd for i seconds
    2525 which(command);        search for command and return absolute path, if found
     26 primecoeffs(J[,q]);    primefactors <= min(p,32003) of coeffs of J
     27 primefactors(n [,p]);  primefactors <= min(p,32003) of n
    2628           (parameters in square brackets [] are optional)
    2729";
    2830
    2931LIB "inout.lib";
     32LIB "poly.lib";
     33LIB "matrix.lib";
    3034///////////////////////////////////////////////////////////////////////////////
    3135
     
    12111215}
    12121216///////////////////////////////////////////////////////////////////////////////
     1217proc primefactors (n, list #)
     1218"USAGE:   primefactors(n [,p]); n = int or number, p = integer
     1219COMPUTE: primefactors <= min(p,32003) of n (default p = 32003)
     1220RETURN:  a list, say l,
     1221         l[1] : primefactors <= min(p,32003) of n
     1222         l[2] : l[2][i] = multiplicity of l[1][i]
     1223         l[3] : remaining factor ( n=product{ (l[1][i]^l[2][i])*l[3]} )
     1224                type(l[3])=typeof(n)
     1225NOTE:    If n is a long integer (of type number) then the procedure
     1226         finds primefactors <= min(p,32003) but n may be larger as
     1227         2147483647 (max. integer representation)
     1228WARNING: the procedure works for small integers only, just by testing all
     1229         primes (not to be considerd as serious prime factorization!)
     1230EXAMPLE: example primefactors; shows an example
     1231"
     1232{
     1233   int ii,jj,z,p,num,w3,q;
     1234   intvec w1,w2,v;
     1235   list l;
     1236   if (size(#) == 0)
     1237   {
     1238      p=32003;
     1239   }
     1240   else
     1241   {
     1242     if( typeof(#[1]) != "int")
     1243     {
     1244       ERROR("2nd parameter must be of type int"+newline);
     1245     }
     1246     p=#[1];
     1247   }
     1248   if( n<0) { n=-n;};
     1249
     1250// ----------------- case: 1st parameter is a number --------------------   
     1251   if (typeof(n) =="number")
     1252   {
     1253     kill w3;
     1254     number w3;
     1255     if( n > 2147483647 )           //2147483647 max. integer representation
     1256     {
     1257        v = primes(2,p);
     1258        number m;
     1259        for( ii=1; ii<=size(v); ii++)
     1260        {
     1261          jj=0;
     1262          while(1)
     1263          {
     1264            q  = v[ii];
     1265            jj = jj+1;             
     1266            m  = n/q;                  //divide n as often as possible
     1267            if (denominator(m)!=1) { break;  }
     1268            n=m;
     1269          }
     1270          if( jj>1 )
     1271          {
     1272             w1 = w1,v[ii];          //primes
     1273             w2 = w2,jj-1;           //powers
     1274          }
     1275          if( n <= 2147483647 ) { break;  }
     1276        }
     1277     }
     1278
     1279     if( n >  2147483647 )            //n is still too big
     1280     {
     1281       if( size(w1) >1 )               //at least 1 primefactor was found
     1282       {
     1283         w1 = w1[2..size(w1)];
     1284         w2 = w2[2..size(w2)];
     1285       }
     1286       else                           //no primefactor was found
     1287       {
     1288         w1 = 1; w2 = 1;
     1289       }     
     1290       l  = w1,w2,n;
     1291       return(l);
     1292     }
     1293
     1294     if( n <= 2147483647 )          //n is in inter range
     1295     {
     1296       num = int(n);
     1297       kill n;
     1298       int n = num;
     1299     }
     1300   }
     1301   
     1302// --------------------------- trivial cases --------------------
     1303   if( n==0 )
     1304   { 
     1305     w1=1; w2=1; w3=0; l=w1,w2,w3;
     1306     return(l);
     1307   }
     1308   
     1309   if( n==1 )
     1310   {   
     1311       w3=1;
     1312       if( size(w1) >1 )               //at least 1 primefactor was found
     1313       {
     1314         w1 = w1[2..size(w1)];
     1315         w2 = w2[2..size(w2)];
     1316       }
     1317       else                           //no primefactor was found
     1318       {
     1319         w1 = 1; w2 = 1;
     1320       }     
     1321      l=w1,w2,w3;
     1322      return(l);
     1323   }
     1324   if ( prime(n)==n )         //note: prime(n) <= 32003 in Singular
     1325   {                          //case n is a prime
     1326      if (p > n)
     1327      { 
     1328        w1=w1,n; w2=w2,1; w3=1;
     1329        w1 = w1[2..size(w1)];
     1330        w2 = w2[2..size(w2)];
     1331        l=w1,w2,w3;
     1332        return(l);
     1333      }
     1334      else
     1335      {
     1336        w3=n;
     1337        if( size(w1) >1 )               //at least 1 primefactor was found
     1338        {
     1339           w1 = w1[2..size(w1)];
     1340           w2 = w2[2..size(w2)];
     1341         }
     1342         else                           //no primefactor was found
     1343         {
     1344           w1 = 1; w2 = 1;
     1345         }     
     1346         l=w1,w2,w3;
     1347        return(l);
     1348      }     
     1349   }
     1350   else
     1351   {
     1352      if ( p >= n)
     1353      {
     1354         v = primes(q,n div 2 + 1);
     1355      }
     1356      else
     1357      {
     1358         v = primes(q,p);
     1359      }
     1360//------------- search for primfactors <= last entry of v ------------   
     1361      for(ii=1; ii<=size(v); ii++)
     1362      {
     1363         z=0;
     1364         while( (n mod v[ii]) == 0 )
     1365         { 
     1366            z=z+1;
     1367            n = n div v[ii];
     1368         }
     1369         if (z!=0)
     1370         {
     1371            w1 = w1,v[ii];        //primes
     1372            w2 = w2,z;            //multiplicities
     1373         }
     1374      }
     1375   }
     1376//--------------- case:at least 1 primefactor was found ---------------
     1377   if( size(w1) >1 )               //at least 1 primefactor was found
     1378   {
     1379      w1 = w1[2..size(w1)];
     1380      w2 = w2[2..size(w2)];
     1381   }
     1382   else                           //no primefactor was found
     1383   {
     1384     w1 = 1; w2 = 1;
     1385   }
     1386   w3 = n;
     1387   l  = w1,w2,w3;
     1388   return(l);
     1389}
     1390example
     1391{ "EXAMPLE:"; echo = 2;
     1392   primefactors(7*8*121);
     1393   ring r = 0,x,dp;
     1394   primefactors(123456789100);
     1395
     1396
     1397///////////////////////////////////////////////////////////////////////////////
     1398proc primecoeffs(J, list #)
     1399"USAGE:   primecoeffs(J[,q]); J any type which can be converted to a matrix
     1400         e.g. ideal, matrix, vector, module, int, intvec
     1401         q = intger
     1402COMPUTE: primefactors <= min(p,32003) of coeffs of J (default p = 32003)
     1403RETURN:  a list, say l, of two intvectors:
     1404         l[1] : the different primefactors of all coefficients of J
     1405         l[2] : the different remaining factors
     1406NOTE:    the procedure works for small integers only, just by testing all
     1407         primes (not to be considerd as serious prime factorization!)
     1408EXAMPLE: example primecoeffs; shows an example
     1409"
     1410{
     1411   int q,ii,n,mark;;
     1412   if (size(#) == 0)
     1413   {
     1414      q=32003;
     1415   }
     1416   else
     1417   {
     1418     if( typeof(#[1]) != "int")
     1419     {
     1420       ERROR("2nd parameter must be of type int"+newline);
     1421     }
     1422     q=#[1];
     1423   }
     1424
     1425   if (defined(basering) == 0)
     1426   {
     1427     mark=1;
     1428     ring r = 0,x,dp;
     1429   }
     1430   def I = ideal(matrix(J));
     1431   poly p = product(maxideal(1));
     1432   matrix Coef=coef(I[1],p);
     1433   ideal id, jd, rest;
     1434   intvec v,re;
     1435   list result,l;
     1436   for(ii=2; ii<=ncols(I); ii++)
     1437   {
     1438     Coef=concat(Coef,coef(I[ii],p));
     1439   }
     1440   id = Coef[2,1..ncols(Coef)];
     1441   id = simplify(id,6);
     1442   for (ii=1; ii<=size(id); ii++) 
     1443   {   
     1444     l = primefactors(number(id[ii]),q);     
     1445     jd = jd,l[1];
     1446     rest = rest,l[3];
     1447   }
     1448   jd = simplify(jd,6);
     1449   for (ii=1; ii<=size(jd); ii++) 
     1450   {
     1451     v[ii]=int(jd[ii]);
     1452   }
     1453   v = sort(v)[1];
     1454   rest = simplify(rest,6);
     1455   id = sort(id)[1];
     1456   if (mark)
     1457   {
     1458     for (ii=1; ii<=size(rest); ii++)
     1459     {
     1460       re[ii] = int(rest[ii]);
     1461     }
     1462     result = v,re;
     1463   }
     1464   else
     1465   {
     1466      result = v,rest;
     1467   }
     1468   return(result);
     1469}
     1470example
     1471{ "EXAMPLE:"; echo = 2;
     1472   primecoeffs(intvec(7*8*121,7*8));"";
     1473   ring r = 0,(b,c,t),dp;
     1474   ideal I = -13b6c3t+4b5c4t,-10b4c2t-5b4ct2;
     1475   primecoeffs(I);
     1476
     1477///////////////////////////////////////////////////////////////////////////////
  • Singular/LIB/presolve.lib

    r5b3302c r8b87364  
    11//last change: 13.02.2001 (Eric Westenberger)
    22///////////////////////////////////////////////////////////////////////////////
    3 version="$Id: presolve.lib,v 1.18 2001-08-27 14:47:57 Singular Exp $";
     3version="$Id: presolve.lib,v 1.19 2001-11-05 16:06:29 pfister Exp $";
    44category="Symbolic-numerical solving";
    55info="
     
    2121 sortvars(id[n1,p1..]); sort vars w.r.t. complexity in id [different blocks]
    2222 valvars(id[..]);       valuation of vars w.r.t. to their complexity in id
    23            (parameters in square brackets [] are optional)
     23 idealsSimplify(id);    ideal I = eliminate(Id,m) m is a product of variables
     24                       which are only linearly involved in the generators of id
     25 idealSplit(id,tF,fS);  a list of ideals such that their intersection
     26                        has the same radical as id
     27                       ( parameters in square brackets [] are optional)
    2428";
    2529
     
    11661170}
    11671171///////////////////////////////////////////////////////////////////////////////
     1172proc idealSplit(ideal I,list #)
     1173"USAGE:  idealSplit(id,timeF,timeS);  id ideal and optioonal
     1174         timeF ,timeS integers to bound the time which can be used
     1175         for factorization resp. standard basis computation
     1176RETURN:  a list of ideals such that their intersection
     1177         has the same radical as id
     1178EXAMPLE: example idealSplit; shows an example
     1179"
     1180{
     1181   option(redSB);
     1182   int j,k,e;
     1183   int i=1;
     1184   int l=attrib(I,"isSB");
     1185   ideal J;
     1186   int timeF;
     1187   int timeS;
     1188   list re,fac,te;
     1189
     1190   if(size(#)==1)
     1191   {
     1192     if(typeof(#[1])=="ideal")
     1193     {
     1194        re=#;
     1195     }
     1196     else
     1197     {
     1198       timeF=#[1];
     1199     }
     1200   }
     1201   if(size(#)==2)
     1202   {
     1203     if(typeof(#[1])=="list")
     1204     {
     1205        re=#[1];
     1206        timeF=#[2];
     1207     }
     1208     else
     1209     {
     1210       timeF=#[1];
     1211       timeS=#[2];
     1212     }
     1213   }
     1214   if(size(#)==3){re=#[1];timeF=#[2];timeS=#[3];}
     1215
     1216   fac=timeFactorize(I[1],timeF);
     1217
     1218   while((size(fac[1])==2)&&(i<size(I)))
     1219   {
     1220      i++;
     1221      fac=timeFactorize(I[i],timeF);
     1222   }
     1223   if(size(fac[1])>2)
     1224   {
     1225      for(j=2;j<=size(fac[1]);j++)
     1226      {
     1227         I[i]=fac[1][j];
     1228         attrib(I,"isSB",1);
     1229         e=1;
     1230         k=0;
     1231         while(k<size(re))
     1232         {
     1233            k++;
     1234            if(size(reduce(re[k],I))==0){e=0;break;}
     1235            attrib(re[k],"isSB",1);
     1236            if(size(reduce(I,re[k]))==0){re=delete(re,k);k--;}
     1237         }
     1238         if(e)
     1239         {
     1240            if(l)
     1241            {
     1242               J=I;
     1243               J[i]=0;
     1244               J=simplify(J,2);
     1245               attrib(J,"isSB",1);
     1246               re=idealSplit(std(J,fac[1][j]),re,timeF,timeS);
     1247            }
     1248            else
     1249            {
     1250               re=idealSplit(timeStd(I,timeS),re,timeF,timeS);
     1251            }
     1252         }
     1253      }
     1254      return(re);
     1255   }
     1256   J=timeStd(I,timeS);
     1257   attrib(I,"isSB",1);
     1258   if(size(reduce(J,I))==0){return(re+list(I));}
     1259   return(re+idealSplit(J,re,timeF,timeS));
     1260}
     1261example
     1262{ "EXAMPLE:"; echo = 2;
     1263   ring r=32003,(b,s,t,u,v,w,x,y,z),dp;
     1264   ideal i=
     1265   bv+su,
     1266   bw+tu,
     1267   sw+tv,
     1268   by+sx,
     1269   bz+tx,
     1270   sz+ty,
     1271   uy+vx,
     1272   uz+wx,
     1273   vz+wy,
     1274   bvz;
     1275   idealSplit(i);
     1276}
     1277///////////////////////////////////////////////////////////////////////////////
     1278proc idealSimplify(ideal J,list #)
     1279"USAGE:  idealSimplify(id);  id ideal
     1280RETURN:  ideal I = eliminate(Id,m) m is a product of variables
     1281         which are only linearly involved in the generators of id
     1282EXAMPLE: example idealSimplify; shows an example
     1283"
     1284{
     1285   ideal I=J;
     1286   if(size(#)!=0){I=#[1];}
     1287   def R=basering;
     1288   matrix M=jacob(I);
     1289   ideal ma=maxideal(1);
     1290   int i,j,k;
     1291   map phi;
     1292
     1293   for(i=1;i<=nrows(M);i++)
     1294   {
     1295      for(j=1;j<=ncols(M);j++)
     1296      {       
     1297         if(deg(M[i,j])==0)
     1298         {
     1299            ma[j]=(-1/M[i,j])*(I[i]-M[i,j]*var(j));
     1300            phi=R,ma;
     1301            I=phi(I);
     1302            J=phi(J);
     1303            for(k=1;k<=ncols(I);k++){I[k]=cleardenom(I[k]);}
     1304            M=jacob(I);
     1305         }
     1306      }
     1307   }
     1308   J=simplify(J,2);
     1309   for(i=1;i<=size(J);i++){J[i]=cleardenom(J[i]);}
     1310   return(J);
     1311}
     1312example
     1313{ "EXAMPLE:"; echo = 2;
     1314   ring r=0,(x,y,z,w,t),dp;
     1315   ideal i=
     1316   t,
     1317   x3+y2+2z,
     1318   x2+3y,
     1319   x2+y2+z2,
     1320   w2+z;
     1321   ideal j=idealSimplify(i);
     1322   ideal k=eliminate(i,zyt);
     1323   reduce(k,std(j));
     1324   reduce(j,std(k));
     1325}
     1326
     1327///////////////////////////////////////////////////////////////////////////////
     1328
    11681329/*
    11691330
  • Singular/LIB/standard.lib

    r5b3302c r8b87364  
    11//////////////////////////////////////////////////////////////////////////////
    2 version="$Id: standard.lib,v 1.60 2001-08-28 12:54:05 Singular Exp $";
     2version="$Id: standard.lib,v 1.61 2001-11-05 16:05:08 pfister Exp $";
    33category="Miscellaneous";
    44info="
     
    1414 fprintf(link,fmt,..)   writes formatted string to link
    1515 printf(fmt,...)        displays formatted string
     16 timeStd(i,d)           std(i) if the standard basis computation finished after
     17                        d-1 seconds and i otherwhise
     18 timeFactorize(p,d)     factorize(p) if the factorization finished after d-1
     19                        seconds otherwhise f is considered to be irreducible
     20factorH(p)              changes variables to become the last variable the
     21                        principal one in the multivariate factorization and
     22                        factorizes then the polynomial
     23
    1624";
    1725
     
    10251033}
    10261034
     1035proc timeFactorize(poly i,list #)
     1036"USAGE:  timeFactorize(p,d)  poly p , integer d
     1037RETURN:  factorize(p) if the factorization finished after d-1
     1038         seconds otherwhise f is considered to be irreducible
     1039EXAMPLE: example timeFactorize; shows an example
     1040"
     1041{
     1042  def P=basering;
     1043  if (size(#) > 0)
     1044  {
     1045    if (system("with", "MP"))
     1046    {
     1047      if ((typeof(#[1]) == "int")&&(#[1]))
     1048      {
     1049        int wait = #[1];
     1050        int j = 10;
     1051
     1052        string bs = nameof(basering);
     1053        link l_fork = "MPtcp:fork";
     1054        open(l_fork);
     1055        write(l_fork, quote(system("pid")));
     1056        int pid = read(l_fork);
     1057        write(l_fork, quote(timeFactorize(eval(i))));
     1058
     1059        // sleep in small intervalls for appr. one second
     1060        if (wait > 0)
     1061        {
     1062          while(j < 1000000)
     1063          {
     1064            if (status(l_fork, "read", "ready", j)) {break;}
     1065            j = j + j;
     1066          }
     1067        }
     1068
     1069        // sleep in intervalls of one second from now on
     1070        j = 1;
     1071        while (j < wait)
     1072        {
     1073          if (status(l_fork, "read", "ready", 1000000)) {break;}
     1074          j = j + 1;
     1075        }
     1076
     1077        if (status(l_fork, "read", "ready"))
     1078        {
     1079          def result = read(l_fork);
     1080          if (bs != nameof(basering))
     1081          {
     1082            def PP = basering;
     1083            setring P;
     1084            def result = imap(PP, result);
     1085            kill PP;
     1086          }
     1087          kill (l_fork);
     1088        }
     1089        else
     1090        {
     1091          list result;
     1092          intvec v=1,1;
     1093          result[1]=list(1,i);
     1094          result[2]=v;
     1095          j = system("sh", "kill " + string(pid));
     1096        }
     1097        return (result);
     1098      }
     1099    }
     1100  }
     1101  return(factorH(i));
     1102}
     1103example
     1104{ "EXAMPLE:"; echo = 2;
     1105   ring r=0,(x,y),dp;
     1106   poly p=((x2+y3)^2+xy6)*((x3+y2)^2+x10y);
     1107   p=p^2;
     1108   timeFactorize(p,2);
     1109   timeFactorize(p,20);
     1110}
     1111
     1112proc timeStd(ideal i,list #)
     1113"USAGE:  timeStd(i,d), i ideal, d integer
     1114RETURN:  std(i) if the standard basis computation finished after
     1115         d-1 seconds and i otherwhise
     1116EXAMPLE: example timeStd; shows an example
     1117"
     1118{
     1119  def P=basering;
     1120  if (size(#) > 0)
     1121  {
     1122    if (system("with", "MP"))
     1123    {
     1124      if ((typeof(#[1]) == "int")&&(#[1]))
     1125      {
     1126        int wait = #[1];
     1127        int j = 10;
     1128
     1129        string bs = nameof(basering);
     1130        link l_fork = "MPtcp:fork";
     1131        open(l_fork);
     1132        write(l_fork, quote(system("pid")));
     1133        int pid = read(l_fork);
     1134        write(l_fork, quote(timeStd(eval(i))));
     1135
     1136        // sleep in small intervalls for appr. one second
     1137        if (wait > 0)
     1138        {
     1139          while(j < 1000000)
     1140          {
     1141            if (status(l_fork, "read", "ready", j)) {break;}
     1142            j = j + j;
     1143          }
     1144        }
     1145        j = 1;
     1146        while (j < wait)
     1147        {
     1148          if (status(l_fork, "read", "ready", 1000000)) {break;}
     1149          j = j + 1;
     1150        }
     1151        if (status(l_fork, "read", "ready"))
     1152        {
     1153          def result = read(l_fork);
     1154          if (bs != nameof(basering))
     1155          {
     1156            def PP = basering;
     1157            setring P;
     1158            def result = imap(PP, result);
     1159            kill PP;
     1160          }
     1161          kill (l_fork);
     1162        }
     1163        else
     1164        {
     1165          ideal result=i;
     1166          j = system("sh", "kill " + string(pid));
     1167        }
     1168        return (result);
     1169      }
     1170    }
     1171  }
     1172  return(std(i));
     1173}
     1174example
     1175{ "EXAMPLE:"; echo = 2;
     1176   ring r=32003,(a,b,c,d,e),dp;
     1177   int n=6;
     1178   ideal i=
     1179   a^n-b^n,
     1180   b^n-c^n,
     1181   c^n-d^n,
     1182   d^n-e^n,
     1183   a^(n-1)*b+b^(n-1)*c+c^(n-1)*d+d^(n-1)*e+e^(n-1)*a;
     1184   timeStd(i,2);
     1185   timeStd(i,20);
     1186}
     1187
     1188proc factorH(poly p)
     1189"USAGE:  factorH(p)  p poly
     1190RETURN:  factorize(p)
     1191NOTE:    changes variables to become the last variable the principal
     1192         one in the multivariate factorization and factorizes then
     1193         the polynomial
     1194EXAMPLE: example factorH; shows an example
     1195"
     1196{
     1197   def R=basering;
     1198   int i,j;
     1199   int n=1;
     1200   int d=nrows(coeffs(p,var(1)));
     1201   for(i=1;i<=nvars(R);i++)
     1202   {
     1203      j=nrows(coeffs(p,var(i)));
     1204      if(d>j)
     1205      {
     1206         n=i;
     1207         d=j;
     1208      }
     1209   }
     1210   ideal ma=maxideal(1);    //die letzte Variable ist die Hauptvariable
     1211   ma[nvars(R)]=var(n);
     1212   ma[n]=var(nvars(R));
     1213   map phi=R,ma;
     1214   list fac=factorize(phi(p));
     1215   list re=phi(fac);
     1216   return(re);
     1217}
     1218example
     1219{ "EXAMPLE:"; echo = 2;
     1220  system("random",992851144);
     1221  ring r=32003,(x,y,z,w,t),lp;
     1222  poly p=y2w9+yz7t-yz5w4-z2w4t4-w8t3;
     1223  factorize(p);  //fast
     1224  system("random",992851262);
     1225  factorize(p);  //slow
     1226  system("random",992851262);
     1227  factorH(p);
     1228}
     1229
    10271230/*
    10281231proc minres(list #)
Note: See TracChangeset for help on using the changeset viewer.