Skip to content

Commit

Permalink
Use public properties instead of internal attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Apr 22, 2022
1 parent ec011ec commit 392aeaf
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 104 deletions.
2 changes: 1 addition & 1 deletion galois/_codes/_bch.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def decode(self, codeword, errors=False):
syndrome = codeword.view(self.field) @ self.H[:,-ns:].T

if self.field.ufunc_mode != "python-calculate":
dec_codeword = self._decode_jit(codeword.astype(np.int64), syndrome.astype(np.int64), self.t, int(self.field.primitive_element), self._add_jit, self._subtract_jit, self._multiply_jit, self._reciprocal_jit, self._power_jit, self._berlekamp_massey_jit, self._poly_roots_jit, self.field.characteristic, self.field.degree, self.field._irreducible_poly_int)
dec_codeword = self._decode_jit(codeword.astype(np.int64), syndrome.astype(np.int64), self.t, int(self.field.primitive_element), self._add_jit, self._subtract_jit, self._multiply_jit, self._reciprocal_jit, self._power_jit, self._berlekamp_massey_jit, self._poly_roots_jit, self.field.characteristic, self.field.degree, int(self.field.irreducible_poly))
N_errors = dec_codeword[:, -1]

if self.systematic:
Expand Down
2 changes: 1 addition & 1 deletion galois/_codes/_reed_solomon.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ def decode(self, codeword, errors=False):
syndrome = codeword.view(self.field) @ self.H[:,-ns:].T

if self.field.ufunc_mode != "python-calculate":
dec_codeword = self._decode_jit(codeword.astype(np.int64), syndrome.astype(np.int64), self.c, self.t, int(self.field.primitive_element), self._add_jit, self._subtract_jit, self._multiply_jit, self._reciprocal_jit, self._power_jit, self._berlekamp_massey_jit, self._poly_roots_jit, self._poly_eval_jit, self._convolve_jit, self.field.characteristic, self.field.degree, self.field._irreducible_poly_int)
dec_codeword = self._decode_jit(codeword.astype(np.int64), syndrome.astype(np.int64), self.c, self.t, int(self.field.primitive_element), self._add_jit, self._subtract_jit, self._multiply_jit, self._reciprocal_jit, self._power_jit, self._berlekamp_massey_jit, self._poly_roots_jit, self._poly_eval_jit, self._convolve_jit, self.field.characteristic, self.field.degree, int(self.field.irreducible_poly))
N_errors = dec_codeword[:, -1]

if self.systematic:
Expand Down
24 changes: 12 additions & 12 deletions galois/_fields/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ def _verify_array_like_types_and_values(cls, x: Union[ElementLike, ArrayLike]) -
if x.dtype == np.object_:
x = cls._verify_element_types_and_convert(x, object_=True)
elif not np.issubdtype(x.dtype, np.integer):
raise TypeError(f"{cls._name} arrays must have integer dtypes, not {x.dtype}.")
raise TypeError(f"{cls.name} arrays must have integer dtypes, not {x.dtype}.")
cls._verify_array_values(x)
else:
raise TypeError(f"{cls._name} arrays can be created with scalars of type int/str, lists/tuples, or ndarrays, not {type(x)}.")
raise TypeError(f"{cls.name} arrays can be created with scalars of type int/str, lists/tuples, or ndarrays, not {type(x)}.")

return x

Expand All @@ -145,18 +145,18 @@ def _verify_scalar_value(cls, scalar: int):
"""
Verify the single integer element is within the valid range [0, order).
"""
if not 0 <= scalar < cls._order:
raise ValueError(f"{cls._name} scalars must be in `0 <= x < {cls._order}`, not {scalar}.")
if not 0 <= scalar < cls.order:
raise ValueError(f"{cls.name} scalars must be in `0 <= x < {cls.order}`, not {scalar}.")

@classmethod
def _verify_array_values(cls, array: np.ndarray):
"""
Verify all the elements of the integer array are within the valid range [0, order).
"""
if np.any(array < 0) or np.any(array >= cls._order):
idxs = np.logical_or(array < 0, array >= cls._order)
if np.any(array < 0) or np.any(array >= cls.order):
idxs = np.logical_or(array < 0, array >= cls.order)
values = array if array.ndim == 0 else array[idxs]
raise ValueError(f"{cls._name} arrays must have elements in `0 <= x < {cls._order}`, not {values}.")
raise ValueError(f"{cls.name} arrays must have elements in `0 <= x < {cls.order}`, not {values}.")

###############################################################################
# Element conversion routines
Expand All @@ -170,7 +170,7 @@ def _convert_to_element(cls, element: ElementLike) -> int:
if isinstance(element, (int, np.integer)):
element = int(element)
elif isinstance(element, str):
element = str_to_integer(element, cls._prime_subfield)
element = str_to_integer(element, cls.prime_subfield)
elif isinstance(element, FieldArray):
element = int(element)
else:
Expand Down Expand Up @@ -1364,7 +1364,7 @@ def field_trace(self) -> "FieldArray":
subfield = field.prime_subfield
p = field.characteristic
m = field.degree
conjugates = np.power.outer(x, p**np.arange(0, m, dtype=field._dtypes[-1]))
conjugates = np.power.outer(x, p**np.arange(0, m, dtype=field.dtypes[-1]))
trace = np.add.reduce(conjugates, axis=-1)
return subfield._view(trace)

Expand Down Expand Up @@ -1410,9 +1410,9 @@ def field_norm(self) -> "FieldArray":
if field.is_prime_field:
return x.copy()
else:
subfield = field._prime_subfield
p = field._characteristic
m = field._degree
subfield = field.prime_subfield
p = field.characteristic
m = field.degree
norm = x**((p**m - 1) // (p - 1))
return subfield._view(norm)

Expand Down
58 changes: 29 additions & 29 deletions galois/_fields/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _function(cls, name):
Returns the function for the specific routine. The function compilation is based on `ufunc_mode`.
"""
if name not in cls._functions:
if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
cls._functions[name] = cls._function_calculate(name)
else:
cls._functions[name] = cls._function_python(name)
Expand Down Expand Up @@ -151,21 +151,21 @@ def _fft(cls, x, n=None, axis=-1, norm=None, forward=True, scaled=True):
if not forward:
omega = omega ** -1

if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
x = x.astype(np.int64)
omega = np.int64(omega)
add = cls._func_calculate("add")
subtract = cls._func_calculate("subtract")
multiply = cls._func_calculate("multiply")
y = cls._function("dft_jit")(x, omega, add, subtract, multiply, cls._characteristic, cls._degree, cls._irreducible_poly_int)
y = cls._function("dft_jit")(x, omega, add, subtract, multiply, cls.characteristic, cls.degree, int(cls.irreducible_poly))
y = y.astype(dtype)
else:
x = x.view(np.ndarray)
omega = int(omega)
add = cls._func_python("add")
subtract = cls._func_python("subtract")
multiply = cls._func_python("multiply")
y = cls._function("dft_python")(x, omega, add, subtract, multiply, cls._characteristic, cls._degree, cls._irreducible_poly_int)
y = cls._function("dft_python")(x, omega, add, subtract, multiply, cls.characteristic, cls.degree, int(cls.irreducible_poly))
y = field._view(y)

# Scale the inverse NTT such that x = INTT(NTT(x))
Expand Down Expand Up @@ -210,19 +210,19 @@ def _matmul(cls, A, B, out=None, **kwargs): # pylint: disable=unused-argument
# new_shape = list(B.shape[:-2]) + list(A.shape)
# A = np.broadcast_to(A, new_shape)

if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
A = A.astype(np.int64)
B = B.astype(np.int64)
add = cls._func_calculate("add")
multiply = cls._func_calculate("multiply")
C = cls._function("matmul")(A, B, add, multiply, cls._characteristic, cls._degree, cls._irreducible_poly_int)
C = cls._function("matmul")(A, B, add, multiply, cls.characteristic, cls.degree, int(cls.irreducible_poly))
C = C.astype(dtype)
else:
A = A.view(np.ndarray)
B = B.view(np.ndarray)
add = cls._func_python("add")
multiply = cls._func_python("multiply")
C = cls._function("matmul")(A, B, add, multiply, cls._characteristic, cls._degree, cls._irreducible_poly_int)
C = cls._function("matmul")(A, B, add, multiply, cls.characteristic, cls.degree, int(cls.irreducible_poly))
C = field._view(C)

shape = list(C.shape)
Expand All @@ -249,17 +249,17 @@ def _convolve(cls, a, b, mode="full"):
field = type(a)
dtype = a.dtype

if cls._ufunc_mode == "python-calculate":
if cls.ufunc_mode == "python-calculate":
a = a.view(np.ndarray)
b = b.view(np.ndarray)
add = cls._func_python("add")
multiply = cls._func_python("multiply")
c = cls._function("convolve")(a, b, add, multiply, cls._characteristic, cls._degree, cls._irreducible_poly_int)
c = cls._function("convolve")(a, b, add, multiply, cls.characteristic, cls.degree, int(cls.irreducible_poly))
elif field.is_prime_field:
# Determine the minimum dtype to hold the entire product and summation without overflowing
n_sum = min(a.size, b.size)
max_value = n_sum * (field.characteristic - 1)**2
dtypes = [dtype for dtype in cls._dtypes if np.iinfo(dtype).max >= max_value]
dtypes = [dtype for dtype in cls.dtypes if np.iinfo(dtype).max >= max_value]
dtype = np.object_ if len(dtypes) == 0 else dtypes[0]
return_dtype = a.dtype
a = a.view(np.ndarray).astype(dtype)
Expand All @@ -272,7 +272,7 @@ def _convolve(cls, a, b, mode="full"):
b = b.astype(np.int64)
add = cls._func_calculate("add")
multiply = cls._func_calculate("multiply")
c = cls._function("convolve")(a, b, add, multiply, cls._characteristic, cls._degree, cls._irreducible_poly_int)
c = cls._function("convolve")(a, b, add, multiply, cls.characteristic, cls.degree, int(cls.irreducible_poly))
c = c.astype(dtype)
c = field._view(c)

Expand All @@ -285,19 +285,19 @@ def _poly_evaluate(cls, coeffs, x):
shape = x.shape
x = np.atleast_1d(x.flatten())

if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
coeffs = coeffs.astype(np.int64)
x = x.astype(np.int64)
add = cls._func_calculate("add")
multiply = cls._func_calculate("multiply")
results = cls._function("poly_evaluate")(coeffs, x, add, multiply, cls._characteristic, cls._degree, cls._irreducible_poly_int)
results = cls._function("poly_evaluate")(coeffs, x, add, multiply, cls.characteristic, cls.degree, int(cls.irreducible_poly))
results = results.astype(dtype)
else:
coeffs = coeffs.view(np.ndarray)
x = x.view(np.ndarray)
add = cls._func_python("add")
multiply = cls._func_python("multiply")
results = cls._function("poly_evaluate")(coeffs, x, add, multiply, cls._characteristic, cls._degree, cls._irreducible_poly_int)
results = cls._function("poly_evaluate")(coeffs, x, add, multiply, cls.characteristic, cls.degree, int(cls.irreducible_poly))
results = field._view(results)
results = results.reshape(shape)

Expand Down Expand Up @@ -327,21 +327,21 @@ def _poly_divmod(cls, a, b):
q_degree = a.shape[-1] - b.shape[-1]
r_degree = b.shape[-1] - 1

if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
a = a.astype(np.int64)
b = b.astype(np.int64)
subtract = cls._func_calculate("subtract")
multiply = cls._func_calculate("multiply")
divide = cls._func_calculate("divide")
qr = cls._function("poly_divmod")(a, b, subtract, multiply, divide, cls._characteristic, cls._degree, cls._irreducible_poly_int)
qr = cls._function("poly_divmod")(a, b, subtract, multiply, divide, cls.characteristic, cls.degree, int(cls.irreducible_poly))
qr = qr.astype(dtype)
else:
a = a.view(np.ndarray)
b = b.view(np.ndarray)
subtract = cls._func_python("subtract")
multiply = cls._func_python("multiply")
divide = cls._func_python("divide")
qr = cls._function("poly_divmod")(a, b, subtract, multiply, divide, cls._characteristic, cls._degree, cls._irreducible_poly_int)
qr = cls._function("poly_divmod")(a, b, subtract, multiply, divide, cls.characteristic, cls.degree, int(cls.irreducible_poly))
qr = field._view(qr)

q = qr[:, 0:q_degree + 1]
Expand All @@ -360,21 +360,21 @@ def _poly_floordiv(cls, a, b):
field = type(a)
dtype = a.dtype

if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
a = a.astype(np.int64)
b = b.astype(np.int64)
subtract = cls._func_calculate("subtract")
multiply = cls._func_calculate("multiply")
divide = cls._func_calculate("divide")
q = cls._function("poly_floordiv")(a, b, subtract, multiply, divide, cls._characteristic, cls._degree, cls._irreducible_poly_int)
q = cls._function("poly_floordiv")(a, b, subtract, multiply, divide, cls.characteristic, cls.degree, int(cls.irreducible_poly))
q = q.astype(dtype)
else:
a = a.view(np.ndarray)
b = b.view(np.ndarray)
subtract = cls._func_python("subtract")
multiply = cls._func_python("multiply")
divide = cls._func_python("divide")
q = cls._function("poly_floordiv")(a, b, subtract, multiply, divide, cls._characteristic, cls._degree, cls._irreducible_poly_int)
q = cls._function("poly_floordiv")(a, b, subtract, multiply, divide, cls.characteristic, cls.degree, int(cls.irreducible_poly))
q = field._view(q)

return q
Expand All @@ -386,21 +386,21 @@ def _poly_mod(cls, a, b):
field = type(a)
dtype = a.dtype

if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
a = a.astype(np.int64)
b = b.astype(np.int64)
subtract = cls._func_calculate("subtract")
multiply = cls._func_calculate("multiply")
divide = cls._func_calculate("divide")
r = cls._function("poly_mod")(a, b, subtract, multiply, divide, cls._characteristic, cls._degree, cls._irreducible_poly_int)
r = cls._function("poly_mod")(a, b, subtract, multiply, divide, cls.characteristic, cls.degree, int(cls.irreducible_poly))
r = r.astype(dtype)
else:
a = a.view(np.ndarray)
b = b.view(np.ndarray)
subtract = cls._func_python("subtract")
multiply = cls._func_python("multiply")
divide = cls._func_python("divide")
r = cls._function("poly_mod")(a, b, subtract, multiply, divide, cls._characteristic, cls._degree, cls._irreducible_poly_int)
r = cls._function("poly_mod")(a, b, subtract, multiply, divide, cls.characteristic, cls.degree, int(cls.irreducible_poly))
r = field._view(r)

return r
Expand All @@ -422,7 +422,7 @@ def _poly_pow(cls, a, b, c=None):
b_vec.append(b)
b_vec = np.array(b_vec[::-1], dtype=np.uint64) # Make vector MSWord -> LSWord

if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
a = a.astype(np.int64)
c = np.array([], dtype=np.int64) if c is None else c.astype(np.int64)
add = cls._func_calculate("add")
Expand All @@ -431,7 +431,7 @@ def _poly_pow(cls, a, b, c=None):
divide = cls._func_calculate("divide")
convolve = cls._function("convolve")
poly_mod = cls._function("poly_mod")
z = cls._function("poly_pow")(a, b_vec, c, add, subtract, multiply, divide, convolve, poly_mod, cls._characteristic, cls._degree, cls._irreducible_poly_int)
z = cls._function("poly_pow")(a, b_vec, c, add, subtract, multiply, divide, convolve, poly_mod, cls.characteristic, cls.degree, int(cls.irreducible_poly))
z = z.astype(dtype)
else:
a = a.view(np.ndarray)
Expand All @@ -442,7 +442,7 @@ def _poly_pow(cls, a, b, c=None):
divide = cls._func_python("divide")
convolve = cls._function("convolve")
poly_mod = cls._function("poly_mod")
z = cls._function("poly_pow")(a, b_vec, c, add, subtract, multiply, divide, convolve, poly_mod, cls._characteristic, cls._degree, cls._irreducible_poly_int)
z = cls._function("poly_pow")(a, b_vec, c, add, subtract, multiply, divide, convolve, poly_mod, cls.characteristic, cls.degree, int(cls.irreducible_poly))
z = field._view(z)

return z
Expand All @@ -453,21 +453,21 @@ def _poly_roots(cls, nonzero_degrees, nonzero_coeffs):
field = cls
dtype = nonzero_coeffs.dtype

if cls._ufunc_mode != "python-calculate":
if cls.ufunc_mode != "python-calculate":
nonzero_degrees = nonzero_degrees.astype(np.int64)
nonzero_coeffs = nonzero_coeffs.astype(np.int64)
add = cls._func_calculate("add")
multiply = cls._func_calculate("multiply")
power = cls._func_calculate("power")
roots = cls._function("poly_roots")(nonzero_degrees, nonzero_coeffs, np.int64(cls._primitive_element), add, multiply, power, cls._characteristic, cls._degree, cls._irreducible_poly_int)[0,:]
roots = cls._function("poly_roots")(nonzero_degrees, nonzero_coeffs, np.int64(cls.primitive_element), add, multiply, power, cls.characteristic, cls.degree, int(cls.irreducible_poly))[0,:]
roots = roots.astype(dtype)
else:
nonzero_degrees = nonzero_degrees.view(np.ndarray)
nonzero_coeffs = nonzero_coeffs.view(np.ndarray)
add = cls._func_python("add")
multiply = cls._func_python("multiply")
power = cls._func_python("power")
roots = cls._function("poly_roots")(nonzero_degrees, nonzero_coeffs, int(cls._primitive_element), add, multiply, power, cls._characteristic, cls._degree, cls._irreducible_poly_int)[0,:]
roots = cls._function("poly_roots")(nonzero_degrees, nonzero_coeffs, int(cls.primitive_element), add, multiply, power, cls.characteristic, cls.degree, int(cls.irreducible_poly))[0,:]
roots = field._view(roots)

idxs = np.argsort(roots)
Expand Down
Loading

0 comments on commit 392aeaf

Please sign in to comment.