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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
"""
Test functions for gufuncs_linalg module
Heavily inspired (ripped in part) test_linalg
"""
from __future__ import division, print_function
################################################################################
# The following functions are implemented in the module "gufuncs_linalg"
#
# category "linalg"
# - inv (TestInv)
# - poinv (TestPoinv)
# - det (TestDet)
# - slogdet (TestDet)
# - eig (TestEig)
# - eigh (TestEigh)
# - eigvals (TestEigvals)
# - eigvalsh (TestEigvalsh)
# - cholesky
# - solve (TestSolve)
# - chosolve (TestChosolve)
# - svd (TestSVD)
# ** unimplemented **
# - qr
# - matrix_power
# - matrix_rank
# - pinv
# - lstsq
# - tensorinv
# - tensorsolve
# - norm
# - cond
#
# category "inspired by pdl"
# - quadratic_form
# - matrix_multiply3
# - add3 (TestAdd3)
# - multiply3 (TestMultiply3)
# - multiply3_add (TestMultiply3Add)
# - multiply_add (TestMultiplyAdd)
# - multiply_add2 (TestMultiplyAdd2)
# - multiply4 (TestMultiply4)
# - multiply4_add (TestMultiply4Add)
#
# category "others"
# - convolve
# - inner1d
# - innerwt
# - matrix_multiply
from nose.plugins.skip import Skip, SkipTest
import numpy as np
from numpy.testing import (TestCase, assert_, assert_equal, assert_raises,
assert_array_equal, assert_almost_equal,
run_module_suite)
from numpy import array, single, double, csingle, cdouble, dot, identity
from numpy import multiply, inf
import numpy.linalg._gufuncs_linalg as gula
old_assert_almost_equal = assert_almost_equal
def assert_almost_equal(a, b, **kw):
if a.dtype.type in (single, csingle):
decimal = 5
else:
decimal = 10
old_assert_almost_equal(a, b, decimal = decimal, **kw)
def assert_valid_eigen_no_broadcast(M, w, v, **kw):
lhs = gula.matrix_multiply(M,v)
rhs = w*v
assert_almost_equal(lhs, rhs, **kw)
def assert_valid_eigen_recurse(M, w, v, **kw):
"""check that w and v are valid eigenvalues/eigenvectors for matrix M
broadcast"""
if len(M.shape) > 2:
for i in range(M.shape[0]):
assert_valid_eigen_recurse(M[i], w[i], v[i], **kw)
else:
if len(M.shape) == 2:
assert_valid_eigen_no_broadcast(M, w, v, **kw)
else:
raise AssertionError('Not enough dimensions')
def assert_valid_eigen(M, w, v, **kw):
if np.any(np.isnan(M)):
raise AssertionError('nan found in matrix')
if np.any(np.isnan(w)):
raise AssertionError('nan found in eigenvalues')
if np.any(np.isnan(v)):
raise AssertionError('nan found in eigenvectors')
assert_valid_eigen_recurse(M, w, v, **kw)
def assert_valid_eigenvals_no_broadcast(M, w, **kw):
ident = np.eye(M.shape[0], dtype=M.dtype)
for i in range(w.shape[0]):
assert_almost_equal(gula.det(M - w[i]*ident), 0.0, **kw)
def assert_valid_eigenvals_recurse(M, w, **kw):
if len(M.shape) > 2:
for i in range(M.shape[0]):
assert_valid_eigenvals_recurse(M[i], w[i], **kw)
else:
if len(M.shape) == 2:
assert_valid_eigenvals_no_broadcast(M, w, **kw)
else:
raise AssertionError('Not enough dimensions')
def assert_valid_eigenvals(M, w, **kw):
if np.any(np.isnan(M)):
raise AssertionError('nan found in matrix')
if np.any(np.isnan(w)):
raise AssertionError('nan found in eigenvalues')
assert_valid_eigenvals_recurse(M, w, **kw)
class MatrixGenerator(object):
def real_matrices(self):
a = [[1,2],
[3,4]]
b = [[4,3],
[2,1]]
return a, b
def real_symmetric_matrices(self):
a = [[ 2 ,-1],
[-1 , 2]]
b = [[4,3],
[2,1]]
return a, b
def complex_matrices(self):
a = [[1+2j,2+3j],
[3+4j,4+5j]]
b = [[4+3j,3+2j],
[2+1j,1+0j]]
return a, b
def complex_hermitian_matrices(self):
a = [[2,-1],
[-1, 2]]
b = [[4+3j,3+2j],
[2-1j,1+0j]]
return a, b
def real_matrices_vector(self):
a, b = self.real_matrices()
return [a], [b]
def real_symmetric_matrices_vector(self):
a, b = self.real_symmetric_matrices()
return [a], [b]
def complex_matrices_vector(self):
a, b = self.complex_matrices()
return [a], [b]
def complex_hermitian_matrices_vector(self):
a, b = self.complex_hermitian_matrices()
return [a], [b]
class GeneralTestCase(MatrixGenerator):
def test_single(self):
a,b = self.real_matrices()
self.do(array(a, dtype=single),
array(b, dtype=single))
def test_double(self):
a, b = self.real_matrices()
self.do(array(a, dtype=double),
array(b, dtype=double))
def test_csingle(self):
a, b = self.complex_matrices()
self.do(array(a, dtype=csingle),
array(b, dtype=csingle))
def test_cdouble(self):
a, b = self.complex_matrices()
self.do(array(a, dtype=cdouble),
array(b, dtype=cdouble))
def test_vector_single(self):
a,b = self.real_matrices_vector()
self.do(array(a, dtype=single),
array(b, dtype=single))
def test_vector_double(self):
a, b = self.real_matrices_vector()
self.do(array(a, dtype=double),
array(b, dtype=double))
def test_vector_csingle(self):
a, b = self.complex_matrices_vector()
self.do(array(a, dtype=csingle),
array(b, dtype=csingle))
def test_vector_cdouble(self):
a, b = self.complex_matrices_vector()
self.do(array(a, dtype=cdouble),
array(b, dtype=cdouble))
class HermitianTestCase(MatrixGenerator):
def test_single(self):
a,b = self.real_symmetric_matrices()
self.do(array(a, dtype=single),
array(b, dtype=single))
def test_double(self):
a, b = self.real_symmetric_matrices()
self.do(array(a, dtype=double),
array(b, dtype=double))
def test_csingle(self):
a, b = self.complex_hermitian_matrices()
self.do(array(a, dtype=csingle),
array(b, dtype=csingle))
def test_cdouble(self):
a, b = self.complex_hermitian_matrices()
self.do(array(a, dtype=cdouble),
array(b, dtype=cdouble))
def test_vector_single(self):
a,b = self.real_symmetric_matrices_vector()
self.do(array(a, dtype=single),
array(b, dtype=single))
def test_vector_double(self):
a, b = self.real_symmetric_matrices_vector()
self.do(array(a, dtype=double),
array(b, dtype=double))
def test_vector_csingle(self):
a, b = self.complex_hermitian_matrices_vector()
self.do(array(a, dtype=csingle),
array(b, dtype=csingle))
def test_vector_cdouble(self):
a, b = self.complex_hermitian_matrices_vector()
self.do(array(a, dtype=cdouble),
array(b, dtype=cdouble))
class TestMatrixMultiply(GeneralTestCase):
def do(self, a, b):
res = gula.matrix_multiply(a,b)
if a.ndim == 2:
assert_almost_equal(res, np.dot(a,b))
else:
assert_almost_equal(res[0], np.dot(a[0],b[0]))
def test_column_matrix(self):
A = np.arange(2*2).reshape((2,2))
B = np.arange(2*1).reshape((2,1))
res = gula.matrix_multiply(A,B)
assert_almost_equal(res, np.dot(A,B))
class TestInv(GeneralTestCase, TestCase):
def do(self, a, b):
a_inv = gula.inv(a)
ident = identity(a.shape[-1])
if 3 == len(a.shape):
ident = ident.reshape((1, ident.shape[0], ident.shape[1]))
assert_almost_equal(gula.matrix_multiply(a, a_inv), ident)
class TestPoinv(HermitianTestCase, TestCase):
def do(self, a, b):
a_inv = gula.poinv(a)
ident = identity(a.shape[-1])
if 3 == len(a.shape):
ident = ident.reshape((1,ident.shape[0], ident.shape[1]))
assert_almost_equal(a_inv, gula.inv(a))
assert_almost_equal(gula.matrix_multiply(a, a_inv), ident)
class TestDet(GeneralTestCase, TestCase):
def do(self, a, b):
d = gula.det(a)
s, ld = gula.slogdet(a)
assert_almost_equal(s * np.exp(ld), d)
if np.csingle == a.dtype.type or np.single == a.dtype.type:
cmp_type=np.csingle
else:
cmp_type=np.cdouble
ev = gula.eigvals(a.astype(cmp_type))
assert_almost_equal(d.astype(cmp_type),
multiply.reduce(ev.astype(cmp_type),
axis=(ev.ndim-1)))
if s != 0:
assert_almost_equal(np.abs(s), 1)
else:
assert_equal(ld, -inf)
def test_zero(self):
assert_equal(gula.det(array([[0.0]], dtype=single)), 0.0)
assert_equal(gula.det(array([[0.0]], dtype=double)), 0.0)
assert_equal(gula.det(array([[0.0]], dtype=csingle)), 0.0)
assert_equal(gula.det(array([[0.0]], dtype=cdouble)), 0.0)
assert_equal(gula.slogdet(array([[0.0]], dtype=single)), (0.0, -inf))
assert_equal(gula.slogdet(array([[0.0]], dtype=double)), (0.0, -inf))
assert_equal(gula.slogdet(array([[0.0]], dtype=csingle)), (0.0, -inf))
assert_equal(gula.slogdet(array([[0.0]], dtype=cdouble)), (0.0, -inf))
def test_types(self):
for typ in [(single, single),
(double, double),
(csingle, single),
(cdouble, double)]:
for x in [ [0], [[0]], [[[0]]] ]:
assert_equal(gula.det(array(x, dtype=typ[0])).dtype, typ[0])
assert_equal(gula.slogdet(array(x, dtype=typ[0]))[0].dtype, typ[0])
assert_equal(gula.slogdet(array(x, dtype=typ[0]))[1].dtype, typ[1])
class TestEig(GeneralTestCase, TestCase):
def do(self, a, b):
evalues, evectors = gula.eig(a)
assert_valid_eigenvals(a, evalues)
assert_valid_eigen(a, evalues, evectors)
ev = gula.eigvals(a)
assert_valid_eigenvals(a, evalues)
assert_almost_equal(ev, evalues)
class TestEigh(HermitianTestCase, TestCase):
def do(self, a, b):
evalues_lo, evectors_lo = gula.eigh(a, UPLO='L')
evalues_up, evectors_up = gula.eigh(a, UPLO='U')
assert_valid_eigenvals(a, evalues_lo)
assert_valid_eigenvals(a, evalues_up)
assert_valid_eigen(a, evalues_lo, evectors_lo)
assert_valid_eigen(a, evalues_up, evectors_up)
assert_almost_equal(evalues_lo, evalues_up)
assert_almost_equal(evectors_lo, evectors_up)
ev_lo = gula.eigvalsh(a, UPLO='L')
ev_up = gula.eigvalsh(a, UPLO='U')
assert_valid_eigenvals(a, ev_lo)
assert_valid_eigenvals(a, ev_up)
assert_almost_equal(ev_lo, evalues_lo)
assert_almost_equal(ev_up, evalues_up)
class TestSolve(GeneralTestCase,TestCase):
def do(self, a, b):
x = gula.solve(a,b)
assert_almost_equal(b, gula.matrix_multiply(a,x))
class TestChosolve(HermitianTestCase, TestCase):
def do(self, a, b):
"""
inner1d not defined for complex types.
todo: implement alternative test
"""
if csingle == a.dtype or cdouble == a.dtype:
raise SkipTest
x_lo = gula.chosolve(a, b, UPLO='L')
x_up = gula.chosolve(a, b, UPLO='U')
assert_almost_equal(x_lo, x_up)
# inner1d not defined for complex types
# todo: implement alternative test
assert_almost_equal(b, gula.matrix_multiply(a,x_lo))
assert_almost_equal(b, gula.matrix_multiply(a,x_up))
class TestSVD(GeneralTestCase, TestCase):
def do(self, a, b):
""" still work in progress """
raise SkipTest
u, s, vt = gula.svd(a, 0)
assert_almost_equal(a, dot(multiply(u, s), vt))
"""
class TestCholesky(HermitianTestCase, TestCase):
def do(self, a, b):
pass
"""
################################################################################
# ufuncs inspired by pdl
# - add3
# - multiply3
# - multiply3_add
# - multiply_add
# - multiply_add2
# - multiply4
# - multiply4_add
class UfuncTestCase(object):
parameter = range(0,10)
def _check_for_type(self, typ):
a = np.array(self.__class__.parameter, dtype=typ)
self.do(a)
def _check_for_type_vector(self, typ):
parameter = self.__class__.parameter
a = np.array([parameter, parameter], dtype=typ)
self.do(a)
def test_single(self):
self._check_for_type(single)
def test_double(self):
self._check_for_type(double)
def test_csingle(self):
self._check_for_type(csingle)
def test_cdouble(self):
self._check_for_type(cdouble)
def test_single_vector(self):
self._check_for_type_vector(single)
def test_double_vector(self):
self._check_for_type_vector(double)
def test_csingle_vector(self):
self._check_for_type_vector(csingle)
def test_cdouble_vector(self):
self._check_for_type_vector(cdouble)
class TestAdd3(UfuncTestCase, TestCase):
def do(self, a):
r = gula.add3(a,a,a)
assert_almost_equal(r, a+a+a)
class TestMultiply3(UfuncTestCase, TestCase):
def do(self, a):
r = gula.multiply3(a,a,a)
assert_almost_equal(r, a*a*a)
class TestMultiply3Add(UfuncTestCase, TestCase):
def do(self, a):
r = gula.multiply3_add(a,a,a,a)
assert_almost_equal(r, a*a*a+a)
class TestMultiplyAdd(UfuncTestCase, TestCase):
def do(self, a):
r = gula.multiply_add(a,a,a)
assert_almost_equal(r, a*a+a)
class TestMultiplyAdd2(UfuncTestCase, TestCase):
def do(self, a):
r = gula.multiply_add2(a,a,a,a)
assert_almost_equal(r, a*a+a+a)
class TestMultiply4(UfuncTestCase, TestCase):
def do(self, a):
r = gula.multiply4(a,a,a,a)
assert_almost_equal(r, a*a*a*a)
class TestMultiply4_add(UfuncTestCase, TestCase):
def do(self, a):
r = gula.multiply4_add(a,a,a,a,a)
assert_almost_equal(r, a*a*a*a+a)
if __name__ == "__main__":
print('testing gufuncs_linalg; gufuncs version: %s' % gula._impl.__version__)
run_module_suite()
|