0

これは、入力として受け取り、多項式を文字列形式で出力するクラスです (どちらも同じ形式)。いくつかの演算は、さまざまな方法で実行されます。このクラスを別のクラスに継承して、最初のクラスの __mod__() 特殊メソッドを使用しようとしました (または、必要に応じて独自の特殊メソッドを作成しますが、元のクラスを使用できない方法がわかりません)メソッド) インテークで mod を実行します。これは __init__() に入るようですが、親クラスを変更するまで、これの5つの異なるバージョンを試しましたが、どこにも行きません。私は独学で Python を学んでいるので、経験の浅い Python 開発者でも、どこが間違っているのかわかるはずです。

import re

class GF2Polynomial(object): #classes should generally inherit from object

    def __init__(self, string):
        '''__init__ is a standard special method used to initialize objects.
        Here __init__ will initialize a gf2infix object based on a string.'''
        self.string = string  #basically the initial string (polynomial)
        self.key,self.lst = self.parsePolyVariable(string) # key determines polynomial compatibility
        self.bin = self.prepBinary(string)  #main value used in operations

    def id(self,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(self,lst):
        """converts list to integer for later use"""
        result = self.id(lst)
        return int(''.join(map(str,result)))

    def parsePolyToListInput(self,poly):
        """
        replaced by parsePolyVariable. still functional but not needed.
        performs regex on raw string and converts to list
        """
        c = [int(i.group(0)) for i in re.finditer(r'\d+', poly)]
        return [1 if x in c else 0  for x in xrange(max(c), -1, -1)]

    def parsePolyVariable(self,poly):
        """
        performs regex on raw string, converts to list.
        also determines key (main variable used) in each polynomial on intake
        """
        c = [int(m.group(0)) for m in re.finditer(r'\d+', poly)] #re.finditer returns an iterator
        letter = [str(m.group(0)) for m in re.finditer(r'[a-z]', poly)]
        m = max(c); varmatch = True; key = letter[0]
        for i in range(len(letter)):
            if letter[i] != key: varmatch = False
            else: varmatch = True
        if varmatch == False: return "error: not all variables in %s are the same"%a
        d = [1 if x in c else (1 if x==0 else (1 if x=='x' else 0))  for x in xrange(m, -1, -1)]
        return key,d

    def polyVariableCheck(self,other):
        return self.key == other.key

    def prepBinary(self,poly):
        """converts to base 2; bina,binb are binary values like 110100101100....."""
        x = self.lst; a = self.listToInt(x)
        return int(str(a),2)

    def __mod__(self,other):
        """
        __mod__ is the special method for overriding the % operator
        returns remainder formatted as polynomial
        """
        if self.polyVariableCheck(other) == False:
            return "error: variables of %s and %s do not match"%(self.string,other.string)
        if self.bin == other.bin: return 0
        return GF2Polynomial(self.outFormat(self.bin%other.bin))

    def __str__(self):
        return self.string

    def outFormat(self,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]))
        proc1 = processed.replace("x**1","x"); proc2 = proc1.replace("x**0","1")
        if len(g) == 0: return 0 #return 0 if list empty
        return proc2  #returns result in gf(2) polynomial form

望ましい結果は、親の型を持つ新しい (子の) クラスで、親のクラスをできるだけ変更せずに (たとえあったとしても) 呼び出すことができるようにすることです。クラス "BinaryField" が対象の子クラスであることに注意してください。

p=GF2Polynomial("x**2+x**1+x**0")
a=BinaryField("x**1+x**0", p)
b=BinaryField("x**1", p)

取り込み時に、与えられた多項式はモジュラスを 2 番目の要素 (ここでは「p」) で除算したものでなければなりません。これは、有限体演算に必要です。

編集:-で実行する場合

## "x**1 + x**0" polynomial string style input
poly1 = "x**14 + x**1 + x**0"; poly2 = "x**6 + x**2 + x**1"; poly3 = "y**6 + y**2 + y**1"
a = GF2Polynomial(poly1); b = GF2Polynomial(poly2); c = GF2Polynomial(poly3)
## "x+1" polynomial string style input
poly4 = "x**14 + x + 1"; poly5 = "x**6 + x**2 + x"; poly6 = "y**6 + y**2 + 1"
d = GF2Polynomial(poly4); e = GF2Polynomial(poly5); f = GF2Polynomial(poly6)
bf1 = BinaryField(poly1,b); print bf1
bf2 = BinaryField(poly4,e); print bf2

これらのスタイルは両方とも、コーディング方法によって可能ですが、どちらも同じ答えを返すはずです。ただし、そのコードの結果は次のとおりです。

>>> 
x**5 + x**4 + x**3 + 1
x**5 + x

また、GF2Polynomial() の初期化と同じ文字列である BinaryField(poly4,d) を使用すると、次のようなエラーが発生します。 AttributeError: 'int' object has no attribute 'string'

4

1 に答える 1

1

これで問題は解決しますか?

class BinaryField(GF2Polynomial):
    def __init__(self, string, mod):
        modded = GF2Polynomial(string) % mod
        super(BinaryField, self).__init__(modded.string)


>>> p = GF2Polynomial("x**2+x**1+x**0")
>>> a = BinaryField("x**1+x**0", p)
>>> print a
x + 1

BinaryFieldクラスを単なるファクトリ メソッドにすることもできます。

def BinaryField(string, mod):
    return GF2Polynomial(string) % mod
于 2013-08-09T23:36:27.980 に答える