Changeset 475
- Timestamp:
- Nov 1, 2004, 3:30:45 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/cg_solve.py
r467 r475 3 3 4 4 5 def conjugate_gradient(A,b,x0 ,imax=10000,tol=1.0e-8,iprint=0):5 def conjugate_gradient(A,b,x0=None,imax=10000,tol=1.0e-8,iprint=0): 6 6 """ 7 7 Try to solve linear equation Ax = b using … … 10 10 Input 11 11 A: matrix or function which applies a matrix, assumed symmetric 12 A can be either dense or sparse 12 13 b: right hand side 13 x0: inital guess 14 x0: inital guess (default the 0 vector) 14 15 imax: max number of iterations 15 16 tol: tolerance used for residual … … 19 20 """ 20 21 21 #if nargin<3 22 # x0 = zeros(b); 23 #end 24 25 from Numeric import dot, asarray, Float 26 #from operator import mod 27 28 b = asarray(b, typecode=Float) 29 x0 = asarray(x0, typecode=Float) 30 #A = asarray(A, typecode=Float) 31 32 #print "A shape",A.shape 33 #print "b shape",len(b.shape) 34 #print "x0 shape",x0.shape[0] 22 from Numeric import dot, array, Float, zeros 23 24 b = array(b, typecode=Float) 25 26 if x0 is None: 27 x0 = zeros(b.shape, typecode=Float) 28 else: 29 x0 = array(x0, typecode=Float) 30 35 31 36 32 if len(b.shape) != 1 : 37 33 raise VectorShapeError, 'input vector should consist of only one column' 38 34 35 #FIXME: Should test using None 39 36 if iprint == 0: 40 37 iprint = imax -
inundation/ga/storm_surge/pyvolution/test_cg_solve.py
r454 r475 3 3 import unittest 4 4 5 from Matrix import * 5 6 6 from Numeric import dot, allclose, array, transpose, arange, ones, Float 7 7 from cg_solve import * 8 from s cipy import sparse, mat8 from sparse import Sparse 9 9 10 10 11 11 class TestCase(unittest.TestCase): 12 13 ## def test_solve(self):14 ## """Small Matrix"""15 16 ## A = [[2.0, -1.0, 0.0, 0.0 ],17 ## [-1.0, 2.0, -1.0, 0.0],18 ## [0.0, -1.0, 2.0, -1.0],19 ## [0.0, 0.0, -1.0, 2.0]]20 21 ## A = mat(A)22 23 ## xe = asarray([0.0, 1.0, 2.0, 3.0])24 ## b = A*xe25 ## x = asarray([0.0, 0.0, 0.0, 0.0])26 27 ## print A28 ## print xe29 ## print b30 ## print b.shape31 32 ## x = conjugate_gradient(A,b,x,iprint=1)33 34 ## assert allclose(x,xe)35 12 36 13 def test_sparse_solve(self): … … 42 19 [0.0,0.0, -1.0, 2.0]] 43 20 44 A = sparse.dok_matrix(A)21 A = Sparse(A) 45 22 46 23 xe = [0.0, 1.0, 2.0, 3.0] … … 56 33 """Standard 1d laplacian """ 57 34 58 A = sparse.dok_matrix()59 60 35 n = 50 36 A = Sparse(n,n) 37 61 38 for i in arange(0,n): 62 39 A[i,i] = 1.0 … … 76 53 """Standard 2d laplacian""" 77 54 78 A = sparse.dok_matrix()79 80 55 n = 20 81 56 m = 10 57 58 A = Sparse(m*n, m*n) 59 82 60 for i in arange(0,n): 83 61 for j in arange(0,m): … … 101 79 102 80 81 def test_solve_large_2d_with_default_guess(self): 82 """Standard 2d laplacian using default first guess""" 83 84 n = 20 85 m = 10 86 87 A = Sparse(m*n, m*n) 88 89 for i in arange(0,n): 90 for j in arange(0,m): 91 I = j+m*i 92 A[I,I] = 4.0 93 if i > 0 : 94 A[I,I-m] = -1.0 95 if i < n-1 : 96 A[I,I+m] = -1.0 97 if j > 0 : 98 A[I,I-1] = -1.0 99 if j < m-1 : 100 A[I,I+1] = -1.0 101 102 xe = ones( (n*m,), Float) 103 104 b = A*xe 105 x = conjugate_gradient(A,b) 106 107 assert allclose(x,xe) 108 109 103 110 def test_vector_shape_error(self): 104 111 """Raise VectorShapeError""" … … 109 116 [0.0,0.0, -1.0, 2.0]] 110 117 111 A = sparse.dok_matrix(A)118 A = Sparse(A) 112 119 113 120 xe = [[0.0,2.0], [1.0,3.0], [2.0,4.0], [3.0,2.0]] 114 b = A*xe115 x = [0.0, 0.0, 0.0, 0.0]116 121 117 122 try: 118 x = conjugate_gradient(A, b,b,iprint=0)123 x = conjugate_gradient(A,xe,xe,iprint=0) 119 124 except: 120 125 pass -
inundation/ga/storm_surge/pyvolution/test_least_squares.py
r466 r475 671 671 if __name__ == "__main__": 672 672 suite = unittest.makeSuite(TestCase,'test') 673 runner = unittest.TextTestRunner( ) #(verbosity=2)673 runner = unittest.TextTestRunner(verbosity=1) 674 674 runner.run(suite) 675 675
Note: See TracChangeset
for help on using the changeset viewer.