7-4 稀疏矩阵加法的Python解决办法

稀疏矩阵的加法采用有序数对(组)的方式效率比较高
而Python在数据处理尤其是字符串处理方面要优于C,Java等
所以对于数据处理的认为我们一般(无其他要求时)使用Python

7-4 稀疏矩阵加法
给定两个矩阵A和B,求其和矩阵C=A+B。

输入格式:
第一行包含两个数Row和Col,分别表示矩阵的行数和列数,A和B的维度是一致的。

第二行只有一个数N
​1
​​ ,表示接下来要输入的A中的非零元素的个数。

接下来是N
​1
​​ 行,每一行都是i j A[i,j] 这样的形式,表示的A中第i行第j列的元素A[i,j],为了与大多数编程语言保持一致,它们都是从零开始的,也就是说下标的有效范围是[0,Row−1]×[0,Col−1]。

在N
​1
​​ 行之后,是一个数N
​2
​​ ,表示矩阵B中非零元素的数量,此后N
​2
​​ 行描述B中的非零元素,它们与此前描述A中非零元素的形式一致。

矩阵元素的输入均遵循行主序。这里的所有的输入均可用int类型正确表示,可以假设输入均是合法的。

输出格式:
第一行输出和矩阵C=A+B中的绝对值大于0.1的元素个数N
​3
​​ ,此后是N
​3
​​ 行,按照行主序输出其中的非零元素,依次是行、列的下标和对应的元素。

输入样例:
2 2
1
1 1 1
1
0 0 1
输出样例:
2
0 0 1
1 1 1

# 这里就题论题来说我们不需要知道矩阵的行列数,因为我们根本不是要创建矩阵
# 而是使用字典来实现两个矩阵同一位置的匹配
input()  
a = int(input())
dic = {
   }
list_ = []
# 执行第一轮录入
for i in range(0, a):
    b 
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
稀疏矩阵是指矩阵中大部分元素为0的矩阵。为了节省存储空间和提高运算效率,可以使用稀疏矩阵的存储方式——十字链表。十字链表是一种特殊的链表结构,它由行链表和列链表组成,每个节点同时属于行链表和列链表。在十字链表中,行链表和列链表的头节点分别为行头节点和列头节点,它们的数据域分别存储行号和列号。数据节点的数据域存储矩阵中的非零元素值,指针域分别指向同一行和同一列的下一个非零元素节点。 以下是Python实现稀疏矩阵十字链表的基本步骤: 1. 定义节点类,包括数据域和指针域。 2. 定义行头节点和列头节点。 3. 读入矩阵,创建数据节点,并将其插入到行链表和列链表中。 4. 实现矩阵加法、减法和乘法等运算。 具体实现细节可以参考下面的代码示例: ``` class MatNode: def __init__(self, data=0, row=0, col=0): self.data = data self.row = row self.col = col self.right = None self.down = None class SparseMatrix: def __init__(self, mat=[]): self.rows = len(mat) self.cols = len(mat[0]) self.rheads = [MatNode() for i in range(self.rows)] self.cheads = [MatNode() for i in range(self.cols)] for i in range(self.rows): for j in range(self.cols): if mat[i][j] != 0: node = MatNode(mat[i][j], i, j) self.insert(node) def insert(self, node): row, col = node.row, node.col rhead, chead = self.rheads[row], self.cheads[col] p, q = rhead, chead while p.right and p.right.col < col: p = p.right while q.down and q.down.row < row: q = q.down node.right = p.right node.down = q.down p.right = q.down = node def add(self, other): if self.rows != other.rows or self.cols != other.cols: return None res = SparseMatrix() for i in range(self.rows): p, q = self.rheads[i], other.rheads[i] while p.right and q.right: if p.right.col < q.right.col: res.insert(MatNode(p.right.data, i, p.right.col)) p = p.right elif p.right.col > q.right.col: res.insert(MatNode(q.right.data, i, q.right.col)) q = q.right else: res.insert(MatNode(p.right.data + q.right.data, i, p.right.col)) p, q = p.right, q.right while p.right: res.insert(MatNode(p.right.data, i, p.right.col)) p = p.right while q.right: res.insert(MatNode(q.right.data, i, q.right.col)) q = q.right return res def sub(self, other): if self.rows != other.rows or self.cols != other.cols: return None res = SparseMatrix() for i in range(self.rows): p, q = self.rheads[i], other.rheads[i] while p.right and q.right: if p.right.col < q.right.col: res.insert(MatNode(p.right.data, i, p.right.col)) p = p.right elif p.right.col > q.right.col: res.insert(MatNode(-q.right.data, i, q.right.col)) q = q.right else: res.insert(MatNode(p.right.data - q.right.data, i, p.right.col)) p, q = p.right, q.right while p.right: res.insert(MatNode(p.right.data, i, p.right.col)) p = p.right while q.right: res.insert(MatNode(-q.right.data, i, q.right.col)) q = q.right return res def mul(self, other): if self.cols != other.rows: return None res = SparseMatrix([[0] * other.cols for i in range(self.rows)]) for i in range(self.rows): p = self.rheads[i].right while p: j = p.col q = other.cheads[j].down while q: k = q.row res[i][k] += p.data * q.data q = q.down p = p.right return res def display(self): for i in range(self.rows): p = self.rheads[i].right for j in range(self.cols): if p and p.col == j: print(p.data, end=' ') p = p.right else: print(0, end=' ') print() # 测试代码 mat1 = [[1, 0, 0], [0, 2, 0], [0, 0, 3]] mat2 = [[4, 0, 0], [0, 5, 0], [0, 0, 6]] smat1 = SparseMatrix(mat1) smat2 = SparseMatrix(mat2) smat3 = smat1.add(smat2) smat4 = smat1.sub(smat2) smat5 = smat1.mul(smat2) smat1.display() smat2.display() smat3.display() smat4.display() smat5.display() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值