1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
"""Test functions for matrix module
"""
from __future__ import division, absolute_import, print_function
from numpy.testing import *
from numpy import ( arange, rot90, add, fliplr, flipud, zeros, ones, eye,
array, diag, histogram2d, tri, mask_indices, triu_indices,
triu_indices_from, tril_indices, tril_indices_from )
import numpy as np
from numpy.compat import asbytes, asbytes_nested
def get_mat(n):
data = arange(n)
data = add.outer(data,data)
return data
class TestEye(TestCase):
def test_basic(self):
assert_equal(eye(4),array([[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]]))
assert_equal(eye(4,dtype='f'),array([[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]],'f'))
assert_equal(eye(3) == 1, eye(3,dtype=bool))
def test_diag(self):
assert_equal(eye(4,k=1),array([[0,1,0,0],
[0,0,1,0],
[0,0,0,1],
[0,0,0,0]]))
assert_equal(eye(4,k=-1),array([[0,0,0,0],
[1,0,0,0],
[0,1,0,0],
[0,0,1,0]]))
def test_2d(self):
assert_equal(eye(4,3),array([[1,0,0],
[0,1,0],
[0,0,1],
[0,0,0]]))
assert_equal(eye(3,4),array([[1,0,0,0],
[0,1,0,0],
[0,0,1,0]]))
def test_diag2d(self):
assert_equal(eye(3,4,k=2),array([[0,0,1,0],
[0,0,0,1],
[0,0,0,0]]))
assert_equal(eye(4,3,k=-2),array([[0,0,0],
[0,0,0],
[1,0,0],
[0,1,0]]))
def test_eye_bounds(self):
assert_equal(eye(2, 2, 1), [[0, 1], [0, 0]])
assert_equal(eye(2, 2, -1), [[0, 0], [1, 0]])
assert_equal(eye(2, 2, 2), [[0, 0], [0, 0]])
assert_equal(eye(2, 2, -2), [[0, 0], [0, 0]])
assert_equal(eye(3, 2, 2), [[0, 0], [0, 0], [0, 0]])
assert_equal(eye(3, 2, 1), [[0, 1], [0, 0], [0, 0]])
assert_equal(eye(3, 2, -1), [[0, 0], [1, 0], [0, 1]])
assert_equal(eye(3, 2, -2), [[0, 0], [0, 0], [1, 0]])
assert_equal(eye(3, 2, -3), [[0, 0], [0, 0], [0, 0]])
def test_strings(self):
assert_equal(eye(2, 2, dtype='S3'),
asbytes_nested([['1', ''], ['', '1']]))
def test_bool(self):
assert_equal(eye(2, 2, dtype=bool), [[True, False], [False, True]])
class TestDiag(TestCase):
def test_vector(self):
vals = (100 * arange(5)).astype('l')
b = zeros((5, 5))
for k in range(5):
b[k, k] = vals[k]
assert_equal(diag(vals), b)
b = zeros((7, 7))
c = b.copy()
for k in range(5):
b[k, k + 2] = vals[k]
c[k + 2, k] = vals[k]
assert_equal(diag(vals, k=2), b)
assert_equal(diag(vals, k=-2), c)
def test_matrix(self, vals=None):
if vals is None:
vals = (100 * get_mat(5) + 1).astype('l')
b = zeros((5,))
for k in range(5):
b[k] = vals[k,k]
assert_equal(diag(vals), b)
b = b * 0
for k in range(3):
b[k] = vals[k, k + 2]
assert_equal(diag(vals, 2), b[:3])
for k in range(3):
b[k] = vals[k + 2, k]
assert_equal(diag(vals, -2), b[:3])
def test_fortran_order(self):
vals = array((100 * get_mat(5) + 1), order='F', dtype='l')
self.test_matrix(vals)
def test_diag_bounds(self):
A = [[1, 2], [3, 4], [5, 6]]
assert_equal(diag(A, k=2), [])
assert_equal(diag(A, k=1), [2])
assert_equal(diag(A, k=0), [1, 4])
assert_equal(diag(A, k=-1), [3, 6])
assert_equal(diag(A, k=-2), [5])
assert_equal(diag(A, k=-3), [])
def test_failure(self):
self.assertRaises(ValueError, diag, [[[1]]])
class TestFliplr(TestCase):
def test_basic(self):
self.assertRaises(ValueError, fliplr, ones(4))
a = get_mat(4)
b = a[:,::-1]
assert_equal(fliplr(a),b)
a = [[0,1,2],
[3,4,5]]
b = [[2,1,0],
[5,4,3]]
assert_equal(fliplr(a),b)
class TestFlipud(TestCase):
def test_basic(self):
a = get_mat(4)
b = a[::-1,:]
assert_equal(flipud(a),b)
a = [[0,1,2],
[3,4,5]]
b = [[3,4,5],
[0,1,2]]
assert_equal(flipud(a),b)
class TestRot90(TestCase):
def test_basic(self):
self.assertRaises(ValueError, rot90, ones(4))
a = [[0,1,2],
[3,4,5]]
b1 = [[2,5],
[1,4],
[0,3]]
b2 = [[5,4,3],
[2,1,0]]
b3 = [[3,0],
[4,1],
[5,2]]
b4 = [[0,1,2],
[3,4,5]]
for k in range(-3,13,4):
assert_equal(rot90(a,k=k),b1)
for k in range(-2,13,4):
assert_equal(rot90(a,k=k),b2)
for k in range(-1,13,4):
assert_equal(rot90(a,k=k),b3)
for k in range(0,13,4):
assert_equal(rot90(a,k=k),b4)
def test_axes(self):
a = ones((50,40,3))
assert_equal(rot90(a).shape,(40,50,3))
class TestHistogram2d(TestCase):
def test_simple(self):
x = array([ 0.41702200, 0.72032449, 0.00011437481, 0.302332573, 0.146755891])
y = array([ 0.09233859, 0.18626021, 0.34556073, 0.39676747, 0.53881673])
xedges = np.linspace(0,1,10)
yedges = np.linspace(0,1,10)
H = histogram2d(x, y, (xedges, yedges))[0]
answer = array([[0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]])
assert_array_equal(H.T, answer)
H = histogram2d(x, y, xedges)[0]
assert_array_equal(H.T, answer)
H,xedges,yedges = histogram2d(list(range(10)),list(range(10)))
assert_array_equal(H, eye(10,10))
assert_array_equal(xedges, np.linspace(0,9,11))
assert_array_equal(yedges, np.linspace(0,9,11))
def test_asym(self):
x = array([1, 1, 2, 3, 4, 4, 4, 5])
y = array([1, 3, 2, 0, 1, 2, 3, 4])
H, xed, yed = histogram2d(x,y, (6, 5), range = [[0,6],[0,5]], normed=True)
answer = array([[0.,0,0,0,0],
[0,1,0,1,0],
[0,0,1,0,0],
[1,0,0,0,0],
[0,1,1,1,0],
[0,0,0,0,1]])
assert_array_almost_equal(H, answer/8., 3)
assert_array_equal(xed, np.linspace(0,6,7))
assert_array_equal(yed, np.linspace(0,5,6))
def test_norm(self):
x = array([1,2,3,1,2,3,1,2,3])
y = array([1,1,1,2,2,2,3,3,3])
H, xed, yed = histogram2d(x,y,[[1,2,3,5], [1,2,3,5]], normed=True)
answer=array([[1,1,.5],
[1,1,.5],
[.5,.5,.25]])/9.
assert_array_almost_equal(H, answer, 3)
def test_all_outliers(self):
r = rand(100)+1.
H, xed, yed = histogram2d(r, r, (4, 5), range=([0,1], [0,1]))
assert_array_equal(H, 0)
def test_empty(self):
a, edge1, edge2 = histogram2d([],[], bins=([0,1],[0,1]))
assert_array_max_ulp(a, array([[ 0.]]))
a, edge1, edge2 = histogram2d([], [], bins=4)
assert_array_max_ulp(a, np.zeros((4, 4)))
class TestTri(TestCase):
def test_dtype(self):
out = array([[1,0,0],
[1,1,0],
[1,1,1]])
assert_array_equal(tri(3),out)
assert_array_equal(tri(3,dtype=bool),out.astype(bool))
def test_tril_triu():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.ones((2, 2), dtype=dtype)
b = np.tril(a)
c = np.triu(a)
assert_array_equal(b, [[1, 0], [1, 1]])
assert_array_equal(c, b.T)
# should return the same dtype as the original array
assert_equal(b.dtype, a.dtype)
assert_equal(c.dtype, a.dtype)
def test_mask_indices():
# simple test without offset
iu = mask_indices(3, np.triu)
a = np.arange(9).reshape(3, 3)
yield (assert_array_equal, a[iu], array([0, 1, 2, 4, 5, 8]))
# Now with an offset
iu1 = mask_indices(3, np.triu, 1)
yield (assert_array_equal, a[iu1], array([1, 2, 5]))
def test_tril_indices():
# indices without and with offset
il1 = tril_indices(4)
il2 = tril_indices(4, 2)
a = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
# indexing:
yield (assert_array_equal, a[il1],
array([ 1, 5, 6, 9, 10, 11, 13, 14, 15, 16]) )
# And for assigning values:
a[il1] = -1
yield (assert_array_equal, a,
array([[-1, 2, 3, 4],
[-1, -1, 7, 8],
[-1, -1, -1, 12],
[-1, -1, -1, -1]]) )
# These cover almost the whole array (two diagonals right of the main one):
a[il2] = -10
yield (assert_array_equal, a,
array([[-10, -10, -10, 4],
[-10, -10, -10, -10],
[-10, -10, -10, -10],
[-10, -10, -10, -10]]) )
class TestTriuIndices(object):
def test_triu_indices(self):
iu1 = triu_indices(4)
iu2 = triu_indices(4, 2)
a = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
# Both for indexing:
yield (assert_array_equal, a[iu1],
array([1, 2, 3, 4, 6, 7, 8, 11, 12, 16]))
# And for assigning values:
a[iu1] = -1
yield (assert_array_equal, a,
array([[-1, -1, -1, -1],
[ 5, -1, -1, -1],
[ 9, 10, -1, -1],
[13, 14, 15, -1]]) )
# These cover almost the whole array (two diagonals right of the main one):
a[iu2] = -10
yield ( assert_array_equal, a,
array([[ -1, -1, -10, -10],
[ 5, -1, -1, -10],
[ 9, 10, -1, -1],
[ 13, 14, 15, -1]]) )
class TestTrilIndicesFrom(object):
def test_exceptions(self):
assert_raises(ValueError, tril_indices_from, np.ones((2,)))
assert_raises(ValueError, tril_indices_from, np.ones((2,2,2)))
assert_raises(ValueError, tril_indices_from, np.ones((2,3)))
class TestTriuIndicesFrom(object):
def test_exceptions(self):
assert_raises(ValueError, triu_indices_from, np.ones((2,)))
assert_raises(ValueError, triu_indices_from, np.ones((2,2,2)))
assert_raises(ValueError, triu_indices_from, np.ones((2,3)))
if __name__ == "__main__":
run_module_suite()
|