Python 使用单链表实现简单的稀疏矩阵
2015-01-23 17:35
926 查看
#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-23 @author: beyondzhou @name: SparseMatrix.py ''' # Implementation of the Sparse Matrix ADT using an array of linked lists from myarray import Array class SparseMatrix: # Cleates a sparse matrix of size numRows * numCols initialized to 0 def __init__(self, numRows, numCols): self._numCols = numCols self._listOfRows = Array(numRows) # Returns the number of rows in the matrix def numRows(self): return len(self._listOfRows) # Returns the number of columns in the matrix def numCols(self): return self._numCols # Returns the value of element (i,j) def __getitem__(self, ndxTuple): pass # Sets the value of element (i,j) to the value s def __setitem__(self, ndxTuple, value): predNode = None curNode = self._listOfRows[ndxTuple[0]] while curNode is not None and curNode.col != ndxTuple[1]: predNode = curNode curNode = curNode.next # See if the element is in the list if curNode is not None and curNode.col == ndxTuple[1]: if value == 0.0: if curNode == self._listOfRows[ndxTuple[0]]: self._listOfRows[ndxTuple[0]] = curNode.next else: predNode.next = curNode.next else: curNode.value = value # Otherwise, the element is not in the list elif value != 0.0: newNode = _MatrixElementNode(ndxTuple[1], value) newNode.next = curNode if curNode == self._listOfRows[ndxTuple[0]]: self._listOfRows[ndxTuple[0]] = newNode else: predNode.next = newNode # Scales the matrix by the given scalar def scaleBy(self, scalar): for row in range(self.numRows()): curNode = self._listOfRows[row] while curNode is not None: curNode.value *= scalar curNode = curNode.next # Creates and returns a new matrix that is the transpose of this matrix def transpose(self): pass # Matrix addition: newMatrix = self + rhsMatrix def __add__(self, rhsMatrix): # Make sure the two matrices have the correct size assert rhsMatrix.numRows() == self.numRows() and \ rhsMatrix.numCols() == self.numCols(), \ "Matrix sizes not comatable for adding." # Create a new sparse matrix of the same size newMatrix = SparseMatrix(self.numRows(), self.numCols()) # Add the elements of this matrix to the new matrix for row in range(rhsMatrix.numRows()): curNode = rhsMatrix._listOfRows[row] while curNode is not None: value = newMatrix[row, curNode.col] value += curNode.value newMatrix[row, curNode.col] = value curNode = curNode.next # Return the new matrix return newMatrix # Matrix subtraction def __sub__(self, rhsMatrix): # Make sure the two matrices have the correct size assert rhsMatrix.numRows() == self.numRows() and \ rhsMatrix.numCols() == self.numCols(), \ "Matrix sizes not comatable for adding." # Create a new sparse matrix of the same size newMatrix = SparseMatrix(self.numRows(), self.numCols()) # Add the elements of this matrix to the new matrix for row in range(rhsMatrix.numRows()): curNode = rhsMatrix._listOfRows[row] while curNode is not None: value = newMatrix[row, curNode.col] value -= curNode.value newMatrix[row, curNode.col] = value curNode = curNode.next # Return the new matrix return newMatrix # Multiplication def __mul__(self, rhsMatrix): pass # Storage class for creating matrix element nodes class _MatrixElementNode: def __init__(self, col, value): self.col = col self.value = value self.next = None
相关文章推荐
- python数据结构学习笔记-2016-10-28-02-使用链表实现稀疏矩阵
- Python 实现简单的稀疏矩阵
- python下学生管理系统:从文件中读取30位学生的信息(含邮箱),并实现简单的增、删、查找、统计(邮箱使用人数)。---附程序哦!
- python —— 使用logging模块简单实现日志系统
- Python 使用list实现简单的集合set
- 使用Python的Tornado框架实现一个简单的WebQQ机器人
- Python 使用单链表实现队列 (基于class, 包含迭代器)
- 使用python多线程实现一个简单spider
- 借助apktool.jar工具,使用python代码简化批量反编译apk安装包的简单实现
- Python 使用类实现简单的计时器
- 使用Python的Twisted框架实现一个简单的服务器
- python3使用tkinter实现ui界面简单实例
- Python使用稀疏矩阵节省内存实例
- Python 使用单链表实现多项式 (Polynomial)
- 用Python实现简单的HTTP服务器(1)--使用Firebug简单分析HTTP协议
- 简单爬虫python实现02——BeautifulSoup的使用
- Python单链表的简单实现方法
- 在Python3下使用tornado和SQLAlchemy实现一个简单的MVC网站
- Python中使用摄像头实现简单的延时摄影技术
- Python 使用单链表实现堆栈 (基于class, 包含迭代器)