Changeset 2841
- Timestamp:
- May 10, 2006, 4:08:08 PM (19 years ago)
- Location:
- inundation/utilities
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/utilities/cg_solve.py
r2731 r2841 3 3 class ConvergenceError(exceptions.Exception): pass 4 4 5 from Numeric import dot, array, Float, zeros 6 5 7 import logging, logging.config 6 8 logger = logging.getLogger('cg_solve') … … 13 15 14 16 def conjugate_gradient(A,b,x0=None,imax=10000,tol=1.0e-8,iprint=0): 17 """ 18 Try to solve linear equation Ax = b using 19 conjugate gradient method 20 21 If b is an array, solve it as if it was a set of vectors, solving each 22 vector. 23 """ 24 25 if x0 is None: 26 x0 = zeros(b.shape, typecode=Float) 27 else: 28 x0 = array(x0, typecode=Float) 29 30 b = array(b, typecode=Float) 31 if len(b.shape) != 1 : 32 33 for i in range(b.shape[1]): 34 x0[:,i] = _conjugate_gradient(A, b[:,i], x0[:,i], 35 imax, tol, iprint) 36 else: 37 x0 = _conjugate_gradient(A, b, x0, imax, tol, iprint) 38 39 return x0 40 41 def _conjugate_gradient(A,b,x0=None,imax=10000,tol=1.0e-8,iprint=0): 15 42 """ 16 43 Try to solve linear equation Ax = b using … … 29 56 """ 30 57 31 from Numeric import dot, array, Float, zeros32 58 33 59 b = array(b, typecode=Float) … … 75 101 76 102 return x 103 -
inundation/utilities/test_cg_solve.py
r2731 r2841 8 8 from Numeric import dot, allclose, array, transpose, arange, ones, Float 9 9 from utilities.cg_solve import * 10 from utilities.cg_solve import _conjugate_gradient 10 11 from utilities.sparse import Sparse, Sparse_CSR 11 12 … … 178 179 179 180 try: 180 x = conjugate_gradient(A,xe,xe,iprint=0)181 x = _conjugate_gradient(A,xe,xe,iprint=0) 181 182 except VectorShapeError: 182 183 pass … … 185 186 raise TestError, msg 186 187 188 189 def test_sparse_solve_matrix(self): 190 """Solve Small Sparse Matrix""" 191 192 A = [[2.0, -1.0, 0.0, 0.0 ], 193 [-1.0, 2.0, -1.0, 0.0], 194 [0.0, -1.0, 2.0, -1.0], 195 [0.0,0.0, -1.0, 2.0]] 196 197 A = Sparse(A) 198 199 xe = [[0.0, 0.0],[1.0, 1.0],[2.0 ,2.0],[3.0, 3.0]] 200 b = A*xe 201 x = [[0.0, 0.0],[0.0, 0.0],[0.0 ,0.0],[0.0, 0.0]] 202 x = conjugate_gradient(A,b,x,iprint=0) 203 204 assert allclose(x,xe) 187 205 188 206 #-------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.