Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/polynomial/tests/test_legendre.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 legendre module. | |
2 | |
3 """ | |
4 from __future__ import division, absolute_import, print_function | |
5 | |
6 import numpy as np | |
7 import numpy.polynomial.legendre as leg | |
8 from numpy.polynomial.polynomial import polyval | |
9 from numpy.testing import ( | |
10 TestCase, assert_almost_equal, assert_raises, | |
11 assert_equal, assert_, run_module_suite) | |
12 | |
13 L0 = np.array([1]) | |
14 L1 = np.array([0, 1]) | |
15 L2 = np.array([-1, 0, 3])/2 | |
16 L3 = np.array([0, -3, 0, 5])/2 | |
17 L4 = np.array([3, 0, -30, 0, 35])/8 | |
18 L5 = np.array([0, 15, 0, -70, 0, 63])/8 | |
19 L6 = np.array([-5, 0, 105, 0, -315, 0, 231])/16 | |
20 L7 = np.array([0, -35, 0, 315, 0, -693, 0, 429])/16 | |
21 L8 = np.array([35, 0, -1260, 0, 6930, 0, -12012, 0, 6435])/128 | |
22 L9 = np.array([0, 315, 0, -4620, 0, 18018, 0, -25740, 0, 12155])/128 | |
23 | |
24 Llist = [L0, L1, L2, L3, L4, L5, L6, L7, L8, L9] | |
25 | |
26 | |
27 def trim(x): | |
28 return leg.legtrim(x, tol=1e-6) | |
29 | |
30 | |
31 class TestConstants(TestCase): | |
32 | |
33 def test_legdomain(self): | |
34 assert_equal(leg.legdomain, [-1, 1]) | |
35 | |
36 def test_legzero(self): | |
37 assert_equal(leg.legzero, [0]) | |
38 | |
39 def test_legone(self): | |
40 assert_equal(leg.legone, [1]) | |
41 | |
42 def test_legx(self): | |
43 assert_equal(leg.legx, [0, 1]) | |
44 | |
45 | |
46 class TestArithmetic(TestCase): | |
47 x = np.linspace(-1, 1, 100) | |
48 | |
49 def test_legadd(self): | |
50 for i in range(5): | |
51 for j in range(5): | |
52 msg = "At i=%d, j=%d" % (i, j) | |
53 tgt = np.zeros(max(i, j) + 1) | |
54 tgt[i] += 1 | |
55 tgt[j] += 1 | |
56 res = leg.legadd([0]*i + [1], [0]*j + [1]) | |
57 assert_equal(trim(res), trim(tgt), err_msg=msg) | |
58 | |
59 def test_legsub(self): | |
60 for i in range(5): | |
61 for j in range(5): | |
62 msg = "At i=%d, j=%d" % (i, j) | |
63 tgt = np.zeros(max(i, j) + 1) | |
64 tgt[i] += 1 | |
65 tgt[j] -= 1 | |
66 res = leg.legsub([0]*i + [1], [0]*j + [1]) | |
67 assert_equal(trim(res), trim(tgt), err_msg=msg) | |
68 | |
69 def test_legmulx(self): | |
70 assert_equal(leg.legmulx([0]), [0]) | |
71 assert_equal(leg.legmulx([1]), [0, 1]) | |
72 for i in range(1, 5): | |
73 tmp = 2*i + 1 | |
74 ser = [0]*i + [1] | |
75 tgt = [0]*(i - 1) + [i/tmp, 0, (i + 1)/tmp] | |
76 assert_equal(leg.legmulx(ser), tgt) | |
77 | |
78 def test_legmul(self): | |
79 # check values of result | |
80 for i in range(5): | |
81 pol1 = [0]*i + [1] | |
82 val1 = leg.legval(self.x, pol1) | |
83 for j in range(5): | |
84 msg = "At i=%d, j=%d" % (i, j) | |
85 pol2 = [0]*j + [1] | |
86 val2 = leg.legval(self.x, pol2) | |
87 pol3 = leg.legmul(pol1, pol2) | |
88 val3 = leg.legval(self.x, pol3) | |
89 assert_(len(pol3) == i + j + 1, msg) | |
90 assert_almost_equal(val3, val1*val2, err_msg=msg) | |
91 | |
92 def test_legdiv(self): | |
93 for i in range(5): | |
94 for j in range(5): | |
95 msg = "At i=%d, j=%d" % (i, j) | |
96 ci = [0]*i + [1] | |
97 cj = [0]*j + [1] | |
98 tgt = leg.legadd(ci, cj) | |
99 quo, rem = leg.legdiv(tgt, ci) | |
100 res = leg.legadd(leg.legmul(quo, ci), rem) | |
101 assert_equal(trim(res), trim(tgt), err_msg=msg) | |
102 | |
103 | |
104 class TestEvaluation(TestCase): | |
105 # coefficients of 1 + 2*x + 3*x**2 | |
106 c1d = np.array([2., 2., 2.]) | |
107 c2d = np.einsum('i,j->ij', c1d, c1d) | |
108 c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d) | |
109 | |
110 # some random values in [-1, 1) | |
111 x = np.random.random((3, 5))*2 - 1 | |
112 y = polyval(x, [1., 2., 3.]) | |
113 | |
114 def test_legval(self): | |
115 #check empty input | |
116 assert_equal(leg.legval([], [1]).size, 0) | |
117 | |
118 #check normal input) | |
119 x = np.linspace(-1, 1) | |
120 y = [polyval(x, c) for c in Llist] | |
121 for i in range(10): | |
122 msg = "At i=%d" % i | |
123 tgt = y[i] | |
124 res = leg.legval(x, [0]*i + [1]) | |
125 assert_almost_equal(res, tgt, err_msg=msg) | |
126 | |
127 #check that shape is preserved | |
128 for i in range(3): | |
129 dims = [2]*i | |
130 x = np.zeros(dims) | |
131 assert_equal(leg.legval(x, [1]).shape, dims) | |
132 assert_equal(leg.legval(x, [1, 0]).shape, dims) | |
133 assert_equal(leg.legval(x, [1, 0, 0]).shape, dims) | |
134 | |
135 def test_legval2d(self): | |
136 x1, x2, x3 = self.x | |
137 y1, y2, y3 = self.y | |
138 | |
139 #test exceptions | |
140 assert_raises(ValueError, leg.legval2d, x1, x2[:2], self.c2d) | |
141 | |
142 #test values | |
143 tgt = y1*y2 | |
144 res = leg.legval2d(x1, x2, self.c2d) | |
145 assert_almost_equal(res, tgt) | |
146 | |
147 #test shape | |
148 z = np.ones((2, 3)) | |
149 res = leg.legval2d(z, z, self.c2d) | |
150 assert_(res.shape == (2, 3)) | |
151 | |
152 def test_legval3d(self): | |
153 x1, x2, x3 = self.x | |
154 y1, y2, y3 = self.y | |
155 | |
156 #test exceptions | |
157 assert_raises(ValueError, leg.legval3d, x1, x2, x3[:2], self.c3d) | |
158 | |
159 #test values | |
160 tgt = y1*y2*y3 | |
161 res = leg.legval3d(x1, x2, x3, self.c3d) | |
162 assert_almost_equal(res, tgt) | |
163 | |
164 #test shape | |
165 z = np.ones((2, 3)) | |
166 res = leg.legval3d(z, z, z, self.c3d) | |
167 assert_(res.shape == (2, 3)) | |
168 | |
169 def test_leggrid2d(self): | |
170 x1, x2, x3 = self.x | |
171 y1, y2, y3 = self.y | |
172 | |
173 #test values | |
174 tgt = np.einsum('i,j->ij', y1, y2) | |
175 res = leg.leggrid2d(x1, x2, self.c2d) | |
176 assert_almost_equal(res, tgt) | |
177 | |
178 #test shape | |
179 z = np.ones((2, 3)) | |
180 res = leg.leggrid2d(z, z, self.c2d) | |
181 assert_(res.shape == (2, 3)*2) | |
182 | |
183 def test_leggrid3d(self): | |
184 x1, x2, x3 = self.x | |
185 y1, y2, y3 = self.y | |
186 | |
187 #test values | |
188 tgt = np.einsum('i,j,k->ijk', y1, y2, y3) | |
189 res = leg.leggrid3d(x1, x2, x3, self.c3d) | |
190 assert_almost_equal(res, tgt) | |
191 | |
192 #test shape | |
193 z = np.ones((2, 3)) | |
194 res = leg.leggrid3d(z, z, z, self.c3d) | |
195 assert_(res.shape == (2, 3)*3) | |
196 | |
197 | |
198 class TestIntegral(TestCase): | |
199 | |
200 def test_legint(self): | |
201 # check exceptions | |
202 assert_raises(ValueError, leg.legint, [0], .5) | |
203 assert_raises(ValueError, leg.legint, [0], -1) | |
204 assert_raises(ValueError, leg.legint, [0], 1, [0, 0]) | |
205 | |
206 # test integration of zero polynomial | |
207 for i in range(2, 5): | |
208 k = [0]*(i - 2) + [1] | |
209 res = leg.legint([0], m=i, k=k) | |
210 assert_almost_equal(res, [0, 1]) | |
211 | |
212 # check single integration with integration constant | |
213 for i in range(5): | |
214 scl = i + 1 | |
215 pol = [0]*i + [1] | |
216 tgt = [i] + [0]*i + [1/scl] | |
217 legpol = leg.poly2leg(pol) | |
218 legint = leg.legint(legpol, m=1, k=[i]) | |
219 res = leg.leg2poly(legint) | |
220 assert_almost_equal(trim(res), trim(tgt)) | |
221 | |
222 # check single integration with integration constant and lbnd | |
223 for i in range(5): | |
224 scl = i + 1 | |
225 pol = [0]*i + [1] | |
226 legpol = leg.poly2leg(pol) | |
227 legint = leg.legint(legpol, m=1, k=[i], lbnd=-1) | |
228 assert_almost_equal(leg.legval(-1, legint), i) | |
229 | |
230 # check single integration with integration constant and scaling | |
231 for i in range(5): | |
232 scl = i + 1 | |
233 pol = [0]*i + [1] | |
234 tgt = [i] + [0]*i + [2/scl] | |
235 legpol = leg.poly2leg(pol) | |
236 legint = leg.legint(legpol, m=1, k=[i], scl=2) | |
237 res = leg.leg2poly(legint) | |
238 assert_almost_equal(trim(res), trim(tgt)) | |
239 | |
240 # check multiple integrations with default k | |
241 for i in range(5): | |
242 for j in range(2, 5): | |
243 pol = [0]*i + [1] | |
244 tgt = pol[:] | |
245 for k in range(j): | |
246 tgt = leg.legint(tgt, m=1) | |
247 res = leg.legint(pol, m=j) | |
248 assert_almost_equal(trim(res), trim(tgt)) | |
249 | |
250 # check multiple integrations with defined k | |
251 for i in range(5): | |
252 for j in range(2, 5): | |
253 pol = [0]*i + [1] | |
254 tgt = pol[:] | |
255 for k in range(j): | |
256 tgt = leg.legint(tgt, m=1, k=[k]) | |
257 res = leg.legint(pol, m=j, k=list(range(j))) | |
258 assert_almost_equal(trim(res), trim(tgt)) | |
259 | |
260 # check multiple integrations with lbnd | |
261 for i in range(5): | |
262 for j in range(2, 5): | |
263 pol = [0]*i + [1] | |
264 tgt = pol[:] | |
265 for k in range(j): | |
266 tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1) | |
267 res = leg.legint(pol, m=j, k=list(range(j)), lbnd=-1) | |
268 assert_almost_equal(trim(res), trim(tgt)) | |
269 | |
270 # check multiple integrations with scaling | |
271 for i in range(5): | |
272 for j in range(2, 5): | |
273 pol = [0]*i + [1] | |
274 tgt = pol[:] | |
275 for k in range(j): | |
276 tgt = leg.legint(tgt, m=1, k=[k], scl=2) | |
277 res = leg.legint(pol, m=j, k=list(range(j)), scl=2) | |
278 assert_almost_equal(trim(res), trim(tgt)) | |
279 | |
280 def test_legint_axis(self): | |
281 # check that axis keyword works | |
282 c2d = np.random.random((3, 4)) | |
283 | |
284 tgt = np.vstack([leg.legint(c) for c in c2d.T]).T | |
285 res = leg.legint(c2d, axis=0) | |
286 assert_almost_equal(res, tgt) | |
287 | |
288 tgt = np.vstack([leg.legint(c) for c in c2d]) | |
289 res = leg.legint(c2d, axis=1) | |
290 assert_almost_equal(res, tgt) | |
291 | |
292 tgt = np.vstack([leg.legint(c, k=3) for c in c2d]) | |
293 res = leg.legint(c2d, k=3, axis=1) | |
294 assert_almost_equal(res, tgt) | |
295 | |
296 | |
297 class TestDerivative(TestCase): | |
298 | |
299 def test_legder(self): | |
300 # check exceptions | |
301 assert_raises(ValueError, leg.legder, [0], .5) | |
302 assert_raises(ValueError, leg.legder, [0], -1) | |
303 | |
304 # check that zeroth deriviative does nothing | |
305 for i in range(5): | |
306 tgt = [0]*i + [1] | |
307 res = leg.legder(tgt, m=0) | |
308 assert_equal(trim(res), trim(tgt)) | |
309 | |
310 # check that derivation is the inverse of integration | |
311 for i in range(5): | |
312 for j in range(2, 5): | |
313 tgt = [0]*i + [1] | |
314 res = leg.legder(leg.legint(tgt, m=j), m=j) | |
315 assert_almost_equal(trim(res), trim(tgt)) | |
316 | |
317 # check derivation with scaling | |
318 for i in range(5): | |
319 for j in range(2, 5): | |
320 tgt = [0]*i + [1] | |
321 res = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5) | |
322 assert_almost_equal(trim(res), trim(tgt)) | |
323 | |
324 def test_legder_axis(self): | |
325 # check that axis keyword works | |
326 c2d = np.random.random((3, 4)) | |
327 | |
328 tgt = np.vstack([leg.legder(c) for c in c2d.T]).T | |
329 res = leg.legder(c2d, axis=0) | |
330 assert_almost_equal(res, tgt) | |
331 | |
332 tgt = np.vstack([leg.legder(c) for c in c2d]) | |
333 res = leg.legder(c2d, axis=1) | |
334 assert_almost_equal(res, tgt) | |
335 | |
336 | |
337 class TestVander(TestCase): | |
338 # some random values in [-1, 1) | |
339 x = np.random.random((3, 5))*2 - 1 | |
340 | |
341 def test_legvander(self): | |
342 # check for 1d x | |
343 x = np.arange(3) | |
344 v = leg.legvander(x, 3) | |
345 assert_(v.shape == (3, 4)) | |
346 for i in range(4): | |
347 coef = [0]*i + [1] | |
348 assert_almost_equal(v[..., i], leg.legval(x, coef)) | |
349 | |
350 # check for 2d x | |
351 x = np.array([[1, 2], [3, 4], [5, 6]]) | |
352 v = leg.legvander(x, 3) | |
353 assert_(v.shape == (3, 2, 4)) | |
354 for i in range(4): | |
355 coef = [0]*i + [1] | |
356 assert_almost_equal(v[..., i], leg.legval(x, coef)) | |
357 | |
358 def test_legvander2d(self): | |
359 # also tests polyval2d for non-square coefficient array | |
360 x1, x2, x3 = self.x | |
361 c = np.random.random((2, 3)) | |
362 van = leg.legvander2d(x1, x2, [1, 2]) | |
363 tgt = leg.legval2d(x1, x2, c) | |
364 res = np.dot(van, c.flat) | |
365 assert_almost_equal(res, tgt) | |
366 | |
367 # check shape | |
368 van = leg.legvander2d([x1], [x2], [1, 2]) | |
369 assert_(van.shape == (1, 5, 6)) | |
370 | |
371 def test_legvander3d(self): | |
372 # also tests polyval3d for non-square coefficient array | |
373 x1, x2, x3 = self.x | |
374 c = np.random.random((2, 3, 4)) | |
375 van = leg.legvander3d(x1, x2, x3, [1, 2, 3]) | |
376 tgt = leg.legval3d(x1, x2, x3, c) | |
377 res = np.dot(van, c.flat) | |
378 assert_almost_equal(res, tgt) | |
379 | |
380 # check shape | |
381 van = leg.legvander3d([x1], [x2], [x3], [1, 2, 3]) | |
382 assert_(van.shape == (1, 5, 24)) | |
383 | |
384 | |
385 class TestFitting(TestCase): | |
386 | |
387 def test_legfit(self): | |
388 def f(x): | |
389 return x*(x - 1)*(x - 2) | |
390 | |
391 # Test exceptions | |
392 assert_raises(ValueError, leg.legfit, [1], [1], -1) | |
393 assert_raises(TypeError, leg.legfit, [[1]], [1], 0) | |
394 assert_raises(TypeError, leg.legfit, [], [1], 0) | |
395 assert_raises(TypeError, leg.legfit, [1], [[[1]]], 0) | |
396 assert_raises(TypeError, leg.legfit, [1, 2], [1], 0) | |
397 assert_raises(TypeError, leg.legfit, [1], [1, 2], 0) | |
398 assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[[1]]) | |
399 assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[1, 1]) | |
400 | |
401 # Test fit | |
402 x = np.linspace(0, 2) | |
403 y = f(x) | |
404 # | |
405 coef3 = leg.legfit(x, y, 3) | |
406 assert_equal(len(coef3), 4) | |
407 assert_almost_equal(leg.legval(x, coef3), y) | |
408 # | |
409 coef4 = leg.legfit(x, y, 4) | |
410 assert_equal(len(coef4), 5) | |
411 assert_almost_equal(leg.legval(x, coef4), y) | |
412 # | |
413 coef2d = leg.legfit(x, np.array([y, y]).T, 3) | |
414 assert_almost_equal(coef2d, np.array([coef3, coef3]).T) | |
415 # test weighting | |
416 w = np.zeros_like(x) | |
417 yw = y.copy() | |
418 w[1::2] = 1 | |
419 y[0::2] = 0 | |
420 wcoef3 = leg.legfit(x, yw, 3, w=w) | |
421 assert_almost_equal(wcoef3, coef3) | |
422 # | |
423 wcoef2d = leg.legfit(x, np.array([yw, yw]).T, 3, w=w) | |
424 assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T) | |
425 # test scaling with complex values x points whose square | |
426 # is zero when summed. | |
427 x = [1, 1j, -1, -1j] | |
428 assert_almost_equal(leg.legfit(x, x, 1), [0, 1]) | |
429 | |
430 | |
431 class TestCompanion(TestCase): | |
432 | |
433 def test_raises(self): | |
434 assert_raises(ValueError, leg.legcompanion, []) | |
435 assert_raises(ValueError, leg.legcompanion, [1]) | |
436 | |
437 def test_dimensions(self): | |
438 for i in range(1, 5): | |
439 coef = [0]*i + [1] | |
440 assert_(leg.legcompanion(coef).shape == (i, i)) | |
441 | |
442 def test_linear_root(self): | |
443 assert_(leg.legcompanion([1, 2])[0, 0] == -.5) | |
444 | |
445 | |
446 class TestGauss(TestCase): | |
447 | |
448 def test_100(self): | |
449 x, w = leg.leggauss(100) | |
450 | |
451 # test orthogonality. Note that the results need to be normalized, | |
452 # otherwise the huge values that can arise from fast growing | |
453 # functions like Laguerre can be very confusing. | |
454 v = leg.legvander(x, 99) | |
455 vv = np.dot(v.T * w, v) | |
456 vd = 1/np.sqrt(vv.diagonal()) | |
457 vv = vd[:, None] * vv * vd | |
458 assert_almost_equal(vv, np.eye(100)) | |
459 | |
460 # check that the integral of 1 is correct | |
461 tgt = 2.0 | |
462 assert_almost_equal(w.sum(), tgt) | |
463 | |
464 | |
465 class TestMisc(TestCase): | |
466 | |
467 def test_legfromroots(self): | |
468 res = leg.legfromroots([]) | |
469 assert_almost_equal(trim(res), [1]) | |
470 for i in range(1, 5): | |
471 roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2]) | |
472 pol = leg.legfromroots(roots) | |
473 res = leg.legval(roots, pol) | |
474 tgt = 0 | |
475 assert_(len(pol) == i + 1) | |
476 assert_almost_equal(leg.leg2poly(pol)[-1], 1) | |
477 assert_almost_equal(res, tgt) | |
478 | |
479 def test_legroots(self): | |
480 assert_almost_equal(leg.legroots([1]), []) | |
481 assert_almost_equal(leg.legroots([1, 2]), [-.5]) | |
482 for i in range(2, 5): | |
483 tgt = np.linspace(-1, 1, i) | |
484 res = leg.legroots(leg.legfromroots(tgt)) | |
485 assert_almost_equal(trim(res), trim(tgt)) | |
486 | |
487 def test_legtrim(self): | |
488 coef = [2, -1, 1, 0] | |
489 | |
490 # Test exceptions | |
491 assert_raises(ValueError, leg.legtrim, coef, -1) | |
492 | |
493 # Test results | |
494 assert_equal(leg.legtrim(coef), coef[:-1]) | |
495 assert_equal(leg.legtrim(coef, 1), coef[:-3]) | |
496 assert_equal(leg.legtrim(coef, 2), [0]) | |
497 | |
498 def test_legline(self): | |
499 assert_equal(leg.legline(3, 4), [3, 4]) | |
500 | |
501 def test_leg2poly(self): | |
502 for i in range(10): | |
503 assert_almost_equal(leg.leg2poly([0]*i + [1]), Llist[i]) | |
504 | |
505 def test_poly2leg(self): | |
506 for i in range(10): | |
507 assert_almost_equal(leg.poly2leg(Llist[i]), [0]*i + [1]) | |
508 | |
509 def test_weight(self): | |
510 x = np.linspace(-1, 1, 11) | |
511 tgt = 1. | |
512 res = leg.legweight(x) | |
513 assert_almost_equal(res, tgt) | |
514 | |
515 | |
516 if __name__ == "__main__": | |
517 run_module_suite() |