Changeset 474
- Timestamp:
- Nov 1, 2004, 3:17:49 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/sparse.py
r471 r474 2 2 """ 3 3 4 #from scipy_base import * #Hardly worth importing scipy just to get isscalar.5 from cg_solve import conjugate_gradient, VectorShapeError6 4 7 5 class Sparse: 8 6 9 def __init__(self, M, N): 10 """Set dimensions 11 """ 12 13 self.M = M 14 self.N = N 15 self.shape = (M,N) 7 def __init__(self, *args): 8 """Create sparse matrix. 9 There are two construction forms 10 Usage: 11 12 Sparse(A) #Creates sparse matrix from dense matrix A 13 Sparse(M, N) #Creates empty MxN sparse matrix 14 15 16 17 """ 18 16 19 self.A = {} 20 21 if len(args) == 1: 22 from Numeric import array 23 try: 24 A = array(args[0]) 25 except: 26 raise 'Input must be convertable to a Numeric array' 27 28 assert len(A.shape) == 2, 'Input must be a 2d matrix' 29 30 self.M, self.N = A.shape 31 for i in range(self.M): 32 for j in range(self.N): 33 if A[i, j] != 0.0: 34 self.A[i, j] = A[i, j] 35 36 37 elif len(args) == 2: 38 self.M = args[0] 39 self.N = args[1] 40 else: 41 raise 'Invalid construction' 42 43 self.shape = (self.M, self.N) 44 17 45 18 46 def __repr__(self): 19 return '%d X %d sparse matrix:\n' %(self.M, self.N) + `self.A` 20 47 return '%d X %d sparse matrix:\n' %(self.M, self.N) + `self.A` 48 49 def __len__(self): 50 """Return number of nonzeros of A 51 """ 52 return len(self.A) 53 54 def nonzeros(self): 55 """Return number of nonzeros of A 56 """ 57 return len(self) 58 21 59 def __setitem__(self, key, x): 22 60 … … 43 81 44 82 def copy(self): 45 83 #FIXME: Use the copy module instead 46 84 new = Sparse(self.M,self.N) 47 85 … … 81 119 #Assume numeric types from now on 82 120 R = zeros((self.M,), Float) #Result 83 84 if len(B.shape) == 1: 121 122 if len(B.shape) == 0: 123 #Scalar - use __rmul__ method 124 R = B*self 125 elif len(B.shape) == 1: 85 126 #Vector 86 87 ## print 'B.shape ',B.shape88 ## print 'self.shape ',self.shape89 90 127 assert B.shape[0] == self.N, 'Mismatching dimensions' 91 128 … … 95 132 96 133 R[i] += self.A[key]*B[j] 97 98 else: 99 raise ValueError, 'Numeric matrix not yet implemented' 134 elif len(B.shape) == 2: 135 raise ValueError, 'Numeric matrix not yet implemented' 136 else: 137 raise ValueError, 'Dimension too high: d=%d' %len(B.shape) 100 138 101 139 return R 140 102 141 103 142 def __add__(self, other): … … 108 147 109 148 new = other.copy() 110 111 # print 'self.shape',self.shape112 # print 'other.shape',other.shape113 114 149 for key in self.A.keys(): 115 150 i, j = key 116 151 117 new[i,j] = new[i,j] +self.A[key]152 new[i,j] += self.A[key] 118 153 119 154 return new … … 129 164 other = float(other) 130 165 except: 131 raise 'only right multiple with scalar implemented' 166 msg = 'Sparse matrix can only "right-multiply" onto a scalar' 167 raise TypeError, msg 132 168 else: 133 169 new = self.copy() … … 142 178 143 179 def trans_mult(self, other): 144 """Multiply the transpose of 180 """Multiply the transpose of matrix with 'other' which can be 145 181 a Numeric vector. 146 182 """ … … 155 191 156 192 #Assume numeric types from now on 157 158 159 193 if len(B.shape) == 1: 160 194 #Vector -
inundation/ga/storm_surge/pyvolution/test_all.py
r464 r474 17 17 #exclude = ['test_least_squares.py', 'test_cg_solve.py', 18 18 # 'test_interpolate_sww.py'] 19 exclude = ['test_cg_solve.py']19 #exclude = ['test_cg_solve.py'] 20 20 21 21 … … 34 34 35 35 36 for file in exclude: 37 files.remove(file) 38 print 'WARNING: File '+ file + ' excluded from testing' 36 if globals().has_key('exclude'): 37 for file in exclude: 38 files.remove(file) 39 print 'WARNING: File '+ file + ' excluded from testing' 39 40 40 41
Note: See TracChangeset
for help on using the changeset viewer.