python数据结构学习笔记-2016-10-15-01-矩阵ADT

       2.4 矩阵ADT

       矩阵是一个大小为m×n的数值表,包含如下方法:

  • Matrix(rows, cols):创建一个rows×cols的矩阵,并将其内所有数值初始化为0;
  • numRows():返回矩阵的行数;
  • numCols():返回矩阵的列数;
  • getitem(row, col):返回row行col列的数值;
  • setitem(row, col, scalar):将row行col列的数值设定为scalar;
  • scaleBy(scalar):数乘运算;
  • tanspose():返回转置矩阵;
  • add(other):加法;
  • substract(other):减法;
  • multiply(other):乘法。
        2.4.1 矩阵操作

       加法和减法,数乘,乘法,转置,具体百度或Google就行。

        2.4.2 矩阵的实现

#-*-coding: utf-8-*-

# 矩阵的实现

from myarray2d import Array2D

class Matrix(object):
    def __init__(self, numRows, numCols):
        self._theGrid = Array2D(numRows, numCols)
        self._theGrid.clear(0) # 将矩阵中所有数初始化为零

    def numRows(self):
        return self._theGrid.numRows()

    def numCols(self):
        return self._theGrid.numCols()

    def __getitem__(self, ndxTuple):
        return self._theGrid[ndxTuple[0], ndxTuple[1]]

    def __setitem__(self, ndxTuple, scalar):
        self._theGrid[ndxTuple[0], ndxTuple[1]] = scalar

    # 数乘运算
    def scaleBy(self, scalar):
        for r in range(self.numRows()):
            for c in range(self.numCols()):
                self[r, c] *= scalar

    # 转置运算
    def transpose(self):
        newMatrix = Matrix(self.numCols(), self.numRows())
        for i in range(self.numRows()):
            for j in range(self.numCols()):
                newMatrix[j, i] = self[i, j]
        return newMatrix

    # 矩阵加法
    def __addd__(self, other): 
        assert other.numRows() == self.numRows() and other.numCols() == self.numCols(), "Matrix sizes not compatible for the add operation."
        newMatrix = Matrix(self.numRows(), self.numCols())
        for r in range(self.numRows()):
            for c in range(self.numCols()):
                newMatrix[r, c] = self[r, c] + other[r, c]
        return newMatrix

    # 矩阵减法
    def __sub__(self, other):
        assert other.numRows() == self.numRows() and other.numCols() == self.numCols(), "Matrix sizes not compatible for the substract operation."
        newMatrix = Matrix(self.numRows(), self.numCols())
        for r in range(self.numRows()):
            for c in range(self.numCols()):
                newMatrix[r, c] = self[r, c] - other[r, c]
        return newMatrix

    # 矩阵乘法
    def __mul__(self, other):
        assert other.numRows() == self.numCols(), "Matrix sizes not compatible for the multiple operation."
        newMatrix = Matrix(self.numRows(), other.numCols())
        for r in range(self.numRows()):
            for c in range(other.numCols()):
                newMatrix[r, c] = sum([self[r, i] * other[i, c] for i in range(self.numCols())])
        return newMatrix






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值