#!/usr/bin/env python # Script to read M. Rubinstein's modular polynomials saved in # "compressed binary" form. See: # # http://www.math.uwaterloo.ca/~mrubinst/modularpolynomials/phi_l.html # # -- reads a ".bic" file and prints out the coefficients of the mod. pol. # # It can be used as: # # bic2dec.py phi_2.bic phi_2.dec # bic2dec.py phi_2.bic (prints to STDOUT) # bic2dec.py < phi_2.bic > phi_2.dec import struct, sys case=len(sys.argv) # no. of command line paramenters if case > 3: print "Usage: read_phi " elif case == 3: f = open(sys.argv[1], "rb") out = open(sys.argv[2],"w") elif case == 2: f = open(sys.argv[1], "rb") out = sys.stdout else: # case == 1 f = sys.stdin out = sys.stdout while True: # reads "len" header = f.read(4) if len(header) == 0: break #EOF # convert to an integer # # note that in principle "i" is unsgined, # but we get the correct sign this way... len_array = struct.unpack("i", header)[0] if len_array != 0: # non-zero coefficient # read other terms rest = f.read(6) # "H" = short int. (2 bytes) e2, e3, e5 = struct.unpack("HHH", rest) # print(len_array, e2, e3, e5) # get the sign if len_array > 0: sign=1 else: sign=-1 # terms of the sum in an array of bytes byte_array = f.read(abs(len_array)) # term is the last term of the product # that gives the coefficient (i.e., the sum) term, i = 0, 0 for b in byte_array: bi = struct.unpack("B", b)[0] term+=bi*(256**i) i+=1 # coefficient! # print(sign*(2**e2)*(3**e3)*(5**e5)*term) out.write(str(sign*(2**e2)*(3**e3)*(5**e5)*term) + "\n") else: # coefficient is zero # print(0) out.write(str(0) + "\n") f.close() out.close()