#!/usr/bin/python #-*-Coding:utf-8-*-"' Created on 2015-1-7@author:beyondzhou@name:myarray.py '" # Implementation of The matrix ADT using a 2D arrayfrom myarray import array2dclass Matrix: # Creates a matrix of size numrows * numcols I nitialized to 0 def __init__ (self, NumRows, numcols): Self._thegrid = Array2D (NumRows, Numcols) self._the Grid.clear (0) # Returns the number of rows in the Matrix Def NumRows (self): return self._thegrid.numrows () # Returns The number of columns in The Matrix Def Numcols (self): return Self._thegrid.numcols () # Returns th E Value of Element (I, J): X[i, J] def __getitem__ (self, ndxtuple): Return self._thegrid[ndxtuple[0], ndxtuple[1 ] # Sets the value of element (I,J) to the value s:x[i, j] = s def __setitem__ (self, ndxtuple, scalar): sel F._thegrid[ndxtuple[0], ndxtuple[1]] = scalar # Scales The matrix by the given scalar def scaleby (self, scalar): for R in range (SELF.NUmrows ()): For C in range (Self.numcols ()): Self[r,c] *= scalar # Creates and returns a new MA Trix that's the transpose of this matrix Def transpose (self): # Create the new matrix Newmatrix = Matrix (Self.numcols (), Self.numrows ()) # ADD the corresponding elements in the and the matrices for R in range (self.nu Mrows ()): For C in range (Self.numcols ()): newmatrix[c,r] = Self[r,c] return Newmatrix # creates and returns a new matrix that results from the matrix addition def __add__ (self, Rhsmatrix): Assert Rhsma Trix.numrows () = = Self.numrows () and rhsmatrix.numcols () = = Self.numcols (), "Matrix size s not compatible for the add operation. " # Create the new matrix Newmatrix = Matrix (Self.numrows (), Self.numcols ()) # ADD the corresponding elements In the and matrices for R in range (Self.numrows ()): For C in range (Self.numcols ()): Newmatrix[r,c] = Self[r,c] + rhsmatrix[r,c] Return Newmatrix # Creates and returns a new Matr IX that results from the matrix Sub def __sub__ (self, Rhsmatrix): Assert rhsmatrix.numrows () = = Self.numrows () and Rhsmatrix.numcols () = = Self.numcols (), "Matrix sizes not compatible for the add operation ." # Create the new matrix Newmatrix = Matrix (Self.numrows (), Self.numcols ()) # ADD the corresponding elements In the and matrices for R in range (Self.numrows ()): For C in range (Self.numcols ()): newma TRIX[R,C] = Self[r,c]-rhsmatrix[r,c] Return Newmatrix # Creates and returns a new matrix resulting from matrix Multiplcation def __mul__ (self, Rhsmatrix): Assert rhsmatrix.numrows () = = Self.numcols (), "Ma Trix Sizes not compatible for the multi operation. " # Create the new matrix Newmatrix = Matrix (Self.numrows (), RHSMATRIX.NUmcols ()) # Mul the corresponding elements in the and the matrices for R in range (Self.numrows ()): For C in range (Rhsmatrix.numcols ()): MySum = 0.0 for k in range (Self.numcols ()): MySum + = self[r,k] * Rhsmatrix[k,r] newmatrix[r,c] = mysum return Newmatrix
#!/usr/bin/python #-*-Coding:utf-8-*-"Created on 2015-1-7@author:beyondzhou@name:test_matrix.py" Def Test_ Matrix (): # import from Mymatrix import matrix import Random # Set default value for Matrix Amatrix = Ma Trix (2,3) Bmatrix = Matrix (2,3) Fmatrix = Matrix (3,2) for I in Range (Amatrix.numrows ()): for J in Range (AM Atrix.numcols ()): amatrix[i,j] = Random.random () bmatrix[i,j] = Random.random () for I in range (FM Atrix.numrows ()): for J in Range (Fmatrix.numcols ()): fmatrix[i,j] = Random.random () print ' The primary value of Amatrix ' for I in Range (Amatrix.numrows ()): for J in Range (Amatrix.numcols ()): print '%s '% amatrix[i,j], print ' \ R ' print ' \nthe primary value of Bmatrix ' for I in range (BM Atrix.numrows ()): for J in Range (Bmatrix.numcols ()): print '%s '% bmatrix[i,j], print ' \ R ' print ' \nthe primary vAlue of Fmatrix ' for I in Range (Fmatrix.numrows ()): for J in Range (Fmatrix.numcols ()): print '%s '% FMATRIX[I,J], print ' \ R ' # Add Amatrix and Bmatrix to Cmatrix Cmatrix = Amatrix + Bmatrix print ' \nthe value of Cmatrix (Amatrix + bmatrix) ' For I in Range (Cmatrix.numrows ()): for J in Range (Cmatrix.numcols ()) : print '%s '% cmatrix[i,j], print ' \ R ' # sub Amatrix and Bmatrix to Dmatrix Dmatrix = Amatri X-bmatrix print ' \nthe value of Dmatrix (Amatrix-bmatrix) ' For I in Range (Dmatrix.numrows ()): for J I N Range (Dmatrix.numcols ()): print '%s '% dmatrix[i,j], print ' \ R ' # Mul Amatrix and Fmatrix t o Ematrix ematrix = Amatrix * Fmatrix print ' \nthe value of Ematrix (Amatrix * fmatrix) ' For I in range (Ematr Ix.numrows ()): for J in Range (Ematrix.numcols ()): print '%s '% ematrix[i,j], print ' \ R '# Scale the Amatrix by 3 Amatrix.scaleby (3) print ' \nthe scale value of Amatrix ' for I in range (AMATRIX.NUMRO WS ()): for J in Range (Amatrix.numcols ()): print '%s '% amatrix[i,j], print ' \ R ' # Transpose the Amatrix Dmatrix = Amatrix.transpose () print ' \nthe transpose value of Amatrix ' for I in range (Dmatrix.numrows ()): for J in Range (Dmatrix.numcols ()): print '%s '% dmatrix[i,j], print ' \ R ' if __name__ = = "__main__": Test_matrix ()
Result:
The primary value of amatrix0.886197406941 0.304295996721 0.293469382347 0.154702139448 0.511075267985 0.387057640727 The primary value of bmatrix0.574674206609 0.364815615899 0.493367650314 0.438101377839 0.801271107474 0.0891226289 712 The primary value of fmatrix0.00716087704081 0.537519043084 0.451888654276 0.234306298527 0.572987747957 0.47905 9183861 the value of Cmatrix (Amatrix + bmatrix) 1.46087161355 0.66911161262 0.78683703266 0.592803517287 1.3123463754 6 0.476180269699 The value of Dmatrix (Amatrix-bmatrix) 0.311523200332-0.0605196191784-0.199898267967-0.283399238 391-0.290195839489 0.297935011756 The value of Ematrix (Amatrix * fmatrix) 0.31200821961 0.31200821961 0.388327017743 0.388327017743 the scale value of amatrix2.65859222082 0.912887990162 0.88040814704 0.464106418343 1.53322580395 1 .16117292218 the transpose value of amatrix2.65859222082 0.464106418343 0.912887990162 1.53322580395 0.88040814704 1 .16117292218
Python implements a simple matrix