summaryrefslogtreecommitdiff
path: root/numpy/polynomial/tests
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2022-06-01 15:02:43 -0700
committerGitHub <noreply@github.com>2022-06-01 15:02:43 -0700
commitb84a53df9346a73fe8f6df0aaad8727f9bf56076 (patch)
treed09619c6728e28c27963f873afe96627201f25b3 /numpy/polynomial/tests
parent07709f3040da23e57defd003cfa761ba3311b1b8 (diff)
downloadnumpy-b84a53df9346a73fe8f6df0aaad8727f9bf56076.tar.gz
ENH: Add support for symbol to polynomial package (#16154)
Adds a symbol attribute to the polynomials from the np.polynomial package to allow the user to control/modify the symbol used to represent the independent variable for a polynomial expression. This attribute corresponds to the variable attribute of the poly1d class from the old np.lib.polynomial module. Marked as draft for now as it depends on #15666 - all _str* and _repr* methods of ABCPolyBase and derived classes would need to be modified (and tested) to support this change. Co-authored-by: Warren Weckesser <warren.weckesser@gmail.com>
Diffstat (limited to 'numpy/polynomial/tests')
-rw-r--r--numpy/polynomial/tests/test_printing.py79
-rw-r--r--numpy/polynomial/tests/test_symbol.py216
2 files changed, 289 insertions, 6 deletions
diff --git a/numpy/polynomial/tests/test_printing.py b/numpy/polynomial/tests/test_printing.py
index 4e9902a69..0c4316223 100644
--- a/numpy/polynomial/tests/test_printing.py
+++ b/numpy/polynomial/tests/test_printing.py
@@ -309,35 +309,66 @@ class TestFormat:
format(p, '.2f')
+@pytest.mark.parametrize(('poly', 'tgt'), (
+ (poly.Polynomial, '1.0 + 2.0·z¹ + 3.0·z²'),
+ (poly.Chebyshev, '1.0 + 2.0·T₁(z) + 3.0·T₂(z)'),
+ (poly.Hermite, '1.0 + 2.0·H₁(z) + 3.0·H₂(z)'),
+ (poly.HermiteE, '1.0 + 2.0·He₁(z) + 3.0·He₂(z)'),
+ (poly.Laguerre, '1.0 + 2.0·L₁(z) + 3.0·L₂(z)'),
+ (poly.Legendre, '1.0 + 2.0·P₁(z) + 3.0·P₂(z)'),
+))
+def test_symbol(poly, tgt):
+ p = poly([1, 2, 3], symbol='z')
+ assert_equal(f"{p:unicode}", tgt)
+
+
class TestRepr:
def test_polynomial_str(self):
res = repr(poly.Polynomial([0, 1]))
- tgt = 'Polynomial([0., 1.], domain=[-1, 1], window=[-1, 1])'
+ tgt = (
+ "Polynomial([0., 1.], domain=[-1, 1], window=[-1, 1], "
+ "symbol='x')"
+ )
assert_equal(res, tgt)
def test_chebyshev_str(self):
res = repr(poly.Chebyshev([0, 1]))
- tgt = 'Chebyshev([0., 1.], domain=[-1, 1], window=[-1, 1])'
+ tgt = (
+ "Chebyshev([0., 1.], domain=[-1, 1], window=[-1, 1], "
+ "symbol='x')"
+ )
assert_equal(res, tgt)
def test_legendre_repr(self):
res = repr(poly.Legendre([0, 1]))
- tgt = 'Legendre([0., 1.], domain=[-1, 1], window=[-1, 1])'
+ tgt = (
+ "Legendre([0., 1.], domain=[-1, 1], window=[-1, 1], "
+ "symbol='x')"
+ )
assert_equal(res, tgt)
def test_hermite_repr(self):
res = repr(poly.Hermite([0, 1]))
- tgt = 'Hermite([0., 1.], domain=[-1, 1], window=[-1, 1])'
+ tgt = (
+ "Hermite([0., 1.], domain=[-1, 1], window=[-1, 1], "
+ "symbol='x')"
+ )
assert_equal(res, tgt)
def test_hermiteE_repr(self):
res = repr(poly.HermiteE([0, 1]))
- tgt = 'HermiteE([0., 1.], domain=[-1, 1], window=[-1, 1])'
+ tgt = (
+ "HermiteE([0., 1.], domain=[-1, 1], window=[-1, 1], "
+ "symbol='x')"
+ )
assert_equal(res, tgt)
def test_laguerre_repr(self):
res = repr(poly.Laguerre([0, 1]))
- tgt = 'Laguerre([0., 1.], domain=[0, 1], window=[0, 1])'
+ tgt = (
+ "Laguerre([0., 1.], domain=[0, 1], window=[0, 1], "
+ "symbol='x')"
+ )
assert_equal(res, tgt)
@@ -388,3 +419,39 @@ class TestLatexRepr:
p = poly.HermiteE([1, 2, 3])
assert_equal(self.as_latex(p),
r'$x \mapsto 1.0\,{He}_{0}(x) + 2.0\,{He}_{1}(x) + 3.0\,{He}_{2}(x)$')
+
+ def test_symbol_basic(self):
+ # default input
+ p = poly.Polynomial([1, 2, 3], symbol='z')
+ assert_equal(self.as_latex(p),
+ r'$z \mapsto 1.0 + 2.0\,z + 3.0\,z^{2}$')
+
+ # translated input
+ p = poly.Polynomial([1, 2, 3], domain=[-2, 0], symbol='z')
+ assert_equal(
+ self.as_latex(p),
+ (
+ r'$z \mapsto 1.0 + 2.0\,\left(1.0 + z\right) + 3.0\,'
+ r'\left(1.0 + z\right)^{2}$'
+ ),
+ )
+
+ # scaled input
+ p = poly.Polynomial([1, 2, 3], domain=[-0.5, 0.5], symbol='z')
+ assert_equal(
+ self.as_latex(p),
+ (
+ r'$z \mapsto 1.0 + 2.0\,\left(2.0z\right) + 3.0\,'
+ r'\left(2.0z\right)^{2}$'
+ ),
+ )
+
+ # affine input
+ p = poly.Polynomial([1, 2, 3], domain=[-1, 0], symbol='z')
+ assert_equal(
+ self.as_latex(p),
+ (
+ r'$z \mapsto 1.0 + 2.0\,\left(1.0 + 2.0z\right) + 3.0\,'
+ r'\left(1.0 + 2.0z\right)^{2}$'
+ ),
+ )
diff --git a/numpy/polynomial/tests/test_symbol.py b/numpy/polynomial/tests/test_symbol.py
new file mode 100644
index 000000000..4ea6035ef
--- /dev/null
+++ b/numpy/polynomial/tests/test_symbol.py
@@ -0,0 +1,216 @@
+"""
+Tests related to the ``symbol`` attribute of the ABCPolyBase class.
+"""
+
+import pytest
+import numpy.polynomial as poly
+from numpy.core import array
+from numpy.testing import assert_equal, assert_raises, assert_
+
+
+class TestInit:
+ """
+ Test polynomial creation with symbol kwarg.
+ """
+ c = [1, 2, 3]
+
+ def test_default_symbol(self):
+ p = poly.Polynomial(self.c)
+ assert_equal(p.symbol, 'x')
+
+ @pytest.mark.parametrize(('bad_input', 'exception'), (
+ ('', ValueError),
+ ('3', ValueError),
+ (None, TypeError),
+ (1, TypeError),
+ ))
+ def test_symbol_bad_input(self, bad_input, exception):
+ with pytest.raises(exception):
+ p = poly.Polynomial(self.c, symbol=bad_input)
+
+ @pytest.mark.parametrize('symbol', (
+ 'x',
+ 'x_1',
+ 'A',
+ 'xyz',
+ 'β',
+ ))
+ def test_valid_symbols(self, symbol):
+ """
+ Values for symbol that should pass input validation.
+ """
+ p = poly.Polynomial(self.c, symbol=symbol)
+ assert_equal(p.symbol, symbol)
+
+ def test_property(self):
+ """
+ 'symbol' attribute is read only.
+ """
+ p = poly.Polynomial(self.c, symbol='x')
+ with pytest.raises(AttributeError):
+ p.symbol = 'z'
+
+ def test_change_symbol(self):
+ p = poly.Polynomial(self.c, symbol='y')
+ # Create new polynomial from p with different symbol
+ pt = poly.Polynomial(p.coef, symbol='t')
+ assert_equal(pt.symbol, 't')
+
+
+class TestUnaryOperators:
+ p = poly.Polynomial([1, 2, 3], symbol='z')
+
+ def test_neg(self):
+ n = -self.p
+ assert_equal(n.symbol, 'z')
+
+ def test_scalarmul(self):
+ out = self.p * 10
+ assert_equal(out.symbol, 'z')
+
+ def test_rscalarmul(self):
+ out = 10 * self.p
+ assert_equal(out.symbol, 'z')
+
+ def test_pow(self):
+ out = self.p ** 3
+ assert_equal(out.symbol, 'z')
+
+
+@pytest.mark.parametrize(
+ 'rhs',
+ (
+ poly.Polynomial([4, 5, 6], symbol='z'),
+ array([4, 5, 6]),
+ ),
+)
+class TestBinaryOperatorsSameSymbol:
+ """
+ Ensure symbol is preserved for numeric operations on polynomials with
+ the same symbol
+ """
+ p = poly.Polynomial([1, 2, 3], symbol='z')
+
+ def test_add(self, rhs):
+ out = self.p + rhs
+ assert_equal(out.symbol, 'z')
+
+ def test_sub(self, rhs):
+ out = self.p - rhs
+ assert_equal(out.symbol, 'z')
+
+ def test_polymul(self, rhs):
+ out = self.p * rhs
+ assert_equal(out.symbol, 'z')
+
+ def test_divmod(self, rhs):
+ for out in divmod(self.p, rhs):
+ assert_equal(out.symbol, 'z')
+
+ def test_radd(self, rhs):
+ out = rhs + self.p
+ assert_equal(out.symbol, 'z')
+
+ def test_rsub(self, rhs):
+ out = rhs - self.p
+ assert_equal(out.symbol, 'z')
+
+ def test_rmul(self, rhs):
+ out = rhs * self.p
+ assert_equal(out.symbol, 'z')
+
+ def test_rdivmod(self, rhs):
+ for out in divmod(rhs, self.p):
+ assert_equal(out.symbol, 'z')
+
+
+class TestBinaryOperatorsDifferentSymbol:
+ p = poly.Polynomial([1, 2, 3], symbol='x')
+ other = poly.Polynomial([4, 5, 6], symbol='y')
+ ops = (p.__add__, p.__sub__, p.__mul__, p.__floordiv__, p.__mod__)
+
+ @pytest.mark.parametrize('f', ops)
+ def test_binops_fails(self, f):
+ assert_raises(ValueError, f, self.other)
+
+
+class TestEquality:
+ p = poly.Polynomial([1, 2, 3], symbol='x')
+
+ def test_eq(self):
+ other = poly.Polynomial([1, 2, 3], symbol='x')
+ assert_(self.p == other)
+
+ def test_neq(self):
+ other = poly.Polynomial([1, 2, 3], symbol='y')
+ assert_(not self.p == other)
+
+
+class TestExtraMethods:
+ """
+ Test other methods for manipulating/creating polynomial objects.
+ """
+ p = poly.Polynomial([1, 2, 3, 0], symbol='z')
+
+ def test_copy(self):
+ other = self.p.copy()
+ assert_equal(other.symbol, 'z')
+
+ def test_trim(self):
+ other = self.p.trim()
+ assert_equal(other.symbol, 'z')
+
+ def test_truncate(self):
+ other = self.p.truncate(2)
+ assert_equal(other.symbol, 'z')
+
+ @pytest.mark.parametrize('kwarg', (
+ {'domain': [-10, 10]},
+ {'window': [-10, 10]},
+ {'kind': poly.Chebyshev},
+ ))
+ def test_convert(self, kwarg):
+ other = self.p.convert(**kwarg)
+ assert_equal(other.symbol, 'z')
+
+ def test_integ(self):
+ other = self.p.integ()
+ assert_equal(other.symbol, 'z')
+
+ def test_deriv(self):
+ other = self.p.deriv()
+ assert_equal(other.symbol, 'z')
+
+
+def test_composition():
+ p = poly.Polynomial([3, 2, 1], symbol="t")
+ q = poly.Polynomial([5, 1, 0, -1], symbol="λ_1")
+ r = p(q)
+ assert r.symbol == "λ_1"
+
+
+#
+# Class methods that result in new polynomial class instances
+#
+
+
+def test_fit():
+ x, y = (range(10),)*2
+ p = poly.Polynomial.fit(x, y, deg=1, symbol='z')
+ assert_equal(p.symbol, 'z')
+
+
+def test_froomroots():
+ roots = [-2, 2]
+ p = poly.Polynomial.fromroots(roots, symbol='z')
+ assert_equal(p.symbol, 'z')
+
+
+def test_identity():
+ p = poly.Polynomial.identity(domain=[-1, 1], window=[5, 20], symbol='z')
+ assert_equal(p.symbol, 'z')
+
+
+def test_basis():
+ p = poly.Polynomial.basis(3, symbol='z')
+ assert_equal(p.symbol, 'z')