Python 使用单链表实现简单的稀疏矩阵

#!/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值