From d1405387983cb4c3bfa8afbdbc132c4369d7679a Mon Sep 17 00:00:00 2001 From: Cameron Neville Date: Tue, 20 Feb 2018 21:20:03 -0500 Subject: [PATCH] more work on field class --- GF.py | 80 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/GF.py b/GF.py index b40adc9..08ac3a7 100644 --- a/GF.py +++ b/GF.py @@ -1,4 +1,4 @@ -from math import log +import math #defines the field itself #responsible for creating elements and performing operations @@ -8,23 +8,16 @@ from math import log class GF: def __init__(self, *args): - def characteristic(a): - if a%2 == 0: - b = log(a, 2) - if b != int(b): - raise ValueError('%i is not a prime power' % a) - return 2, int(b) - i = 3 - while i <= 23: + def getcharacteristic(a): + for i in (2, 3, 5, 7, 11, 13, 17, 19, 23): if a%i == 0: - b = log(a, i) - if b != int(b): + b = math.log(a, i) + if not math.isclose(b, int(b+1e-6), abs_tol=1e-6) : raise ValueError('%i is not a prime power' % a) - return i, int(b) - i += 2 + return i, int(b+1e-6) if len(args) == 1: - p, n = characteristic(args[0]) + p, n = getcharacteristic(args[0]) elif len(args) > 2: raise TypeError('too many arguments') else: @@ -35,10 +28,57 @@ class GF: file = open('gf' + str(p) + '.txt', 'r') pol = file.readlines()[n-2] file.close() - pol = pol.split(',') - #integer coefficients of defining polynomial - #index is the exponent - self.poly = [None]*n + [1] + #removes newline then splits into individual terms + pol = pol[:-1].split(',') + #(exponent, coefficient) for each term of the polynomial + self.poly = [] + for i in pol: + if '(' in i: + a, b = i.split('(') + #constant term isn't given an exponent in the txt file + a = a if a else 0 + self.poly.append((int(a), int(b[:-1]))) + else: + self.poly.append((int(i),1)) if p == 2: - self.poly[0] = 1 - for i in pol \ No newline at end of file + self.poly.append((0,1)) + + + #creates a new element with the given value + def new(val): + + #splits integers into their binary components + def bisplit(bal): + pass + + if isisntance(val, int): + if self.characteristic != 2: + raise TypeError('integer definition of field elements is only allowed in fields of characteristic 2') + #covers the case that the value is an iterable of individual terms + #first term is for exponent zero, etc. + #accepts integers or strings + else: + pol = [0] * self.degree + for i in range(len(val)): + if isinstance(val[i], int): + pol[i] = val[i] + else: + #TODO for christs sake refactor this + try: + pol[i] = val[i] + except ValueError: + try: + pol[i] = int(val[i]) + except ValueError: + a = ord(val[i].tolower()) + if 97 <= a <= 122: + pol[i] = a - 87 + else: + raise ValueError('polynomial coefficient is uninterprettable') + + + + +class GFElement: + def __init_(self, value, field): + self.field = field \ No newline at end of file