File size: 5,372 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
"""
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')
|