Changeset 452 for inundation/ga/storm_surge/pyvolution/sparse.py
- Timestamp:
- Oct 26, 2004, 11:55:00 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/sparse.py
r447 r452 1 1 """Proof of concept sparse matrix code 2 2 """ 3 4 3 from scipy_base import * 4 from cg_solve import conjugate_gradient, VectorShapeError 5 5 6 6 class Sparse: … … 12 12 self.M = M 13 13 self.N = N 14 self.shape = (M,N) 14 15 self.A = {} 15 16 … … 40 41 return 0.0 41 42 43 def copy(self): 44 45 new = Sparse(self.M,self.N) 46 47 for key in self.A.keys(): 48 i, j = key 49 50 new[i,j] = self.A[i,j] 51 52 return new 53 42 54 43 55 def todense(self): … … 49 61 for j in range(self.N): 50 62 if self.A.has_key( (i,j) ): 51 D[i, j] = A[ (i,j) ]63 D[i, j] = self.A[ (i,j) ] 52 64 return D 53 65 … … 67 79 68 80 #Assume numeric types from now on 69 R = zeros( B.shape, Float) #Result81 R = zeros((self.M,), Float) #Result 70 82 71 83 if len(B.shape) == 1: 72 84 #Vector 73 85 86 ## print 'B.shape ',B.shape 87 ## print 'self.shape ',self.shape 88 74 89 assert B.shape[0] == self.N, 'Mismatching dimensions' 75 90 … … 78 93 i, j = key 79 94 80 R[i] += A[key]*B[j]81 82 else: 83 raise 'Numeric matrix not yet implemented'95 R[i] += self.A[key]*B[j] 96 97 else: 98 raise ValueError, 'Numeric matrix not yet implemented' 84 99 85 100 return R 86 87 101 102 def __add__(self, other): 103 """Add this matrix onto 'other' 104 """ 105 106 from Numeric import array, zeros, Float 107 108 new = other.copy() 109 110 # print 'self.shape',self.shape 111 # print 'other.shape',other.shape 112 113 for key in self.A.keys(): 114 i, j = key 115 116 new[i,j] = new[i,j] + self.A[key] 117 118 return new 119 120 121 def __rmul__(self, other): 122 """Right multiply this matrix with scalar 123 """ 124 125 from Numeric import array, zeros, Float 126 127 if isscalar(other): 128 new = self.copy() 129 #Multiply nonzero elements 130 for key in new.A.keys(): 131 i, j = key 132 133 new.A[key] = other*new.A[key] 134 else: 135 raise 'only right multiple with scalar implemented' 136 137 # print 'new.shape',new.shape 138 139 return new 140 141 142 def trans_mult(self, other): 143 """Multiply the transpose of matrix with 'other' which can be 144 a Numeric vector. 145 """ 146 147 from Numeric import array, zeros, Float 148 149 try: 150 B = array(other) 151 except: 152 print 'FIXME: Only Numeric types implemented so far' 153 154 155 #Assume numeric types from now on 156 157 158 if len(B.shape) == 1: 159 #Vector 160 161 assert B.shape[0] == self.M, 'Mismatching dimensions' 162 163 R = zeros((self.N,), Float) #Result 164 165 #Multiply nonzero elements 166 for key in self.A.keys(): 167 i, j = key 168 169 R[j] += self.A[key]*B[i] 170 171 else: 172 raise 'Can only multiply with 1d array' 173 174 return R 175 176 177 178 88 179 if __name__ == '__main__': 89 180 … … 122 213 assert allclose(u, [6,14,4]) 123 214 215 u = A.trans_mult(v) 216 print u 217 assert allclose(u, [6,6,10]) 218 124 219 #Right hand side column 125 220 v = array([[2,4],[3,4],[4,4]]) … … 130 225 #u = A*v[:,1] 131 226 #print u 227 print A.shape 228 229 B = 3*A 230 print B.todense() 231 232 B[1,0] = 2 233 234 C = A+B 235 236 print C.todense()
Note: See TracChangeset
for help on using the changeset viewer.