source: branches/numpy/anuga/utilities/test_sparse.py @ 6304

Last change on this file since 6304 was 6304, checked in by rwilson, 15 years ago

Initial commit of numpy changes. Still a long way to go.

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