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