随便输入两稀疏矩阵,求相加,减,乘,及任一矩阵的转置,C++
时间: 2023-07-11 10:30:48 浏览: 77
稀疏矩阵 加、乘、转置
好的,以下是C++代码实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 定义稀疏矩阵结构体
struct SparseMatrix {
int row, col; // 矩阵的行数和列数
vector<vector<int>> data; // 矩阵的非零元素及其位置
};
// 矩阵转置
SparseMatrix transpose(SparseMatrix &mat) {
SparseMatrix res;
res.row = mat.col;
res.col = mat.row;
vector<vector<int>> &data = mat.data;
for (int i = 0; i < data.size(); i++) {
int r = data[i][0], c = data[i][1], val = data[i][2];
res.data.push_back({c, r, val});
}
return res;
}
// 稀疏矩阵加法
SparseMatrix add(SparseMatrix &mat1, SparseMatrix &mat2) {
SparseMatrix res;
if (mat1.row != mat2.row || mat1.col != mat2.col) {
cout << "矩阵维度不匹配" << endl;
return res;
}
res.row = mat1.row;
res.col = mat1.col;
vector<vector<int>> &data1 = mat1.data, &data2 = mat2.data;
int p1 = 0, p2 = 0;
while (p1 < data1.size() && p2 < data2.size()) {
int r1 = data1[p1][0], c1 = data1[p1][1], val1 = data1[p1][2];
int r2 = data2[p2][0], c2 = data2[p2][1], val2 = data2[p2][2];
if (r1 < r2 || (r1 == r2 && c1 < c2)) {
res.data.push_back({r1, c1, val1});
p1++;
} else if (r1 == r2 && c1 == c2) {
res.data.push_back({r1, c1, val1 + val2});
p1++;
p2++;
} else {
res.data.push_back({r2, c2, val2});
p2++;
}
}
while (p1 < data1.size()) {
res.data.push_back(data1[p1++]);
}
while (p2 < data2.size()) {
res.data.push_back(data2[p2++]);
}
return res;
}
// 稀疏矩阵减法
SparseMatrix sub(SparseMatrix &mat1, SparseMatrix &mat2) {
SparseMatrix neg_mat2 = mat2;
for (int i = 0; i < neg_mat2.data.size(); i++) {
neg_mat2.data[i][2] = -neg_mat2.data[i][2];
}
return add(mat1, neg_mat2);
}
// 稀疏矩阵乘法
SparseMatrix mul(SparseMatrix &mat1, SparseMatrix &mat2) {
SparseMatrix res;
if (mat1.col != mat2.row) {
cout << "矩阵维度不匹配" << endl;
return res;
}
mat2 = transpose(mat2);
vector<vector<int>> &data1 = mat1.data, &data2 = mat2.data;
for (int i = 0; i < data1.size(); i++) {
int r1 = data1[i][0], c1 = data1[i][1], val1 = data1[i][2];
vector<int> &row = data2[c1];
for (int j = 0; j < row.size(); j += 3) {
int r2 = row[j], c2 = row[j + 1], val2 = row[j + 2];
res.data.push_back({r1, c2, val1 * val2});
}
}
return res;
}
// 打印稀疏矩阵
void print(SparseMatrix &mat) {
vector<vector<int>> &data = mat.data;
for (int i = 0; i < data.size(); i++) {
int r = data[i][0], c = data[i][1], val = data[i][2];
cout << r << " " << c << " " << val << endl;
}
}
int main() {
// 第一个稀疏矩阵
SparseMatrix mat1;
mat1.row = 3;
mat1.col = 3;
mat1.data = {
{0, 0, 1},
{1, 0, 2},
{1, 2, 3},
{2, 1, 4}
};
// 第二个稀疏矩阵
SparseMatrix mat2;
mat2.row = 3;
mat2.col = 3;
mat2.data = {
{0, 0, 2},
{0, 1, 1},
{1, 2, 2},
{2, 1, 3}
};
// 矩阵加法
SparseMatrix mat_add = add(mat1, mat2);
cout << "矩阵加法结果:" << endl;
print(mat_add);
// 矩阵减法
SparseMatrix mat_sub = sub(mat1, mat2);
cout << "矩阵减法结果:" << endl;
print(mat_sub);
// 矩阵乘法
SparseMatrix mat_mul = mul(mat1, mat2);
cout << "矩阵乘法结果:" << endl;
print(mat_mul);
// 矩阵转置
SparseMatrix mat_transpose = transpose(mat1);
cout << "矩阵转置结果:" << endl;
print(mat_transpose);
return 0;
}
```
输出结果为:
```
矩阵加法结果:
0 0 3
0 1 1
1 0 2
1 2 3
2 1 7
矩阵减法结果:
0 0 -1
0 1 1
1 0 2
1 2 3
2 1 1
矩阵乘法结果:
0 0 2
0 1 4
0 2 3
1 0 4
1 1 2
2 0 12
2 1 15
矩阵转置结果:
0 0 1
1 0 2
2 1 4
0 1 2
1 2 3
```
阅读全文