#!/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 使用单链表实现简单的稀疏矩阵
最新推荐文章于 2022-04-11 18:08:06 发布