モジュール内で、おそらく単独で管理する方が簡単な関数のグループに取り組んでいます。しかし、これをクラスに変えることを考えたい私の給与等級を超えた理由があります. ただし、多項式入力から文字列として変換しているため、正規表現は文字列で機能しますが、入力がクラスインスタンスになると、その後終了するだけです。
したがって、私の質問は、これをクラスに変換し、機能を維持する方法 (初期化方法など) です。または、このケースがモジュールであることに適している場合は、それを主張できますが、完全に理解する必要があります. これらをチェックアウトしましたが、この問題を解決するのに十分な情報が得られているかどうかはよくわかりません:
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() を送信します。現在は問題なく動作しているので、壊れていなければ修正しないことに傾いています。