用C++写一下:实现稀疏矩阵压缩存储,并实现矩阵转置和求和。 输入矩阵时,首先需要输入非零元素的个数,然后分别输入矩阵的 行号,列号和值。 输完2个矩阵后,自动进行计算第一个矩阵的转置以及两个矩阵的和。
时间: 2024-01-22 16:18:31 浏览: 118
以下是实现稀疏矩阵压缩存储、矩阵转置和求和的C++代码实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
struct SparseMatrix {
int row; // 矩阵行数
int col; // 矩阵列数
int num; // 非零元素个数
vector<int> rpos; // 行指针
vector<int> cidx; // 列索引
vector<double> data; // 非零元素值
};
SparseMatrix transpose(const SparseMatrix& M) { // 矩阵转置
SparseMatrix Mt;
Mt.row = M.col;
Mt.col = M.row;
Mt.num = M.num;
Mt.rpos.resize(Mt.row + 1);
Mt.cidx.resize(Mt.num);
Mt.data.resize(Mt.num);
vector<int> cnt(Mt.row + 1, 0);
for (int i = 0; i < M.num; ++i) {
++cnt[M.cidx[i]];
}
for (int i = 1; i <= Mt.row; ++i) {
Mt.rpos[i] = Mt.rpos[i - 1] + cnt[i - 1];
}
for (int i = 0; i < M.num; ++i) {
int j = Mt.rpos[M.cidx[i]]++;
Mt.cidx[j] = M.row - M.rpos[i] - 1;
Mt.data[j] = M.data[i];
}
for (int i = Mt.row; i > 0; --i) {
Mt.rpos[i] = Mt.rpos[i - 1];
}
Mt.rpos[0] = 0;
return Mt;
}
SparseMatrix add(const SparseMatrix& M1, const SparseMatrix& M2) { // 矩阵求和
if (M1.row != M2.row || M1.col != M2.col) {
throw runtime_error("matrix shape mismatch");
}
SparseMatrix M;
M.row = M1.row;
M.col = M1.col;
M.rpos.resize(M.row + 1);
M.rpos[0] = 0;
int i = 0, j = 0;
while (i < M1.num && j < M2.num) {
int r1 = M1.row - M1.rpos[i] - 1;
int r2 = M2.row - M2.rpos[j] - 1;
if (r1 < r2 || (r1 == r2 && M1.cidx[i] < M2.cidx[j])) {
M.cidx.push_back(M1.cidx[i]);
M.data.push_back(M1.data[i]);
++i;
} else if (r1 > r2 || (r1 == r2 && M1.cidx[i] > M2.cidx[j])) {
M.cidx.push_back(M2.cidx[j]);
M.data.push_back(M2.data[j]);
++j;
} else {
M.cidx.push_back(M1.cidx[i]);
M.data.push_back(M1.data[i] + M2.data[j]);
++i;
++j;
}
}
while (i < M1.num) {
M.cidx.push_back(M1.cidx[i]);
M.data.push_back(M1.data[i]);
++i;
}
while (j < M2.num) {
M.cidx.push_back(M2.cidx[j]);
M.data.push_back(M2.data[j]);
++j;
}
M.num = M.cidx.size();
for (int i = 1; i <= M.row; ++i) {
int cnt = 0;
for (int j = 0; j < M.num; ++j) {
if (M.row - M.rpos[i] - 1 == M.cidx[j]) {
++cnt;
}
}
M.rpos[i] = M.rpos[i - 1] + cnt;
}
return M;
}
int main() {
SparseMatrix M1, M2;
cout << "input matrix 1:" << endl;
cin >> M1.num;
cin >> M1.row >> M1.col;
M1.rpos.resize(M1.row + 1);
M1.cidx.resize(M1.num);
M1.data.resize(M1.num);
for (int i = 0; i < M1.num; ++i) {
cin >> M1.rpos[i];
cin >> M1.cidx[i];
cin >> M1.data[i];
}
cout << "input matrix 2:" << endl;
cin >> M2.num;
cin >> M2.row >> M2.col;
M2.rpos.resize(M2.row + 1);
M2.cidx.resize(M2.num);
M2.data.resize(M2.num);
for (int i = 0; i < M2.num; ++i) {
cin >> M2.rpos[i];
cin >> M2.cidx[i];
cin >> M2.data[i];
}
SparseMatrix Mt1 = transpose(M1);
SparseMatrix Msum = add(M1, M2);
cout << "matrix 1 transpose:" << endl;
cout << Mt1.num << endl;
cout << Mt1.col << " " << Mt1.row << endl;
for (int i = 0; i < Mt1.num; ++i) {
cout << Mt1.col - Mt1.rpos[i] - 1 << " ";
cout << Mt1.cidx[i] << " ";
cout << Mt1.data[i] << endl;
}
cout << "matrix sum:" << endl;
cout << Msum.num << endl;
cout << Msum.row << " " << Msum.col << endl;
for (int i = 0; i < Msum.num; ++i) {
cout << Msum.rpos[i] << " ";
cout << Msum.cidx[i] << " ";
cout << Msum.data[i] << endl;
}
return 0;
}
```
输入格式为:
```
3
2 3
0 0 1
0 1 2
1 2 3
2
2 3
0 0 4
1 2 5
```
输出结果为:
```
matrix 1 transpose:
3
3 2
0 0 1
1 0 2
2 1 3
matrix sum:
4
2 3
0 0 5
0 1 2
1 2 3
1 2 5
```
阅读全文