Ignore:
Timestamp:
Oct 26, 2004, 11:55:00 PM (20 years ago)
Author:
steve
Message:

Made some quick and dirty additions to sparse.

File:
1 edited

Legend:

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

    r447 r452  
    11"""Proof of concept sparse matrix code
    22"""
    3 
    4 
     3from scipy_base import *
     4from cg_solve import conjugate_gradient, VectorShapeError
    55
    66class Sparse:
     
    1212        self.M = M
    1313        self.N = N
     14        self.shape = (M,N)
    1415        self.A = {}
    1516
     
    4041            return 0.0
    4142
     43    def copy(self):
     44
     45        new = Sparse(self.M,self.N)
     46
     47        for key in self.A.keys():
     48            i, j = key
     49
     50            new[i,j] = self.A[i,j]
     51
     52        return new
     53
    4254
    4355    def todense(self):
     
    4961            for j in range(self.N):
    5062                if self.A.has_key( (i,j) ):               
    51                     D[i, j] = A[ (i,j) ]
     63                    D[i, j] = self.A[ (i,j) ]
    5264        return D
    5365
     
    6779
    6880        #Assume numeric types from now on
    69         R = zeros(B.shape, Float) #Result
     81        R = zeros((self.M,), Float) #Result
    7082       
    7183        if len(B.shape) == 1:
    7284            #Vector
    7385
     86##             print 'B.shape      ',B.shape
     87##             print 'self.shape ',self.shape
     88           
    7489            assert B.shape[0] == self.N, 'Mismatching dimensions'
    7590
     
    7893                i, j = key
    7994
    80                 R[i] += A[key]*B[j]
    81 
    82         else:
    83             raise 'Numeric matrix not yet implemented'
     95                R[i] += self.A[key]*B[j]
     96
     97        else:
     98            raise ValueError, 'Numeric matrix not yet implemented'
    8499
    85100        return R
    86        
    87 
     101
     102    def __add__(self, other):
     103        """Add this matrix onto 'other'
     104        """
     105
     106        from Numeric import array, zeros, Float
     107       
     108        new = other.copy()
     109
     110 #       print 'self.shape',self.shape
     111 #       print 'other.shape',other.shape
     112       
     113        for key in self.A.keys():
     114            i, j = key
     115
     116            new[i,j] = new[i,j] + self.A[key]
     117
     118        return new
     119
     120
     121    def __rmul__(self, other):
     122        """Right multiply this matrix with scalar
     123        """
     124
     125        from Numeric import array, zeros, Float
     126       
     127        if isscalar(other):
     128            new = self.copy()
     129            #Multiply nonzero elements
     130            for key in new.A.keys():
     131                i, j = key
     132
     133                new.A[key] = other*new.A[key]
     134        else:
     135            raise 'only right multiple with scalar implemented'
     136
     137#        print 'new.shape',new.shape
     138
     139        return new
     140
     141
     142    def trans_mult(self, other):
     143        """Multiply the transpose of  matrix with 'other' which can be
     144        a Numeric vector.
     145        """
     146
     147        from Numeric import array, zeros, Float
     148       
     149        try:
     150            B = array(other)
     151        except:
     152            print 'FIXME: Only Numeric types implemented so far'
     153
     154
     155        #Assume numeric types from now on
     156
     157       
     158        if len(B.shape) == 1:
     159            #Vector
     160
     161            assert B.shape[0] == self.M, 'Mismatching dimensions'
     162
     163            R = zeros((self.N,), Float) #Result
     164
     165            #Multiply nonzero elements
     166            for key in self.A.keys():
     167                i, j = key
     168
     169                R[j] += self.A[key]*B[i]
     170
     171        else:
     172            raise 'Can only multiply with 1d array'
     173
     174        return R
     175
     176
     177   
     178   
    88179if __name__ == '__main__':
    89180
     
    122213    assert allclose(u, [6,14,4])
    123214
     215    u = A.trans_mult(v)
     216    print u
     217    assert allclose(u, [6,6,10])
     218
    124219    #Right hand side column
    125220    v = array([[2,4],[3,4],[4,4]])
     
    130225    #u = A*v[:,1]
    131226    #print u
     227    print A.shape
     228
     229    B = 3*A
     230    print B.todense()
     231
     232    B[1,0] = 2
     233
     234    C = A+B
     235
     236    print C.todense()
Note: See TracChangeset for help on using the changeset viewer.