diff options
| author | Ross Barnowski <rossbar@berkeley.edu> | 2020-05-07 14:04:24 -0700 |
|---|---|---|
| committer | Ross Barnowski <rossbar@berkeley.edu> | 2020-05-13 15:01:36 -0700 |
| commit | 66365a57ff9b68a191fd232dc823cc2cf69d267f (patch) | |
| tree | 2ad0e30d6cff55d893333432515dafeabee56c8f | |
| parent | 9c83b13ce1b08aed8181d284566d002086393a89 (diff) | |
| download | numpy-66365a57ff9b68a191fd232dc823cc2cf69d267f.tar.gz | |
Handle TypeError in _generate_str for coefs.
Add a fallback for TypeErrors that are raised when attempting
to compare arbitrary elements (e.g. strings or Python complex)
to 0 in _generate_str.
| -rw-r--r-- | numpy/polynomial/_polybase.py | 12 | ||||
| -rw-r--r-- | numpy/polynomial/tests/test_printing.py | 49 |
2 files changed, 54 insertions, 7 deletions
diff --git a/numpy/polynomial/_polybase.py b/numpy/polynomial/_polybase.py index 253aa74e8..7616b95c7 100644 --- a/numpy/polynomial/_polybase.py +++ b/numpy/polynomial/_polybase.py @@ -347,10 +347,16 @@ class ABCPolyBase(abc.ABC): out += " " power = str(i + 1) # Polynomial coefficient - if coef >= 0: + try: + if coef >= 0: + next_term = f"+ {coef}" + else: + next_term = f"- {-coef}" + # The coefficient array can be an object array with elements that + # will raise a TypeError with >= 0 (e.g. strings or Python + # complex). In this case, represent the coeficient as-is. + except TypeError: next_term = f"+ {coef}" - else: - next_term = f"- {-coef}" # Polynomial term next_term += term_method(power, "x") # Length of the current line with next term added diff --git a/numpy/polynomial/tests/test_printing.py b/numpy/polynomial/tests/test_printing.py index acccb23f5..db71277d5 100644 --- a/numpy/polynomial/tests/test_printing.py +++ b/numpy/polynomial/tests/test_printing.py @@ -1,8 +1,12 @@ import pytest -from numpy.core import arange, printoptions +from numpy.core import array, arange, printoptions import numpy.polynomial as poly from numpy.testing import assert_equal, assert_ +# For testing polynomial printing with object arrays +from fractions import Fraction +from decimal import Decimal + class TestStrUnicodeSuperSubscripts: @@ -233,11 +237,48 @@ def test_set_default_printoptions(): def test_complex_coefficients(): - p = poly.Polynomial([0+1j, 1+1j, -2+2j, 3+0j]) + """Test both numpy and built-in complex.""" + coefs = [0+1j, 1+1j, -2+2j, 3+0j] + # numpy complex + p1 = poly.Polynomial(coefs) + # Python complex + p2 = poly.Polynomial(array(coefs, dtype=object)) poly.set_default_printstyle('unicode') - assert_equal(str(p), "1j + (1+1j)·x¹ - (2-2j)·x² + (3+0j)·x³") + assert_equal(str(p1), "1j + (1+1j)·x¹ - (2-2j)·x² + (3+0j)·x³") + assert_equal(str(p2), "1j + (1+1j)·x¹ + (-2+2j)·x² + (3+0j)·x³") poly.set_default_printstyle('ascii') - assert_equal(str(p), "1j + (1+1j) x**1 - (2-2j) x**2 + (3+0j) x**3") + assert_equal(str(p1), "1j + (1+1j) x**1 - (2-2j) x**2 + (3+0j) x**3") + assert_equal(str(p2), "1j + (1+1j) x**1 + (-2+2j) x**2 + (3+0j) x**3") + + +@pytest.mark.parametrize(('coefs', 'tgt'), ( + (array([Fraction(1, 2), Fraction(3, 4)], dtype=object), ( + "1/2 + 3/4·x¹" + )), + (array([1, 2, Fraction(5, 7)], dtype=object), ( + "1 + 2·x¹ + 5/7·x²" + )), + (array([Decimal('1.00'), Decimal('2.2'), 3], dtype=object), ( + "1.00 + 2.2·x¹ + 3·x²" + )), +)) +def test_numeric_object_coefficients(coefs, tgt): + p = poly.Polynomial(coefs) + poly.set_default_printstyle('unicode') + assert_equal(str(p), tgt) + + +@pytest.mark.parametrize(('coefs', 'tgt'), ( + (array([1, 2, 'f'], dtype=object), '1 + 2·x¹ + f·x²'), + (array([1, 2, [3, 4]], dtype=object), '1 + 2·x¹ + [3, 4]·x²'), +)) +def test_nonnumeric_object_coefficients(coefs, tgt): + """ + Test coef fallback for object arrays of non-numeric coefficients. + """ + p = poly.Polynomial(coefs) + poly.set_default_printstyle('unicode') + assert_equal(str(p), tgt) class TestFormat: |
