矩阵运算 Matrix Product, Hadamard Product, Kronecker Product

这篇博客汇总了矩阵运算的三种类型:Matrix Product, Hadamard Product和Kronecker Product。Matrix Product是最常见的矩阵乘积,要求前一矩阵的列等于后一矩阵的行;Hadamard Product需要同型矩阵,对应位置元素相乘;Kronecker Product则涉及前一矩阵的每个元素与后一矩阵的乘积,形成新的大矩阵。" 92711990,8256442,Ubuntu16.04配置NVIDIA Tesla P40显卡驱动指南,"['运维', '操作系统', 'GPU驱动', '深度学习']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在读一些paper,遇到了一些矩阵相关的计算。通过网上查阅相关资料,现在将其汇总一下,以备后续不时之需;其中主要包括Matrix Product, Hadamard Product以及Kronecker Product。

1、Matrix Product(矩阵乘积):该计算最常见,学过线性代数的同学可以跳过。

矩阵乘积要求:前一矩阵的列等于后一矩阵的行

例如,n{\color{Blue} }\times m{\color{Blue} }的矩阵A与m{\color{Blue} }\times p{\color{Blue} }的矩阵B相乘,得到n{\color{Blue} }\times p{\color{Blue} }的矩阵C;

2、Hadamard Product:

要求矩阵为同型矩阵,该乘积为矩阵中对应位置元素相乘

例如,

下面是使用二维数组初始化两个需要进行矩阵乘法运算的不定阶数的矩阵,并显示可以进行的矩阵乘法运算方式的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) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值