comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/polynomial/tests/test_hermite.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 hermite module.
2
3 """
4 from __future__ import division, absolute_import, print_function
5
6 import numpy as np
7 import numpy.polynomial.hermite as herm
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 H0 = np.array([1])
14 H1 = np.array([0, 2])
15 H2 = np.array([-2, 0, 4])
16 H3 = np.array([0, -12, 0, 8])
17 H4 = np.array([12, 0, -48, 0, 16])
18 H5 = np.array([0, 120, 0, -160, 0, 32])
19 H6 = np.array([-120, 0, 720, 0, -480, 0, 64])
20 H7 = np.array([0, -1680, 0, 3360, 0, -1344, 0, 128])
21 H8 = np.array([1680, 0, -13440, 0, 13440, 0, -3584, 0, 256])
22 H9 = np.array([0, 30240, 0, -80640, 0, 48384, 0, -9216, 0, 512])
23
24 Hlist = [H0, H1, H2, H3, H4, H5, H6, H7, H8, H9]
25
26
27 def trim(x):
28 return herm.hermtrim(x, tol=1e-6)
29
30
31 class TestConstants(TestCase):
32
33 def test_hermdomain(self):
34 assert_equal(herm.hermdomain, [-1, 1])
35
36 def test_hermzero(self):
37 assert_equal(herm.hermzero, [0])
38
39 def test_hermone(self):
40 assert_equal(herm.hermone, [1])
41
42 def test_hermx(self):
43 assert_equal(herm.hermx, [0, .5])
44
45
46 class TestArithmetic(TestCase):
47 x = np.linspace(-3, 3, 100)
48
49 def test_hermadd(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 = herm.hermadd([0]*i + [1], [0]*j + [1])
57 assert_equal(trim(res), trim(tgt), err_msg=msg)
58
59 def test_hermsub(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 = herm.hermsub([0]*i + [1], [0]*j + [1])
67 assert_equal(trim(res), trim(tgt), err_msg=msg)
68
69 def test_hermmulx(self):
70 assert_equal(herm.hermmulx([0]), [0])
71 assert_equal(herm.hermmulx([1]), [0, .5])
72 for i in range(1, 5):
73 ser = [0]*i + [1]
74 tgt = [0]*(i - 1) + [i, 0, .5]
75 assert_equal(herm.hermmulx(ser), tgt)
76
77 def test_hermmul(self):
78 # check values of result
79 for i in range(5):
80 pol1 = [0]*i + [1]
81 val1 = herm.hermval(self.x, pol1)
82 for j in range(5):
83 msg = "At i=%d, j=%d" % (i, j)
84 pol2 = [0]*j + [1]
85 val2 = herm.hermval(self.x, pol2)
86 pol3 = herm.hermmul(pol1, pol2)
87 val3 = herm.hermval(self.x, pol3)
88 assert_(len(pol3) == i + j + 1, msg)
89 assert_almost_equal(val3, val1*val2, err_msg=msg)
90
91 def test_hermdiv(self):
92 for i in range(5):
93 for j in range(5):
94 msg = "At i=%d, j=%d" % (i, j)
95 ci = [0]*i + [1]
96 cj = [0]*j + [1]
97 tgt = herm.hermadd(ci, cj)
98 quo, rem = herm.hermdiv(tgt, ci)
99 res = herm.hermadd(herm.hermmul(quo, ci), rem)
100 assert_equal(trim(res), trim(tgt), err_msg=msg)
101
102
103 class TestEvaluation(TestCase):
104 # coefficients of 1 + 2*x + 3*x**2
105 c1d = np.array([2.5, 1., .75])
106 c2d = np.einsum('i,j->ij', c1d, c1d)
107 c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
108
109 # some random values in [-1, 1)
110 x = np.random.random((3, 5))*2 - 1
111 y = polyval(x, [1., 2., 3.])
112
113 def test_hermval(self):
114 #check empty input
115 assert_equal(herm.hermval([], [1]).size, 0)
116
117 #check normal input)
118 x = np.linspace(-1, 1)
119 y = [polyval(x, c) for c in Hlist]
120 for i in range(10):
121 msg = "At i=%d" % i
122 tgt = y[i]
123 res = herm.hermval(x, [0]*i + [1])
124 assert_almost_equal(res, tgt, err_msg=msg)
125
126 #check that shape is preserved
127 for i in range(3):
128 dims = [2]*i
129 x = np.zeros(dims)
130 assert_equal(herm.hermval(x, [1]).shape, dims)
131 assert_equal(herm.hermval(x, [1, 0]).shape, dims)
132 assert_equal(herm.hermval(x, [1, 0, 0]).shape, dims)
133
134 def test_hermval2d(self):
135 x1, x2, x3 = self.x
136 y1, y2, y3 = self.y
137
138 #test exceptions
139 assert_raises(ValueError, herm.hermval2d, x1, x2[:2], self.c2d)
140
141 #test values
142 tgt = y1*y2
143 res = herm.hermval2d(x1, x2, self.c2d)
144 assert_almost_equal(res, tgt)
145
146 #test shape
147 z = np.ones((2, 3))
148 res = herm.hermval2d(z, z, self.c2d)
149 assert_(res.shape == (2, 3))
150
151 def test_hermval3d(self):
152 x1, x2, x3 = self.x
153 y1, y2, y3 = self.y
154
155 #test exceptions
156 assert_raises(ValueError, herm.hermval3d, x1, x2, x3[:2], self.c3d)
157
158 #test values
159 tgt = y1*y2*y3
160 res = herm.hermval3d(x1, x2, x3, self.c3d)
161 assert_almost_equal(res, tgt)
162
163 #test shape
164 z = np.ones((2, 3))
165 res = herm.hermval3d(z, z, z, self.c3d)
166 assert_(res.shape == (2, 3))
167
168 def test_hermgrid2d(self):
169 x1, x2, x3 = self.x
170 y1, y2, y3 = self.y
171
172 #test values
173 tgt = np.einsum('i,j->ij', y1, y2)
174 res = herm.hermgrid2d(x1, x2, self.c2d)
175 assert_almost_equal(res, tgt)
176
177 #test shape
178 z = np.ones((2, 3))
179 res = herm.hermgrid2d(z, z, self.c2d)
180 assert_(res.shape == (2, 3)*2)
181
182 def test_hermgrid3d(self):
183 x1, x2, x3 = self.x
184 y1, y2, y3 = self.y
185
186 #test values
187 tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
188 res = herm.hermgrid3d(x1, x2, x3, self.c3d)
189 assert_almost_equal(res, tgt)
190
191 #test shape
192 z = np.ones((2, 3))
193 res = herm.hermgrid3d(z, z, z, self.c3d)
194 assert_(res.shape == (2, 3)*3)
195
196
197 class TestIntegral(TestCase):
198
199 def test_hermint(self):
200 # check exceptions
201 assert_raises(ValueError, herm.hermint, [0], .5)
202 assert_raises(ValueError, herm.hermint, [0], -1)
203 assert_raises(ValueError, herm.hermint, [0], 1, [0, 0])
204
205 # test integration of zero polynomial
206 for i in range(2, 5):
207 k = [0]*(i - 2) + [1]
208 res = herm.hermint([0], m=i, k=k)
209 assert_almost_equal(res, [0, .5])
210
211 # check single integration with integration constant
212 for i in range(5):
213 scl = i + 1
214 pol = [0]*i + [1]
215 tgt = [i] + [0]*i + [1/scl]
216 hermpol = herm.poly2herm(pol)
217 hermint = herm.hermint(hermpol, m=1, k=[i])
218 res = herm.herm2poly(hermint)
219 assert_almost_equal(trim(res), trim(tgt))
220
221 # check single integration with integration constant and lbnd
222 for i in range(5):
223 scl = i + 1
224 pol = [0]*i + [1]
225 hermpol = herm.poly2herm(pol)
226 hermint = herm.hermint(hermpol, m=1, k=[i], lbnd=-1)
227 assert_almost_equal(herm.hermval(-1, hermint), i)
228
229 # check single integration with integration constant and scaling
230 for i in range(5):
231 scl = i + 1
232 pol = [0]*i + [1]
233 tgt = [i] + [0]*i + [2/scl]
234 hermpol = herm.poly2herm(pol)
235 hermint = herm.hermint(hermpol, m=1, k=[i], scl=2)
236 res = herm.herm2poly(hermint)
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 = herm.hermint(tgt, m=1)
246 res = herm.hermint(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 = herm.hermint(tgt, m=1, k=[k])
256 res = herm.hermint(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 = herm.hermint(tgt, m=1, k=[k], lbnd=-1)
266 res = herm.hermint(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 = herm.hermint(tgt, m=1, k=[k], scl=2)
276 res = herm.hermint(pol, m=j, k=list(range(j)), scl=2)
277 assert_almost_equal(trim(res), trim(tgt))
278
279 def test_hermint_axis(self):
280 # check that axis keyword works
281 c2d = np.random.random((3, 4))
282
283 tgt = np.vstack([herm.hermint(c) for c in c2d.T]).T
284 res = herm.hermint(c2d, axis=0)
285 assert_almost_equal(res, tgt)
286
287 tgt = np.vstack([herm.hermint(c) for c in c2d])
288 res = herm.hermint(c2d, axis=1)
289 assert_almost_equal(res, tgt)
290
291 tgt = np.vstack([herm.hermint(c, k=3) for c in c2d])
292 res = herm.hermint(c2d, k=3, axis=1)
293 assert_almost_equal(res, tgt)
294
295
296 class TestDerivative(TestCase):
297
298 def test_hermder(self):
299 # check exceptions
300 assert_raises(ValueError, herm.hermder, [0], .5)
301 assert_raises(ValueError, herm.hermder, [0], -1)
302
303 # check that zeroth deriviative does nothing
304 for i in range(5):
305 tgt = [0]*i + [1]
306 res = herm.hermder(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 = herm.hermder(herm.hermint(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 = herm.hermder(herm.hermint(tgt, m=j, scl=2), m=j, scl=.5)
321 assert_almost_equal(trim(res), trim(tgt))
322
323 def test_hermder_axis(self):
324 # check that axis keyword works
325 c2d = np.random.random((3, 4))
326
327 tgt = np.vstack([herm.hermder(c) for c in c2d.T]).T
328 res = herm.hermder(c2d, axis=0)
329 assert_almost_equal(res, tgt)
330
331 tgt = np.vstack([herm.hermder(c) for c in c2d])
332 res = herm.hermder(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_hermvander(self):
341 # check for 1d x
342 x = np.arange(3)
343 v = herm.hermvander(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], herm.hermval(x, coef))
348
349 # check for 2d x
350 x = np.array([[1, 2], [3, 4], [5, 6]])
351 v = herm.hermvander(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], herm.hermval(x, coef))
356
357 def test_hermvander2d(self):
358 # also tests hermval2d for non-square coefficient array
359 x1, x2, x3 = self.x
360 c = np.random.random((2, 3))
361 van = herm.hermvander2d(x1, x2, [1, 2])
362 tgt = herm.hermval2d(x1, x2, c)
363 res = np.dot(van, c.flat)
364 assert_almost_equal(res, tgt)
365
366 # check shape
367 van = herm.hermvander2d([x1], [x2], [1, 2])
368 assert_(van.shape == (1, 5, 6))
369
370 def test_hermvander3d(self):
371 # also tests hermval3d for non-square coefficient array
372 x1, x2, x3 = self.x
373 c = np.random.random((2, 3, 4))
374 van = herm.hermvander3d(x1, x2, x3, [1, 2, 3])
375 tgt = herm.hermval3d(x1, x2, x3, c)
376 res = np.dot(van, c.flat)
377 assert_almost_equal(res, tgt)
378
379 # check shape
380 van = herm.hermvander3d([x1], [x2], [x3], [1, 2, 3])
381 assert_(van.shape == (1, 5, 24))
382
383
384 class TestFitting(TestCase):
385
386 def test_hermfit(self):
387 def f(x):
388 return x*(x - 1)*(x - 2)
389
390 # Test exceptions
391 assert_raises(ValueError, herm.hermfit, [1], [1], -1)
392 assert_raises(TypeError, herm.hermfit, [[1]], [1], 0)
393 assert_raises(TypeError, herm.hermfit, [], [1], 0)
394 assert_raises(TypeError, herm.hermfit, [1], [[[1]]], 0)
395 assert_raises(TypeError, herm.hermfit, [1, 2], [1], 0)
396 assert_raises(TypeError, herm.hermfit, [1], [1, 2], 0)
397 assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[[1]])
398 assert_raises(TypeError, herm.hermfit, [1], [1], 0, w=[1, 1])
399
400 # Test fit
401 x = np.linspace(0, 2)
402 y = f(x)
403 #
404 coef3 = herm.hermfit(x, y, 3)
405 assert_equal(len(coef3), 4)
406 assert_almost_equal(herm.hermval(x, coef3), y)
407 #
408 coef4 = herm.hermfit(x, y, 4)
409 assert_equal(len(coef4), 5)
410 assert_almost_equal(herm.hermval(x, coef4), y)
411 #
412 coef2d = herm.hermfit(x, np.array([y, y]).T, 3)
413 assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
414 # test weighting
415 w = np.zeros_like(x)
416 yw = y.copy()
417 w[1::2] = 1
418 y[0::2] = 0
419 wcoef3 = herm.hermfit(x, yw, 3, w=w)
420 assert_almost_equal(wcoef3, coef3)
421 #
422 wcoef2d = herm.hermfit(x, np.array([yw, yw]).T, 3, w=w)
423 assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
424 # test scaling with complex values x points whose square
425 # is zero when summed.
426 x = [1, 1j, -1, -1j]
427 assert_almost_equal(herm.hermfit(x, x, 1), [0, .5])
428
429
430 class TestCompanion(TestCase):
431
432 def test_raises(self):
433 assert_raises(ValueError, herm.hermcompanion, [])
434 assert_raises(ValueError, herm.hermcompanion, [1])
435
436 def test_dimensions(self):
437 for i in range(1, 5):
438 coef = [0]*i + [1]
439 assert_(herm.hermcompanion(coef).shape == (i, i))
440
441 def test_linear_root(self):
442 assert_(herm.hermcompanion([1, 2])[0, 0] == -.25)
443
444
445 class TestGauss(TestCase):
446
447 def test_100(self):
448 x, w = herm.hermgauss(100)
449
450 # test orthogonality. Note that the results need to be normalized,
451 # otherwise the huge values that can arise from fast growing
452 # functions like Laguerre can be very confusing.
453 v = herm.hermvander(x, 99)
454 vv = np.dot(v.T * w, v)
455 vd = 1/np.sqrt(vv.diagonal())
456 vv = vd[:, None] * vv * vd
457 assert_almost_equal(vv, np.eye(100))
458
459 # check that the integral of 1 is correct
460 tgt = np.sqrt(np.pi)
461 assert_almost_equal(w.sum(), tgt)
462
463
464 class TestMisc(TestCase):
465
466 def test_hermfromroots(self):
467 res = herm.hermfromroots([])
468 assert_almost_equal(trim(res), [1])
469 for i in range(1, 5):
470 roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
471 pol = herm.hermfromroots(roots)
472 res = herm.hermval(roots, pol)
473 tgt = 0
474 assert_(len(pol) == i + 1)
475 assert_almost_equal(herm.herm2poly(pol)[-1], 1)
476 assert_almost_equal(res, tgt)
477
478 def test_hermroots(self):
479 assert_almost_equal(herm.hermroots([1]), [])
480 assert_almost_equal(herm.hermroots([1, 1]), [-.5])
481 for i in range(2, 5):
482 tgt = np.linspace(-1, 1, i)
483 res = herm.hermroots(herm.hermfromroots(tgt))
484 assert_almost_equal(trim(res), trim(tgt))
485
486 def test_hermtrim(self):
487 coef = [2, -1, 1, 0]
488
489 # Test exceptions
490 assert_raises(ValueError, herm.hermtrim, coef, -1)
491
492 # Test results
493 assert_equal(herm.hermtrim(coef), coef[:-1])
494 assert_equal(herm.hermtrim(coef, 1), coef[:-3])
495 assert_equal(herm.hermtrim(coef, 2), [0])
496
497 def test_hermline(self):
498 assert_equal(herm.hermline(3, 4), [3, 2])
499
500 def test_herm2poly(self):
501 for i in range(10):
502 assert_almost_equal(herm.herm2poly([0]*i + [1]), Hlist[i])
503
504 def test_poly2herm(self):
505 for i in range(10):
506 assert_almost_equal(herm.poly2herm(Hlist[i]), [0]*i + [1])
507
508 def test_weight(self):
509 x = np.linspace(-5, 5, 11)
510 tgt = np.exp(-x**2)
511 res = herm.hermweight(x)
512 assert_almost_equal(res, tgt)
513
514
515 if __name__ == "__main__":
516 run_module_suite()