inheritance - Python -- using __init__ with an inherited method for polynomials class -


this class take in input , output polynomial in string form (both ways same format). arithmetic performed in various methods. i've been trying inherit class class use __mod__() special method of first class (or make it's own special method if necessary don't see how can't use original method) perform mod on intake. seems goes __init__() i've tried 5 different versions of this, going far change parent class, , i'm getting nowhere. i'm teaching myself python i'm sure junior python dev can see i'm going totally wrong.

import re  class gf2polynomial(object): #classes should inherit object      def __init__(self, string):         '''__init__ standard special method used initialize objects.         here __init__ initialize gf2infix object based on string.'''         self.string = string  #basically 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,....) input lists"""         return [int(lst[i])%2 in range(len(lst))]      def listtoint(self,lst):         """converts list integer later use"""         result = self.id(lst)         return int(''.join(map(str,result)))      def parsepolytolistinput(self,poly):         """         replaced parsepolyvariable. still functional not needed.         performs regex on raw string , converts list         """         c = [int(i.group(0)) in re.finditer(r'\d+', poly)]         return [1 if x in c else 0  x in xrange(max(c), -1, -1)]      def parsepolyvariable(self,poly):         """         performs regex on raw string, converts list.         determines key (main variable used) in each polynomial on intake         """         c = [int(m.group(0)) m in re.finditer(r'\d+', poly)] #re.finditer returns iterator         letter = [str(m.group(0)) m in re.finditer(r'[a-z]', poly)]         m = max(c); varmatch = true; key = letter[0]         in range(len(letter)):             if letter[i] != key: varmatch = false             else: varmatch = true         if varmatch == false: return "error: not variables in %s same"%a         d = [1 if x in c else (1 if x==0 else (1 if x=='x' else 0))  x in xrange(m, -1, -1)]         return key,d      def polyvariablecheck(self,other):         return self.key == other.key      def prepbinary(self,poly):         """converts base 2; bina,binb binary values 110100101100....."""         x = self.lst; = self.listtoint(x)         return int(str(a),2)      def __mod__(self,other):         """         __mod__ special method overriding % operator         returns remainder formatted polynomial         """         if self.polyvariablecheck(other) == false:             return "error: variables of %s , %s 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 polynomial format"""         raw = "{0:b}".format(raw); raw = str(raw[::-1]); g = [] #reverse binary string enumeration         g = [i 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 

the desired result able call on new (child) class parent type , while changing parent class little possible (if @ all). note class "binaryfield" intended child class:

p=gf2polynomial("x**2+x**1+x**0") a=binaryfield("x**1+x**0", p) b=binaryfield("x**1", p) 

on intake, given polynomial should modulus divided 2nd element (here it's 'p'). necessary finite field math.

edit: when running --

## "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" = 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 

both of these styles possible because of way coded it, should both return same answer. result on code is:

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

also, when using binaryfield(poly4,d), same string it's gf2polynomial() initialization, errors as: attributeerror: 'int' object has no attribute 'string'

does solves problem?

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") >>> = binaryfield("x**1+x**0", p) >>> print x + 1 

you can make binaryfield class factory method:

def binaryfield(string, mod):     return gf2polynomial(string) % mod 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -