[485] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import unittest |
---|
| 4 | from math import sqrt |
---|
| 5 | |
---|
| 6 | from sparse import * |
---|
| 7 | from Numeric import allclose, array, transpose, Float |
---|
| 8 | |
---|
| 9 | class TestCase(unittest.TestCase): |
---|
| 10 | |
---|
| 11 | def setUp(self): |
---|
| 12 | pass |
---|
| 13 | |
---|
| 14 | def tearDown(self): |
---|
| 15 | pass |
---|
| 16 | |
---|
| 17 | def test_init1(Self): |
---|
| 18 | """Test initialisation from dimensions |
---|
| 19 | """ |
---|
| 20 | A = Sparse(3,3) |
---|
| 21 | A[1,1] = 4 |
---|
| 22 | |
---|
| 23 | for i in range(3): |
---|
| 24 | for j in range(3): |
---|
| 25 | if i==1 and j==1: |
---|
| 26 | assert A[i,j] == 4.0 |
---|
| 27 | else: |
---|
| 28 | assert A[i,j] == 0.0 |
---|
| 29 | |
---|
| 30 | def test_init2(Self): |
---|
| 31 | """Test initialisation from dense matrix |
---|
| 32 | """ |
---|
| 33 | |
---|
| 34 | A = Sparse(4,3) |
---|
| 35 | A[1,1] = 4 |
---|
| 36 | A[2,0] = 6 |
---|
| 37 | A[0,1] = 13 |
---|
| 38 | A[0,2] = -6 |
---|
| 39 | A[2,2] = 1 |
---|
| 40 | |
---|
| 41 | B = A.todense() |
---|
| 42 | |
---|
| 43 | C = Sparse(B) |
---|
| 44 | |
---|
| 45 | assert allclose(C.todense(), B) |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | def test_dense(self): |
---|
| 49 | A = Sparse(4,3) |
---|
| 50 | A[1,1] = 4 |
---|
| 51 | |
---|
| 52 | assert allclose(A.todense(), [[0,0,0], [0,4,0], [0,0,0], [0,0,0]]) |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | def test_reset_to_zero_possible(self): |
---|
| 56 | """Test that nonzero element can be reset to zero |
---|
| 57 | (This was not possible when we tried sparse from scipy). |
---|
| 58 | """ |
---|
| 59 | |
---|
| 60 | A = Sparse(3,3) |
---|
| 61 | A[1,1] = 4 |
---|
| 62 | A[1,1] = 0 |
---|
| 63 | |
---|
| 64 | assert len(A) == 0 |
---|
| 65 | assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]]) |
---|
| 66 | |
---|
| 67 | #Set an existing zero element to zero |
---|
| 68 | A[1,2] = 0 |
---|
| 69 | assert len(A) == 0 |
---|
| 70 | assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]]) |
---|
| 71 | |
---|
| 72 | def test_sparse_multiplication_vector(self): |
---|
| 73 | A = Sparse(3,3) |
---|
| 74 | |
---|
| 75 | A[0,0] = 3 |
---|
| 76 | A[1,1] = 2 |
---|
| 77 | A[1,2] = 2 |
---|
| 78 | A[2,2] = 1 |
---|
| 79 | |
---|
| 80 | #Right hand side vector |
---|
| 81 | v = [2,3,4] |
---|
| 82 | |
---|
| 83 | u = A*v |
---|
| 84 | assert allclose(u, [6,14,4]) |
---|
| 85 | |
---|
| 86 | #Right hand side column |
---|
| 87 | v = array([[2,4],[3,4],[4,4]]) |
---|
| 88 | |
---|
| 89 | u = A*v[:,0] |
---|
| 90 | assert allclose(u, [6,14,4]) |
---|
| 91 | |
---|
| 92 | u = A*v[:,1] |
---|
| 93 | assert allclose(u, [12,16,4]) |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | def test_sparse_multiplication_matrix(self): |
---|
| 97 | A = Sparse(3,3) |
---|
| 98 | |
---|
| 99 | A[0,0] = 3 |
---|
| 100 | A[1,1] = 2 |
---|
| 101 | A[1,2] = 2 |
---|
| 102 | A[2,2] = 1 |
---|
| 103 | |
---|
| 104 | #Right hand side matrix |
---|
| 105 | v = array([[2,4],[3,4],[4,4]]) |
---|
| 106 | |
---|
| 107 | u = A*v |
---|
| 108 | assert allclose(u, [[6,12], [14,16], [4,4]]) |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | def test_sparse_transpose_multiplication(self): |
---|
| 113 | A = Sparse(3,3) |
---|
| 114 | |
---|
| 115 | A[0,0] = 3 |
---|
| 116 | A[1,1] = 2 |
---|
| 117 | A[1,2] = 2 |
---|
| 118 | A[2,2] = 1 |
---|
| 119 | |
---|
| 120 | #Right hand side vector |
---|
| 121 | v = [2,3,4] |
---|
| 122 | |
---|
| 123 | u = A.trans_mult(v) |
---|
| 124 | assert allclose(u, [6,6,10]) |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | def test_scalar_multiplication(self): |
---|
| 128 | """Test method __rmul__ |
---|
| 129 | """ |
---|
| 130 | |
---|
| 131 | A = Sparse(3,3) |
---|
| 132 | |
---|
| 133 | A[0,0] = 3 |
---|
| 134 | A[1,1] = 2 |
---|
| 135 | A[1,2] = 2 |
---|
| 136 | A[2,2] = 1 |
---|
| 137 | |
---|
| 138 | B = 3*A |
---|
| 139 | assert allclose(B.todense(), 3*A.todense()) |
---|
| 140 | |
---|
| 141 | B = A*3 |
---|
| 142 | assert allclose(B.todense(), 3*A.todense()) |
---|
| 143 | |
---|
| 144 | try: |
---|
| 145 | B = 'a'*A |
---|
| 146 | except TypeError: |
---|
| 147 | pass |
---|
| 148 | else: |
---|
| 149 | raise 'Should have failed' |
---|
| 150 | |
---|
| 151 | def test_sparse_addition(self): |
---|
[596] | 152 | """ Test sparse addition with dok format |
---|
| 153 | """ |
---|
[485] | 154 | |
---|
| 155 | A = Sparse(3,3) |
---|
| 156 | |
---|
| 157 | A[0,0] = 3 |
---|
| 158 | A[1,1] = 2 |
---|
| 159 | A[1,2] = 2 |
---|
| 160 | A[2,2] = 1 |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | B = 3*A |
---|
| 164 | B[1,0] = 2 |
---|
| 165 | |
---|
| 166 | C = A+B |
---|
| 167 | |
---|
[586] | 168 | assert allclose(C.todense(), [[12,0,0], [2,8,8], [0,0,4]]) |
---|
| 169 | |
---|
| 170 | def test_sparse_tocsr(self): |
---|
[596] | 171 | """ Test conversion to csr format |
---|
| 172 | """ |
---|
| 173 | |
---|
[586] | 174 | A = Sparse(4,3) |
---|
| 175 | |
---|
| 176 | A[0,0] = 3 |
---|
| 177 | A[1,1] = 2 |
---|
| 178 | A[1,2] = 2 |
---|
| 179 | A[2,2] = 1 |
---|
| 180 | A[0,2] = 4 |
---|
| 181 | A[2,0] = 5 |
---|
| 182 | |
---|
[599] | 183 | #print ' ' |
---|
| 184 | #print A.todense() |
---|
[586] | 185 | |
---|
| 186 | B = Sparse_CSR(A) |
---|
| 187 | |
---|
[599] | 188 | #print B.todense() |
---|
[586] | 189 | |
---|
| 190 | C = [1, 2, 3] |
---|
| 191 | |
---|
| 192 | assert allclose(B*C, [15.0, 10.0 ,8.0, 0.0]) |
---|
| 193 | |
---|
| 194 | C2 = [[1,2],[2,4],[3,6]] |
---|
| 195 | |
---|
[599] | 196 | #print B*C2 |
---|
[586] | 197 | |
---|
| 198 | assert allclose(B*C2, [[15.0, 30.0],[10.0, 20.0],[8.0, 16.0],[0.0, 0.0]]) |
---|
[485] | 199 | |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | #------------------------------------------------------------- |
---|
| 203 | if __name__ == "__main__": |
---|
| 204 | suite = unittest.makeSuite(TestCase, 'test') |
---|
| 205 | runner = unittest.TextTestRunner() |
---|
| 206 | runner.run(suite) |
---|
| 207 | |
---|