Chris@87
|
1 """Test inter-conversion of different polynomial classes.
|
Chris@87
|
2
|
Chris@87
|
3 This tests the convert and cast methods of all the polynomial classes.
|
Chris@87
|
4
|
Chris@87
|
5 """
|
Chris@87
|
6 from __future__ import division, absolute_import, print_function
|
Chris@87
|
7
|
Chris@87
|
8 import operator as op
|
Chris@87
|
9 from numbers import Number
|
Chris@87
|
10
|
Chris@87
|
11 import numpy as np
|
Chris@87
|
12 from numpy.polynomial import (
|
Chris@87
|
13 Polynomial, Legendre, Chebyshev, Laguerre, Hermite, HermiteE)
|
Chris@87
|
14 from numpy.testing import (
|
Chris@87
|
15 assert_almost_equal, assert_raises, assert_equal, assert_,
|
Chris@87
|
16 run_module_suite)
|
Chris@87
|
17 from numpy.compat import long
|
Chris@87
|
18
|
Chris@87
|
19
|
Chris@87
|
20 classes = (
|
Chris@87
|
21 Polynomial, Legendre, Chebyshev, Laguerre,
|
Chris@87
|
22 Hermite, HermiteE)
|
Chris@87
|
23
|
Chris@87
|
24
|
Chris@87
|
25 def test_class_methods():
|
Chris@87
|
26 for Poly1 in classes:
|
Chris@87
|
27 for Poly2 in classes:
|
Chris@87
|
28 yield check_conversion, Poly1, Poly2
|
Chris@87
|
29 yield check_cast, Poly1, Poly2
|
Chris@87
|
30 for Poly in classes:
|
Chris@87
|
31 yield check_call, Poly
|
Chris@87
|
32 yield check_identity, Poly
|
Chris@87
|
33 yield check_basis, Poly
|
Chris@87
|
34 yield check_fromroots, Poly
|
Chris@87
|
35 yield check_fit, Poly
|
Chris@87
|
36 yield check_equal, Poly
|
Chris@87
|
37 yield check_not_equal, Poly
|
Chris@87
|
38 yield check_add, Poly
|
Chris@87
|
39 yield check_sub, Poly
|
Chris@87
|
40 yield check_mul, Poly
|
Chris@87
|
41 yield check_floordiv, Poly
|
Chris@87
|
42 yield check_truediv, Poly
|
Chris@87
|
43 yield check_mod, Poly
|
Chris@87
|
44 yield check_divmod, Poly
|
Chris@87
|
45 yield check_pow, Poly
|
Chris@87
|
46 yield check_integ, Poly
|
Chris@87
|
47 yield check_deriv, Poly
|
Chris@87
|
48 yield check_roots, Poly
|
Chris@87
|
49 yield check_linspace, Poly
|
Chris@87
|
50 yield check_mapparms, Poly
|
Chris@87
|
51 yield check_degree, Poly
|
Chris@87
|
52 yield check_copy, Poly
|
Chris@87
|
53 yield check_cutdeg, Poly
|
Chris@87
|
54 yield check_truncate, Poly
|
Chris@87
|
55 yield check_trim, Poly
|
Chris@87
|
56
|
Chris@87
|
57
|
Chris@87
|
58 #
|
Chris@87
|
59 # helper functions
|
Chris@87
|
60 #
|
Chris@87
|
61 random = np.random.random
|
Chris@87
|
62
|
Chris@87
|
63
|
Chris@87
|
64 def assert_poly_almost_equal(p1, p2, msg=""):
|
Chris@87
|
65 try:
|
Chris@87
|
66 assert_(np.all(p1.domain == p2.domain))
|
Chris@87
|
67 assert_(np.all(p1.window == p2.window))
|
Chris@87
|
68 assert_almost_equal(p1.coef, p2.coef)
|
Chris@87
|
69 except AssertionError:
|
Chris@87
|
70 msg = "Result: %s\nTarget: %s", (p1, p2)
|
Chris@87
|
71 raise AssertionError(msg)
|
Chris@87
|
72
|
Chris@87
|
73
|
Chris@87
|
74 #
|
Chris@87
|
75 # conversion methods that depend on two classes
|
Chris@87
|
76 #
|
Chris@87
|
77
|
Chris@87
|
78
|
Chris@87
|
79 def check_conversion(Poly1, Poly2):
|
Chris@87
|
80 x = np.linspace(0, 1, 10)
|
Chris@87
|
81 coef = random((3,))
|
Chris@87
|
82
|
Chris@87
|
83 d1 = Poly1.domain + random((2,))*.25
|
Chris@87
|
84 w1 = Poly1.window + random((2,))*.25
|
Chris@87
|
85 p1 = Poly1(coef, domain=d1, window=w1)
|
Chris@87
|
86
|
Chris@87
|
87 d2 = Poly2.domain + random((2,))*.25
|
Chris@87
|
88 w2 = Poly2.window + random((2,))*.25
|
Chris@87
|
89 p2 = p1.convert(kind=Poly2, domain=d2, window=w2)
|
Chris@87
|
90
|
Chris@87
|
91 assert_almost_equal(p2.domain, d2)
|
Chris@87
|
92 assert_almost_equal(p2.window, w2)
|
Chris@87
|
93 assert_almost_equal(p2(x), p1(x))
|
Chris@87
|
94
|
Chris@87
|
95
|
Chris@87
|
96 def check_cast(Poly1, Poly2):
|
Chris@87
|
97 x = np.linspace(0, 1, 10)
|
Chris@87
|
98 coef = random((3,))
|
Chris@87
|
99
|
Chris@87
|
100 d1 = Poly1.domain + random((2,))*.25
|
Chris@87
|
101 w1 = Poly1.window + random((2,))*.25
|
Chris@87
|
102 p1 = Poly1(coef, domain=d1, window=w1)
|
Chris@87
|
103
|
Chris@87
|
104 d2 = Poly2.domain + random((2,))*.25
|
Chris@87
|
105 w2 = Poly2.window + random((2,))*.25
|
Chris@87
|
106 p2 = Poly2.cast(p1, domain=d2, window=w2)
|
Chris@87
|
107
|
Chris@87
|
108 assert_almost_equal(p2.domain, d2)
|
Chris@87
|
109 assert_almost_equal(p2.window, w2)
|
Chris@87
|
110 assert_almost_equal(p2(x), p1(x))
|
Chris@87
|
111
|
Chris@87
|
112
|
Chris@87
|
113 #
|
Chris@87
|
114 # methods that depend on one class
|
Chris@87
|
115 #
|
Chris@87
|
116
|
Chris@87
|
117
|
Chris@87
|
118 def check_identity(Poly):
|
Chris@87
|
119 d = Poly.domain + random((2,))*.25
|
Chris@87
|
120 w = Poly.window + random((2,))*.25
|
Chris@87
|
121 x = np.linspace(d[0], d[1], 11)
|
Chris@87
|
122 p = Poly.identity(domain=d, window=w)
|
Chris@87
|
123 assert_equal(p.domain, d)
|
Chris@87
|
124 assert_equal(p.window, w)
|
Chris@87
|
125 assert_almost_equal(p(x), x)
|
Chris@87
|
126
|
Chris@87
|
127
|
Chris@87
|
128 def check_basis(Poly):
|
Chris@87
|
129 d = Poly.domain + random((2,))*.25
|
Chris@87
|
130 w = Poly.window + random((2,))*.25
|
Chris@87
|
131 p = Poly.basis(5, domain=d, window=w)
|
Chris@87
|
132 assert_equal(p.domain, d)
|
Chris@87
|
133 assert_equal(p.window, w)
|
Chris@87
|
134 assert_equal(p.coef, [0]*5 + [1])
|
Chris@87
|
135
|
Chris@87
|
136
|
Chris@87
|
137 def check_fromroots(Poly):
|
Chris@87
|
138 # check that requested roots are zeros of a polynomial
|
Chris@87
|
139 # of correct degree, domain, and window.
|
Chris@87
|
140 d = Poly.domain + random((2,))*.25
|
Chris@87
|
141 w = Poly.window + random((2,))*.25
|
Chris@87
|
142 r = random((5,))
|
Chris@87
|
143 p1 = Poly.fromroots(r, domain=d, window=w)
|
Chris@87
|
144 assert_equal(p1.degree(), len(r))
|
Chris@87
|
145 assert_equal(p1.domain, d)
|
Chris@87
|
146 assert_equal(p1.window, w)
|
Chris@87
|
147 assert_almost_equal(p1(r), 0)
|
Chris@87
|
148
|
Chris@87
|
149 # check that polynomial is monic
|
Chris@87
|
150 pdom = Polynomial.domain
|
Chris@87
|
151 pwin = Polynomial.window
|
Chris@87
|
152 p2 = Polynomial.cast(p1, domain=pdom, window=pwin)
|
Chris@87
|
153 assert_almost_equal(p2.coef[-1], 1)
|
Chris@87
|
154
|
Chris@87
|
155
|
Chris@87
|
156 def check_fit(Poly):
|
Chris@87
|
157
|
Chris@87
|
158 def f(x):
|
Chris@87
|
159 return x*(x - 1)*(x - 2)
|
Chris@87
|
160 x = np.linspace(0, 3)
|
Chris@87
|
161 y = f(x)
|
Chris@87
|
162
|
Chris@87
|
163 # check default value of domain and window
|
Chris@87
|
164 p = Poly.fit(x, y, 3)
|
Chris@87
|
165 assert_almost_equal(p.domain, [0, 3])
|
Chris@87
|
166 assert_almost_equal(p(x), y)
|
Chris@87
|
167 assert_equal(p.degree(), 3)
|
Chris@87
|
168
|
Chris@87
|
169 # check with given domains and window
|
Chris@87
|
170 d = Poly.domain + random((2,))*.25
|
Chris@87
|
171 w = Poly.window + random((2,))*.25
|
Chris@87
|
172 p = Poly.fit(x, y, 3, domain=d, window=w)
|
Chris@87
|
173 assert_almost_equal(p(x), y)
|
Chris@87
|
174 assert_almost_equal(p.domain, d)
|
Chris@87
|
175 assert_almost_equal(p.window, w)
|
Chris@87
|
176
|
Chris@87
|
177 # check with class domain default
|
Chris@87
|
178 p = Poly.fit(x, y, 3, [])
|
Chris@87
|
179 assert_equal(p.domain, Poly.domain)
|
Chris@87
|
180 assert_equal(p.window, Poly.window)
|
Chris@87
|
181
|
Chris@87
|
182 # check that fit accepts weights.
|
Chris@87
|
183 w = np.zeros_like(x)
|
Chris@87
|
184 z = y + random(y.shape)*.25
|
Chris@87
|
185 w[::2] = 1
|
Chris@87
|
186 p1 = Poly.fit(x[::2], z[::2], 3)
|
Chris@87
|
187 p2 = Poly.fit(x, z, 3, w=w)
|
Chris@87
|
188 assert_almost_equal(p1(x), p2(x))
|
Chris@87
|
189
|
Chris@87
|
190
|
Chris@87
|
191 def check_equal(Poly):
|
Chris@87
|
192 p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3])
|
Chris@87
|
193 p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3])
|
Chris@87
|
194 p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3])
|
Chris@87
|
195 p4 = Poly([1, 2, 3], domain=[0, 1], window=[1, 2])
|
Chris@87
|
196 assert_(p1 == p1)
|
Chris@87
|
197 assert_(not p1 == p2)
|
Chris@87
|
198 assert_(not p1 == p3)
|
Chris@87
|
199 assert_(not p1 == p4)
|
Chris@87
|
200
|
Chris@87
|
201
|
Chris@87
|
202 def check_not_equal(Poly):
|
Chris@87
|
203 p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3])
|
Chris@87
|
204 p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3])
|
Chris@87
|
205 p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3])
|
Chris@87
|
206 p4 = Poly([1, 2, 3], domain=[0, 1], window=[1, 2])
|
Chris@87
|
207 assert_(not p1 != p1)
|
Chris@87
|
208 assert_(p1 != p2)
|
Chris@87
|
209 assert_(p1 != p3)
|
Chris@87
|
210 assert_(p1 != p4)
|
Chris@87
|
211
|
Chris@87
|
212
|
Chris@87
|
213 def check_add(Poly):
|
Chris@87
|
214 # This checks commutation, not numerical correctness
|
Chris@87
|
215 c1 = list(random((4,)) + .5)
|
Chris@87
|
216 c2 = list(random((3,)) + .5)
|
Chris@87
|
217 p1 = Poly(c1)
|
Chris@87
|
218 p2 = Poly(c2)
|
Chris@87
|
219 p3 = p1 + p2
|
Chris@87
|
220 assert_poly_almost_equal(p2 + p1, p3)
|
Chris@87
|
221 assert_poly_almost_equal(p1 + c2, p3)
|
Chris@87
|
222 assert_poly_almost_equal(c2 + p1, p3)
|
Chris@87
|
223 assert_poly_almost_equal(p1 + tuple(c2), p3)
|
Chris@87
|
224 assert_poly_almost_equal(tuple(c2) + p1, p3)
|
Chris@87
|
225 assert_poly_almost_equal(p1 + np.array(c2), p3)
|
Chris@87
|
226 assert_poly_almost_equal(np.array(c2) + p1, p3)
|
Chris@87
|
227 assert_raises(TypeError, op.add, p1, Poly([0], domain=Poly.domain + 1))
|
Chris@87
|
228 assert_raises(TypeError, op.add, p1, Poly([0], window=Poly.window + 1))
|
Chris@87
|
229 if Poly is Polynomial:
|
Chris@87
|
230 assert_raises(TypeError, op.add, p1, Chebyshev([0]))
|
Chris@87
|
231 else:
|
Chris@87
|
232 assert_raises(TypeError, op.add, p1, Polynomial([0]))
|
Chris@87
|
233
|
Chris@87
|
234
|
Chris@87
|
235 def check_sub(Poly):
|
Chris@87
|
236 # This checks commutation, not numerical correctness
|
Chris@87
|
237 c1 = list(random((4,)) + .5)
|
Chris@87
|
238 c2 = list(random((3,)) + .5)
|
Chris@87
|
239 p1 = Poly(c1)
|
Chris@87
|
240 p2 = Poly(c2)
|
Chris@87
|
241 p3 = p1 - p2
|
Chris@87
|
242 assert_poly_almost_equal(p2 - p1, -p3)
|
Chris@87
|
243 assert_poly_almost_equal(p1 - c2, p3)
|
Chris@87
|
244 assert_poly_almost_equal(c2 - p1, -p3)
|
Chris@87
|
245 assert_poly_almost_equal(p1 - tuple(c2), p3)
|
Chris@87
|
246 assert_poly_almost_equal(tuple(c2) - p1, -p3)
|
Chris@87
|
247 assert_poly_almost_equal(p1 - np.array(c2), p3)
|
Chris@87
|
248 assert_poly_almost_equal(np.array(c2) - p1, -p3)
|
Chris@87
|
249 assert_raises(TypeError, op.sub, p1, Poly([0], domain=Poly.domain + 1))
|
Chris@87
|
250 assert_raises(TypeError, op.sub, p1, Poly([0], window=Poly.window + 1))
|
Chris@87
|
251 if Poly is Polynomial:
|
Chris@87
|
252 assert_raises(TypeError, op.sub, p1, Chebyshev([0]))
|
Chris@87
|
253 else:
|
Chris@87
|
254 assert_raises(TypeError, op.sub, p1, Polynomial([0]))
|
Chris@87
|
255
|
Chris@87
|
256
|
Chris@87
|
257 def check_mul(Poly):
|
Chris@87
|
258 c1 = list(random((4,)) + .5)
|
Chris@87
|
259 c2 = list(random((3,)) + .5)
|
Chris@87
|
260 p1 = Poly(c1)
|
Chris@87
|
261 p2 = Poly(c2)
|
Chris@87
|
262 p3 = p1 * p2
|
Chris@87
|
263 assert_poly_almost_equal(p2 * p1, p3)
|
Chris@87
|
264 assert_poly_almost_equal(p1 * c2, p3)
|
Chris@87
|
265 assert_poly_almost_equal(c2 * p1, p3)
|
Chris@87
|
266 assert_poly_almost_equal(p1 * tuple(c2), p3)
|
Chris@87
|
267 assert_poly_almost_equal(tuple(c2) * p1, p3)
|
Chris@87
|
268 assert_poly_almost_equal(p1 * np.array(c2), p3)
|
Chris@87
|
269 assert_poly_almost_equal(np.array(c2) * p1, p3)
|
Chris@87
|
270 assert_poly_almost_equal(p1 * 2, p1 * Poly([2]))
|
Chris@87
|
271 assert_poly_almost_equal(2 * p1, p1 * Poly([2]))
|
Chris@87
|
272 assert_raises(TypeError, op.mul, p1, Poly([0], domain=Poly.domain + 1))
|
Chris@87
|
273 assert_raises(TypeError, op.mul, p1, Poly([0], window=Poly.window + 1))
|
Chris@87
|
274 if Poly is Polynomial:
|
Chris@87
|
275 assert_raises(TypeError, op.mul, p1, Chebyshev([0]))
|
Chris@87
|
276 else:
|
Chris@87
|
277 assert_raises(TypeError, op.mul, p1, Polynomial([0]))
|
Chris@87
|
278
|
Chris@87
|
279
|
Chris@87
|
280 def check_floordiv(Poly):
|
Chris@87
|
281 c1 = list(random((4,)) + .5)
|
Chris@87
|
282 c2 = list(random((3,)) + .5)
|
Chris@87
|
283 c3 = list(random((2,)) + .5)
|
Chris@87
|
284 p1 = Poly(c1)
|
Chris@87
|
285 p2 = Poly(c2)
|
Chris@87
|
286 p3 = Poly(c3)
|
Chris@87
|
287 p4 = p1 * p2 + p3
|
Chris@87
|
288 c4 = list(p4.coef)
|
Chris@87
|
289 assert_poly_almost_equal(p4 // p2, p1)
|
Chris@87
|
290 assert_poly_almost_equal(p4 // c2, p1)
|
Chris@87
|
291 assert_poly_almost_equal(c4 // p2, p1)
|
Chris@87
|
292 assert_poly_almost_equal(p4 // tuple(c2), p1)
|
Chris@87
|
293 assert_poly_almost_equal(tuple(c4) // p2, p1)
|
Chris@87
|
294 assert_poly_almost_equal(p4 // np.array(c2), p1)
|
Chris@87
|
295 assert_poly_almost_equal(np.array(c4) // p2, p1)
|
Chris@87
|
296 assert_poly_almost_equal(2 // p2, Poly([0]))
|
Chris@87
|
297 assert_poly_almost_equal(p2 // 2, 0.5*p2)
|
Chris@87
|
298 assert_raises(
|
Chris@87
|
299 TypeError, op.floordiv, p1, Poly([0], domain=Poly.domain + 1))
|
Chris@87
|
300 assert_raises(
|
Chris@87
|
301 TypeError, op.floordiv, p1, Poly([0], window=Poly.window + 1))
|
Chris@87
|
302 if Poly is Polynomial:
|
Chris@87
|
303 assert_raises(TypeError, op.floordiv, p1, Chebyshev([0]))
|
Chris@87
|
304 else:
|
Chris@87
|
305 assert_raises(TypeError, op.floordiv, p1, Polynomial([0]))
|
Chris@87
|
306
|
Chris@87
|
307
|
Chris@87
|
308 def check_truediv(Poly):
|
Chris@87
|
309 # true division is valid only if the denominator is a Number and
|
Chris@87
|
310 # not a python bool.
|
Chris@87
|
311 p1 = Poly([1,2,3])
|
Chris@87
|
312 p2 = p1 * 5
|
Chris@87
|
313
|
Chris@87
|
314 for stype in np.ScalarType:
|
Chris@87
|
315 if not issubclass(stype, Number) or issubclass(stype, bool):
|
Chris@87
|
316 continue
|
Chris@87
|
317 s = stype(5)
|
Chris@87
|
318 assert_poly_almost_equal(op.truediv(p2, s), p1)
|
Chris@87
|
319 assert_raises(TypeError, op.truediv, s, p2)
|
Chris@87
|
320 for stype in (int, long, float):
|
Chris@87
|
321 s = stype(5)
|
Chris@87
|
322 assert_poly_almost_equal(op.truediv(p2, s), p1)
|
Chris@87
|
323 assert_raises(TypeError, op.truediv, s, p2)
|
Chris@87
|
324 for stype in [complex]:
|
Chris@87
|
325 s = stype(5, 0)
|
Chris@87
|
326 assert_poly_almost_equal(op.truediv(p2, s), p1)
|
Chris@87
|
327 assert_raises(TypeError, op.truediv, s, p2)
|
Chris@87
|
328 for s in [tuple(), list(), dict(), bool(), np.array([1])]:
|
Chris@87
|
329 assert_raises(TypeError, op.truediv, p2, s)
|
Chris@87
|
330 assert_raises(TypeError, op.truediv, s, p2)
|
Chris@87
|
331 for ptype in classes:
|
Chris@87
|
332 assert_raises(TypeError, op.truediv, p2, ptype(1))
|
Chris@87
|
333
|
Chris@87
|
334
|
Chris@87
|
335 def check_mod(Poly):
|
Chris@87
|
336 # This checks commutation, not numerical correctness
|
Chris@87
|
337 c1 = list(random((4,)) + .5)
|
Chris@87
|
338 c2 = list(random((3,)) + .5)
|
Chris@87
|
339 c3 = list(random((2,)) + .5)
|
Chris@87
|
340 p1 = Poly(c1)
|
Chris@87
|
341 p2 = Poly(c2)
|
Chris@87
|
342 p3 = Poly(c3)
|
Chris@87
|
343 p4 = p1 * p2 + p3
|
Chris@87
|
344 c4 = list(p4.coef)
|
Chris@87
|
345 assert_poly_almost_equal(p4 % p2, p3)
|
Chris@87
|
346 assert_poly_almost_equal(p4 % c2, p3)
|
Chris@87
|
347 assert_poly_almost_equal(c4 % p2, p3)
|
Chris@87
|
348 assert_poly_almost_equal(p4 % tuple(c2), p3)
|
Chris@87
|
349 assert_poly_almost_equal(tuple(c4) % p2, p3)
|
Chris@87
|
350 assert_poly_almost_equal(p4 % np.array(c2), p3)
|
Chris@87
|
351 assert_poly_almost_equal(np.array(c4) % p2, p3)
|
Chris@87
|
352 assert_poly_almost_equal(2 % p2, Poly([2]))
|
Chris@87
|
353 assert_poly_almost_equal(p2 % 2, Poly([0]))
|
Chris@87
|
354 assert_raises(TypeError, op.mod, p1, Poly([0], domain=Poly.domain + 1))
|
Chris@87
|
355 assert_raises(TypeError, op.mod, p1, Poly([0], window=Poly.window + 1))
|
Chris@87
|
356 if Poly is Polynomial:
|
Chris@87
|
357 assert_raises(TypeError, op.mod, p1, Chebyshev([0]))
|
Chris@87
|
358 else:
|
Chris@87
|
359 assert_raises(TypeError, op.mod, p1, Polynomial([0]))
|
Chris@87
|
360
|
Chris@87
|
361
|
Chris@87
|
362 def check_divmod(Poly):
|
Chris@87
|
363 # This checks commutation, not numerical correctness
|
Chris@87
|
364 c1 = list(random((4,)) + .5)
|
Chris@87
|
365 c2 = list(random((3,)) + .5)
|
Chris@87
|
366 c3 = list(random((2,)) + .5)
|
Chris@87
|
367 p1 = Poly(c1)
|
Chris@87
|
368 p2 = Poly(c2)
|
Chris@87
|
369 p3 = Poly(c3)
|
Chris@87
|
370 p4 = p1 * p2 + p3
|
Chris@87
|
371 c4 = list(p4.coef)
|
Chris@87
|
372 quo, rem = divmod(p4, p2)
|
Chris@87
|
373 assert_poly_almost_equal(quo, p1)
|
Chris@87
|
374 assert_poly_almost_equal(rem, p3)
|
Chris@87
|
375 quo, rem = divmod(p4, c2)
|
Chris@87
|
376 assert_poly_almost_equal(quo, p1)
|
Chris@87
|
377 assert_poly_almost_equal(rem, p3)
|
Chris@87
|
378 quo, rem = divmod(c4, p2)
|
Chris@87
|
379 assert_poly_almost_equal(quo, p1)
|
Chris@87
|
380 assert_poly_almost_equal(rem, p3)
|
Chris@87
|
381 quo, rem = divmod(p4, tuple(c2))
|
Chris@87
|
382 assert_poly_almost_equal(quo, p1)
|
Chris@87
|
383 assert_poly_almost_equal(rem, p3)
|
Chris@87
|
384 quo, rem = divmod(tuple(c4), p2)
|
Chris@87
|
385 assert_poly_almost_equal(quo, p1)
|
Chris@87
|
386 assert_poly_almost_equal(rem, p3)
|
Chris@87
|
387 quo, rem = divmod(p4, np.array(c2))
|
Chris@87
|
388 assert_poly_almost_equal(quo, p1)
|
Chris@87
|
389 assert_poly_almost_equal(rem, p3)
|
Chris@87
|
390 quo, rem = divmod(np.array(c4), p2)
|
Chris@87
|
391 assert_poly_almost_equal(quo, p1)
|
Chris@87
|
392 assert_poly_almost_equal(rem, p3)
|
Chris@87
|
393 quo, rem = divmod(p2, 2)
|
Chris@87
|
394 assert_poly_almost_equal(quo, 0.5*p2)
|
Chris@87
|
395 assert_poly_almost_equal(rem, Poly([0]))
|
Chris@87
|
396 quo, rem = divmod(2, p2)
|
Chris@87
|
397 assert_poly_almost_equal(quo, Poly([0]))
|
Chris@87
|
398 assert_poly_almost_equal(rem, Poly([2]))
|
Chris@87
|
399 assert_raises(TypeError, divmod, p1, Poly([0], domain=Poly.domain + 1))
|
Chris@87
|
400 assert_raises(TypeError, divmod, p1, Poly([0], window=Poly.window + 1))
|
Chris@87
|
401 if Poly is Polynomial:
|
Chris@87
|
402 assert_raises(TypeError, divmod, p1, Chebyshev([0]))
|
Chris@87
|
403 else:
|
Chris@87
|
404 assert_raises(TypeError, divmod, p1, Polynomial([0]))
|
Chris@87
|
405
|
Chris@87
|
406
|
Chris@87
|
407 def check_roots(Poly):
|
Chris@87
|
408 d = Poly.domain + random((2,))*.25
|
Chris@87
|
409 w = Poly.window + random((2,))*.25
|
Chris@87
|
410 tgt = np.sort(random((5,)))
|
Chris@87
|
411 res = np.sort(Poly.fromroots(tgt, domain=d, window=w).roots())
|
Chris@87
|
412 assert_almost_equal(res, tgt)
|
Chris@87
|
413 # default domain and window
|
Chris@87
|
414 res = np.sort(Poly.fromroots(tgt).roots())
|
Chris@87
|
415 assert_almost_equal(res, tgt)
|
Chris@87
|
416
|
Chris@87
|
417
|
Chris@87
|
418 def check_degree(Poly):
|
Chris@87
|
419 p = Poly.basis(5)
|
Chris@87
|
420 assert_equal(p.degree(), 5)
|
Chris@87
|
421
|
Chris@87
|
422
|
Chris@87
|
423 def check_copy(Poly):
|
Chris@87
|
424 p1 = Poly.basis(5)
|
Chris@87
|
425 p2 = p1.copy()
|
Chris@87
|
426 assert_(p1 == p2)
|
Chris@87
|
427 assert_(p1 is not p2)
|
Chris@87
|
428 assert_(p1.coef is not p2.coef)
|
Chris@87
|
429 assert_(p1.domain is not p2.domain)
|
Chris@87
|
430 assert_(p1.window is not p2.window)
|
Chris@87
|
431
|
Chris@87
|
432
|
Chris@87
|
433 def check_integ(Poly):
|
Chris@87
|
434 P = Polynomial
|
Chris@87
|
435 # Check defaults
|
Chris@87
|
436 p0 = Poly.cast(P([1*2, 2*3, 3*4]))
|
Chris@87
|
437 p1 = P.cast(p0.integ())
|
Chris@87
|
438 p2 = P.cast(p0.integ(2))
|
Chris@87
|
439 assert_poly_almost_equal(p1, P([0, 2, 3, 4]))
|
Chris@87
|
440 assert_poly_almost_equal(p2, P([0, 0, 1, 1, 1]))
|
Chris@87
|
441 # Check with k
|
Chris@87
|
442 p0 = Poly.cast(P([1*2, 2*3, 3*4]))
|
Chris@87
|
443 p1 = P.cast(p0.integ(k=1))
|
Chris@87
|
444 p2 = P.cast(p0.integ(2, k=[1, 1]))
|
Chris@87
|
445 assert_poly_almost_equal(p1, P([1, 2, 3, 4]))
|
Chris@87
|
446 assert_poly_almost_equal(p2, P([1, 1, 1, 1, 1]))
|
Chris@87
|
447 # Check with lbnd
|
Chris@87
|
448 p0 = Poly.cast(P([1*2, 2*3, 3*4]))
|
Chris@87
|
449 p1 = P.cast(p0.integ(lbnd=1))
|
Chris@87
|
450 p2 = P.cast(p0.integ(2, lbnd=1))
|
Chris@87
|
451 assert_poly_almost_equal(p1, P([-9, 2, 3, 4]))
|
Chris@87
|
452 assert_poly_almost_equal(p2, P([6, -9, 1, 1, 1]))
|
Chris@87
|
453 # Check scaling
|
Chris@87
|
454 d = 2*Poly.domain
|
Chris@87
|
455 p0 = Poly.cast(P([1*2, 2*3, 3*4]), domain=d)
|
Chris@87
|
456 p1 = P.cast(p0.integ())
|
Chris@87
|
457 p2 = P.cast(p0.integ(2))
|
Chris@87
|
458 assert_poly_almost_equal(p1, P([0, 2, 3, 4]))
|
Chris@87
|
459 assert_poly_almost_equal(p2, P([0, 0, 1, 1, 1]))
|
Chris@87
|
460
|
Chris@87
|
461
|
Chris@87
|
462 def check_deriv(Poly):
|
Chris@87
|
463 # Check that the derivative is the inverse of integration. It is
|
Chris@87
|
464 # assumes that the integration has been checked elsewhere.
|
Chris@87
|
465 d = Poly.domain + random((2,))*.25
|
Chris@87
|
466 w = Poly.window + random((2,))*.25
|
Chris@87
|
467 p1 = Poly([1, 2, 3], domain=d, window=w)
|
Chris@87
|
468 p2 = p1.integ(2, k=[1, 2])
|
Chris@87
|
469 p3 = p1.integ(1, k=[1])
|
Chris@87
|
470 assert_almost_equal(p2.deriv(1).coef, p3.coef)
|
Chris@87
|
471 assert_almost_equal(p2.deriv(2).coef, p1.coef)
|
Chris@87
|
472 # default domain and window
|
Chris@87
|
473 p1 = Poly([1, 2, 3])
|
Chris@87
|
474 p2 = p1.integ(2, k=[1, 2])
|
Chris@87
|
475 p3 = p1.integ(1, k=[1])
|
Chris@87
|
476 assert_almost_equal(p2.deriv(1).coef, p3.coef)
|
Chris@87
|
477 assert_almost_equal(p2.deriv(2).coef, p1.coef)
|
Chris@87
|
478
|
Chris@87
|
479
|
Chris@87
|
480 def check_linspace(Poly):
|
Chris@87
|
481 d = Poly.domain + random((2,))*.25
|
Chris@87
|
482 w = Poly.window + random((2,))*.25
|
Chris@87
|
483 p = Poly([1, 2, 3], domain=d, window=w)
|
Chris@87
|
484 # check default domain
|
Chris@87
|
485 xtgt = np.linspace(d[0], d[1], 20)
|
Chris@87
|
486 ytgt = p(xtgt)
|
Chris@87
|
487 xres, yres = p.linspace(20)
|
Chris@87
|
488 assert_almost_equal(xres, xtgt)
|
Chris@87
|
489 assert_almost_equal(yres, ytgt)
|
Chris@87
|
490 # check specified domain
|
Chris@87
|
491 xtgt = np.linspace(0, 2, 20)
|
Chris@87
|
492 ytgt = p(xtgt)
|
Chris@87
|
493 xres, yres = p.linspace(20, domain=[0, 2])
|
Chris@87
|
494 assert_almost_equal(xres, xtgt)
|
Chris@87
|
495 assert_almost_equal(yres, ytgt)
|
Chris@87
|
496
|
Chris@87
|
497
|
Chris@87
|
498 def check_pow(Poly):
|
Chris@87
|
499 d = Poly.domain + random((2,))*.25
|
Chris@87
|
500 w = Poly.window + random((2,))*.25
|
Chris@87
|
501 tgt = Poly([1], domain=d, window=w)
|
Chris@87
|
502 tst = Poly([1, 2, 3], domain=d, window=w)
|
Chris@87
|
503 for i in range(5):
|
Chris@87
|
504 assert_poly_almost_equal(tst**i, tgt)
|
Chris@87
|
505 tgt = tgt * tst
|
Chris@87
|
506 # default domain and window
|
Chris@87
|
507 tgt = Poly([1])
|
Chris@87
|
508 tst = Poly([1, 2, 3])
|
Chris@87
|
509 for i in range(5):
|
Chris@87
|
510 assert_poly_almost_equal(tst**i, tgt)
|
Chris@87
|
511 tgt = tgt * tst
|
Chris@87
|
512 # check error for invalid powers
|
Chris@87
|
513 assert_raises(ValueError, op.pow, tgt, 1.5)
|
Chris@87
|
514 assert_raises(ValueError, op.pow, tgt, -1)
|
Chris@87
|
515
|
Chris@87
|
516
|
Chris@87
|
517 def check_call(Poly):
|
Chris@87
|
518 P = Polynomial
|
Chris@87
|
519 d = Poly.domain
|
Chris@87
|
520 x = np.linspace(d[0], d[1], 11)
|
Chris@87
|
521
|
Chris@87
|
522 # Check defaults
|
Chris@87
|
523 p = Poly.cast(P([1, 2, 3]))
|
Chris@87
|
524 tgt = 1 + x*(2 + 3*x)
|
Chris@87
|
525 res = p(x)
|
Chris@87
|
526 assert_almost_equal(res, tgt)
|
Chris@87
|
527
|
Chris@87
|
528
|
Chris@87
|
529 def check_cutdeg(Poly):
|
Chris@87
|
530 p = Poly([1, 2, 3])
|
Chris@87
|
531 assert_raises(ValueError, p.cutdeg, .5)
|
Chris@87
|
532 assert_raises(ValueError, p.cutdeg, -1)
|
Chris@87
|
533 assert_equal(len(p.cutdeg(3)), 3)
|
Chris@87
|
534 assert_equal(len(p.cutdeg(2)), 3)
|
Chris@87
|
535 assert_equal(len(p.cutdeg(1)), 2)
|
Chris@87
|
536 assert_equal(len(p.cutdeg(0)), 1)
|
Chris@87
|
537
|
Chris@87
|
538
|
Chris@87
|
539 def check_truncate(Poly):
|
Chris@87
|
540 p = Poly([1, 2, 3])
|
Chris@87
|
541 assert_raises(ValueError, p.truncate, .5)
|
Chris@87
|
542 assert_raises(ValueError, p.truncate, 0)
|
Chris@87
|
543 assert_equal(len(p.truncate(4)), 3)
|
Chris@87
|
544 assert_equal(len(p.truncate(3)), 3)
|
Chris@87
|
545 assert_equal(len(p.truncate(2)), 2)
|
Chris@87
|
546 assert_equal(len(p.truncate(1)), 1)
|
Chris@87
|
547
|
Chris@87
|
548
|
Chris@87
|
549 def check_trim(Poly):
|
Chris@87
|
550 c = [1, 1e-6, 1e-12, 0]
|
Chris@87
|
551 p = Poly(c)
|
Chris@87
|
552 assert_equal(p.trim().coef, c[:3])
|
Chris@87
|
553 assert_equal(p.trim(1e-10).coef, c[:2])
|
Chris@87
|
554 assert_equal(p.trim(1e-5).coef, c[:1])
|
Chris@87
|
555
|
Chris@87
|
556
|
Chris@87
|
557 def check_mapparms(Poly):
|
Chris@87
|
558 # check with defaults. Should be identity.
|
Chris@87
|
559 d = Poly.domain
|
Chris@87
|
560 w = Poly.window
|
Chris@87
|
561 p = Poly([1], domain=d, window=w)
|
Chris@87
|
562 assert_almost_equal([0, 1], p.mapparms())
|
Chris@87
|
563 #
|
Chris@87
|
564 w = 2*d + 1
|
Chris@87
|
565 p = Poly([1], domain=d, window=w)
|
Chris@87
|
566 assert_almost_equal([1, 2], p.mapparms())
|
Chris@87
|
567
|
Chris@87
|
568
|
Chris@87
|
569 if __name__ == "__main__":
|
Chris@87
|
570 run_module_suite()
|