Annotation of OpenXM/src/Macaulay2/m2/oxcommon.m2, Revision 1.1
1.1 ! takayama 1: ID = "$OpenXM$"
! 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
! 145: newstring := read (OX.F);
! 146: stderr << "Read " << #newstring << " bytes: " << ascii newstring
! 147: << endl << flush;
! 148: left := #OX.buffer - OX.loc;
! 149: if left > 0 then
! 150: OX.buffer = substring(OX.buffer, OX.loc, left)
! 151: | newstring
! 152: else
! 153: OX.buffer = newstring;
! 154: OX.loc = 0;
! 155: )
! 156:
! 157: get32bits = (OX) -> (
! 158: -- OX is an OXINPUT
! 159: if #OX.buffer < OX.loc + 4
! 160: then readMoreData OX;
! 161: result := fromNetwork(substring(OX.buffer, OX.loc, 4));
! 162: OX.loc = OX.loc + 4;
! 163: result
! 164: )
! 165:
! 166: getBytes = (OX,n) -> (
! 167: -- OX is an OXINPUT
! 168: if #OX.buffer < OX.loc + n
! 169: then readMoreData OX;
! 170: result := substring(OX.buffer, OX.loc, n);
! 171: OX.loc = OX.loc + n;
! 172: result)
! 173:
! 174: CMOfromBinary = (OX) -> (
! 175: -- OX is an OXINPUT
! 176: local result;
! 177: cmoTag := get32bits OX;
! 178: if cmoTag === CMO'NULL then
! 179: null
! 180: else if cmoTag === CMO'INT32 then (
! 181: -- next 4 bytes are an integer
! 182: result = get32bits OX;
! 183: if (result > 2^31 - 1) then result = result - 2^32;
! 184: INT32 result)
! 185: else if cmoTag === CMO'STRING then (
! 186: len = get32bits OX;
! 187: getBytes(OX,len))
! 188: else if cmoTag === CMO'LIST then (
! 189: len = get32bits OX;
! 190: apply(len, i -> CMOfromBinary OX))
! 191: else if cmoTag === CMO'ERROR2 then (
! 192: CMOfromBinary OX)
! 193: else if cmoTag === CMO'ZZ then (
! 194: len = get32bits OX;
! 195: if (len > 2^31-1) then len = len - 2^32;
! 196: result = 0;
! 197: i = 0;
! 198: while (i < abs(len)) do (
! 199: result = result + ( (get32bits OX) << (32*i) );
! 200: i = i+1;
! 201: );
! 202: if (len < 0) then result = -result;
! 203: result
! 204: )
! 205: else if cmoTag === CMO'QQ then (
! 206: -- make the numerator
! 207: len = get32bits OX;
! 208: if (len > 2^31-1) then len = len - 2^32;
! 209: num := 0;
! 210: i := 0;
! 211: while (i < abs(len)) do (
! 212: num = num + ( (get32bits OX) << (32*i) );
! 213: i = i+1;
! 214: );
! 215: if (len < 0) then num = -num;
! 216: -- make the denominator
! 217: len = get32bits OX;
! 218: if (len > 2^31-1) then len = len - 2^32;
! 219: den := 0;
! 220: i = 0;
! 221: while (i < abs(len)) do (
! 222: den = den + ( (get32bits OX) << (32*i) );
! 223: i = i+1;
! 224: );
! 225: if (len < 0) then den = -den;
! 226: num/den
! 227: )
! 228: else if cmoTag === CMO'MATHCAP then (
! 229: MATHCAP(CMOfromBinary OX)
! 230: )
! 231: else if cmoTag === CMO'RECURSIVE'POLYNOMIAL then (
! 232: -- recursive'varlist is a global variable for now
! 233: -- otherwise, we need way to pass it as a parameter in recursion
! 234: recursive'varlist = apply(CMOfromBinary OX, i -> value i);
! 235: CMOfromBinary OX
! 236: )
! 237: else if cmoTag === CMO'POLYNOMIAL'IN'ONE'VARIABLE then (
! 238: m := get32bits(OX);
! 239: v := get32bits(OX);
! 240: i = 0;
! 241: result = 0;
! 242: while (i < m) do (
! 243: exponent := get32bits(OX);
! 244: result=result+(recursive'varlist_v)^exponent*CMOfromBinary(OX);
! 245: i = i+1;
! 246: );
! 247: result
! 248: )
! 249: else if cmoTag === CMO'INDETERMINATE then (
! 250: CMOfromBinary OX
! 251: )
! 252: else error "unknown CMO tag"
! 253: )
! 254:
! 255: ---------------------------
! 256: -- CMObject --> binary ----
! 257: ---------------------------
! 258: CMOSerialize = method()
! 259:
! 260: CMOSerialize(File,List) := (F,x) -> (
! 261: F << CMOLIST << toNetwork(#x);
! 262: scan(x, a -> CMOSerialize(F,a)))
! 263:
! 264: CMOSerialize(File,ZZ) := (F,x) -> (
! 265: -- BUG: write this
! 266: temp := abs(x);
! 267: stringform := "";
! 268: i := 0;
! 269: while (temp != 0) do (
! 270: stringform = stringform | toNetwork(temp % (2^32));
! 271: temp = temp >> 32;
! 272: i = i+1;
! 273: );
! 274: if (i > 2^31-1) then error "integer too large to push";
! 275: if (x < 0) then stringform = toNetwork(2^32-i) | stringform
! 276: else stringform = toNetwork(i) | stringform;
! 277: F << CMOZZ << stringform;
! 278: )
! 279:
! 280: CMOSerialize(File,QQ) := (F,x) -> (
! 281: -- BUG: write this
! 282: temp := abs(numerator x);
! 283: numstring := "";
! 284: i := 0;
! 285: while (temp != 0) do (
! 286: numstring = numstring | toNetwork(temp % (2^32));
! 287: temp = temp >> 32;
! 288: i = i+1;
! 289: );
! 290: if (i > 2^31-1) then error "number too large to push";
! 291: if (numerator x < 0) then numstring = toNetwork(2^32-i) | numstring
! 292: else numstring = toNetwork(i) | numstring;
! 293: temp = abs(denominator x);
! 294: denstring := "";
! 295: i = 0;
! 296: while (temp != 0) do (
! 297: denstring = denstring | toNetwork(temp % (2^32));
! 298: temp = temp >> 32;
! 299: i = i+1;
! 300: );
! 301: if (i > 2^31-1) then error "number too large to push";
! 302: denstring = toNetwork(i) | denstring;
! 303: F << CMOQQ << numstring << denstring;
! 304: )
! 305:
! 306: CMOSerialize(File,INT32) := (F,x) -> (
! 307: -- Recall that INT32 handles 2-complemented numbers already.
! 308: F << CMOINT32 << toNetwork(x#0);
! 309: )
! 310:
! 311: CMOSerialize(File,String) := (F,x) -> (
! 312: F << CMOSTRING << toNetwork(#x) << x;
! 313: )
! 314:
! 315: CMOSerialize(File,Nothing) := (F,x) -> (
! 316: F << CMONULL;
! 317: )
! 318:
! 319: CMOSerialize(File,ERROR) := (F,x) -> (
! 320: F << CMOERROR2;
! 321: CMOSerialize(F,toList x);
! 322: )
! 323:
! 324: CMOSerialize(File,MATHCAP) := (F,x) -> (
! 325: F << CMOMATHCAP;
! 326: CMOSerialize(x#0);
! 327: )
! 328:
! 329: CMOSerialize(File, RingElement) := (F, x) -> (
! 330: -- make list of indeterminates
! 331: L := apply(generators ring x, i -> toString i);
! 332: L = apply(L, i -> (CMOINDETERMINATE | CMOSTRING | toNetwork(#i) | i));
! 333: L = CMOLIST | toNetwork(#L) | demark("", L);
! 334: F << CMORECURSIVEPOLYNOMIAL << L;
! 335: -- bug: if 1_R is pushed, then 1_ZZ is popped
! 336: if (sum mingle exponents x == 0) then (F << CMOPOLYNOMIALINONEVARIABLE
! 337: << toNetwork(1) << toNetwork(0) << toNetwork(0));
! 338: CMOSerializePolyInOneVar(F, x);
! 339: )
! 340:
! 341: CMOSerializePolyInOneVar = (F, f) -> (
! 342: local tmp;
! 343: local allVars;
! 344: -- number of variables appearing in f
! 345: allVars = positions(sum exponents f, i -> i > 0);
! 346: if (allVars === {}) then (
! 347: CMOSerialize(F, substitute(f, coefficientRing ring f));
! 348: )
! 349: else (
! 350: -- given a polynomial with only one variable x appearing,
! 351: -- make list of sorted pairs where the first element is
! 352: -- a monomial in x and the second is the coefficient
! 353: tmp = coefficients({allVars#0}, f);
! 354: tmp = sort pack(2, mingle {(entries tmp#0)#0, (entries tmp#1)#0});
! 355: j := 0;
! 356: F << CMOPOLYNOMIALINONEVARIABLE << toNetwork(#tmp) <<
! 357: toNetwork(allVars#0);
! 358: if (#allVars == 1) then (
! 359: -- if f is a polynomial of a single variable
! 360: while (j < #tmp) do (
! 361: F << toNetwork(sum mingle exponents tmp#j#0);
! 362: CMOSerialize(F, substitute(tmp#j#1, coefficientRing ring tmp#j#1));
! 363: j = j+1;
! 364: );
! 365: )
! 366: else (
! 367: -- otherwise need to do recursion
! 368: while(j < #tmp) do (
! 369: F << toNetwork(sum mingle exponents (tmp#j#0));
! 370: CMOSerializePolyInOneVar(F, tmp#j#1);
! 371: j = j+1;
! 372: );
! 373: );
! 374: );
! 375: )
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>