[BACK]Return to oxcommon.m2 CVS log [TXT][DIR] Up to [local] / OpenXM / src / Macaulay2 / m2

Annotation of OpenXM/src/Macaulay2/m2/oxcommon.m2, Revision 1.2

1.2     ! takayama    1: ID = "$OpenXM: OpenXM/src/Macaulay2/m2/oxcommon.m2,v 1.1 2000/09/21 09:20:53 takayama Exp $"
1.1       takayama    2: --- class INT32
                      3: INT32 = new SelfInitializingType of BasicList
                      4: new INT32 from ZZ := (a,n) -> {n}
                      5: int32 = method()
                      6: int32(INT32) := (n)->(n)
                      7: int32(ZZ) := (n)-> (
                      8:      if ((abs(n) >> 31) > 0) then error "expected signed 32-bit integer";
                      9:      tmp := n;
                     10:      if (n < 0) then tmp = 2^32+n; -- 2's complement
                     11:      new INT32 from tmp
                     12:         )
                     13:
                     14: --- class ERROR, MATHCAP
                     15: ERROR = new SelfInitializingType of BasicList
                     16:
                     17: -- class MATHCAP
                     18: MATHCAP = new SelfInitializingType of BasicList
                     19:
                     20: MATHCAP'OF'M2 = MATHCAP
                     21:   {{INT32{1001003}, "M2 client " | ID},
                     22:        {},
                     23:        {{INT32{OX'DATA},
                     24:          {INT32{CMO'NULL}, INT32{CMO'INT32}, INT32{CMO'STRING},
                     25:           INT32{CMO'LIST}, INT32{CMO'ERROR2}, INT32{CMO'MATHCAP},
                     26:          INT32{CMO'ZZ}}
                     27:      }}
                     28: }
                     29:
                     30: MATHCAP'OF'M2'SERVER = MATHCAP
                     31:   {{INT32{1001003}, "M2 client " | ID},
                     32:        {INT32{SM'popCMO},
                     33:        INT32{SM'popString},
                     34:        INT32{SM'pops},
                     35:        INT32{SM'nop},
                     36:        INT32{SM'setName},
                     37:        INT32{SM'evalName},
                     38:        INT32{SM'executeStringByLocalParser},
                     39:        INT32{SM'executeFunction},
                     40:        INT32{SM'dupErrors},
                     41:        INT32{SM'setMathCap},
                     42:        INT32{SM'mathcap}
                     43:        },
                     44:        {{INT32{OX'DATA},
                     45:          {INT32{CMO'NULL}, INT32{CMO'INT32}, INT32{CMO'STRING},
                     46:           INT32{CMO'LIST}, INT32{CMO'ERROR2}, INT32{CMO'MATHCAP},
                     47:          INT32{CMO'ZZ}}
                     48:      }}
                     49: }
                     50:
                     51:
                     52: --- class OXSESSION
                     53: OXSESSION = new Type of MutableHashTable;
                     54:
                     55: toNetwork = (n) -> (
                     56:      n3 := n % 256;
                     57:      n = n >> 8;
                     58:      n2 := n % 256;
                     59:      n = n >> 8;
                     60:      n1 := n % 256;
                     61:      n0 := n >> 8;
                     62:      (ascii n0) | (ascii n1) | (ascii n2) | (ascii n3))
                     63:
                     64: fromNetwork = (s) -> (
                     65:      s = ascii substring(s,0,4);
                     66:      n := s#0;
                     67:      n = (n << 8) + s#1;
                     68:      n = (n << 8) + s#2;
                     69:      n = (n << 8) + s#3;
                     70:      n)
                     71:
                     72:
                     73: OXCOMMAND = toNetwork 513
                     74: OXDATA = toNetwork 514
                     75:
                     76: OX'COMMAND = 513
                     77: OX'DATA = 514
                     78:
                     79: CMO'NULL = 1
                     80: CMO'INT32 = 2
                     81: CMO'STRING = 4
                     82: CMO'LIST = 17
                     83: CMO'ZZ = 20
                     84: CMO'QQ = 21
                     85: CMO'ERROR2 = 2130706432 + 2
                     86: CMO'MATHCAP = 5
                     87: CMO'RECURSIVE'POLYNOMIAL = 27
                     88: CMO'POLYNOMIAL'IN'ONE'VARIABLE = 33
                     89: CMO'INDETERMINATE = 60
                     90:
                     91: CMONULL = toNetwork CMO'NULL
                     92: CMOINT32 = toNetwork CMO'INT32
                     93: CMOSTRING = toNetwork CMO'STRING
                     94: CMOLIST = toNetwork CMO'LIST
                     95: CMOZZ = toNetwork CMO'ZZ
                     96: CMOQQ = toNetwork CMO'QQ
                     97: CMOERROR2 = toNetwork CMO'ERROR2
                     98: CMOMATHCAP = toNetwork CMO'MATHCAP
                     99: CMORECURSIVEPOLYNOMIAL = toNetwork CMO'RECURSIVE'POLYNOMIAL
                    100: CMOPOLYNOMIALINONEVARIABLE = toNetwork CMO'POLYNOMIAL'IN'ONE'VARIABLE
                    101: CMOINDETERMINATE = toNetwork CMO'INDETERMINATE
                    102:
                    103: -- Defines taken from the OpenXM documentation
                    104: -------------------
                    105: -- Stack machine --
                    106: -------------------
                    107: SM'popSerializedLocalObject = 258
                    108: SM'popCMO = 262
                    109: SM'popString = 263
                    110:
                    111: SM'mathcap = 264
                    112: SM'pops = 265
                    113: SM'setName = 266
                    114: SM'evalName = 267
                    115: SM'executeStringByLocalParser = 268
                    116: SM'executeFunction = 269
                    117: SM'beginBlock  = 270
                    118: SM'endBlock    = 271
                    119: SM'shutdown    = 272
                    120: SM'setMathCap  = 273
                    121: SM'executeStringByLocalParserInBatchMode = 274
                    122: SM'getsp   = 275
                    123: SM'dupErrors = 276
                    124: SM'pushCMOtag = 277
                    125:
                    126: SM'control'kill = 1024
                    127: SM'control'reset'connection  = 1030
                    128: SM'control'to'debug'mode = 1025
                    129: SM'control'exit'debug'mode = 1026
                    130:
                    131: --------------------------------
                    132: -- binary string --> CMObject --
                    133: --------------------------------
                    134:
                    135: OXINPUT = new Type of MutableHashTable
                    136: makeOXINPUT = (F) -> (
                    137:      result := new OXINPUT;
                    138:      result.F = F;
                    139:      result.buffer = "";
                    140:      result.loc = 0;
                    141:      result)
                    142:
                    143: readMoreData = (OX) -> (
                    144:      -- OX is an OXINPUT
1.2     ! takayama  145:      --stderr <<"before read: "<<OX.loc << ascii OX.buffer <<endl << flush;
        !           146:      --OX.buffer = copy(OX.buffer); --copy it because "read" distroy it.
        !           147:      OX.buffer = ascii(ascii(OX.buffer)); --copy by a dirty trick. BUG
1.1       takayama  148:      newstring := read (OX.F);
                    149:      stderr << "Read " << #newstring << " bytes: " << ascii newstring
                    150:             << endl << flush;
                    151:      left := #OX.buffer - OX.loc;
1.2     ! takayama  152:      --stderr <<"before: "<<OX.loc << ascii OX.buffer <<endl << flush;
1.1       takayama  153:      if left > 0 then
                    154:         OX.buffer = substring(OX.buffer, OX.loc, left)
                    155:                     | newstring
                    156:      else
                    157:         OX.buffer = newstring;
1.2     ! takayama  158:      --stderr <<"after: "<<ascii OX.buffer <<endl << flush;
1.1       takayama  159:      OX.loc = 0;
                    160:      )
                    161:
                    162: get32bits = (OX) -> (
                    163:      -- OX is an OXINPUT
1.2     ! takayama  164:      while  #OX.buffer < OX.loc + 4 do (
        !           165:        readMoreData OX; );
1.1       takayama  166:      result := fromNetwork(substring(OX.buffer, OX.loc, 4));
1.2     ! takayama  167:      OX.loc = OX.loc + 4;
        !           168:      --debug: stderr << "get32bits" << ascii OX.buffer <<endl <<flush;
1.1       takayama  169:      result
                    170:      )
                    171:
                    172: getBytes = (OX,n) -> (
                    173:      -- OX is an OXINPUT
1.2     ! takayama  174:      while #OX.buffer < OX.loc + n  do (
        !           175:        readMoreData OX; );
1.1       takayama  176:      result := substring(OX.buffer, OX.loc, n);
                    177:      OX.loc = OX.loc + n;
                    178:      result)
                    179:
                    180: CMOfromBinary = (OX) -> (
                    181:      -- OX is an OXINPUT
                    182:      local result;
                    183:      cmoTag := get32bits OX;
                    184:      if cmoTag === CMO'NULL then
                    185:          null
                    186:      else if cmoTag === CMO'INT32 then (
                    187:          -- next 4 bytes are an integer
                    188:          result = get32bits OX;
                    189:          if (result > 2^31 - 1) then result = result - 2^32;
                    190:          INT32 result)
                    191:      else if cmoTag === CMO'STRING then (
                    192:          len = get32bits OX;
                    193:          getBytes(OX,len))
                    194:      else if cmoTag === CMO'LIST then (
                    195:          len = get32bits OX;
                    196:          apply(len, i -> CMOfromBinary OX))
                    197:      else if cmoTag === CMO'ERROR2 then (
                    198:          CMOfromBinary OX)
                    199:      else if cmoTag === CMO'ZZ then (
                    200:          len = get32bits OX;
                    201:          if (len > 2^31-1) then len = len - 2^32;
                    202:          result = 0;
                    203:          i = 0;
                    204:          while (i < abs(len)) do (
                    205:               result = result + ( (get32bits OX) << (32*i) );
                    206:               i = i+1;
                    207:               );
                    208:          if (len < 0) then result = -result;
                    209:          result
                    210:          )
                    211:      else if cmoTag === CMO'QQ then (
                    212:          -- make the numerator
                    213:          len = get32bits OX;
                    214:          if (len > 2^31-1) then len = len - 2^32;
                    215:          num := 0;
                    216:          i := 0;
                    217:          while (i < abs(len)) do (
                    218:               num = num + ( (get32bits OX) << (32*i) );
                    219:               i = i+1;
                    220:               );
                    221:          if (len < 0) then num = -num;
                    222:          -- make the denominator
                    223:          len = get32bits OX;
                    224:          if (len > 2^31-1) then len = len - 2^32;
                    225:          den := 0;
                    226:          i = 0;
                    227:          while (i < abs(len)) do (
                    228:               den = den + ( (get32bits OX) << (32*i) );
                    229:               i = i+1;
                    230:               );
                    231:          if (len < 0) then den = -den;
                    232:          num/den
                    233:          )
                    234:      else if cmoTag === CMO'MATHCAP then (
                    235:          MATHCAP(CMOfromBinary OX)
                    236:          )
                    237:      else if cmoTag === CMO'RECURSIVE'POLYNOMIAL then (
                    238:          -- recursive'varlist is a global variable for now
                    239:          -- otherwise, we need way to pass it as a parameter in recursion
                    240:          recursive'varlist = apply(CMOfromBinary OX, i -> value i);
                    241:          CMOfromBinary OX
                    242:          )
                    243:      else if cmoTag === CMO'POLYNOMIAL'IN'ONE'VARIABLE then (
                    244:          m := get32bits(OX);
                    245:          v := get32bits(OX);
                    246:          i = 0;
                    247:          result = 0;
                    248:          while (i < m) do (
                    249:               exponent := get32bits(OX);
                    250:               result=result+(recursive'varlist_v)^exponent*CMOfromBinary(OX);
                    251:               i = i+1;
                    252:               );
                    253:          result
                    254:          )
                    255:      else if cmoTag === CMO'INDETERMINATE then (
                    256:          CMOfromBinary OX
                    257:          )
                    258:      else error "unknown CMO tag"
                    259:      )
                    260:
                    261: ---------------------------
                    262: -- CMObject --> binary ----
                    263: ---------------------------
                    264: CMOSerialize = method()
                    265:
                    266: CMOSerialize(File,List) := (F,x) -> (
                    267:      F << CMOLIST << toNetwork(#x);
                    268:      scan(x, a -> CMOSerialize(F,a)))
                    269:
                    270: CMOSerialize(File,ZZ) := (F,x) -> (
                    271:      -- BUG: write this
                    272:      temp := abs(x);
                    273:      stringform := "";
                    274:      i := 0;
                    275:      while (temp != 0) do (
                    276:          stringform = stringform | toNetwork(temp % (2^32));
                    277:          temp = temp >> 32;
                    278:          i = i+1;
                    279:          );
                    280:      if (i > 2^31-1) then error "integer too large to push";
                    281:      if (x < 0) then stringform = toNetwork(2^32-i) | stringform
                    282:      else stringform = toNetwork(i) | stringform;
                    283:      F << CMOZZ << stringform;
                    284:      )
                    285:
                    286: CMOSerialize(File,QQ) := (F,x) -> (
                    287:      -- BUG: write this
                    288:      temp := abs(numerator x);
                    289:      numstring := "";
                    290:      i := 0;
                    291:      while (temp != 0) do (
                    292:          numstring = numstring | toNetwork(temp % (2^32));
                    293:          temp = temp >> 32;
                    294:          i = i+1;
                    295:          );
                    296:      if (i > 2^31-1) then error "number too large to push";
                    297:      if (numerator x < 0) then numstring = toNetwork(2^32-i) | numstring
                    298:      else numstring = toNetwork(i) | numstring;
                    299:      temp = abs(denominator x);
                    300:      denstring := "";
                    301:      i = 0;
                    302:      while (temp != 0) do (
                    303:          denstring = denstring | toNetwork(temp % (2^32));
                    304:          temp = temp >> 32;
                    305:          i = i+1;
                    306:          );
                    307:      if (i > 2^31-1) then error "number too large to push";
                    308:      denstring = toNetwork(i) | denstring;
                    309:      F << CMOQQ << numstring << denstring;
                    310:      )
                    311:
                    312: CMOSerialize(File,INT32) := (F,x) -> (
                    313:      -- Recall that INT32 handles 2-complemented numbers already.
                    314:      F << CMOINT32 << toNetwork(x#0);
                    315:      )
                    316:
                    317: CMOSerialize(File,String) := (F,x) -> (
                    318:      F << CMOSTRING << toNetwork(#x) << x;
                    319:      )
                    320:
                    321: CMOSerialize(File,Nothing) := (F,x) -> (
                    322:      F << CMONULL;
                    323:      )
                    324:
                    325: CMOSerialize(File,ERROR) := (F,x) -> (
                    326:      F << CMOERROR2;
                    327:      CMOSerialize(F,toList x);
                    328:      )
                    329:
                    330: CMOSerialize(File,MATHCAP) := (F,x) -> (
                    331:      F << CMOMATHCAP;
                    332:      CMOSerialize(x#0);
                    333:      )
                    334:
                    335: CMOSerialize(File, RingElement) := (F, x) -> (
                    336:      -- make list of indeterminates
                    337:      L := apply(generators ring x, i -> toString i);
                    338:      L = apply(L, i -> (CMOINDETERMINATE | CMOSTRING | toNetwork(#i) | i));
                    339:      L = CMOLIST | toNetwork(#L) | demark("", L);
                    340:      F << CMORECURSIVEPOLYNOMIAL << L;
                    341:      -- bug: if 1_R is pushed, then 1_ZZ is popped
                    342:      if (sum mingle exponents x == 0) then (F << CMOPOLYNOMIALINONEVARIABLE
                    343:          << toNetwork(1) << toNetwork(0) << toNetwork(0));
                    344:      CMOSerializePolyInOneVar(F, x);
                    345:      )
                    346:
                    347: CMOSerializePolyInOneVar = (F, f) -> (
                    348:      local tmp;
                    349:      local allVars;
                    350:      -- number of variables appearing in f
                    351:      allVars = positions(sum exponents f, i -> i > 0);
                    352:      if (allVars === {}) then (
                    353:          CMOSerialize(F, substitute(f, coefficientRing ring f));
                    354:          )
                    355:      else (
                    356:          -- given a polynomial with only one variable x appearing,
                    357:          -- make list of sorted pairs where the first element is
                    358:          -- a monomial in x and the second is the coefficient
                    359:          tmp = coefficients({allVars#0}, f);
                    360:          tmp = sort pack(2, mingle {(entries tmp#0)#0, (entries tmp#1)#0});
                    361:          j := 0;
                    362:          F << CMOPOLYNOMIALINONEVARIABLE << toNetwork(#tmp) <<
                    363:          toNetwork(allVars#0);
                    364:          if (#allVars == 1) then (
                    365:               -- if f is a polynomial of a single variable
                    366:               while (j < #tmp) do (
                    367:                    F << toNetwork(sum mingle exponents tmp#j#0);
                    368:                    CMOSerialize(F, substitute(tmp#j#1, coefficientRing ring tmp#j#1));
                    369:                    j = j+1;
                    370:                    );
                    371:               )
                    372:          else (
                    373:               -- otherwise need to do recursion
                    374:               while(j < #tmp) do (
                    375:                    F << toNetwork(sum mingle exponents (tmp#j#0));
                    376:                    CMOSerializePolyInOneVar(F, tmp#j#1);
                    377:                    j = j+1;
                    378:                    );
                    379:               );
                    380:          );
                    381:      )

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>