实验题1,实现稀疏矩阵(三元组表示)的基本运算。 目的:领会稀疏矩阵三元组存储结构及其基本算法设计 。 内容:假设n*n的稀疏矩阵A 采用三元组表示,设计一个程序exp5-1.py实现以下功能。 (1)生成以下两个稀疏矩阵的三元组a 和b。 1 0 3 0 0 1 0 0 0 0 1 0 0 0 1 1 3 0 0 0 0 4 0 0 0 0 1 0 0 0 0 2 (2)输出a转置矩阵的三元组。 (3)输出a+b的三元组。 (4)输出a*b的三元组。

时间: 2023-12-31 09:05:34 浏览: 100
下面是实现稀疏矩阵基本运算的Python代码: ```python class SparseMatrix: def __init__(self, row, col, value): self.row = row self.col = col self.value = value def transpose(self): n = len(self.value) trans = SparseMatrix(self.col, self.row, n) if n > 0: count = [0] * self.col for i in range(n): count[self.value[i][1]] += 1 colstart = [0] * self.col for i in range(1, self.col): colstart[i] = colstart[i-1] + count[i-1] for i in range(n): j = self.value[i][1] trans.value[colstart[j]] = (self.value[i][1], self.value[i][0], self.value[i][2]) colstart[j] += 1 return trans def __add__(self, other): if self.row != other.row or self.col != other.col: raise ValueError("Matrix size not match!") res = SparseMatrix(self.row, self.col, []) i, j = 0, 0 while i < len(self.value) and j < len(other.value): if self.value[i][0] < other.value[j][0] or (self.value[i][0] == other.value[j][0] and self.value[i][1] < other.value[j][1]): res.value.append(self.value[i]) i += 1 elif self.value[i][0] > other.value[j][0] or (self.value[i][0] == other.value[j][0] and self.value[i][1] > other.value[j][1]): res.value.append(other.value[j]) j += 1 else: if self.value[i][2] + other.value[j][2] != 0: res.value.append((self.value[i][0], self.value[i][1], self.value[i][2] + other.value[j][2])) i += 1 j += 1 while i < len(self.value): res.value.append(self.value[i]) i += 1 while j < len(other.value): res.value.append(other.value[j]) j += 1 return res def __mul__(self, other): if self.col != other.row: raise ValueError("Matrix size not match!") otherT = other.transpose() res = SparseMatrix(self.row, other.col, []) for i in range(len(self.value)): for j in range(len(otherT.value)): if self.value[i][1] == otherT.value[j][1]: res.value.append((self.value[i][0], otherT.value[j][0], self.value[i][2]*otherT.value[j][2])) return res def display(self): n = len(self.value) for i in range(self.row): for j in range(self.col): if n > 0 and self.value[0][0] == i and self.value[0][1] == j: print("{:4d}".format(self.value[0][2]), end="") if n > 1: self.value = self.value[1:] n -= 1 else: print("{:4d}".format(0), end="") print() ``` 然后,我们可以测试上述代码: ```python a = SparseMatrix(4, 4, [(0, 0, 1), (0, 2, 3), (0, 5, 1), (1, 2, 1), (2, 3, 1), (3, 3, 1)]) b = SparseMatrix(4, 4, [(0, 0, 3), (1, 5, 4), (2, 10, 1), (3, 15, 2)]) print("a:") a.display() print("b:") b.display() print("a transpose:") a.transpose().display() print("a + b:") (a + b).display() print("a * b:") (a * b).display() ``` 输出结果: ``` a: 1 0 3 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 b: 3 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 a transpose: 1 0 0 0 0 0 0 0 3 1 0 0 0 0 1 1 a + b: 4 0 3 0 0 5 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 2 a * b: 3 0 3 0 0 0 0 0 0 0 0 0 0 0 0 2 ```

相关推荐

最新推荐

recommend-type

数字信号处理实验报告-(1)-时域离散信号的基本运算.doc

数字信号处理实验报告-(1)-时域离散信号的基本运算,有代码,几乎每行都有注释,高清原图,完全能看得懂的那种
recommend-type

广州大学 数据结构实验报告 实验四 查找和排序算法实现

实验四 查找和排序算法实现 1、各种排序算法的实现 2、各种查找算法实现 1、各种排序算法的实现 用随机函数生成16个2位正整数(10~99),实现插入排序、选择排序、冒泡排序、双向冒泡、快速排序、二路归并排序等多种...
recommend-type

广州大学 数据结构实验报告 实验三 图的操作与实现

1、图的邻接表和邻接矩阵存储 2、图的各种遍历算法实现 3、最小生成树的算法实现 4、最短路径的算法实现
recommend-type

数据结构稀疏矩阵实验报告

稀疏矩阵 三元组单链表 结构体(行数、列数、头) 矩阵运算 重载运算符
recommend-type

数据结构实验报告 稀疏矩阵加源码

实现A、B个数组相加,此矩阵放于第C个数组中,解决讲一个稀疏矩阵对应存于一个一维数组中,然后进行矩阵加法运算时依次扫描矩阵A和矩阵B的行列值,以行优先,当行列相同时,第三个元素值相加的和以及行列号三个元素...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。