comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/polynomial/tests/test_polynomial.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 """Tests for polynomial module.
2
3 """
4 from __future__ import division, absolute_import, print_function
5
6 import numpy as np
7 import numpy.polynomial.polynomial as poly
8 from numpy.testing import (
9 TestCase, assert_almost_equal, assert_raises,
10 assert_equal, assert_, run_module_suite)
11
12
13 def trim(x):
14 return poly.polytrim(x, tol=1e-6)
15
16 T0 = [1]
17 T1 = [0, 1]
18 T2 = [-1, 0, 2]
19 T3 = [0, -3, 0, 4]
20 T4 = [1, 0, -8, 0, 8]
21 T5 = [0, 5, 0, -20, 0, 16]
22 T6 = [-1, 0, 18, 0, -48, 0, 32]
23 T7 = [0, -7, 0, 56, 0, -112, 0, 64]
24 T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128]
25 T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256]
26
27 Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
28
29
30 class TestConstants(TestCase):
31
32 def test_polydomain(self):
33 assert_equal(poly.polydomain, [-1, 1])
34
35 def test_polyzero(self):
36 assert_equal(poly.polyzero, [0])
37
38 def test_polyone(self):
39 assert_equal(poly.polyone, [1])
40
41 def test_polyx(self):
42 assert_equal(poly.polyx, [0, 1])
43
44
45 class TestArithmetic(TestCase):
46
47 def test_polyadd(self):
48 for i in range(5):
49 for j in range(5):
50 msg = "At i=%d, j=%d" % (i, j)
51 tgt = np.zeros(max(i, j) + 1)
52 tgt[i] += 1
53 tgt[j] += 1
54 res = poly.polyadd([0]*i + [1], [0]*j + [1])
55 assert_equal(trim(res), trim(tgt), err_msg=msg)
56
57 def test_polysub(self):
58 for i in range(5):
59 for j in range(5):
60 msg = "At i=%d, j=%d" % (i, j)
61 tgt = np.zeros(max(i, j) + 1)
62 tgt[i] += 1
63 tgt[j] -= 1
64 res = poly.polysub([0]*i + [1], [0]*j + [1])
65 assert_equal(trim(res), trim(tgt), err_msg=msg)
66
67 def test_polymulx(self):
68 assert_equal(poly.polymulx([0]), [0])
69 assert_equal(poly.polymulx([1]), [0, 1])
70 for i in range(1, 5):
71 ser = [0]*i + [1]
72 tgt = [0]*(i + 1) + [1]
73 assert_equal(poly.polymulx(ser), tgt)
74
75 def test_polymul(self):
76 for i in range(5):
77 for j in range(5):
78 msg = "At i=%d, j=%d" % (i, j)
79 tgt = np.zeros(i + j + 1)
80 tgt[i + j] += 1
81 res = poly.polymul([0]*i + [1], [0]*j + [1])
82 assert_equal(trim(res), trim(tgt), err_msg=msg)
83
84 def test_polydiv(self):
85 # check zero division
86 assert_raises(ZeroDivisionError, poly.polydiv, [1], [0])
87
88 # check scalar division
89 quo, rem = poly.polydiv([2], [2])
90 assert_equal((quo, rem), (1, 0))
91 quo, rem = poly.polydiv([2, 2], [2])
92 assert_equal((quo, rem), ((1, 1), 0))
93
94 # check rest.
95 for i in range(5):
96 for j in range(5):
97 msg = "At i=%d, j=%d" % (i, j)
98 ci = [0]*i + [1, 2]
99 cj = [0]*j + [1, 2]
100 tgt = poly.polyadd(ci, cj)
101 quo, rem = poly.polydiv(tgt, ci)
102 res = poly.polyadd(poly.polymul(quo, ci), rem)
103 assert_equal(res, tgt, err_msg=msg)
104
105
106 class TestEvaluation(TestCase):
107 # coefficients of 1 + 2*x + 3*x**2
108 c1d = np.array([1., 2., 3.])
109 c2d = np.einsum('i,j->ij', c1d, c1d)
110 c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
111
112 # some random values in [-1, 1)
113 x = np.random.random((3, 5))*2 - 1
114 y = poly.polyval(x, [1., 2., 3.])
115
116 def test_polyval(self):
117 #check empty input
118 assert_equal(poly.polyval([], [1]).size, 0)
119
120 #check normal input)
121 x = np.linspace(-1, 1)
122 y = [x**i for i in range(5)]
123 for i in range(5):
124 tgt = y[i]
125 res = poly.polyval(x, [0]*i + [1])
126 assert_almost_equal(res, tgt)
127 tgt = x*(x**2 - 1)
128 res = poly.polyval(x, [0, -1, 0, 1])
129 assert_almost_equal(res, tgt)
130
131 #check that shape is preserved
132 for i in range(3):
133 dims = [2]*i
134 x = np.zeros(dims)
135 assert_equal(poly.polyval(x, [1]).shape, dims)
136 assert_equal(poly.polyval(x, [1, 0]).shape, dims)
137 assert_equal(poly.polyval(x, [1, 0, 0]).shape, dims)
138
139 def test_polyval2d(self):
140 x1, x2, x3 = self.x
141 y1, y2, y3 = self.y
142
143 #test exceptions
144 assert_raises(ValueError, poly.polyval2d, x1, x2[:2], self.c2d)
145
146 #test values
147 tgt = y1*y2
148 res = poly.polyval2d(x1, x2, self.c2d)
149 assert_almost_equal(res, tgt)
150
151 #test shape
152 z = np.ones((2, 3))
153 res = poly.polyval2d(z, z, self.c2d)
154 assert_(res.shape == (2, 3))
155
156 def test_polyval3d(self):
157 x1, x2, x3 = self.x
158 y1, y2, y3 = self.y
159
160 #test exceptions
161 assert_raises(ValueError, poly.polyval3d, x1, x2, x3[:2], self.c3d)
162
163 #test values
164 tgt = y1*y2*y3
165 res = poly.polyval3d(x1, x2, x3, self.c3d)
166 assert_almost_equal(res, tgt)
167
168 #test shape
169 z = np.ones((2, 3))
170 res = poly.polyval3d(z, z, z, self.c3d)
171 assert_(res.shape == (2, 3))
172
173 def test_polygrid2d(self):
174 x1, x2, x3 = self.x
175 y1, y2, y3 = self.y
176
177 #test values
178 tgt = np.einsum('i,j->ij', y1, y2)
179 res = poly.polygrid2d(x1, x2, self.c2d)
180 assert_almost_equal(res, tgt)
181
182 #test shape
183 z = np.ones((2, 3))
184 res = poly.polygrid2d(z, z, self.c2d)
185 assert_(res.shape == (2, 3)*2)
186
187 def test_polygrid3d(self):
188 x1, x2, x3 = self.x
189 y1, y2, y3 = self.y
190
191 #test values
192 tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
193 res = poly.polygrid3d(x1, x2, x3, self.c3d)
194 assert_almost_equal(res, tgt)
195
196 #test shape
197 z = np.ones((2, 3))
198 res = poly.polygrid3d(z, z, z, self.c3d)
199 assert_(res.shape == (2, 3)*3)
200
201
202 class TestIntegral(TestCase):
203
204 def test_polyint(self):
205 # check exceptions
206 assert_raises(ValueError, poly.polyint, [0], .5)
207 assert_raises(ValueError, poly.polyint, [0], -1)
208 assert_raises(ValueError, poly.polyint, [0], 1, [0, 0])
209
210 # test integration of zero polynomial
211 for i in range(2, 5):
212 k = [0]*(i - 2) + [1]
213 res = poly.polyint([0], m=i, k=k)
214 assert_almost_equal(res, [0, 1])
215
216 # check single integration with integration constant
217 for i in range(5):
218 scl = i + 1
219 pol = [0]*i + [1]
220 tgt = [i] + [0]*i + [1/scl]
221 res = poly.polyint(pol, m=1, k=[i])
222 assert_almost_equal(trim(res), trim(tgt))
223
224 # check single integration with integration constant and lbnd
225 for i in range(5):
226 scl = i + 1
227 pol = [0]*i + [1]
228 res = poly.polyint(pol, m=1, k=[i], lbnd=-1)
229 assert_almost_equal(poly.polyval(-1, res), i)
230
231 # check single integration with integration constant and scaling
232 for i in range(5):
233 scl = i + 1
234 pol = [0]*i + [1]
235 tgt = [i] + [0]*i + [2/scl]
236 res = poly.polyint(pol, m=1, k=[i], scl=2)
237 assert_almost_equal(trim(res), trim(tgt))
238
239 # check multiple integrations with default k
240 for i in range(5):
241 for j in range(2, 5):
242 pol = [0]*i + [1]
243 tgt = pol[:]
244 for k in range(j):
245 tgt = poly.polyint(tgt, m=1)
246 res = poly.polyint(pol, m=j)
247 assert_almost_equal(trim(res), trim(tgt))
248
249 # check multiple integrations with defined k
250 for i in range(5):
251 for j in range(2, 5):
252 pol = [0]*i + [1]
253 tgt = pol[:]
254 for k in range(j):
255 tgt = poly.polyint(tgt, m=1, k=[k])
256 res = poly.polyint(pol, m=j, k=list(range(j)))
257 assert_almost_equal(trim(res), trim(tgt))
258
259 # check multiple integrations with lbnd
260 for i in range(5):
261 for j in range(2, 5):
262 pol = [0]*i + [1]
263 tgt = pol[:]
264 for k in range(j):
265 tgt = poly.polyint(tgt, m=1, k=[k], lbnd=-1)
266 res = poly.polyint(pol, m=j, k=list(range(j)), lbnd=-1)
267 assert_almost_equal(trim(res), trim(tgt))
268
269 # check multiple integrations with scaling
270 for i in range(5):
271 for j in range(2, 5):
272 pol = [0]*i + [1]
273 tgt = pol[:]
274 for k in range(j):
275 tgt = poly.polyint(tgt, m=1, k=[k], scl=2)
276 res = poly.polyint(pol, m=j, k=list(range(j)), scl=2)
277 assert_almost_equal(trim(res), trim(tgt))
278
279 def test_polyint_axis(self):
280 # check that axis keyword works
281 c2d = np.random.random((3, 4))
282
283 tgt = np.vstack([poly.polyint(c) for c in c2d.T]).T
284 res = poly.polyint(c2d, axis=0)
285 assert_almost_equal(res, tgt)
286
287 tgt = np.vstack([poly.polyint(c) for c in c2d])
288 res = poly.polyint(c2d, axis=1)
289 assert_almost_equal(res, tgt)
290
291 tgt = np.vstack([poly.polyint(c, k=3) for c in c2d])
292 res = poly.polyint(c2d, k=3, axis=1)
293 assert_almost_equal(res, tgt)
294
295
296 class TestDerivative(TestCase):
297
298 def test_polyder(self):
299 # check exceptions
300 assert_raises(ValueError, poly.polyder, [0], .5)
301 assert_raises(ValueError, poly.polyder, [0], -1)
302
303 # check that zeroth deriviative does nothing
304 for i in range(5):
305 tgt = [0]*i + [1]
306 res = poly.polyder(tgt, m=0)
307 assert_equal(trim(res), trim(tgt))
308
309 # check that derivation is the inverse of integration
310 for i in range(5):
311 for j in range(2, 5):
312 tgt = [0]*i + [1]
313 res = poly.polyder(poly.polyint(tgt, m=j), m=j)
314 assert_almost_equal(trim(res), trim(tgt))
315
316 # check derivation with scaling
317 for i in range(5):
318 for j in range(2, 5):
319 tgt = [0]*i + [1]
320 res = poly.polyder(poly.polyint(tgt, m=j, scl=2), m=j, scl=.5)
321 assert_almost_equal(trim(res), trim(tgt))
322
323 def test_polyder_axis(self):
324 # check that axis keyword works
325 c2d = np.random.random((3, 4))
326
327 tgt = np.vstack([poly.polyder(c) for c in c2d.T]).T
328 res = poly.polyder(c2d, axis=0)
329 assert_almost_equal(res, tgt)
330
331 tgt = np.vstack([poly.polyder(c) for c in c2d])
332 res = poly.polyder(c2d, axis=1)
333 assert_almost_equal(res, tgt)
334
335
336 class TestVander(TestCase):
337 # some random values in [-1, 1)
338 x = np.random.random((3, 5))*2 - 1
339
340 def test_polyvander(self):
341 # check for 1d x
342 x = np.arange(3)
343 v = poly.polyvander(x, 3)
344 assert_(v.shape == (3, 4))
345 for i in range(4):
346 coef = [0]*i + [1]
347 assert_almost_equal(v[..., i], poly.polyval(x, coef))
348
349 # check for 2d x
350 x = np.array([[1, 2], [3, 4], [5, 6]])
351 v = poly.polyvander(x, 3)
352 assert_(v.shape == (3, 2, 4))
353 for i in range(4):
354 coef = [0]*i + [1]
355 assert_almost_equal(v[..., i], poly.polyval(x, coef))
356
357 def test_polyvander2d(self):
358 # also tests polyval2d for non-square coefficient array
359 x1, x2, x3 = self.x
360 c = np.random.random((2, 3))
361 van = poly.polyvander2d(x1, x2, [1, 2])
362 tgt = poly.polyval2d(x1, x2, c)
363 res = np.dot(van, c.flat)
364 assert_almost_equal(res, tgt)
365
366 # check shape
367 van = poly.polyvander2d([x1], [x2], [1, 2])
368 assert_(van.shape == (1, 5, 6))
369
370 def test_polyvander3d(self):
371 # also tests polyval3d for non-square coefficient array
372 x1, x2, x3 = self.x
373 c = np.random.random((2, 3, 4))
374 van = poly.polyvander3d(x1, x2, x3, [1, 2, 3])
375 tgt = poly.polyval3d(x1, x2, x3, c)
376 res = np.dot(van, c.flat)
377 assert_almost_equal(res, tgt)
378
379 # check shape
380 van = poly.polyvander3d([x1], [x2], [x3], [1, 2, 3])
381 assert_(van.shape == (1, 5, 24))
382
383
384 class TestCompanion(TestCase):
385
386 def test_raises(self):
387 assert_raises(ValueError, poly.polycompanion, [])
388 assert_raises(ValueError, poly.polycompanion, [1])
389
390 def test_dimensions(self):
391 for i in range(1, 5):
392 coef = [0]*i + [1]
393 assert_(poly.polycompanion(coef).shape == (i, i))
394
395 def test_linear_root(self):
396 assert_(poly.polycompanion([1, 2])[0, 0] == -.5)
397
398
399 class TestMisc(TestCase):
400
401 def test_polyfromroots(self):
402 res = poly.polyfromroots([])
403 assert_almost_equal(trim(res), [1])
404 for i in range(1, 5):
405 roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
406 tgt = Tlist[i]
407 res = poly.polyfromroots(roots)*2**(i-1)
408 assert_almost_equal(trim(res), trim(tgt))
409
410 def test_polyroots(self):
411 assert_almost_equal(poly.polyroots([1]), [])
412 assert_almost_equal(poly.polyroots([1, 2]), [-.5])
413 for i in range(2, 5):
414 tgt = np.linspace(-1, 1, i)
415 res = poly.polyroots(poly.polyfromroots(tgt))
416 assert_almost_equal(trim(res), trim(tgt))
417
418 def test_polyfit(self):
419 def f(x):
420 return x*(x - 1)*(x - 2)
421
422 # Test exceptions
423 assert_raises(ValueError, poly.polyfit, [1], [1], -1)
424 assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
425 assert_raises(TypeError, poly.polyfit, [], [1], 0)
426 assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
427 assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
428 assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
429 assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
430 assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
431
432 # Test fit
433 x = np.linspace(0, 2)
434 y = f(x)
435 #
436 coef3 = poly.polyfit(x, y, 3)
437 assert_equal(len(coef3), 4)
438 assert_almost_equal(poly.polyval(x, coef3), y)
439 #
440 coef4 = poly.polyfit(x, y, 4)
441 assert_equal(len(coef4), 5)
442 assert_almost_equal(poly.polyval(x, coef4), y)
443 #
444 coef2d = poly.polyfit(x, np.array([y, y]).T, 3)
445 assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
446 # test weighting
447 w = np.zeros_like(x)
448 yw = y.copy()
449 w[1::2] = 1
450 yw[0::2] = 0
451 wcoef3 = poly.polyfit(x, yw, 3, w=w)
452 assert_almost_equal(wcoef3, coef3)
453 #
454 wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w)
455 assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
456 # test scaling with complex values x points whose square
457 # is zero when summed.
458 x = [1, 1j, -1, -1j]
459 assert_almost_equal(poly.polyfit(x, x, 1), [0, 1])
460
461 def test_polytrim(self):
462 coef = [2, -1, 1, 0]
463
464 # Test exceptions
465 assert_raises(ValueError, poly.polytrim, coef, -1)
466
467 # Test results
468 assert_equal(poly.polytrim(coef), coef[:-1])
469 assert_equal(poly.polytrim(coef, 1), coef[:-3])
470 assert_equal(poly.polytrim(coef, 2), [0])
471
472 def test_polyline(self):
473 assert_equal(poly.polyline(3, 4), [3, 4])
474
475
476 if __name__ == "__main__":
477 run_module_suite()