Skip to content

Commit

Permalink
Resolve no-else-return pylint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Dec 11, 2022
1 parent 1801189 commit a0a1115
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 267 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ disable = [
"invalid-unary-operand-type",
"missing-function-docstring",
"missing-module-docstring",
"no-else-return",
"not-callable", # pylint doesn't understand metaclass properties
"protected-access",
"too-many-ancestors",
Expand Down
12 changes: 6 additions & 6 deletions src/galois/_codes/_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def encode(self, message: ArrayLike, output: Literal["codeword", "parity"] = "co

if output == "codeword":
return codeword
else:
parity = self._convert_codeword_to_parity(codeword)
return parity

parity = self._convert_codeword_to_parity(codeword)
return parity

def detect(self, codeword: ArrayLike) -> bool | np.ndarray:
r"""
Expand Down Expand Up @@ -187,11 +187,11 @@ def decode(self, codeword, output="message", errors=False):
if is_codeword_1d:
decoded, N_errors = decoded[0, :], int(N_errors[0])

if not errors:
return decoded
else:
if errors:
return decoded, N_errors

return decoded

# def dual_code(self) -> _LinearCode:
# n = self.n
# k = self.n - self.k
Expand Down
73 changes: 35 additions & 38 deletions src/galois/_domains/_calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ def __call__(self, ufunc, method, inputs, kwargs, meta):
if self.field.ufunc_mode == "jit-lookup" or method != "__call__":
# Use the lookup ufunc on each array entry
return super().__call__(ufunc, method, inputs, kwargs, meta)
else:
# Convert entire array to polynomial/vector representation, perform array operation in GF(p), and convert back to GF(p^m)
self._verify_operands_in_same_field(ufunc, inputs, meta)
inputs, kwargs = self._convert_inputs_to_vector(inputs, kwargs)
output = getattr(ufunc, method)(*inputs, **kwargs)
output = self._convert_output_from_vector(output, meta["dtype"])
return output

# Convert entire array to polynomial/vector representation, perform array operation in GF(p), and convert back to GF(p^m)
self._verify_operands_in_same_field(ufunc, inputs, meta)
inputs, kwargs = self._convert_inputs_to_vector(inputs, kwargs)
output = getattr(ufunc, method)(*inputs, **kwargs)
output = self._convert_output_from_vector(output, meta["dtype"])
return output

def set_calculate_globals(self):
global CHARACTERISTIC, DEGREE
Expand Down Expand Up @@ -213,13 +213,13 @@ def __call__(self, ufunc, method, inputs, kwargs, meta):
if self.field.ufunc_mode == "jit-lookup" or method != "__call__":
# Use the lookup ufunc on each array entry
return super().__call__(ufunc, method, inputs, kwargs, meta)
else:
# Convert entire array to polynomial/vector representation, perform array operation in GF(p), and convert back to GF(p^m)
self._verify_operands_in_same_field(ufunc, inputs, meta)
inputs, kwargs = self._convert_inputs_to_vector(inputs, kwargs)
output = getattr(ufunc, method)(*inputs, **kwargs)
output = self._convert_output_from_vector(output, meta["dtype"])
return output

# Convert entire array to polynomial/vector representation, perform array operation in GF(p), and convert back to GF(p^m)
self._verify_operands_in_same_field(ufunc, inputs, meta)
inputs, kwargs = self._convert_inputs_to_vector(inputs, kwargs)
output = getattr(ufunc, method)(*inputs, **kwargs)
output = self._convert_output_from_vector(output, meta["dtype"])
return output

def set_calculate_globals(self):
global CHARACTERISTIC, DEGREE
Expand Down Expand Up @@ -264,13 +264,13 @@ def __call__(self, ufunc, method, inputs, kwargs, meta):
if self.field.ufunc_mode == "jit-lookup" or method != "__call__":
# Use the lookup ufunc on each array entry
return super().__call__(ufunc, method, inputs, kwargs, meta)
else:
# Convert entire array to polynomial/vector representation, perform array operation in GF(p), and convert back to GF(p^m)
self._verify_operands_in_same_field(ufunc, inputs, meta)
inputs, kwargs = self._convert_inputs_to_vector(inputs, kwargs)
output = getattr(ufunc, method)(*inputs, **kwargs)
output = self._convert_output_from_vector(output, meta["dtype"])
return output

# Convert entire array to polynomial/vector representation, perform array operation in GF(p), and convert back to GF(p^m)
self._verify_operands_in_same_field(ufunc, inputs, meta)
inputs, kwargs = self._convert_inputs_to_vector(inputs, kwargs)
output = getattr(ufunc, method)(*inputs, **kwargs)
output = self._convert_output_from_vector(output, meta["dtype"])
return output

def set_calculate_globals(self):
global CHARACTERISTIC, DEGREE
Expand Down Expand Up @@ -658,28 +658,25 @@ def compute_x(x):
# Equation 3.2
if x % 3 == 1:
return MULTIPLY(beta, x)
elif x % 3 == 2:
if x % 3 == 2:
return MULTIPLY(x, x)
else:
return MULTIPLY(alpha, x)
return MULTIPLY(alpha, x)

def compute_a(a, x):
# Equation 3.3
if x % 3 == 1:
return a
elif x % 3 == 2:
if x % 3 == 2:
return (2 * a) % n
else:
return (a + 1) % n
return (a + 1) % n

def compute_b(b, x):
# Equation 3.4
if x % 3 == 1:
return (b + 1) % n
elif x % 3 == 2:
if x % 3 == 2:
return (2 * b) % n
else:
return b
return b

while True:
xi, ai, bi = compute_x(xi), compute_a(ai, xi), compute_b(bi, xi)
Expand All @@ -693,14 +690,14 @@ def compute_b(b, x):
d, r_inv = EGCD(r, n)[0:2]
assert d == 1
return (r_inv * (a2i - ai)) % n
else:
# Re-try with different x0, a0, and b0
a0 += 1
b0 += 1
x0 = MULTIPLY(x0, beta)
x0 = MULTIPLY(x0, alpha)
xi, ai, bi = x0, a0, b0
x2i, a2i, b2i = xi, ai, bi

# Re-try with different x0, a0, and b0
a0 += 1
b0 += 1
x0 = MULTIPLY(x0, beta)
x0 = MULTIPLY(x0, alpha)
xi, ai, bi = x0, a0, b0
x2i, a2i, b2i = xi, ai, bi


class log_pohlig_hellman(_lookup.log_ufunc):
Expand Down
5 changes: 2 additions & 3 deletions src/galois/_domains/_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ def function(self):
"""
Returns a JIT-compiled or pure-Python function based on field size.
"""
if self.field.ufunc_mode != "python-calculate":
return self.jit
else:
if self.field.ufunc_mode == "python-calculate":
return self.python
return self.jit

@property
def jit(self) -> numba.types.FunctionType:
Expand Down
22 changes: 13 additions & 9 deletions src/galois/_domains/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,22 @@ def __call__(self, a: Array, b: Array, out=None) -> Array:
return _lapack_linalg(self.field, a, b, np.dot, out=out)

if a.ndim == 0 or b.ndim == 0:
return a * b
dot = a * b
elif a.ndim == 1 and b.ndim == 1:
return np.sum(a * b)
dot = np.sum(a * b)
elif a.ndim == 2 and b.ndim == 2:
return np.matmul(a, b, out=out)
dot = np.matmul(a, b, out=out)
elif a.ndim >= 2 and b.ndim == 1:
return np.sum(a * b, axis=-1, out=out)
dot = np.sum(a * b, axis=-1, out=out)
# elif a.dnim >= 2 and b.ndim >= 2:
else:
raise NotImplementedError(
"Currently 'dot' is only supported up to 2-D matrices. "
"Please open a GitHub issue at https://github.com/mhostetter/galois/issues."
)

return dot


class vdot_jit(Function):
"""
Expand Down Expand Up @@ -150,8 +152,8 @@ def __call__(self, a: Array, b: Array, out=None) -> Array:

if self.field._is_prime_field:
return _lapack_linalg(self.field, a, b, np.outer, out=out, n_sum=1)
else:
return np.multiply.outer(a.ravel(), b.ravel(), out=out)

return np.multiply.outer(a.ravel(), b.ravel(), out=out)


class matmul_jit(Function):
Expand Down Expand Up @@ -395,9 +397,9 @@ def __call__(self, A: Array) -> Array:
n = A.shape[0]

if n == 2:
return A[0, 0] * A[1, 1] - A[0, 1] * A[1, 0]
det = A[0, 0] * A[1, 1] - A[0, 1] * A[1, 0]
elif n == 3:
return (
det = (
A[0, 0] * (A[1, 1] * A[2, 2] - A[1, 2] * A[2, 1])
- A[0, 1] * (A[1, 0] * A[2, 2] - A[1, 2] * A[2, 0])
+ A[0, 2] * (A[1, 0] * A[2, 1] - A[1, 1] * A[2, 0])
Expand All @@ -408,7 +410,9 @@ def __call__(self, A: Array) -> Array:
det_P = (-self.field(1)) ** N_permutations
det_L = triangular_det_jit(self.field)(L)
det_U = triangular_det_jit(self.field)(U)
return det_P * det_L * det_U
det = det_P * det_L * det_U

return det


###############################################################################
Expand Down
38 changes: 19 additions & 19 deletions src/galois/_domains/_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def lookup(a: int, b: int) -> int: # pragma: no cover
"""
if a == 0:
return b
elif b == 0:
if b == 0:
return a

m = LOG[a]
Expand All @@ -56,8 +56,8 @@ def lookup(a: int, b: int) -> int: # pragma: no cover
if n - m == ZECH_E:
# zech_log(zech_e) = -Inf and α^(-Inf) = 0
return 0
else:
return EXP[m + ZECH_LOG[n - m]]

return EXP[m + ZECH_LOG[n - m]]


class negative_ufunc(_ufunc.negative_ufunc):
Expand Down Expand Up @@ -85,9 +85,9 @@ def lookup(a: int) -> int: # pragma: no cover
"""
if a == 0:
return 0
else:
m = LOG[a]
return EXP[ZECH_E + m]

m = LOG[a]
return EXP[ZECH_E + m]


class subtract_ufunc(_ufunc.subtract_ufunc):
Expand Down Expand Up @@ -123,7 +123,7 @@ def lookup(a: int, b: int) -> int: # pragma: no cover

if b == 0:
return a
elif a == 0:
if a == 0:
return EXP[n]

if m > n:
Expand Down Expand Up @@ -165,10 +165,10 @@ def lookup(a: int, b: int) -> int: # pragma: no cover
"""
if a == 0 or b == 0:
return 0
else:
m = LOG[a]
n = LOG[b]
return EXP[m + n]

m = LOG[a]
n = LOG[b]
return EXP[m + n]


class reciprocal_ufunc(_ufunc.reciprocal_ufunc):
Expand Down Expand Up @@ -232,10 +232,10 @@ def lookup(a: int, b: int) -> int: # pragma: no cover

if a == 0:
return 0
else:
m = LOG[a]
n = LOG[b]
return EXP[(ORDER - 1) + m - n] # We add `ORDER - 1` to guarantee the index is non-negative

m = LOG[a]
n = LOG[b]
return EXP[(ORDER - 1) + m - n] # We add `ORDER - 1` to guarantee the index is non-negative


class power_ufunc(_ufunc.power_ufunc):
Expand Down Expand Up @@ -269,11 +269,11 @@ def lookup(a: int, b: int) -> int: # pragma: no cover

if b == 0:
return 1
elif a == 0:
if a == 0:
return 0
else:
m = LOG[a]
return EXP[(m * b) % (ORDER - 1)] # TODO: Do b % (ORDER - 1) first? b could be very large and overflow int64

m = LOG[a]
return EXP[(m * b) % (ORDER - 1)] # TODO: Do b % (ORDER - 1) first? b could be very large and overflow int64


class log_ufunc(_ufunc.log_ufunc):
Expand Down
14 changes: 7 additions & 7 deletions src/galois/_domains/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def __init__(cls, name, bases, namespace, **kwargs):
def __repr__(cls) -> str:
if cls.order == 0:
# This is not a runtime-created subclass, so return the base class name.
return f"<class '{cls.__module__}.{cls.__name__}'>"
else:
# When FieldArray instances are created they are added to the `galois._fields._factory` module with a name
# like `FieldArray_<p>_<primitive_element>` or `FieldArray_<p>_<m>_<primitive_element>_<irreducible_poly>`.
# This is visually unappealing. So here we override the repr() to be more succinct and indicate how the class
# was created. So galois._fields._factory.FieldArray_31_3 is converted to galois.GF(31).
return f"<class 'galois.{cls.name}'>"
return f"<class 'galois.{cls.__name__}'>"

# When FieldArray instances are created they are added to the `galois._fields._factory` module with a name
# like `FieldArray_<p>_<primitive_element>` or `FieldArray_<p>_<m>_<primitive_element>_<irreducible_poly>`.
# This is visually unappealing. So here we override the repr() to be more succinct and indicate how the class
# was created. So galois._fields._factory.FieldArray_31_3 is converted to galois.GF(31).
return f"<class 'galois.{cls.name}'>"

def __dir__(cls) -> list[str]:
"""
Expand Down
Loading

0 comments on commit a0a1115

Please sign in to comment.