Skip to content

Commit

Permalink
Minor modification to ValueError messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Sep 11, 2022
1 parent 8677e90 commit 46ef7be
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 150 deletions.
12 changes: 6 additions & 6 deletions src/galois/_codes/_bch.py
Expand Up @@ -36,9 +36,9 @@ def _check_and_compute_field(

p, m = factors(n + 1)
if not (len(p) == 1 and p[0] == 2):
raise ValueError(f"Argument `n` must have value `2^m - 1` for some positive m, not {n}.")
raise ValueError(f"Argument 'n' must equal 2^m - 1 for some positive m, not {n}.")
if not c >= 1:
raise ValueError(f"Argument `c` must be at least 1, not {c}.")
raise ValueError(f"Argument 'c' must be at least 1, not {c}.")
p, m = p[0], m[0]

if primitive_poly is None:
Expand Down Expand Up @@ -93,7 +93,7 @@ def bch_valid_codes(n: int, t_min: int = 1) -> list[tuple[int, int, int]]:
verify_isinstance(n, int)
verify_isinstance(t_min, int)
if not t_min >= 1:
raise ValueError(f"Argument `t_min` must be at least 1, not {t_min}.")
raise ValueError(f"Argument 't_min' must be at least 1, not {t_min}.")

GF = _check_and_compute_field(n, 0, 1, None, None) # NOTE: k isn't needed for generating the field
alpha = GF.primitive_element
Expand Down Expand Up @@ -399,13 +399,13 @@ def encode(self, message: ArrayLike, parity_only: bool = False) -> GF2:
"""
message = GF2(message) # This performs type/value checking
if parity_only and not self.is_systematic:
raise ValueError("Argument `parity_only=True` only applies to systematic codes.")
raise ValueError("Argument 'parity_only=True' only applies to systematic codes.")
if self.is_systematic:
if not message.shape[-1] <= self.k:
raise ValueError(f"For a systematic code, argument `message` must be a 1-D or 2-D array with last dimension less than or equal to {self.k}, not shape {message.shape}.")
raise ValueError(f"For a systematic code, argument 'message' must be a 1-D or 2-D array with last dimension less than or equal to {self.k}, not shape {message.shape}.")
else:
if not message.shape[-1] == self.k:
raise ValueError(f"For a non-systematic code, argument `message` must be a 1-D or 2-D array with last dimension equal to {self.k}, not shape {message.shape}.")
raise ValueError(f"For a non-systematic code, argument 'message' must be a 1-D or 2-D array with last dimension equal to {self.k}, not shape {message.shape}.")

ks = message.shape[-1] # The number of input message bits (could be less than self.k for shortened codes)

Expand Down
4 changes: 2 additions & 2 deletions src/galois/_codes/_linear.py
Expand Up @@ -43,7 +43,7 @@ def generator_to_parity_check_matrix(G: FieldArray) -> FieldArray:
field = type(G)
k, n = G.shape
if not np.array_equal(G[:,0:k], np.eye(k)):
raise ValueError("Argument `G` must be in systematic form [I | P].")
raise ValueError("Argument 'G' must be in systematic form [I | P].")

P = G[:, k:]
I = field.Identity(n-k)
Expand Down Expand Up @@ -88,7 +88,7 @@ def parity_check_to_generator_matrix(H: FieldArray) -> FieldArray:
n_k, n = H.shape
k = n - n_k
if not np.array_equal(H[:,k:], np.eye(n - k)):
raise ValueError("Argument `H` must be in systematic form [-P^T | I].")
raise ValueError("Argument 'H' must be in systematic form [-P^T | I].")

P = -H[:, 0:k].T
I = field.Identity(k)
Expand Down
12 changes: 6 additions & 6 deletions src/galois/_codes/_reed_solomon.py
Expand Up @@ -110,12 +110,12 @@ def __init__(
verify_isinstance(systematic, bool)

if not (n - k) % 2 == 0:
raise ValueError("Arguments `n - k` must be even.")
raise ValueError("Arguments 'n - k' must be even.")
p, m = factors(n + 1)
if not (len(p) == 1 and len(m) == 1):
raise ValueError(f"Argument `n` must have value `q - 1` for a prime power `q`, not {n}.")
raise ValueError(f"Argument 'n' must equal q - 1 for a prime power q, not {n}.")
if not c >= 1:
raise ValueError(f"Argument `c` must be at least 1, not {c}.")
raise ValueError(f"Argument 'c' must be at least 1, not {c}.")
p, m = p[0], m[0]

if primitive_poly is None and m > 1:
Expand Down Expand Up @@ -286,13 +286,13 @@ def encode(self, message: ArrayLike, parity_only: bool = False) -> FieldArray:
"""
message = self.field(message) # This performs type/value checking
if parity_only and not self.is_systematic:
raise ValueError("Argument `parity_only=True` only applies to systematic codes.")
raise ValueError("Argument 'parity_only=True' only applies to systematic codes.")
if self.is_systematic:
if not message.shape[-1] <= self.k:
raise ValueError(f"For a systematic code, argument `message` must be a 1-D or 2-D array with last dimension less than or equal to {self.k}, not shape {message.shape}.")
raise ValueError(f"For a systematic code, argument 'message' must be a 1-D or 2-D array with last dimension less than or equal to {self.k}, not shape {message.shape}.")
else:
if not message.shape[-1] == self.k:
raise ValueError(f"For a non-systematic code, argument `message` must be a 1-D or 2-D array with last dimension equal to {self.k}, not shape {message.shape}.")
raise ValueError(f"For a non-systematic code, argument 'message' must be a 1-D or 2-D array with last dimension equal to {self.k}, not shape {message.shape}.")

ks = message.shape[-1] # The number of input message symbols (could be less than self.k for shortened codes)

Expand Down
10 changes: 5 additions & 5 deletions src/galois/_domains/_array.py
Expand Up @@ -223,9 +223,9 @@ def Range(
dtype = cls._get_dtype(dtype)

if not 0 <= start <= cls.order:
raise ValueError(f"Argument `start` must be within the field's order {cls.order}, not {start}.")
raise ValueError(f"Argument 'start' must be within the field's order {cls.order}, not {start}.")
if not 0 <= stop <= cls.order:
raise ValueError(f"Argument `stop` must be within the field's order {cls.order}, not {stop}.")
raise ValueError(f"Argument 'stop' must be within the field's order {cls.order}, not {stop}.")

array = np.arange(start, stop, step=step, dtype=dtype)

Expand Down Expand Up @@ -351,10 +351,10 @@ def compile(cls, mode: Literal["auto", "jit-lookup", "jit-calculate", "python-ca
"""
verify_isinstance(mode, str)
if not mode in ["auto", "jit-lookup", "jit-calculate", "python-calculate"]:
raise ValueError(f"Argument `mode` must be in ['auto', 'jit-lookup', 'jit-calculate', 'python-calculate'], not {mode!r}.")
raise ValueError(f"Argument 'mode' must be in ['auto', 'jit-lookup', 'jit-calculate', 'python-calculate'], not {mode!r}.")
mode = cls.default_ufunc_mode if mode == "auto" else mode
if mode not in cls.ufunc_modes:
raise ValueError(f"Argument `mode` must be in {cls.ufunc_modes} for {cls._name}, not {mode!r}.")
raise ValueError(f"Argument 'mode' must be in {cls.ufunc_modes} for {cls._name}, not {mode!r}.")

if mode == cls.ufunc_mode:
# Don't need to rebuild these ufuncs
Expand Down Expand Up @@ -456,7 +456,7 @@ def display(cls, mode: Literal["int", "poly", "power"] = "int") -> Generator[Non
"""
verify_isinstance(mode, str, optional=True)
if mode not in ["int", "poly", "power"]:
raise ValueError(f"Argument `mode` must be in ['int', 'poly', 'power'], not {mode!r}.")
raise ValueError(f"Argument 'mode' must be in ['int', 'poly', 'power'], not {mode!r}.")

prev_mode = cls.display_mode
cls._display_mode = mode # Set the new display mode
Expand Down
20 changes: 10 additions & 10 deletions src/galois/_domains/_linalg.py
Expand Up @@ -287,7 +287,7 @@ class lu_decompose_jit(Function):
def __call__(self, A: Array) -> tuple[Array, Array]:
verify_isinstance(A, self.field)
if not A.ndim == 2:
raise ValueError(f"Argument `A` must be a 2-D matrix, not have shape {A.shape}.")
raise ValueError(f"Argument 'A' must be a 2-D matrix, not have shape {A.shape}.")

n = A.shape[0]
Ai = A.copy()
Expand All @@ -300,7 +300,7 @@ def __call__(self, A: Array) -> tuple[Array, Array]:
L[i,i] = 1
continue
else:
raise ValueError("The LU decomposition of `A` does not exist. Use the LUP decomposition instead.")
raise ValueError("The LU decomposition of 'A' does not exist. Use the LUP decomposition instead.")

l = Ai[i+1:,i] / Ai[i,i]
Ai[i+1:,:] -= np.multiply.outer(l, Ai[i,:])
Expand All @@ -319,7 +319,7 @@ class plu_decompose_jit(Function):
def __call__(self, A: Array) -> tuple[Array, Array, Array, int]:
verify_isinstance(A, self.field)
if not A.ndim == 2:
raise ValueError(f"Argument `A` must be a 2-D matrix, not have shape {A.shape}.")
raise ValueError(f"Argument 'A' must be a 2-D matrix, not have shape {A.shape}.")

n = A.shape[0]
Ai = A.copy()
Expand Down Expand Up @@ -365,7 +365,7 @@ class triangular_det_jit(Function):
def __call__(self, A: Array) -> Array:
verify_isinstance(A, self.field)
if not (A.ndim == 2 and A.shape[0] == A.shape[1]):
raise np.linalg.LinAlgError(f"Argument `A` must be square, not {A.shape}.")
raise np.linalg.LinAlgError(f"Argument 'A' must be square, not {A.shape}.")
idxs = np.arange(0, A.shape[0])
return np.multiply.reduce(A[idxs,idxs])

Expand All @@ -378,7 +378,7 @@ class det_jit(Function):
def __call__(self, A: Array) -> Array:
verify_isinstance(A, self.field)
if not (A.ndim == 2 and A.shape[0] == A.shape[1]):
raise np.linalg.LinAlgError(f"Argument `A` must be square, not {A.shape}.")
raise np.linalg.LinAlgError(f"Argument 'A' must be square, not {A.shape}.")

n = A.shape[0]

Expand Down Expand Up @@ -419,7 +419,7 @@ class inv_jit(Function):
def __call__(self, A: Array) -> Array:
verify_isinstance(A, self.field)
if not (A.ndim == 2 and A.shape[0] == A.shape[1]):
raise np.linalg.LinAlgError(f"Argument `A` must be square, not {A.shape}.")
raise np.linalg.LinAlgError(f"Argument 'A' must be square, not {A.shape}.")

n = A.shape[0]
I = self.field.Identity(n, dtype=A.dtype)
Expand All @@ -433,7 +433,7 @@ def __call__(self, A: Array) -> Array:
# The rank is the number of non-zero rows of the row reduced echelon form
rank = np.sum(~np.all(AI_rre[:,0:n] == 0, axis=1))
if not rank == n:
raise np.linalg.LinAlgError(f"Argument `A` is singular and not invertible because it does not have full rank of {n}, but rank of {rank}.")
raise np.linalg.LinAlgError(f"Argument 'A' is singular and not invertible because it does not have full rank of {n}, but rank of {rank}.")

A_inv = AI_rre[:,-n:]

Expand All @@ -449,11 +449,11 @@ def __call__(self, A: Array, b: Array) -> Array:
verify_isinstance(A, self.field)
verify_isinstance(b, self.field)
if not (A.ndim == 2 and A.shape[0] == A.shape[1]):
raise np.linalg.LinAlgError(f"Argument `A` must be square, not {A.shape}.")
raise np.linalg.LinAlgError(f"Argument 'A' must be square, not {A.shape}.")
if not b.ndim in [1, 2]:
raise np.linalg.LinAlgError(f"Argument `b` must have dimension equal to A or one less, not {b.ndim}.")
raise np.linalg.LinAlgError(f"Argument 'b' must have dimension equal to 'A' or one less, not {b.ndim}.")
if not A.shape[-1] == b.shape[0]:
raise np.linalg.LinAlgError(f"The last dimension of `A` must equal the first dimension of `b`, not {A.shape} and {b.shape}.")
raise np.linalg.LinAlgError(f"The last dimension of 'A' must equal the first dimension of 'b', not {A.shape} and {b.shape}.")

A_inv = inv_jit(self.field)(A)
x = A_inv @ b
Expand Down
14 changes: 7 additions & 7 deletions src/galois/_fields/_array.py
Expand Up @@ -332,14 +332,14 @@ def Vandermonde(cls, element: ElementLike, rows: int, cols: int, dtype: DTypeLik
verify_isinstance(rows, int)
verify_isinstance(cols, int)
if not rows > 0:
raise ValueError(f"Argument `rows` must be non-negative, not {rows}.")
raise ValueError(f"Argument 'rows' must be non-negative, not {rows}.")
if not cols > 0:
raise ValueError(f"Argument `cols` must be non-negative, not {cols}.")
raise ValueError(f"Argument 'cols' must be non-negative, not {cols}.")

dtype = cls._get_dtype(dtype)
element = cls(element, dtype=dtype)
if not element.ndim == 0:
raise ValueError(f"Argument `element` must be element scalar, not {element.ndim}-D.")
raise ValueError(f"Argument 'element' must be element scalar, not {element.ndim}-D.")

v = element ** np.arange(0, rows)
V = np.power.outer(v, np.arange(0, cols))
Expand Down Expand Up @@ -455,7 +455,7 @@ def repr_table(cls, element: ElementLike | None = None, sort: Literal["power", "
GF("x^2").multiplicative_order()
"""
if sort not in ["power", "poly", "vector", "int"]:
raise ValueError(f"Argument `sort` must be in ['power', 'poly', 'vector', 'int'], not {sort!r}.")
raise ValueError(f"Argument 'sort' must be in ['power', 'poly', 'vector', 'int'], not {sort!r}.")
if element is None:
element = cls.primitive_element

Expand Down Expand Up @@ -582,7 +582,7 @@ def arithmetic_table(
GF.display()
"""
if not operation in ["+", "-", "*", "/"]:
raise ValueError(f"Argument `operation` must be in ['+', '-', '*', '/'], not {operation!r}.")
raise ValueError(f"Argument 'operation' must be in ['+', '-', '*', '/'], not {operation!r}.")

if cls.display_mode == "power":
# Order elements by powers of the primitive element
Expand Down Expand Up @@ -686,7 +686,7 @@ def primitive_root_of_unity(cls, n: int) -> FieldArray:
"""
verify_isinstance(n, (int, np.ndarray))
if not 1 <= n < cls.order:
raise ValueError(f"Argument `n` must be in [1, {cls.order}), not {n}.")
raise ValueError(f"Argument 'n' must be in [1, {cls.order}), not {n}.")
if not (cls.order - 1) % n == 0:
raise ValueError(f"There are no primitive {n}-th roots of unity in {cls.name}.")

Expand Down Expand Up @@ -750,7 +750,7 @@ def primitive_roots_of_unity(cls, n: int) -> FieldArray:
np.power.outer(root, powers)
"""
if not isinstance(n, (int, np.ndarray)):
raise TypeError(f"Argument `n` must be an int, not {type(n)!r}.")
raise TypeError(f"Argument 'n' must be an int, not {type(n)!r}.")
if not (cls.order - 1) % n == 0:
raise ValueError(f"There are no primitive {n}-th roots of unity in {cls.name}.")

Expand Down
24 changes: 12 additions & 12 deletions src/galois/_fields/_factory.py
Expand Up @@ -203,17 +203,17 @@ def GF(
p, e = factors(order)
if not len(p) == len(e) == 1:
s = " + ".join([f"{pi}**{ei}" for pi, ei in zip(p, e)])
raise ValueError(f"Argument `order` must be a prime power, not {order} = {s}.")
raise ValueError(f"Argument 'order' must be a prime power, not {order} = {s}.")
if not compile in [None, "auto", "jit-lookup", "jit-calculate", "python-calculate"]:
raise ValueError(f"Argument `compile` must be in ['auto', 'jit-lookup', 'jit-calculate', 'python-calculate'], not {compile!r}.")
raise ValueError(f"Argument 'compile' must be in ['auto', 'jit-lookup', 'jit-calculate', 'python-calculate'], not {compile!r}.")
if not display in [None, "int", "poly", "power"]:
raise ValueError(f"Argument `display` must be in ['int', 'poly', 'power'], not {display!r}.")
raise ValueError(f"Argument 'display' must be in ['int', 'poly', 'power'], not {display!r}.")

p, m = p[0], e[0]

if m == 1:
if not irreducible_poly is None:
raise ValueError(f"Argument `irreducible_poly` can only be specified for extension fields, not the prime field GF({p}).")
raise ValueError(f"Argument 'irreducible_poly' can only be specified for extension fields, not the prime field GF({p}).")
return _GF_prime(p, alpha=primitive_element, verify=verify, compile=compile, display=display)
else:
return _GF_extension(p, m, irreducible_poly_=irreducible_poly, alpha=primitive_element, verify=verify, compile=compile, display=display)
Expand Down Expand Up @@ -252,7 +252,7 @@ def _GF_prime(

# Check primitive element range
if not 0 < alpha < p:
raise ValueError(f"Argument `primitive_element` must be non-zero in the field 0 < x < {p}, not {alpha}.")
raise ValueError(f"Argument 'primitive_element' must be non-zero in the field 0 < x < {p}, not {alpha}.")

# If the requested field has already been constructed, return it
name = f"FieldArray_{p}_{alpha}"
Expand All @@ -266,7 +266,7 @@ def _GF_prime(
return field

if verify and not is_primitive_root(alpha, p):
raise ValueError(f"Argument `primitive_element` must be a primitive root modulo {p}, {alpha} is not.")
raise ValueError(f"Argument 'primitive_element' must be a primitive root modulo {p}, {alpha} is not.")

if p == 2:
field = GF2
Expand Down Expand Up @@ -337,13 +337,13 @@ def _GF_extension(

# Check polynomial fields and degrees
if not irreducible_poly_.field.order == p:
raise ValueError(f"Argument `irreducible_poly` must be over {prime_subfield.name}, not {irreducible_poly_.field.name}.")
raise ValueError(f"Argument 'irreducible_poly' must be over {prime_subfield.name}, not {irreducible_poly_.field.name}.")
if not irreducible_poly_.degree == m:
raise ValueError(f"Argument `irreducible_poly` must have degree equal to {m}, not {irreducible_poly_.degree}.")
raise ValueError(f"Argument 'irreducible_poly' must have degree equal to {m}, not {irreducible_poly_.degree}.")
if not alpha.field.order == p:
raise ValueError(f"Argument `primitive_element` must be a polynomial over {prime_subfield.name}, not {alpha.field.name}.")
raise ValueError(f"Argument 'primitive_element' must be a polynomial over {prime_subfield.name}, not {alpha.field.name}.")
if not alpha.degree < m:
raise ValueError(f"Argument `primitive_element` must have degree strictly less than {m}, not {alpha.degree}.")
raise ValueError(f"Argument 'primitive_element' must have degree strictly less than {m}, not {alpha.degree}.")

# If the requested field has already been constructed, return it
name = f"FieldArray_{p}_{m}_{int(alpha)}_{int(irreducible_poly_)}"
Expand All @@ -357,9 +357,9 @@ def _GF_extension(
return field

if verify_poly and not irreducible_poly_.is_irreducible():
raise ValueError(f"Argument `irreducible_poly` must be irreducible, {irreducible_poly_} is not.")
raise ValueError(f"Argument 'irreducible_poly' must be irreducible, {irreducible_poly_} is not.")
if verify_element and not is_primitive_element(alpha, irreducible_poly_):
raise ValueError(f"Argument `primitive_element` must be a multiplicative generator of {name}, {alpha} is not.")
raise ValueError(f"Argument 'primitive_element' must be a multiplicative generator of {name}, {alpha} is not.")

ufunc_mixin = UFuncMixin_2_m if p == 2 else UFuncMixin_p_m

Expand Down

0 comments on commit 46ef7be

Please sign in to comment.