两稀疏矩阵相加,减,乘,及任一矩阵的转置,C++
时间: 2023-07-11 07:30:17 浏览: 175
C#判断一个矩阵是否为对称矩阵及反称矩阵的方法
实现稀疏矩阵的加、减、乘和转置操作,可以采用三元组顺序表的数据结构。
下面是 C++ 的实现代码:
```c++
#include<iostream>
using namespace std;
// 三元组顺序表的结构体定义
struct TriTuple
{
int row, col, value;
};
// 稀疏矩阵加法
void matrixAdd(TriTuple A[], int aSize, TriTuple B[], int bSize)
{
int i = 0, j = 0, k = 0;
TriTuple C[100];
// A 和 B 的行列数必须相等
while (i < aSize && j < bSize)
{
if (A[i].row == B[j].row)
{
if (A[i].col == B[j].col)
{
// 将两个非零元素相加
if (A[i].value + B[j].value != 0)
{
C[k].row = A[i].row;
C[k].col = A[i].col;
C[k].value = A[i].value + B[j].value;
k++;
}
i++;
j++;
}
else if (A[i].col < B[j].col)
{
C[k].row = A[i].row;
C[k].col = A[i].col;
C[k].value = A[i].value;
k++;
i++;
}
else
{
C[k].row = B[j].row;
C[k].col = B[j].col;
C[k].value = B[j].value;
k++;
j++;
}
}
else if (A[i].row < B[j].row)
{
C[k].row = A[i].row;
C[k].col = A[i].col;
C[k].value = A[i].value;
k++;
i++;
}
else
{
C[k].row = B[j].row;
C[k].col = B[j].col;
C[k].value = B[j].value;
k++;
j++;
}
}
while (i < aSize)
{
C[k] = A[i];
k++;
i++;
}
while (j < bSize)
{
C[k] = B[j];
k++;
j++;
}
// 输出结果
cout << "矩阵 A:\n";
for (int i = 0; i < aSize; i++)
{
cout << A[i].value << " ";
if (A[i].col == 2)
cout << "\n";
}
cout << "矩阵 B:\n";
for (int i = 0; i < bSize; i++)
{
cout << B[i].value << " ";
if (B[i].col == 2)
cout << "\n";
}
cout << "矩阵 A + B:\n";
for (int i = 0; i < k; i++)
{
cout << C[i].value << " ";
if (C[i].col == 2)
cout << "\n";
}
}
// 稀疏矩阵减法
void matrixSub(TriTuple A[], int aSize, TriTuple B[], int bSize)
{
int i = 0, j = 0, k = 0;
TriTuple C[100];
// A 和 B 的行列数必须相等
while (i < aSize && j < bSize)
{
if (A[i].row == B[j].row)
{
if (A[i].col == B[j].col)
{
// 将两个非零元素相减
if (A[i].value - B[j].value != 0)
{
C[k].row = A[i].row;
C[k].col = A[i].col;
C[k].value = A[i].value - B[j].value;
k++;
}
i++;
j++;
}
else if (A[i].col < B[j].col)
{
C[k].row = A[i].row;
C[k].col = A[i].col;
C[k].value = A[i].value;
k++;
i++;
}
else
{
C[k].row = B[j].row;
C[k].col = B[j].col;
C[k].value = -B[j].value;
k++;
j++;
}
}
else if (A[i].row < B[j].row)
{
C[k].row = A[i].row;
C[k].col = A[i].col;
C[k].value = A[i].value;
k++;
i++;
}
else
{
C[k].row = B[j].row;
C[k].col = B[j].col;
C[k].value = -B[j].value;
k++;
j++;
}
}
while (i < aSize)
{
C[k] = A[i];
k++;
i++;
}
while (j < bSize)
{
C[k].row = B[j].row;
C[k].col = B[j].col;
C[k].value = -B[j].value;
k++;
j++;
}
// 输出结果
cout << "矩阵 A:\n";
for (int i = 0; i < aSize; i++)
{
cout << A[i].value << " ";
if (A[i].col == 2)
cout << "\n";
}
cout << "矩阵 B:\n";
for (int i = 0; i < bSize; i++)
{
cout << B[i].value << " ";
if (B[i].col == 2)
cout << "\n";
}
cout << "矩阵 A - B:\n";
for (int i = 0; i < k; i++)
{
cout << C[i].value << " ";
if (C[i].col == 2)
cout << "\n";
}
}
// 稀疏矩阵乘法
void matrixMul(TriTuple A[], int aSize, TriTuple B[], int bSize)
{
// 两个矩阵的乘积 C 的行数等于矩阵 A 的行数,列数等于矩阵 B 的列数
int n = 3; // A 的行数
int m = 2; // B 的列数
int k = 2; // A 的列数,B 的行数
int i, j, r, sum;
TriTuple C[100];
int cSize = 0;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
sum = 0;
for (r = 0; r < aSize; r++)
{
if (A[r].row == i && B[r].col == j)
sum += A[r].value * B[r].value;
}
if (sum != 0)
{
C[cSize].row = i;
C[cSize].col = j;
C[cSize].value = sum;
cSize++;
}
}
}
// 输出结果
cout << "矩阵 A:\n";
for (int i = 0; i < aSize; i++)
{
cout << A[i].value << " ";
if (A[i].col == 2)
cout << "\n";
}
cout << "矩阵 B:\n";
for (int i = 0; i < bSize; i++)
{
cout << B[i].value << " ";
if (B[i].col == 2)
cout << "\n";
}
cout << "矩阵 A × B:\n";
for (int i = 0; i < cSize; i++)
{
cout << C[i].value << " ";
if (C[i].col == 2)
cout << "\n";
}
}
// 稀疏矩阵转置
void matrixTrans(TriTuple A[], int aSize)
{
TriTuple B[100];
int bSize = 0;
// 转置后的矩阵 B 的行数等于 A 的列数,列数等于 A 的行数
for (int i = 1; i <= 2; i++)
{
for (int j = 1; j <= 3; j++)
{
// 在 A 中找到第 j 行第 i 列的非零元素,将其转置到 B 中的第 i 行第 j 列
for (int k = 0; k < aSize; k++)
{
if (A[k].row == j && A[k].col == i)
{
B[bSize].row = i;
B[bSize].col = j;
B[bSize].value = A[k].value;
bSize++;
}
}
}
}
// 输出结果
cout << "矩阵 A:\n";
for (int i = 0; i < aSize; i++)
{
cout << A[i].value << " ";
if (A[i].col == 2)
cout << "\n";
}
cout << "矩阵 A 的转置 B:\n";
for (int i = 0; i < bSize; i++)
{
cout << B[i].value << " ";
if (B[i].col == 2)
cout << "\n";
}
}
int main()
{
// 初始化两个稀疏矩阵 A 和 B
TriTuple A[6] = { {1,1,1},{1,2,2},{2,2,3},{3,1,4},{3,2,5} };
TriTuple B[6] = { {1,1,1},{1,2,2},{2,2,3},{3,1,4},{3,2,5} };
// 矩阵加法
matrixAdd(A, 5, B, 5);
// 矩阵减法
matrixSub(A, 5, B, 5);
// 矩阵乘法
matrixMul(A, 5, B, 5);
// 矩阵转置
matrixTrans(A, 5);
return 0;
}
```
阅读全文