我正在研究一组功能,这些功能可能更容易单独管理,只是在一个模块中。但是,除了我的薪水等级之外,还有一些原因想考虑把它变成一门课。但是,由于它是从多项式输入转换为字符串,因此正则表达式适用于字符串,但是一旦输入是类实例,它就会决定在此之后退出。
所以我的问题是如何将它变成一个类并仍然保持功能(如何初始化它等)。或者,如果这种情况更适合作为一个模块,那么我可以争辩说,但我需要完全理解。我检查了这些,但我不确定我是否能从它们中得到足够的东西来解决这个问题:
http://docs.python.org/2/tutorial/classes.html
import re
def id(lst): #returns modulus 2 (1,0,0,1,1,....) for input lists
return [int(lst[i])%2 for i in range(len(lst))]
def listToInt(lst): #converts list to integer for later use
result = id(lst)
return int(''.join(map(str,result)))
def parsePolyToListInput(poly):
c = [int(i.group(0)) for i in re.finditer(r'\d+', poly)] #re.finditer returns an iterator
return [1 if x in c else 0 for x in xrange(max(c), -1, -1)]
def prepBinary(x,y): #converts to base 2 and orders min and max for use
x = parsePolyToListInput(x); y = parsePolyToListInput(y)
a = listToInt(x); b = listToInt(y)
bina = int(str(a),2); binb = int(str(b),2)
#a = min(bina,binb); b = max(bina,binb);
return bina,binb #bina,binb are binary values like 110100101100.....
def add(a,b): # a,b are GF(2) polynomials like x**7 + x**3 + x**0 ....
bina,binb = prepBinary(a,b)
return outFormat(bina^binb) #returns binary string
def subtract(x,y): # same as addition in GF(2)
return add(x,y)
def multiply(a,b): # a,b are GF(2) polynomials like x**7 + x**3 + x**0 ....
a,b = prepBinary(a,b)
return outFormat(a*b) #returns product of 2 polynomials in gf2
def divide(a,b): #a,b are GF(2) polynomials like x**7 + x**3 + x**0 ....
a,b = prepBinary(a,b)
#bitsa = "{0:b}".format(a); bitsb = "{0:b}".format(b)
return outFormat(a/b),outFormat(a%b) #returns remainder and quotient formatted as polynomials
def quotient(a,b): #separate quotient function for clarity when calling
return divide(a,b)[1]
def remainder(a,b): #separate remainder function for clarity when calling
return divide(a,b)[0]
def outFormat(raw): # process resulting values into polynomial format
raw = "{0:b}".format(raw); raw = str(raw[::-1]); g = [] #reverse binary string for enumeration
g = [i for i,c in enumerate(raw) if c == '1']
processed = "x**"+" + x**".join(map(str, g[::-1]))
if len(g) == 0: return 0 #return 0 if list empty
return processed #returns result in gf(2) polynomial form
def extendedEuclideanGF2(a,b): # extended euclidean. a,b are values 10110011... in integer form
inita,initb=a,b; x,prevx=0,1; y,prevy = 1,0
while b != 0:
q = int("{0:b}".format(a//b),2)
a,b = b,int("{0:b}".format(a%b),2);
x,prevx = (int("{0:b}".format(prevx-q*x)), int("{0:b}".format(x,2))); y,prevy=(prevy-q*y, y)
#print("%d * %d + %d * %d = %d" % (inita,prevx,initb,prevy,a))
return a,prevx,prevy # returns gcd of (a,b), and factors s and t
def modular_inverse(a,mod): # a,mod are GF(2) polynomials like x**7 + x**3 + x**0 ....
a,mod = prepBinary(a,mod)
bitsa = int("{0:b}".format(a),2); bitsb = int("{0:b}".format(mod),2)
#return bitsa,bitsb,type(bitsa),type(bitsb),a,mod,type(a),type(mod)
gcd,s,t = extendedEuclideanGF2(a,mod); s = int("{0:b}".format(s))
initmi = s%mod; mi = int("{0:b}".format(initmi))
print ("%d * %d mod %d = 1"%(a,initmi,mod))
if gcd !=1: return outFormat(mi),False
return outFormat(mi) # returns modular inverse of a,mod
a = "x**14 + x**1 + x**0"; b = "x**6 + x**2 + x**1"
c = "x**2 + x**1 + x**0"; d = "x**3 + x**1 + x**0"
e = "x**3 + x**2 + x**1 + x**0"; f = "x**2"; g = "x**1 + x**0"
p = "x**13 + x**1 + x**0"; q = "x**12 + x**1"
print "add: [%s] + [%s] = %s "%(a,b,add(a,b))
print "add: [%s] + [%s] = %s "%(c,d,add(c,d))
print "multiply: [%s] * [%s] = %s "%(a,b,multiply(a,b))
print "multiply: [%s] * [%s] = %s "%(c,d,multiply(c,d))
print "multiply: [%s] * [%s] = %s "%(f,g,multiply(f,g))
print "quotient (max(a,b)/min(a,b): [%s] / [%s] = %s "%(a,b,quotient(a,b))
print "quotient (max(a,b)/min(a,b): [%s] / [%s] = %s "%(c,d,quotient(c,d))
print "remainder (max(a,b) mod min(a,b)): [%s] mod [%s] = %s "%(a,b,remainder(a,b))
print "remainder (max(a,b) mod min(a,b): [%s] mod [%s] = %s "%(c,d,remainder(c,d))
valuemi1 = modular_inverse(a,b)
print "modular_inverse: [%s] * [%s] mod [%s] = 1 [%s]"%(a,valuemi1[0],b,valuemi1[1])
valuemi2 = modular_inverse(p,q)
print "modular_inverse: [%s] * [%s] mod [%s] = 1 [%s]"%(p,valuemi2[0],q,valuemi2[1])
每个算术运算只接受字符串格式的 GF(2) 多项式,然后将其解析为相应的二进制值,然后将其发送到 outFormat() 以转换回多项式。现在工作正常,所以如果它没有坏,我倾向于不修复它。