首页 > 代码库 > Python 实现简单的矩阵
Python 实现简单的矩阵
#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-7 @author: beyondzhou @name: myarray.py ''' # Implementation of the Matrix ADT using a 2D array from myarray import Array2D class Matrix: # Creates a matrix of size numRows * numCols initialized to 0 def __init__(self, numRows, numCols): self._theGrid = Array2D(numRows, numCols) self._theGrid.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 the 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): self._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 matrix that is the transpose of this matrix def transpose(self): # Create the new matrix newMatrix = Matrix(self.numCols(), self.numRows()) # Add the corresponding elements in the two matrices for r in range(self.numRows()): for c in range(self.numCols()): newMatrix[c,r] = self[r,c] return newMatrix # Creates and returns a new matrix that results from matrix addition def __add__(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 two 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 matrix that results from 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 two 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 matrix resulting from matrix multiplcation def __mul__(self, rhsMatrix): assert rhsMatrix.numRows() == self.numCols(), "Matrix sizes not compatible for the multi operation." # Create the new matrix newMatrix = Matrix(self.numRows(), rhsMatrix.numCols()) # Mul the corresponding elements in the two 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 = Matrix(2,3) bMatrix = Matrix(2,3) fMatrix = Matrix(3,2) for i in range(aMatrix.numRows()): for j in range(aMatrix.numCols()): aMatrix[i,j] = random.random() bMatrix[i,j] = random.random() for i in range(fMatrix.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(bMatrix.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 = aMatrix - bMatrix print '\nThe value of dMatrix (aMatrix - bMatrix)' for i in range(dMatrix.numRows()): for j in range(dMatrix.numCols()): print '%s ' % dMatrix[i,j], print '\r' # Mul amatrix and fMatrix to ematrix eMatrix = aMatrix * fMatrix print '\nThe value of eMatrix (aMatrix * fMatrix)' for i in range(eMatrix.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.numRows()): 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 amatrix 0.886197406941 0.304295996721 0.293469382347 0.154702139448 0.511075267985 0.387057640727 The primary value of bmatrix 0.574674206609 0.364815615899 0.493367650314 0.438101377839 0.801271107474 0.0891226289712 The primary value of fmatrix 0.00716087704081 0.537519043084 0.451888654276 0.234306298527 0.572987747957 0.479059183861 The value of cMatrix (aMatrix + bMatrix) 1.46087161355 0.66911161262 0.78683703266 0.592803517287 1.31234637546 0.476180269699 The value of dMatrix (aMatrix - bMatrix) 0.311523200332 -0.0605196191784 -0.199898267967 -0.283399238391 -0.290195839489 0.297935011756 The value of eMatrix (aMatrix * fMatrix) 0.31200821961 0.31200821961 0.388327017743 0.388327017743 The scale value of amatrix 2.65859222082 0.912887990162 0.88040814704 0.464106418343 1.53322580395 1.16117292218 The transpose value of amatrix 2.65859222082 0.464106418343 0.912887990162 1.53322580395 0.88040814704 1.16117292218
Python 实现简单的矩阵
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。