source: inundation/ga/storm_surge/pyvolution/test_sparse.py @ 488

Last change on this file since 488 was 485, checked in by ole, 20 years ago

Cleaned up is sparse and least squares.
Verified that quad trees work.
Implemented sparse matrix x matrix mult and simplified
interpolate in least_squares.
Added more unit testing.

File size: 3.6 KB
Line 
1#!/usr/bin/env python
2
3import unittest
4from math import sqrt
5
6from sparse import *
7from Numeric import allclose, array, transpose, Float
8       
9class 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):
152       
153        A = Sparse(3,3)
154
155        A[0,0] = 3
156        A[1,1] = 2
157        A[1,2] = 2
158        A[2,2] = 1
159
160
161        B = 3*A
162        B[1,0] = 2
163
164        C = A+B
165
166        assert allclose(C.todense(), [[12,0,0], [2,8,8], [0,0,4]])       
167       
168
169
170#-------------------------------------------------------------
171if __name__ == "__main__":
172    suite = unittest.makeSuite(TestCase, 'test')
173    runner = unittest.TextTestRunner()
174    runner.run(suite)
175
Note: See TracBrowser for help on using the repository browser.