mirror of
https://github.com/Ikatono/Galois.git
synced 2025-10-29 04:45:31 -05:00
more work on field class
This commit is contained in:
80
GF.py
80
GF.py
@@ -1,4 +1,4 @@
|
|||||||
from math import log
|
import math
|
||||||
|
|
||||||
#defines the field itself
|
#defines the field itself
|
||||||
#responsible for creating elements and performing operations
|
#responsible for creating elements and performing operations
|
||||||
@@ -8,23 +8,16 @@ from math import log
|
|||||||
class GF:
|
class GF:
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
|
|
||||||
def characteristic(a):
|
def getcharacteristic(a):
|
||||||
if a%2 == 0:
|
for i in (2, 3, 5, 7, 11, 13, 17, 19, 23):
|
||||||
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:
|
|
||||||
if a%i == 0:
|
if a%i == 0:
|
||||||
b = log(a, i)
|
b = math.log(a, i)
|
||||||
if b != int(b):
|
if not math.isclose(b, int(b+1e-6), abs_tol=1e-6) :
|
||||||
raise ValueError('%i is not a prime power' % a)
|
raise ValueError('%i is not a prime power' % a)
|
||||||
return i, int(b)
|
return i, int(b+1e-6)
|
||||||
i += 2
|
|
||||||
|
|
||||||
if len(args) == 1:
|
if len(args) == 1:
|
||||||
p, n = characteristic(args[0])
|
p, n = getcharacteristic(args[0])
|
||||||
elif len(args) > 2:
|
elif len(args) > 2:
|
||||||
raise TypeError('too many arguments')
|
raise TypeError('too many arguments')
|
||||||
else:
|
else:
|
||||||
@@ -35,10 +28,57 @@ class GF:
|
|||||||
file = open('gf' + str(p) + '.txt', 'r')
|
file = open('gf' + str(p) + '.txt', 'r')
|
||||||
pol = file.readlines()[n-2]
|
pol = file.readlines()[n-2]
|
||||||
file.close()
|
file.close()
|
||||||
pol = pol.split(',')
|
#removes newline then splits into individual terms
|
||||||
#integer coefficients of defining polynomial
|
pol = pol[:-1].split(',')
|
||||||
#index is the exponent
|
#(exponent, coefficient) for each term of the polynomial
|
||||||
self.poly = [None]*n + [1]
|
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:
|
if p == 2:
|
||||||
self.poly[0] = 1
|
self.poly.append((0,1))
|
||||||
for i in pol
|
|
||||||
|
|
||||||
|
#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
|
||||||
Reference in New Issue
Block a user