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