-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_EigenNumpy.py
More file actions
executable file
·101 lines (86 loc) · 3.23 KB
/
test_EigenNumpy.py
File metadata and controls
executable file
·101 lines (86 loc) · 3.23 KB
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
#!/usr/bin/env python
import unittest
import EigenNumpy as enp
import numpy as np
class TestStringMethods(unittest.TestCase):
def test_sizes(self):
v = enp.RowMatrix(8, 12)
self.assertEqual(v.rows(), 8)
self.assertEqual(v.cols(), 12)
self.assertEqual(v.size(), 8 * 12)
def test_zero(self):
v = enp.RowMatrix.zero(8, 12)
self.assertEqual(v.rows(), 8)
self.assertEqual(v.cols(), 12)
self.assertEqual(v.sum(), 0.0)
def test_bracket_one(self):
v = enp.RowMatrix.zero(4, 4)
self.assertEqual(v[0, 0], 0.0)
self.assertEqual(v[1, 1], 0.0)
self.assertEqual(v[2, 2], 0.0)
self.assertEqual(v[3, 3], 0.0)
# Test setitem
v[0, 0] = 1
v[1, 1] = 2
v[2, 2], v[3, 3] = 3, 4
# Test getitem
self.assertEqual(v[0, 0], 1.0)
self.assertEqual(v[1, 1], 2.0)
self.assertEqual(v[2, 2], 3.0)
self.assertEqual(v[3, 3], 4.0)
def test_random(self):
v = enp.RowMatrix.random(8, 8)
self.assertEqual(v.rows(), v.cols())
self.assertEqual(v.cols(), 8)
def test_compare(self):
n1 = enp.RowMatrix(np.arange(0, 16).reshape(4, 4).astype(np.double))
n2 = enp.RowMatrix(np.arange(0, 16).reshape(4, 4).astype(np.double))
n3 = enp.RowMatrix.random(4, 4)
self.assertEqual(n1, n2)
self.assertNotEqual(n1, n3)
def test_bracket_range(self):
n = np.arange(0, 16).reshape(4, 4).astype(np.double)
v = enp.RowMatrix(n)
self.assertTrue((v[0, :] == n[0, :]).all()) # Open full range
self.assertTrue((v[1, 1:4] == n[1, 1:4]).all()) # Subrange
self.assertTrue((v[1, ::2] == n[1, ::2]).all()) # Jump range
self.assertTrue((v[1, 0:4:2] == n[1, 0:4:2]).all()) # Subrange Jump
self.assertTrue((v[1, ::-1] == n[1, ::-1]).all()) # Inverse range
self.assertTrue((v[1, ::-2] == n[1, ::-2]).all()) # Inverse range jump
def test_fromNumpy(self):
n = np.random.rand(3, 4)
v = enp.RowMatrix(n)
# Test the constructor from numpy worked
self.assertTrue((n == v).all())
self.assertTrue((v == n).all())
# Test by index
self.assertEqual(v[1, 2], n[1, 2])
self.assertEqual(v[2, 1], n[2, 1])
def test_toNumpy(self):
v = enp.RowMatrix.random(8, 8)
n = np.array(v)
# Test the numpy copy worked
self.assertTrue((n == v).all())
self.assertTrue((v == n).all())
# Test by index
self.assertEqual(v[1, 2], n[1, 2])
self.assertEqual(v[2, 1], n[2, 1])
def test_multMxM(self):
v1 = enp.RowMatrix.random(8, 8)
v2 = enp.RowMatrix.random(8, 8)
# Copy to compare
n1 = np.array(v1)
n2 = np.array(v2)
self.assertTrue((n1 == v1).all())
self.assertTrue((n2 == v2).all())
mulV = v1 * v2
mulN = n1.dot(n2)
self.assertTrue((mulV == mulN).all())
def test_multMxV(self):
v = enp.RowMatrix.random(8, 8)
n = np.array(v)
self.assertTrue((n * 3 == 3 * v).all())
self.assertTrue((3 * n == 3 * v).all())
self.assertEqual(3 * v, v * 3)
if __name__ == '__main__':
unittest.main()