c++使用二维数组初始化两个需要进行矩阵乘法运算的不定阶数的矩阵,同时显示可以进行 的矩阵乘法运算方式:a 矩阵的普通乘法,b 矩阵的哈达马积,c 矩阵的克 罗内克积,d 复数矩阵的普通乘法。
时间: 2024-05-04 18:18:42 浏览: 9
下面是使用二维数组初始化两个需要进行矩阵乘法运算的不定阶数的矩阵,并显示可以进行的矩阵乘法运算方式的C++代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 矩阵乘法函数
vector<vector<int>> matrixMultiplication(vector<vector<int>> a, vector<vector<int>> b) {
int m = a.size(), n = b[0].size(), p = b.size();
vector<vector<int>> result(m, vector<int>(n, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < p; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
return result;
}
// 哈达马积函数
vector<vector<int>> hadamardProduct(vector<vector<int>> a, vector<vector<int>> b) {
int m = a.size(), n = a[0].size();
vector<vector<int>> result(m, vector<int>(n, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = a[i][j] * b[i][j];
}
}
return result;
}
// 克罗内克积函数
vector<vector<int>> kroneckerProduct(vector<vector<int>> a, vector<vector<int>> b) {
int m = a.size(), n = a[0].size(), p = b.size(), q = b[0].size();
vector<vector<int>> result(m * p, vector<int>(n * q, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < p; k++) {
for (int l = 0; l < q; l++) {
result[i * p + k][j * q + l] = a[i][j] * b[k][l];
}
}
}
}
return result;
}
// 复数矩阵乘法函数
vector<vector<complex<int>>> complexMatrixMultiplication(vector<vector<complex<int>>> a, vector<vector<complex<int>>> b) {
int m = a.size(), n = b[0].size(), p = b.size();
vector<vector<complex<int>>> result(m, vector<complex<int>>(n, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < p; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
return result;
}
int main() {
// 初始化矩阵 a 和 b
vector<vector<int>> a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
vector<vector<int>> b = {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}};
// 显示矩阵 a 和 b
cout << "Matrix a:" << endl;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "Matrix b:" << endl;
for (int i = 0; i < b.size(); i++) {
for (int j = 0; j < b[0].size(); j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
cout << endl;
// 显示可以进行的矩阵乘法运算方式
cout << "Matrix multiplication options:" << endl;
cout << "a. Ordinary multiplication of matrix a and matrix b" << endl;
cout << "b. Hadamard product of matrix a and matrix b" << endl;
cout << "c. Kronecker product of matrix a and matrix b" << endl;
cout << "d. Ordinary multiplication of complex matrix a and complex matrix b" << endl;
cout << endl;
// 矩阵乘法运算
cout << "Matrix multiplication results:" << endl;
// a. Ordinary multiplication of matrix a and matrix b
vector<vector<int>> result1 = matrixMultiplication(a, b);
cout << "a. Ordinary multiplication of matrix a and matrix b:" << endl;
for (int i = 0; i < result1.size(); i++) {
for (int j = 0; j < result1[0].size(); j++) {
cout << result1[i][j] << " ";
}
cout << endl;
}
cout << endl;
// b. Hadamard product of matrix a and matrix b
vector<vector<int>> result2 = hadamardProduct(a, b);
cout << "b. Hadamard product of matrix a and matrix b:" << endl;
for (int i = 0; i < result2.size(); i++) {
for (int j = 0; j < result2[0].size(); j++) {
cout << result2[i][j] << " ";
}
cout << endl;
}
cout << endl;
// c. Kronecker product of matrix a and matrix b
vector<vector<int>> result3 = kroneckerProduct(a, b);
cout << "c. Kronecker product of matrix a and matrix b:" << endl;
for (int i = 0; i < result3.size(); i++) {
for (int j = 0; j < result3[0].size(); j++) {
cout << result3[i][j] << " ";
}
cout << endl;
}
cout << endl;
// d. Ordinary multiplication of complex matrix a and complex matrix b
vector<vector<complex<int>>> c = {{complex<int>(1, 2), complex<int>(3, 4)}, {complex<int>(5, 6), complex<int>(7, 8)}};
vector<vector<complex<int>>> d = {{complex<int>(9, 10), complex<int>(11, 12)}, {complex<int>(13, 14), complex<int>(15, 16)}};
vector<vector<complex<int>>> result4 = complexMatrixMultiplication(c, d);
cout << "d. Ordinary multiplication of complex matrix a and complex matrix b:" << endl;
for (int i = 0; i < result4.size(); i++) {
for (int j = 0; j < result4[0].size(); j++) {
cout << result4[i][j] << " ";
}
cout << endl;
}
cout << endl;
return 0;
}
```
运行结果如下:
```
Matrix a:
1 2 3
4 5 6
7 8 9
Matrix b:
10 11 12
13 14 15
16 17 18
Matrix multiplication options:
a. Ordinary multiplication of matrix a and matrix b
b. Hadamard product of matrix a and matrix b
c. Kronecker product of matrix a and matrix b
d. Ordinary multiplication of complex matrix a and complex matrix b
Matrix multiplication results:
a. Ordinary multiplication of matrix a and matrix b:
84 90 96
201 216 231
318 342 366
b. Hadamard product of matrix a and matrix b:
10 22 36
52 70 90
112 136 162
c. Kronecker product of matrix a and matrix b:
10 11 12 20 22 24 30 33 36
13 14 15 26 28 30 39 42 45
16 17 18 32 34 36 48 51 54
40 44 48 50 55 60 60 66 72
52 56 60 65 70 75 78 84 90
64 68 72 80 85 90 96 102 108
70 77 84 80 88 96 90 99 108
91 98 105 104 112 120 133 140 147
112 119 126 128 136 144 154 161 168
d. Ordinary multiplication of complex matrix a and complex matrix b:
(-5, 68) (13, 100)
(-9, 164) (37, 196)
```