Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
test_hermite_e.py555 linesDownload Raw Back to tests
1"""Tests for hermite_e module.
2
3"""
4from functools import reduce
5
6import numpy as np
7import numpy.polynomial.hermite_e as herme
8from numpy.polynomial.polynomial import polyval
9from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
10
11He0 = np.array([1])
12He1 = np.array([0, 1])
13He2 = np.array([-1, 0, 1])
14He3 = np.array([0, -3, 0, 1])
15He4 = np.array([3, 0, -6, 0, 1])
16He5 = np.array([0, 15, 0, -10, 0, 1])
17He6 = np.array([-15, 0, 45, 0, -15, 0, 1])
18He7 = np.array([0, -105, 0, 105, 0, -21, 0, 1])
19He8 = np.array([105, 0, -420, 0, 210, 0, -28, 0, 1])
20He9 = np.array([0, 945, 0, -1260, 0, 378, 0, -36, 0, 1])
21
22Helist = [He0, He1, He2, He3, He4, He5, He6, He7, He8, He9]
23
24
25def trim(x):
26    return herme.hermetrim(x, tol=1e-6)
27
28
29class TestConstants:
30
31    def test_hermedomain(self):
32        assert_equal(herme.hermedomain, [-1, 1])
33
34    def test_hermezero(self):
35        assert_equal(herme.hermezero, [0])
36
37    def test_hermeone(self):
38        assert_equal(herme.hermeone, [1])
39
40    def test_hermex(self):
41        assert_equal(herme.hermex, [0, 1])
42
43
44class TestArithmetic:
45    x = np.linspace(-3, 3, 100)
46
47    def test_hermeadd(self):
48        for i in range(5):
49            for j in range(5):
50                msg = f"At i={i}, j={j}"
51                tgt = np.zeros(max(i, j) + 1)
52                tgt[i] += 1
53                tgt[j] += 1
54                res = herme.hermeadd([0] * i + [1], [0] * j + [1])
55                assert_equal(trim(res), trim(tgt), err_msg=msg)
56
57    def test_hermesub(self):
58        for i in range(5):
59            for j in range(5):
60                msg = f"At i={i}, j={j}"
61                tgt = np.zeros(max(i, j) + 1)
62                tgt[i] += 1
63                tgt[j] -= 1
64                res = herme.hermesub([0] * i + [1], [0] * j + [1])
65                assert_equal(trim(res), trim(tgt), err_msg=msg)
66
67    def test_hermemulx(self):
68        assert_equal(herme.hermemulx([0]), [0])
69        assert_equal(herme.hermemulx([1]), [0, 1])
70        for i in range(1, 5):
71            ser = [0] * i + [1]
72            tgt = [0] * (i - 1) + [i, 0, 1]
73            assert_equal(herme.hermemulx(ser), tgt)
74
75    def test_hermemul(self):
76        # check values of result
77        for i in range(5):
78            pol1 = [0] * i + [1]
79            val1 = herme.hermeval(self.x, pol1)
80            for j in range(5):
81                msg = f"At i={i}, j={j}"
82                pol2 = [0] * j + [1]
83                val2 = herme.hermeval(self.x, pol2)
84                pol3 = herme.hermemul(pol1, pol2)
85                val3 = herme.hermeval(self.x, pol3)
86                assert_(len(pol3) == i + j + 1, msg)
87                assert_almost_equal(val3, val1 * val2, err_msg=msg)
88
89    def test_hermediv(self):
90        for i in range(5):
91            for j in range(5):
92                msg = f"At i={i}, j={j}"
93                ci = [0] * i + [1]
94                cj = [0] * j + [1]
95                tgt = herme.hermeadd(ci, cj)
96                quo, rem = herme.hermediv(tgt, ci)
97                res = herme.hermeadd(herme.hermemul(quo, ci), rem)
98                assert_equal(trim(res), trim(tgt), err_msg=msg)
99
100    def test_hermepow(self):
101        for i in range(5):
102            for j in range(5):
103                msg = f"At i={i}, j={j}"
104                c = np.arange(i + 1)
105                tgt = reduce(herme.hermemul, [c] * j, np.array([1]))
106                res = herme.hermepow(c, j)
107                assert_equal(trim(res), trim(tgt), err_msg=msg)
108
109
110class TestEvaluation:
111    # coefficients of 1 + 2*x + 3*x**2
112    c1d = np.array([4., 2., 3.])
113    c2d = np.einsum('i,j->ij', c1d, c1d)
114    c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
115
116    # some random values in [-1, 1)
117    x = np.random.random((3, 5)) * 2 - 1
118    y = polyval(x, [1., 2., 3.])
119
120    def test_hermeval(self):
121        # check empty input
122        assert_equal(herme.hermeval([], [1]).size, 0)
123
124        # check normal input)
125        x = np.linspace(-1, 1)
126        y = [polyval(x, c) for c in Helist]
127        for i in range(10):
128            msg = f"At i={i}"
129            tgt = y[i]
130            res = herme.hermeval(x, [0] * i + [1])
131            assert_almost_equal(res, tgt, err_msg=msg)
132
133        # check that shape is preserved
134        for i in range(3):
135            dims = [2] * i
136            x = np.zeros(dims)
137            assert_equal(herme.hermeval(x, [1]).shape, dims)
138            assert_equal(herme.hermeval(x, [1, 0]).shape, dims)
139            assert_equal(herme.hermeval(x, [1, 0, 0]).shape, dims)
140
141    def test_hermeval2d(self):
142        x1, x2, x3 = self.x
143        y1, y2, y3 = self.y
144
145        # test exceptions
146        assert_raises(ValueError, herme.hermeval2d, x1, x2[:2], self.c2d)
147
148        # test values
149        tgt = y1 * y2
150        res = herme.hermeval2d(x1, x2, self.c2d)
151        assert_almost_equal(res, tgt)
152
153        # test shape
154        z = np.ones((2, 3))
155        res = herme.hermeval2d(z, z, self.c2d)
156        assert_(res.shape == (2, 3))
157
158    def test_hermeval3d(self):
159        x1, x2, x3 = self.x
160        y1, y2, y3 = self.y
161
162        # test exceptions
163        assert_raises(ValueError, herme.hermeval3d, x1, x2, x3[:2], self.c3d)
164
165        # test values
166        tgt = y1 * y2 * y3
167        res = herme.hermeval3d(x1, x2, x3, self.c3d)
168        assert_almost_equal(res, tgt)
169
170        # test shape
171        z = np.ones((2, 3))
172        res = herme.hermeval3d(z, z, z, self.c3d)
173        assert_(res.shape == (2, 3))
174
175    def test_hermegrid2d(self):
176        x1, x2, x3 = self.x
177        y1, y2, y3 = self.y
178
179        # test values
180        tgt = np.einsum('i,j->ij', y1, y2)
181        res = herme.hermegrid2d(x1, x2, self.c2d)
182        assert_almost_equal(res, tgt)
183
184        # test shape
185        z = np.ones((2, 3))
186        res = herme.hermegrid2d(z, z, self.c2d)
187        assert_(res.shape == (2, 3) * 2)
188
189    def test_hermegrid3d(self):
190        x1, x2, x3 = self.x
191        y1, y2, y3 = self.y
192
193        # test values
194        tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
195        res = herme.hermegrid3d(x1, x2, x3, self.c3d)
196        assert_almost_equal(res, tgt)
197
198        # test shape
199        z = np.ones((2, 3))
200        res = herme.hermegrid3d(z, z, z, self.c3d)
201        assert_(res.shape == (2, 3) * 3)
202
203
204class TestIntegral:
205
206    def test_hermeint(self):
207        # check exceptions
208        assert_raises(TypeError, herme.hermeint, [0], .5)
209        assert_raises(ValueError, herme.hermeint, [0], -1)
210        assert_raises(ValueError, herme.hermeint, [0], 1, [0, 0])
211        assert_raises(ValueError, herme.hermeint, [0], lbnd=[0])
212        assert_raises(ValueError, herme.hermeint, [0], scl=[0])
213        assert_raises(TypeError, herme.hermeint, [0], axis=.5)
214
215        # test integration of zero polynomial
216        for i in range(2, 5):
217            k = [0] * (i - 2) + [1]
218            res = herme.hermeint([0], m=i, k=k)
219            assert_almost_equal(res, [0, 1])
220
221        # check single integration with integration constant
222        for i in range(5):
223            scl = i + 1
224            pol = [0] * i + [1]
225            tgt = [i] + [0] * i + [1 / scl]
226            hermepol = herme.poly2herme(pol)
227            hermeint = herme.hermeint(hermepol, m=1, k=[i])
228            res = herme.herme2poly(hermeint)
229            assert_almost_equal(trim(res), trim(tgt))
230
231        # check single integration with integration constant and lbnd
232        for i in range(5):
233            scl = i + 1
234            pol = [0] * i + [1]
235            hermepol = herme.poly2herme(pol)
236            hermeint = herme.hermeint(hermepol, m=1, k=[i], lbnd=-1)
237            assert_almost_equal(herme.hermeval(-1, hermeint), i)
238
239        # check single integration with integration constant and scaling
240        for i in range(5):
241            scl = i + 1
242            pol = [0] * i + [1]
243            tgt = [i] + [0] * i + [2 / scl]
244            hermepol = herme.poly2herme(pol)
245            hermeint = herme.hermeint(hermepol, m=1, k=[i], scl=2)
246            res = herme.herme2poly(hermeint)
247            assert_almost_equal(trim(res), trim(tgt))
248
249        # check multiple integrations with default 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 = herme.hermeint(tgt, m=1)
256                res = herme.hermeint(pol, m=j)
257                assert_almost_equal(trim(res), trim(tgt))
258
259        # check multiple integrations with defined k
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 = herme.hermeint(tgt, m=1, k=[k])
266                res = herme.hermeint(pol, m=j, k=list(range(j)))
267                assert_almost_equal(trim(res), trim(tgt))
268
269        # check multiple integrations with lbnd
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 = herme.hermeint(tgt, m=1, k=[k], lbnd=-1)
276                res = herme.hermeint(pol, m=j, k=list(range(j)), lbnd=-1)
277                assert_almost_equal(trim(res), trim(tgt))
278
279        # check multiple integrations with scaling
280        for i in range(5):
281            for j in range(2, 5):
282                pol = [0] * i + [1]
283                tgt = pol[:]
284                for k in range(j):
285                    tgt = herme.hermeint(tgt, m=1, k=[k], scl=2)
286                res = herme.hermeint(pol, m=j, k=list(range(j)), scl=2)
287                assert_almost_equal(trim(res), trim(tgt))
288
289    def test_hermeint_axis(self):
290        # check that axis keyword works
291        c2d = np.random.random((3, 4))
292
293        tgt = np.vstack([herme.hermeint(c) for c in c2d.T]).T
294        res = herme.hermeint(c2d, axis=0)
295        assert_almost_equal(res, tgt)
296
297        tgt = np.vstack([herme.hermeint(c) for c in c2d])
298        res = herme.hermeint(c2d, axis=1)
299        assert_almost_equal(res, tgt)
300
301        tgt = np.vstack([herme.hermeint(c, k=3) for c in c2d])
302        res = herme.hermeint(c2d, k=3, axis=1)
303        assert_almost_equal(res, tgt)
304
305
306class TestDerivative:
307
308    def test_hermeder(self):
309        # check exceptions
310        assert_raises(TypeError, herme.hermeder, [0], .5)
311        assert_raises(ValueError, herme.hermeder, [0], -1)
312
313        # check that zeroth derivative does nothing
314        for i in range(5):
315            tgt = [0] * i + [1]
316            res = herme.hermeder(tgt, m=0)
317            assert_equal(trim(res), trim(tgt))
318
319        # check that derivation is the inverse of integration
320        for i in range(5):
321            for j in range(2, 5):
322                tgt = [0] * i + [1]
323                res = herme.hermeder(herme.hermeint(tgt, m=j), m=j)
324                assert_almost_equal(trim(res), trim(tgt))
325
326        # check derivation with scaling
327        for i in range(5):
328            for j in range(2, 5):
329                tgt = [0] * i + [1]
330                res = herme.hermeder(
331                    herme.hermeint(tgt, m=j, scl=2), m=j, scl=.5)
332                assert_almost_equal(trim(res), trim(tgt))
333
334    def test_hermeder_axis(self):
335        # check that axis keyword works
336        c2d = np.random.random((3, 4))
337
338        tgt = np.vstack([herme.hermeder(c) for c in c2d.T]).T
339        res = herme.hermeder(c2d, axis=0)
340        assert_almost_equal(res, tgt)
341
342        tgt = np.vstack([herme.hermeder(c) for c in c2d])
343        res = herme.hermeder(c2d, axis=1)
344        assert_almost_equal(res, tgt)
345
346
347class TestVander:
348    # some random values in [-1, 1)
349    x = np.random.random((3, 5)) * 2 - 1
350
351    def test_hermevander(self):
352        # check for 1d x
353        x = np.arange(3)
354        v = herme.hermevander(x, 3)
355        assert_(v.shape == (3, 4))
356        for i in range(4):
357            coef = [0] * i + [1]
358            assert_almost_equal(v[..., i], herme.hermeval(x, coef))
359
360        # check for 2d x
361        x = np.array([[1, 2], [3, 4], [5, 6]])
362        v = herme.hermevander(x, 3)
363        assert_(v.shape == (3, 2, 4))
364        for i in range(4):
365            coef = [0] * i + [1]
366            assert_almost_equal(v[..., i], herme.hermeval(x, coef))
367
368    def test_hermevander2d(self):
369        # also tests hermeval2d for non-square coefficient array
370        x1, x2, x3 = self.x
371        c = np.random.random((2, 3))
372        van = herme.hermevander2d(x1, x2, [1, 2])
373        tgt = herme.hermeval2d(x1, x2, c)
374        res = np.dot(van, c.flat)
375        assert_almost_equal(res, tgt)
376
377        # check shape
378        van = herme.hermevander2d([x1], [x2], [1, 2])
379        assert_(van.shape == (1, 5, 6))
380
381    def test_hermevander3d(self):
382        # also tests hermeval3d for non-square coefficient array
383        x1, x2, x3 = self.x
384        c = np.random.random((2, 3, 4))
385        van = herme.hermevander3d(x1, x2, x3, [1, 2, 3])
386        tgt = herme.hermeval3d(x1, x2, x3, c)
387        res = np.dot(van, c.flat)
388        assert_almost_equal(res, tgt)
389
390        # check shape
391        van = herme.hermevander3d([x1], [x2], [x3], [1, 2, 3])
392        assert_(van.shape == (1, 5, 24))
393
394
395class TestFitting:
396
397    def test_hermefit(self):
398        def f(x):
399            return x * (x - 1) * (x - 2)
400
401        def f2(x):
402            return x**4 + x**2 + 1
403
404        # Test exceptions
405        assert_raises(ValueError, herme.hermefit, [1], [1], -1)
406        assert_raises(TypeError, herme.hermefit, [[1]], [1], 0)
407        assert_raises(TypeError, herme.hermefit, [], [1], 0)
408        assert_raises(TypeError, herme.hermefit, [1], [[[1]]], 0)
409        assert_raises(TypeError, herme.hermefit, [1, 2], [1], 0)
410        assert_raises(TypeError, herme.hermefit, [1], [1, 2], 0)
411        assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[[1]])
412        assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[1, 1])
413        assert_raises(ValueError, herme.hermefit, [1], [1], [-1,])
414        assert_raises(ValueError, herme.hermefit, [1], [1], [2, -1, 6])
415        assert_raises(TypeError, herme.hermefit, [1], [1], [])
416
417        # Test fit
418        x = np.linspace(0, 2)
419        y = f(x)
420        #
421        coef3 = herme.hermefit(x, y, 3)
422        assert_equal(len(coef3), 4)
423        assert_almost_equal(herme.hermeval(x, coef3), y)
424        coef3 = herme.hermefit(x, y, [0, 1, 2, 3])
425        assert_equal(len(coef3), 4)
426        assert_almost_equal(herme.hermeval(x, coef3), y)
427        #
428        coef4 = herme.hermefit(x, y, 4)
429        assert_equal(len(coef4), 5)
430        assert_almost_equal(herme.hermeval(x, coef4), y)
431        coef4 = herme.hermefit(x, y, [0, 1, 2, 3, 4])
432        assert_equal(len(coef4), 5)
433        assert_almost_equal(herme.hermeval(x, coef4), y)
434        # check things still work if deg is not in strict increasing
435        coef4 = herme.hermefit(x, y, [2, 3, 4, 1, 0])
436        assert_equal(len(coef4), 5)
437        assert_almost_equal(herme.hermeval(x, coef4), y)
438        #
439        coef2d = herme.hermefit(x, np.array([y, y]).T, 3)
440        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
441        coef2d = herme.hermefit(x, np.array([y, y]).T, [0, 1, 2, 3])
442        assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
443        # test weighting
444        w = np.zeros_like(x)
445        yw = y.copy()
446        w[1::2] = 1
447        y[0::2] = 0
448        wcoef3 = herme.hermefit(x, yw, 3, w=w)
449        assert_almost_equal(wcoef3, coef3)
450        wcoef3 = herme.hermefit(x, yw, [0, 1, 2, 3], w=w)
451        assert_almost_equal(wcoef3, coef3)
452        #
453        wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, 3, w=w)
454        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
455        wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
456        assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
457        # test scaling with complex values x points whose square
458        # is zero when summed.
459        x = [1, 1j, -1, -1j]
460        assert_almost_equal(herme.hermefit(x, x, 1), [0, 1])
461        assert_almost_equal(herme.hermefit(x, x, [0, 1]), [0, 1])
462        # test fitting only even Legendre polynomials
463        x = np.linspace(-1, 1)
464        y = f2(x)
465        coef1 = herme.hermefit(x, y, 4)
466        assert_almost_equal(herme.hermeval(x, coef1), y)
467        coef2 = herme.hermefit(x, y, [0, 2, 4])
468        assert_almost_equal(herme.hermeval(x, coef2), y)
469        assert_almost_equal(coef1, coef2)
470
471
472class TestCompanion:
473
474    def test_raises(self):
475        assert_raises(ValueError, herme.hermecompanion, [])
476        assert_raises(ValueError, herme.hermecompanion, [1])
477
478    def test_dimensions(self):
479        for i in range(1, 5):
480            coef = [0] * i + [1]
481            assert_(herme.hermecompanion(coef).shape == (i, i))
482
483    def test_linear_root(self):
484        assert_(herme.hermecompanion([1, 2])[0, 0] == -.5)
485
486
487class TestGauss:
488
489    def test_100(self):
490        x, w = herme.hermegauss(100)
491
492        # test orthogonality. Note that the results need to be normalized,
493        # otherwise the huge values that can arise from fast growing
494        # functions like Laguerre can be very confusing.
495        v = herme.hermevander(x, 99)
496        vv = np.dot(v.T * w, v)
497        vd = 1 / np.sqrt(vv.diagonal())
498        vv = vd[:, None] * vv * vd
499        assert_almost_equal(vv, np.eye(100))
500
501        # check that the integral of 1 is correct
502        tgt = np.sqrt(2 * np.pi)
503        assert_almost_equal(w.sum(), tgt)
504
505
506class TestMisc:
507
508    def test_hermefromroots(self):
509        res = herme.hermefromroots([])
510        assert_almost_equal(trim(res), [1])
511        for i in range(1, 5):
512            roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
513            pol = herme.hermefromroots(roots)
514            res = herme.hermeval(roots, pol)
515            tgt = 0
516            assert_(len(pol) == i + 1)
517            assert_almost_equal(herme.herme2poly(pol)[-1], 1)
518            assert_almost_equal(res, tgt)
519
520    def test_hermeroots(self):
521        assert_almost_equal(herme.hermeroots([1]), [])
522        assert_almost_equal(herme.hermeroots([1, 1]), [-1])
523        for i in range(2, 5):
524            tgt = np.linspace(-1, 1, i)
525            res = herme.hermeroots(herme.hermefromroots(tgt))
526            assert_almost_equal(trim(res), trim(tgt))
527
528    def test_hermetrim(self):
529        coef = [2, -1, 1, 0]
530
531        # Test exceptions
532        assert_raises(ValueError, herme.hermetrim, coef, -1)
533
534        # Test results
535        assert_equal(herme.hermetrim(coef), coef[:-1])
536        assert_equal(herme.hermetrim(coef, 1), coef[:-3])
537        assert_equal(herme.hermetrim(coef, 2), [0])
538
539    def test_hermeline(self):
540        assert_equal(herme.hermeline(3, 4), [3, 4])
541
542    def test_herme2poly(self):
543        for i in range(10):
544            assert_almost_equal(herme.herme2poly([0] * i + [1]), Helist[i])
545
546    def test_poly2herme(self):
547        for i in range(10):
548            assert_almost_equal(herme.poly2herme(Helist[i]), [0] * i + [1])
549
550    def test_weight(self):
551        x = np.linspace(-5, 5, 11)
552        tgt = np.exp(-.5 * x**2)
553        res = herme.hermeweight(x)
554        assert_almost_equal(res, tgt)
555 
codekingpro/portable-devtools · Team Ai