Changeset 594
- Timestamp:
- Nov 18, 2004, 10:53:59 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/sparse.py
r587 r594 302 302 print 'FIXME: Only Numeric types implemented so far' 303 303 304 305 ## R = csr_mv(self,B) 306 307 #Assume numeric types from now on 304 return csr_mv(self,B) 305 306 307 308 """ 309 Setup for C extensions 310 """ 311 312 313 def csr_mv(self, B): 314 315 from Numeric import zeros, Float 316 317 318 #Assume numeric types from now on 308 319 309 if len(B.shape) == 0: 310 #Scalar - use __rmul__ method 311 R = B*self 312 313 elif len(B.shape) == 1: 314 #Vector 315 assert B.shape[0] == self.N, 'Mismatching dimensions' 316 317 R = zeros(self.M, Float) #Result 318 319 #Multiply nonzero elements 320 for i in range(self.M): 321 for ckey in range(self.row_ptr[i],self.row_ptr[i+1]): 322 j = self.colind[ckey] 323 R[i] += self.data[ckey]*B[j] 324 325 elif len(B.shape) == 2: 326 327 R = zeros((self.M, B.shape[1]), Float) #Result matrix 328 329 #Multiply nonzero elements 330 331 for col in range(R.shape[1]): 332 #For each column 333 for i in range(self.M): 334 for ckey in range(self.row_ptr[i],self.row_ptr[i+1]): 335 j = self.colind[ckey] 336 R[i, col] += self.data[ckey]*B[j,col] 337 338 else: 339 raise ValueError, 'Dimension too high: d=%d' %len(B.shape) 340 341 return R 342 343 def csr_mv_python(self, B): 344 345 from Numeric import zeros, Float 346 347 if len(B.shape) == 1: 320 if len(B.shape) == 0: 321 #Scalar - use __rmul__ method 322 R = B*self 323 324 elif len(B.shape) == 1: 348 325 #Vector 349 326 assert B.shape[0] == self.N, 'Mismatching dimensions' 350 327 351 328 R = zeros(self.M, Float) #Result 352 329 353 330 #Multiply nonzero elements 354 331 for i in range(self.M): … … 356 333 j = self.colind[ckey] 357 334 R[i] += self.data[ckey]*B[j] 358 359 return R 335 336 elif len(B.shape) == 2: 337 338 R = zeros((self.M, B.shape[1]), Float) #Result matrix 339 340 #Multiply nonzero elements 341 342 for col in range(R.shape[1]): 343 #For each column 344 for i in range(self.M): 345 for ckey in range(self.row_ptr[i],self.row_ptr[i+1]): 346 j = self.colind[ckey] 347 R[i, col] += self.data[ckey]*B[j,col] 348 360 349 else: 361 350 raise ValueError, 'Dimension too high: d=%d' %len(B.shape) 362 351 363 352 return R 364 353 … … 367 356 if compile.can_use_C_extension('sparse_ext.c'): 368 357 #Replace python version with c implementations 369 from sparse_ext import csr_mv 358 from sparse_ext import csr_mv as csr_mv 370 359 371 360 … … 428 417 429 418 print C.todense() 419 420 C = Sparse_CSR(C) 421 422 y = C*[6,14,4] 423 424 print y 425 426 y2 = C*[[6,4],[4,28],[4,8]] 427 428 print y2 -
inundation/ga/storm_surge/pyvolution/sparse_ext.c
r589 r594 16 16 int _csr_mv(int M, 17 17 double* data, 18 int* colind,19 int* row_ptr,18 long* colind, 19 long* row_ptr, 20 20 double* x, 21 21 double* y) { 22 22 23 inti, j, ckey;23 long i, j, ckey; 24 24 25 25 for (i=0; i<M; i++ ) … … 40 40 41 41 PyObject *csr_sparse, // input sparse matrix 42 * R; // output wrapped vector42 *xin, *R; // output wrapped vector 43 43 44 44 PyArrayObject … … 48 48 *x, //Input vector array 49 49 *y; //Return vector array 50 51 double *xdata; 50 52 51 53 int dimensions[1], M, err; 52 54 53 55 // Convert Python arguments to C 54 if (!PyArg_ParseTuple(args, "OO", &csr_sparse, &x ))56 if (!PyArg_ParseTuple(args, "OO", &csr_sparse, &xin)) 55 57 return NULL; 58 59 x = (PyArrayObject*) PyArray_ContiguousFromObject(xin,PyArray_DOUBLE,1,2); 60 if (!x) 61 return NULL; 62 63 /* printf("x.nd = %i\n",x->nd); */ 64 /* printf("x.descr->type_num = %i %i\n",x->descr->type_num,PyArray_LONG); */ 65 /* printf("x.dimensions[0] = %i\n",x->dimensions[0]); */ 66 /* printf("x.data[0] = %g\n",((double*) x->data)[0]); */ 67 /* printf("x.data[1] = %g\n",((double*) x->data)[1]); */ 68 /* printf("x.data[2] = %g\n",((double*) x->data)[2]); */ 69 /* printf("x.data[3] = %g\n",((double*) x->data)[3]); */ 70 /* printf("x.data[4] = %g\n",((double*) x->data)[4]); */ 71 /* printf("x.data[5] = %g\n",((double*) x->data)[5]); */ 72 73 56 74 57 75 … … 61 79 return NULL; 62 80 81 63 82 colind = (PyArrayObject*) 64 83 PyObject_GetAttrString(csr_sparse, "colind"); 65 84 if (!colind) return NULL; 66 85 67 86 row_ptr = (PyArrayObject*) 68 87 PyObject_GetAttrString(csr_sparse, "row_ptr"); … … 77 96 err = _csr_mv(M, 78 97 (double*) data -> data, 79 ( int*) colind -> data,80 ( int*) row_ptr -> data,98 (long*) colind -> data, 99 (long*) row_ptr -> data, 81 100 (double*) x -> data, 82 101 (double*) y -> data); -
inundation/ga/storm_surge/pyvolution/test_cg_solve.py
r586 r594 81 81 """Standard 2d laplacian""" 82 82 83 n = 5084 m = 5083 n = 100 84 m = 100 85 85 86 86 A = Sparse(m*n, m*n) … … 106 106 #print 'finish covert' 107 107 b = A*xe 108 x = conjugate_gradient(A,b,b,iprint= 10)108 x = conjugate_gradient(A,b,b,iprint=20) 109 109 110 110 assert allclose(x,xe)
Note: See TracChangeset
for help on using the changeset viewer.