codekingpro/portable-devtools
115k
1"""Tests for polyutils module.
2
3"""
4import numpy as np
5import numpy.polynomial.polyutils as pu
6from numpy.testing import assert_, assert_almost_equal, assert_equal, assert_raises
7
8
9class TestMisc:
10
11 def test_trimseq(self):
12 tgt = [1]
13 for num_trailing_zeros in range(5):
14 res = pu.trimseq([1] + [0] * num_trailing_zeros)
15 assert_equal(res, tgt)
16
17 def test_trimseq_empty_input(self):
18 for empty_seq in [[], np.array([], dtype=np.int32)]:
19 assert_equal(pu.trimseq(empty_seq), empty_seq)
20
21 def test_as_series(self):
22 # check exceptions
23 assert_raises(ValueError, pu.as_series, [[]])
24 assert_raises(ValueError, pu.as_series, [[[1, 2]]])
25 assert_raises(ValueError, pu.as_series, [[1], ['a']])
26 # check common types
27 types = ['i', 'd', 'O']
28 for i in range(len(types)):
29 for j in range(i):
30 ci = np.ones(1, types[i])
31 cj = np.ones(1, types[j])
32 [resi, resj] = pu.as_series([ci, cj])
33 assert_(resi.dtype.char == resj.dtype.char)
34 assert_(resj.dtype.char == types[i])
35
36 def test_trimcoef(self):
37 coef = [2, -1, 1, 0]
38 # Test exceptions
39 assert_raises(ValueError, pu.trimcoef, coef, -1)
40 # Test results
41 assert_equal(pu.trimcoef(coef), coef[:-1])
42 assert_equal(pu.trimcoef(coef, 1), coef[:-3])
43 assert_equal(pu.trimcoef(coef, 2), [0])
44
45 def test_vander_nd_exception(self):
46 # n_dims != len(points)
47 assert_raises(ValueError, pu._vander_nd, (), (1, 2, 3), [90])
48 # n_dims != len(degrees)
49 assert_raises(ValueError, pu._vander_nd, (), (), [90.65])
50 # n_dims == 0
51 assert_raises(ValueError, pu._vander_nd, (), (), [])
52
53 def test_div_zerodiv(self):
54 # c2[-1] == 0
55 assert_raises(ZeroDivisionError, pu._div, pu._div, (1, 2, 3), [0])
56
57 def test_pow_too_large(self):
58 # power > maxpower
59 assert_raises(ValueError, pu._pow, (), [1, 2, 3], 5, 4)
60
61class TestDomain:
62
63 def test_getdomain(self):
64 # test for real values
65 x = [1, 10, 3, -1]
66 tgt = [-1, 10]
67 res = pu.getdomain(x)
68 assert_almost_equal(res, tgt)
69
70 # test for complex values
71 x = [1 + 1j, 1 - 1j, 0, 2]
72 tgt = [-1j, 2 + 1j]
73 res = pu.getdomain(x)
74 assert_almost_equal(res, tgt)
75
76 def test_mapdomain(self):
77 # test for real values
78 dom1 = [0, 4]
79 dom2 = [1, 3]
80 tgt = dom2
81 res = pu.mapdomain(dom1, dom1, dom2)
82 assert_almost_equal(res, tgt)
83
84 # test for complex values
85 dom1 = [0 - 1j, 2 + 1j]
86 dom2 = [-2, 2]
87 tgt = dom2
88 x = dom1
89 res = pu.mapdomain(x, dom1, dom2)
90 assert_almost_equal(res, tgt)
91
92 # test for multidimensional arrays
93 dom1 = [0, 4]
94 dom2 = [1, 3]
95 tgt = np.array([dom2, dom2])
96 x = np.array([dom1, dom1])
97 res = pu.mapdomain(x, dom1, dom2)
98 assert_almost_equal(res, tgt)
99
100 # test that subtypes are preserved.
101 class MyNDArray(np.ndarray):
102 pass
103
104 dom1 = [0, 4]
105 dom2 = [1, 3]
106 x = np.array([dom1, dom1]).view(MyNDArray)
107 res = pu.mapdomain(x, dom1, dom2)
108 assert_(isinstance(res, MyNDArray))
109
110 def test_mapparms(self):
111 # test for real values
112 dom1 = [0, 4]
113 dom2 = [1, 3]
114 tgt = [1, .5]
115 res = pu. mapparms(dom1, dom2)
116 assert_almost_equal(res, tgt)
117
118 # test for complex values
119 dom1 = [0 - 1j, 2 + 1j]
120 dom2 = [-2, 2]
121 tgt = [-1 + 1j, 1 - 1j]
122 res = pu.mapparms(dom1, dom2)
123 assert_almost_equal(res, tgt)
124 