Changeset 8b87364 in git for Singular/LIB/general.lib


Ignore:
Timestamp:
Nov 5, 2001, 5:06:29 PM (21 years ago)
Author:
Gerhard Pfister <pfister@…>
Branches:
(u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'a800fe4b3e9d37a38c5a10cc0ae9dfa0c15a4ee6')
Children:
16105c8f405ac4e9d787fbd2bae347a9686645db
Parents:
5b3302cc6f7c9964ee19073aca0b273ad9e606a9
Message:
neue proceduren eingefuegt


git-svn-id: file:///usr/local/Singular/svn/trunk@5668 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 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///////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.