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