Changeset 474


Ignore:
Timestamp:
Nov 1, 2004, 3:17:49 PM (20 years ago)
Author:
ole
Message:

Added test_sparse.py unit test of sparse.py

Location:
inundation/ga/storm_surge/pyvolution
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/sparse.py

    r471 r474  
    22"""
    33
    4 #from scipy_base import * #Hardly worth importing scipy just to get isscalar.
    5 from cg_solve import conjugate_gradient, VectorShapeError
    64
    75class Sparse:
    86
    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
    1619        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
    1745
    1846    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   
    2159    def __setitem__(self, key, x):
    2260
     
    4381
    4482    def copy(self):
    45 
     83        #FIXME: Use the copy module instead
    4684        new = Sparse(self.M,self.N)
    4785
     
    81119        #Assume numeric types from now on
    82120        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:
    85126            #Vector
    86 
    87 ##             print 'B.shape      ',B.shape
    88 ##             print 'self.shape ',self.shape
    89            
    90127            assert B.shape[0] == self.N, 'Mismatching dimensions'
    91128
     
    95132
    96133                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)
    100138
    101139        return R
     140   
    102141
    103142    def __add__(self, other):
     
    108147       
    109148        new = other.copy()
    110 
    111  #       print 'self.shape',self.shape
    112  #       print 'other.shape',other.shape
    113        
    114149        for key in self.A.keys():
    115150            i, j = key
    116151
    117             new[i,j] = new[i,j] + self.A[key]
     152            new[i,j] += self.A[key]
    118153
    119154        return new
     
    129164            other = float(other)
    130165        except:
    131             raise 'only right multiple with scalar implemented'
     166            msg = 'Sparse matrix can only "right-multiply" onto a scalar'
     167            raise TypeError, msg
    132168        else:
    133169            new = self.copy()
     
    142178
    143179    def trans_mult(self, other):
    144         """Multiply the transpose of  matrix with 'other' which can be
     180        """Multiply the transpose of matrix with 'other' which can be
    145181        a Numeric vector.
    146182        """
     
    155191
    156192        #Assume numeric types from now on
    157 
    158        
    159193        if len(B.shape) == 1:
    160194            #Vector
  • inundation/ga/storm_surge/pyvolution/test_all.py

    r464 r474  
    1717#exclude = ['test_least_squares.py', 'test_cg_solve.py',
    1818          # 'test_interpolate_sww.py']
    19 exclude = ['test_cg_solve.py']
     19#exclude = ['test_cg_solve.py']
    2020
    2121
     
    3434
    3535
    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'
    3940       
    4041
Note: See TracChangeset for help on using the changeset viewer.