Changeset 2841


Ignore:
Timestamp:
May 10, 2006, 4:08:08 PM (18 years ago)
Author:
duncan
Message:

cg_solve now works if b is an array

Location:
inundation/utilities
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/utilities/cg_solve.py

    r2731 r2841  
    33class ConvergenceError(exceptions.Exception): pass
    44
     5from Numeric import dot, array, Float, zeros
     6   
    57import logging, logging.config
    68logger = logging.getLogger('cg_solve')
     
    1315
    1416def 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   
     41def _conjugate_gradient(A,b,x0=None,imax=10000,tol=1.0e-8,iprint=0):
    1542   """
    1643   Try to solve linear equation Ax = b using
     
    2956   """
    3057
    31    from Numeric import dot, array, Float, zeros
    3258
    3359   b  = array(b, typecode=Float)
     
    75101
    76102   return x
     103
  • inundation/utilities/test_cg_solve.py

    r2731 r2841  
    88from Numeric import dot, allclose, array, transpose, arange, ones, Float
    99from utilities.cg_solve import *
     10from utilities.cg_solve import _conjugate_gradient
    1011from utilities.sparse import Sparse, Sparse_CSR
    1112
     
    178179
    179180        try:
    180             x = conjugate_gradient(A,xe,xe,iprint=0)
     181            x = _conjugate_gradient(A,xe,xe,iprint=0)
    181182        except VectorShapeError:
    182183            pass
     
    185186            raise TestError, msg
    186187
     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)
    187205
    188206#-------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.