Python 实现简单的矩阵
来源:互联网 发布:gedit ubuntu 编辑:程序博客网 时间:2024/05/21 06:34
#!/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 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 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.0891226289712 The primary value of fmatrix0.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 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
0 0
- Python 实现简单的矩阵
- Python 实现简单的稀疏矩阵
- python实现的简单图像矩阵变换
- Python 使用单链表实现简单的稀疏矩阵
- 简单的稀疏矩阵实现
- 简单的实现螺旋矩阵
- 简单的螺旋矩阵的实现
- tensorflow实现简单的矩阵相乘
- Leetcode: 在原矩阵上实现矩阵的旋转(Python)
- python实现矩阵求行列式的值
- python实现螺旋矩阵的填充
- 矩阵的螺旋排列 Python实现
- Python实现矩阵相乘
- python 实现矩阵相乘
- python 排序的简单实现
- Python实现简单的打包
- Python 实现简单的通讯录
- Python实现的简单计算器
- PL/SQL Developer导入导出数据库方法及说明
- [ ECUG 专题回顾]《再谈 CERL:详论 GO 与 ERLANG 的并发编程模型差异》-许式伟(七牛云存储 CEO)
- sqlite 查询导出文件的方法
- Coding 移动端客户端代码开源
- 分享国外知名的源代码网站
- Python 实现简单的矩阵
- 归并排序(MergeSort)
- find -exec 与 find - xargs 的差别
- Oracle Listener 动态注册 与 静态注册
- FCARM - Output Name not specified, please check 'Options for Target - Utilities'解决方法
- go语言环境搭建
- HTML5 history API,创造更好的浏览体验
- HTML5实例教程——制作酷炫音频播放器插件
- 我们很少有机会看到一个人的所有面