Skip to content

Commit

Permalink
Add () around 0-degree coefficient if it has a + separator
Browse files Browse the repository at this point in the history
Fixes #328
  • Loading branch information
mhostetter committed Apr 3, 2022
1 parent 5f011ae commit 303418b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
7 changes: 5 additions & 2 deletions galois/_poly_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def sparse_poly_to_str(degrees: List[int], coeffs: List[int], poly_var: str = "x
if coeff == 0:
continue

# Use () around elements, except the 0-th degree term
if degree > 1:
if coeff == 1:
c = ""
Expand All @@ -116,7 +115,11 @@ def sparse_poly_to_str(degrees: List[int], coeffs: List[int], poly_var: str = "x
c = f"{coeff!s}"
s = f"{c}{poly_var}"
else:
s = f"{coeff!s}"
# Use () around 0-degree term only if it has a + separator in it ("poly" display mode)
if "+" in str(coeff):
s = f"({coeff!s})"
else:
s = f"{coeff!s}"

x.append(s)

Expand Down
4 changes: 2 additions & 2 deletions tests/polys/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def test_sparse_poly_to_str():
GF = galois.GF(2**8)
with GF.display("poly"):
assert galois._poly_conversion.sparse_poly_to_str([0], GF([0])) == "0"
assert galois._poly_conversion.sparse_poly_to_str([3, 1, 0], GF([1, 2, 3])) == "x^3 + (α)x + α + 1"
assert galois._poly_conversion.sparse_poly_to_str([3, 1, 0], GF([1, 2, 3])) == "x^3 + (α)x + (α + 1)"

assert galois._poly_conversion.sparse_poly_to_str([0], GF([0]), poly_var="y") == "0"
assert galois._poly_conversion.sparse_poly_to_str([3, 1, 0], GF([1, 2, 3]), poly_var="y") == "y^3 + (α)y + α + 1"
assert galois._poly_conversion.sparse_poly_to_str([3, 1, 0], GF([1, 2, 3]), poly_var="y") == "y^3 + (α)y + (α + 1)"


def test_str_to_sparse_poly():
Expand Down
8 changes: 4 additions & 4 deletions tests/polys/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ def test_str():
assert str(poly) == "(α^5)x^3 + (α)x + 1"

GF = galois.GF(2**3)
poly = galois.Poly([2, 0, 3, 1], field=GF)
assert str(poly) == "2x^3 + 3x + 1"
poly = galois.Poly([2, 0, 3, 5], field=GF)
assert str(poly) == "2x^3 + 3x + 5"
with GF.display("poly"):
assert str(poly) == "(α)x^3 + (α + 1)x + 1"
assert str(poly) == "(α)x^3 + (α + 1)x + (α^2 + 1)"
with GF.display("power"):
assert str(poly) == "(α)x^3 + (α^3)x + 1"
assert str(poly) == "(α)x^3 + (α^3)x + α^6"


def test_int():
Expand Down

0 comments on commit 303418b

Please sign in to comment.