现在的位置: 主页 > 新闻中心 > 文章列表

Python 实现简单的矩阵

作者:沈阳市沈河区金瑞龙美术社 来源:www.jrlgg.com 发布时间:2017-09-07 13:30:07
 

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:

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:十堰SEO http://shiyan.raoyu.net

  • 上一篇:用Jasper report实现横向分栏报表
  • 下一篇:最后一页
  •