Changeset ebbe4a in git for Singular/LIB/general.lib


Ignore:
Timestamp:
Dec 12, 2000, 2:49:29 PM (23 years ago)
Author:
Anne Frühbis-Krüger <anne@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
5909989d37a53ff85938a4254092415537555abb
Parents:
c8839774c1058397a14da4aad0dc89c93261c85a
Message:
*anne: added deleteSublist and watchdog


git-svn-id: file:///usr/local/Singular/svn/trunk@4874 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/LIB/general.lib

    rc88397 rebbe4a  
    1 // $Id: general.lib,v 1.27 2000-05-12 12:25:47 Singular Exp $
     1// $Id: general.lib,v 1.28 2000-12-12 13:49:29 anne Exp $
    22//GMG, last modified 18.6.99
    3 ///////////////////////////////////////////////////////////////////////////////
    4 
    5 version="$Id: general.lib,v 1.27 2000-05-12 12:25:47 Singular Exp $";
     3//anne, added deleteSublist and watchdog 12.12.2000
     4///////////////////////////////////////////////////////////////////////////////
     5
     6version="$Id: general.lib,v 1.28 2000-12-12 13:49:29 anne Exp $";
    67info="
    78LIBRARY:  general.lib   PROCEDURES OF GENERAL TYPE
     
    1112 ASCII([n,m]);          string of printable ASCII characters (number n to m)
    1213 binomial(n,m[,../..]); n choose m (type int), [type string/type number]
     14 deleteSublist(iv,l);   delete entries given by iv from list l
    1315 factorial(n[,../..]);  n factorial (=n!) (type int), [type string/number]
    1416 fibonacci(n[,p]);      nth Fibonacci number [char p]
     
    2224 sort(ideal/module);    sort generators according to monomial ordering
    2325 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
    2427 which(command);        search for command and return absolute path, if found
    2528           (parameters in square brackets [] are optional)
     
    10271030}
    10281031///////////////////////////////////////////////////////////////////////////////
     1032
     1033proc watchdog(int i, string cmd)
     1034"USAGE  : watchdog(i,cmd); i -- integer; cmd -- string
     1035RETURNS: Result of cmd, if the result can be computed in
     1036         i seconds. Otherwise the computation is interrupted after
     1037         i seconds, the string "Killed" is returned and the global
     1038         variable 'watchdog_interrupt' is defined.
     1039NOTE:    * the MP package must be enabled
     1040         * the current basering should not be watchdog_rneu
     1041         * if there are variable names of the structure x(i) all
     1042           polynomials have to be put into eval(...) in order to be
     1043           interpreted correctly
     1044         * a second Singular process is started by this procedure
     1045EXAMPLE: example watchdog; shows an example
     1046"
     1047{
     1048  string rname=nameof(basering);
     1049  if (defined(watchdog_rneu))
     1050  {
     1051    kill watchdog_rneu;
     1052  }
     1053// If we do not have MP-links, watchdog cannot be used
     1054  if (system("with","MP"))
     1055  {
     1056    if ( i > 0 )
     1057    {
     1058      int j=10;
     1059      int k=999999;
     1060// fork, get the pid of the child and send it the command   
     1061      link l_fork="MPtcp:fork";
     1062      open(l_fork);
     1063      write(l_fork,quote(system("pid")));
     1064      int pid=read(l_fork);
     1065      execute("write(l_fork,quote(" + cmd + "));");
     1066
     1067
     1068// sleep in small, but growing intervals for appr. 1 second
     1069      while(j < k)
     1070      {
     1071        if (status(l_fork, "read", "ready", j)) {break;}
     1072        j = j + j;
     1073      }
     1074
     1075// sleep in intervals of one second
     1076      j = 1;
     1077      if (!status(l_fork,"read","ready"))
     1078      {
     1079        while (j < i)
     1080        {
     1081          if (status(l_fork, "read", "ready", k)) {break;}
     1082          j = j + 1;
     1083        }
     1084      }
     1085// check, whether we have a result, and return it
     1086      if (status(l_fork, "read", "ready"))
     1087      {
     1088        def result = read(l_fork);
     1089        if (nameof(basering)!=rname)
     1090        {
     1091          def watchdog_rneu=basering;
     1092        }
     1093        if(defined(watchdog_interrupt))
     1094        {
     1095          kill (watchdog_interrupt);
     1096        }
     1097        close(l_fork);
     1098      }
     1099      else
     1100      {
     1101        string result="Killed";
     1102        if(!defined(watchdog_interrupt))
     1103        {
     1104          int watchdog_interrupt=1;
     1105          export watchdog_interrupt;
     1106        }
     1107        close(l_fork);
     1108        j = system("sh","kill " + string(pid));
     1109      }
     1110      if (defined(watchdog_rneu))
     1111      {
     1112        keepring watchdog_rneu;
     1113      }
     1114      return(result);
     1115    }
     1116    else
     1117    {
     1118      ERROR("First argument of watchdog has to be a positive integer.");
     1119    }
     1120    ERROR("MP-support is not enabled in this version of Singular.");
     1121  }
     1122}
     1123example
     1124{ "EXAMPLE:"; echo=2;
     1125  ring r=0,(x,y,z),dp;
     1126  poly f=x^30+y^30;
     1127  watchdog(1,"factorize(eval("+string(f)+"))");
     1128  watchdog(100,"factorize(eval("+string(f)+"))");
     1129}
     1130///////////////////////////////////////////////////////////////////////////////
     1131
     1132proc deleteSublist(intvec v,list l)
     1133"USAGE:   deleteSublist(v,l); -- intvec v; list l
     1134         where the entries of the integer vector v correspond to the
     1135         positions of the elements to be deleted
     1136RETURN:  list without the deleted elements
     1137EXAMPLE: example deleteSublist; shows an example"
     1138{
     1139  list k;
     1140  int i,j,skip;
     1141  j=1;
     1142  skip=0;
     1143  intvec vs=sort(v)[1];
     1144  for ( i=1 ; i <=size(vs) ; i++)
     1145  {
     1146    while ((j+skip) < vs[i])
     1147    {
     1148      k[j] = l[j+skip];
     1149      j++;
     1150    }
     1151    skip++;
     1152  }
     1153  if(vs[size(vs)]<size(l))
     1154  {
     1155    k=k+list(l[(vs[size(vs)]+1)..size(l)]);
     1156  }
     1157  return(k);
     1158}
     1159example
     1160{ "EXAMPLE:"; echo=2;
     1161   list l=1,2,3,4,5;
     1162   intvec v=1,3,4;
     1163   l=deleteSublist(v,l);
     1164   l;
     1165}
     1166///////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.