diff --git a/galois/_poly_conversion.py b/galois/_poly_conversion.py index 9caef7868..692b1d28e 100644 --- a/galois/_poly_conversion.py +++ b/galois/_poly_conversion.py @@ -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 = "" @@ -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) diff --git a/tests/polys/test_conversions.py b/tests/polys/test_conversions.py index 58cf02f05..cc39e7f21 100644 --- a/tests/polys/test_conversions.py +++ b/tests/polys/test_conversions.py @@ -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(): diff --git a/tests/polys/test_operations.py b/tests/polys/test_operations.py index 80ee45e56..541634d71 100644 --- a/tests/polys/test_operations.py +++ b/tests/polys/test_operations.py @@ -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():