假设nn的稀疏矩阵A采用三元组表示,设计一个程序exp6-4.cpp实现如下功能:

稀疏矩阵运算
本文介绍了一个C++程序,用于创建并展示两个稀疏矩阵的三元组表示,并演示了矩阵转置、加法及乘法操作的过程。通过定义TSMatrix结构体来存储稀疏矩阵的非零元素,实现了矩阵的基本运算。
(1)生成如下两个稀疏矩阵的三元组a和b;
       
(2)输出a转置矩阵的三元组;
(3)输出a+b的三元组;
(4)输出ab的三元组。
#include <iostream>
#include<cstdio>
using namespace std;
#define M 4
#define N 4
typedef struct
{
    int r;
    int c;
    int d;
} TupNode;
typedef struct
{
    int rows;
    int cols;
    int nums;
    TupNode data[1000];
} TSMatrix;
void CreatMat(TSMatrix &t,int A[M][N])
{
    int i,j;
    t.rows=M;
    t.cols=N;
    t.nums=0;
    for(i=0; i<M; i++)
        for(j=0; j<N; j++)
        {
            if(A[i][j]!=0)
            {
                t.data[t.nums].r=i;
                t.data[t.nums].c=j;
                t.data[t.nums].d=A[i][j];
                t.nums++;
            }
        }
}
void DispMat(TSMatrix t)
{
    int i;
    if(t.nums<=0)
        return ;
    printf("\t%d\t%d\t%d\n",t.rows,t.cols,t.nums);
    printf("\t--------------------\n");
    for(i=0; i<t.nums; i++)
        printf("\t%d\t%d\t%d\n",t.data[i].r,t.data[i].c,t.data[i].d);
}
void TranTat(TSMatrix t,TSMatrix &tb)
{
    int p,q=0,v;
    tb.rows=t.cols;
    tb.cols=t.rows;
    tb.nums=t.nums;
    if(t.nums!=0)
    {
        for(v=0; v<t.cols; v++)
            for(p=0; p<t.nums; p++)
            {
                tb.data[q].r=t.data[p].c;
                tb.data[q].c=t.data[p].r;
                tb.data[q].d=t.data[p].d;
                q++;
            }
    }
}
int main()
{
    TSMatrix t,t1,tb;
    int c[4][4];
    int a[M][N]= {{1,0,3,0},{0,1,0,0},{0,0,1,0},{0,0,1,1}};
    CreatMat(t,a);
    TranTat(t,tb);
    printf("a的三元组:\n");
    DispMat(t);
    printf("a的转置三元组:\n");
    DispMat(tb);
    int b[M][N]= {{3,0,0,0},{0,4,0,0},{0,0,1,0},{0,0,0,2}};
    CreatMat(t1,b);
    printf("b的三元组:\n");
    DispMat(t1);
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
            c[i][j] = a[i][j] + b[i][j];
    CreatMat(t,c);
    printf("a+b的和:\n");
    DispMat(t);
    int sum;
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            sum = 0;
            for (int k = 0; k < N; k++)
            {
                sum += a[i][k] * b[k][j];
            }
            c[i][j]=sum;
        }
    }
    CreatMat(t,c);
    printf("a*b的三元组:\n");
    DispMat(t);
    return 0;
}

以下是使用Python实现稀疏矩阵采用三元组表示)的基本运算的代码: ```python class SparseMatrix: def __init__(self, rows, cols, triples): self.rows = rows self.cols = cols self.triples = triples def transpose(self): transposed_triples = [(col, row, val) for row, col, val in self.triples] transposed_triples.sort() return SparseMatrix(self.cols, self.rows, transposed_triples) def add(self, other): if self.rows != other.rows or self.cols != other.cols: raise ValueError("Matrices must have the same dimensions for addition.") all_triples = self.triples + other.triples all_triples.sort() result_triples = [] i = 0 while i < len(all_triples): row, col, val = all_triples[i] if i + 1 < len(all_triples) and all_triples[i + 1][:2] == (row, col): val += all_triples[i + 1][2] i += 1 result_triples.append((row, col, val)) i += 1 return SparseMatrix(self.rows, self.cols, result_triples) def multiply(self, other): if self.cols != other.rows: raise ValueError("Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.") result_triples = [] for i in range(self.rows): for j in range(other.cols): dot_product = 0 for row, col, val in self.triples: if row == i: for other_row, other_col, other_val in other.triples: if other_row == col and other_col == j: dot_product += val * other_val if dot_product != 0: result_triples.append((i, j, dot_product)) return SparseMatrix(self.rows, other.cols, result_triples) def __str__(self): return f"Rows: {self.rows}, Cols: {self.cols}, Triples: {self.triples}" # 生成稀疏矩阵a和b的三元组 a = SparseMatrix(4, 4, [(0, 1, 3), (1, 0, 1), (2, 2, 1), (3, 2, 1), (3, 3, 1)]) b = SparseMatrix(4, 4, [(0, 0, 3), (1, 1, 4), (2, 2, 1), (3, 3, 2)]) # 输出a转置矩阵的三元组 a_transpose = a.transpose() print("Transpose of matrix a:") print(a_transpose) # 输出a + b结果的三元组 a_plus_b = a.add(b) print("Matrix a + b:") print(a_plus_b) # 输出a * b结果的三元组 a_times_b = a.multiply(b) print("Matrix a * b:") print(a_times_b) ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值