codekingpro/portable-devtools
114k
1"""
2Tests related to the ``symbol`` attribute of the ABCPolyBase class.
3"""
4
5import pytest
6
7import numpy.polynomial as poly
8from numpy._core import array
9from numpy.testing import assert_, assert_equal, assert_raises
10
11
12class TestInit:
13 """
14 Test polynomial creation with symbol kwarg.
15 """
16 c = [1, 2, 3]
17
18 def test_default_symbol(self):
19 p = poly.Polynomial(self.c)
20 assert_equal(p.symbol, 'x')
21
22 @pytest.mark.parametrize(('bad_input', 'exception'), (
23 ('', ValueError),
24 ('3', ValueError),
25 (None, TypeError),
26 (1, TypeError),
27 ))
28 def test_symbol_bad_input(self, bad_input, exception):
29 with pytest.raises(exception):
30 p = poly.Polynomial(self.c, symbol=bad_input)
31
32 @pytest.mark.parametrize('symbol', (
33 'x',
34 'x_1',
35 'A',
36 'xyz',
37 'β',
38 ))
39 def test_valid_symbols(self, symbol):
40 """
41 Values for symbol that should pass input validation.
42 """
43 p = poly.Polynomial(self.c, symbol=symbol)
44 assert_equal(p.symbol, symbol)
45
46 def test_property(self):
47 """
48 'symbol' attribute is read only.
49 """
50 p = poly.Polynomial(self.c, symbol='x')
51 with pytest.raises(AttributeError):
52 p.symbol = 'z'
53
54 def test_change_symbol(self):
55 p = poly.Polynomial(self.c, symbol='y')
56 # Create new polynomial from p with different symbol
57 pt = poly.Polynomial(p.coef, symbol='t')
58 assert_equal(pt.symbol, 't')
59
60
61class TestUnaryOperators:
62 p = poly.Polynomial([1, 2, 3], symbol='z')
63
64 def test_neg(self):
65 n = -self.p
66 assert_equal(n.symbol, 'z')
67
68 def test_scalarmul(self):
69 out = self.p * 10
70 assert_equal(out.symbol, 'z')
71
72 def test_rscalarmul(self):
73 out = 10 * self.p
74 assert_equal(out.symbol, 'z')
75
76 def test_pow(self):
77 out = self.p ** 3
78 assert_equal(out.symbol, 'z')
79
80
81@pytest.mark.parametrize(
82 'rhs',
83 (
84 poly.Polynomial([4, 5, 6], symbol='z'),
85 array([4, 5, 6]),
86 ),
87)
88class TestBinaryOperatorsSameSymbol:
89 """
90 Ensure symbol is preserved for numeric operations on polynomials with
91 the same symbol
92 """
93 p = poly.Polynomial([1, 2, 3], symbol='z')
94
95 def test_add(self, rhs):
96 out = self.p + rhs
97 assert_equal(out.symbol, 'z')
98
99 def test_sub(self, rhs):
100 out = self.p - rhs
101 assert_equal(out.symbol, 'z')
102
103 def test_polymul(self, rhs):
104 out = self.p * rhs
105 assert_equal(out.symbol, 'z')
106
107 def test_divmod(self, rhs):
108 for out in divmod(self.p, rhs):
109 assert_equal(out.symbol, 'z')
110
111 def test_radd(self, rhs):
112 out = rhs + self.p
113 assert_equal(out.symbol, 'z')
114
115 def test_rsub(self, rhs):
116 out = rhs - self.p
117 assert_equal(out.symbol, 'z')
118
119 def test_rmul(self, rhs):
120 out = rhs * self.p
121 assert_equal(out.symbol, 'z')
122
123 def test_rdivmod(self, rhs):
124 for out in divmod(rhs, self.p):
125 assert_equal(out.symbol, 'z')
126
127
128class TestBinaryOperatorsDifferentSymbol:
129 p = poly.Polynomial([1, 2, 3], symbol='x')
130 other = poly.Polynomial([4, 5, 6], symbol='y')
131 ops = (p.__add__, p.__sub__, p.__mul__, p.__floordiv__, p.__mod__)
132
133 @pytest.mark.parametrize('f', ops)
134 def test_binops_fails(self, f):
135 assert_raises(ValueError, f, self.other)
136
137
138class TestEquality:
139 p = poly.Polynomial([1, 2, 3], symbol='x')
140
141 def test_eq(self):
142 other = poly.Polynomial([1, 2, 3], symbol='x')
143 assert_(self.p == other)
144
145 def test_neq(self):
146 other = poly.Polynomial([1, 2, 3], symbol='y')
147 assert_(not self.p == other)
148
149
150class TestExtraMethods:
151 """
152 Test other methods for manipulating/creating polynomial objects.
153 """
154 p = poly.Polynomial([1, 2, 3, 0], symbol='z')
155
156 def test_copy(self):
157 other = self.p.copy()
158 assert_equal(other.symbol, 'z')
159
160 def test_trim(self):
161 other = self.p.trim()
162 assert_equal(other.symbol, 'z')
163
164 def test_truncate(self):
165 other = self.p.truncate(2)
166 assert_equal(other.symbol, 'z')
167
168 @pytest.mark.parametrize('kwarg', (
169 {'domain': [-10, 10]},
170 {'window': [-10, 10]},
171 {'kind': poly.Chebyshev},
172 ))
173 def test_convert(self, kwarg):
174 other = self.p.convert(**kwarg)
175 assert_equal(other.symbol, 'z')
176
177 def test_integ(self):
178 other = self.p.integ()
179 assert_equal(other.symbol, 'z')
180
181 def test_deriv(self):
182 other = self.p.deriv()
183 assert_equal(other.symbol, 'z')
184
185
186def test_composition():
187 p = poly.Polynomial([3, 2, 1], symbol="t")
188 q = poly.Polynomial([5, 1, 0, -1], symbol="λ_1")
189 r = p(q)
190 assert r.symbol == "λ_1"
191
192
193#
194# Class methods that result in new polynomial class instances
195#
196
197
198def test_fit():
199 x, y = (range(10),) * 2
200 p = poly.Polynomial.fit(x, y, deg=1, symbol='z')
201 assert_equal(p.symbol, 'z')
202
203
204def test_froomroots():
205 roots = [-2, 2]
206 p = poly.Polynomial.fromroots(roots, symbol='z')
207 assert_equal(p.symbol, 'z')
208
209
210def test_identity():
211 p = poly.Polynomial.identity(domain=[-1, 1], window=[5, 20], symbol='z')
212 assert_equal(p.symbol, 'z')
213
214
215def test_basis():
216 p = poly.Polynomial.basis(3, symbol='z')
217 assert_equal(p.symbol, 'z')
218 