# File: TestSparseMatrix.py # Description: Sparse matrix representation has a 1-D list where each # element in that list is a linked list having the column # number and non-zero data in each link # Student Name: # Student UT EID: # Partner Name: # Partner UT EID: # Course Name: CS 313E # Unique Number: # Date Created: # Date Last Modified: class Link (object): def __init__ (self, col = 0, data = 0, next = None): self.col = col self.data = data self.next = next # return a String representation of a Link (col, data) def __str__ (self): s = '' return s class LinkedList (object): def __init__ (self): self.first = None def insert_last (self, col, data): new_link = Link (col, data) current = self.first if (current == None): self.first = new_link return while (current.next != None): current = current.next current.next = new_link # return a String representation of a LinkedList def __str__ (self): s = '' return s class Matrix (object): def __init__ (self, row = 0, col = 0): self.row = row self.col = col self.matrix = [] # perform assignment operation: matrix[row][col] = data def set_element (self, row, col, data): return # add two sparse matrices def __add__ (self, other): return # multiply two sparse matrices def __mul__ (self, other): return # return a list representing a row with the zero elements inserted def get_row (self, n): return # return a list representing a column with the zero elements inserted def get_col (self, n): return # return a String representation of a matrix def __str__ (self): s = '' return s def read_matrix (in_file): line = in_file.readline().rstrip("\n").split() row = int (line[0]) col = int (line[1]) mat = Matrix (row, col) for i in range (row): line = in_file.readline().rstrip("\n").split() new_row = LinkedList() for j in range (col): elt = int (line[j]) if (elt != 0): new_row.insert_last(j, elt) mat.matrix.append (new_row) line = in_file.readline() return mat def main(): in_file = open ("./matrix.txt", "r") print ("Test Matrix Addition") matA = read_matrix (in_file) print (matA) matB = read_matrix (in_file) print (matB) matC = matA + matB print (matC) print ("\nTest Matrix Multiplication") matP = read_matrix (in_file) print (matP) matQ = read_matrix (in_file) print (matQ) matR = matP * matQ print (matR) print "\nTest Setting a Zero Element to a Non-Zero Value" matA.set_element (1, 1, 5) print (matA) print "\nTest Setting a Non-Zero Elements to a Zero Value" matB.set_element (1, 1, 0) print (matB) print "\nTest Getting a Row" row = matP.get_row(1) print (row) print "\nTest Getting a Column" col = matQ.get_col(0) print (col) in_file.close() main()