strassen矩阵乘法算法c语言
时间: 2023-07-16 12:02:46 浏览: 216
矩阵相乘的strassen算法c语言实现
4星 · 用户满意度95%
### 回答1:
Strassen矩阵乘法算法是一种用于计算两个矩阵乘积的高效方法,其基本思想是将原始矩阵划分为较小的子矩阵,并通过递归调用来计算乘积。下面是使用C语言实现Strassen矩阵乘法算法的一个示例:
```c
#include<stdio.h>
void strassen(int n, int A[][n], int B[][n], int C[][n]) {
if (n == 1) {
C[0][0] = A[0][0] * B[0][0];
return;
}
// 计算矩阵的中间大小
int half = n / 2;
// 划分原始矩阵为四个子矩阵
int A11[half][half], A12[half][half], A21[half][half], A22[half][half];
int B11[half][half], B12[half][half], B21[half][half], B22[half][half];
int C11[half][half], C12[half][half], C21[half][half], C22[half][half];
int P[half][half], Q[half][half], R[half][half], S[half][half], T[half][half], U[half][half], V[half][half];
// 初始化子矩阵
for (int i = 0; i < half; i++) {
for (int j = 0; j < half; j++) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + half];
A21[i][j] = A[i + half][j];
A22[i][j] = A[i + half][j + half];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + half];
B21[i][j] = B[i + half][j];
B22[i][j] = B[i + half][j + half];
}
}
// 递归调用计算子矩阵
strassen(half, A11, B11, P);
strassen(half, A12, B21, Q);
strassen(half, A11, B12, R);
strassen(half, A12, B22, S);
strassen(half, A21, B11, T);
strassen(half, A22, B21, U);
strassen(half, A21, B12, V);
// 计算结果矩阵的子矩阵
for (int i = 0; i < half; i++) {
for (int j = 0; j < half; j++) {
C11[i][j] = P[i][j] + Q[i][j];
C12[i][j] = R[i][j] + S[i][j];
C21[i][j] = T[i][j] + U[i][j];
C22[i][j] = R[i][j] + T[i][j] + U[i][j] + V[i][j];
}
}
// 将子矩阵组合为结果矩阵
for (int i = 0; i < half; i++) {
for (int j = 0; j < half; j++) {
C[i][j] = C11[i][j];
C[i][j + half] = C12[i][j];
C[i + half][j] = C21[i][j];
C[i + half][j + half] = C22[i][j];
}
}
}
int main() {
int n;
printf("请输入矩阵维度n:");
scanf("%d", &n);
int A[n][n], B[n][n], C[n][n];
printf("请输入矩阵A:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
printf("请输入矩阵B:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &B[i][j]);
}
}
strassen(n, A, B, C);
printf("结果矩阵C:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
```
这个示例代码实现了一个递归的Strassen矩阵乘法算法。用户需要在运行代码时输入矩阵的维度n,以及矩阵A和B的元素。程序将计算A和B的乘积,并打印结果矩阵C。
### 回答2:
Strassen矩阵乘法算法是一种用于快速计算矩阵乘法的算法,采用分治策略,并且在一些情况下具有比传统算法更高的效率。下面是一个使用C语言实现Strassen矩阵乘法算法的例子:
```c
#include <stdio.h>
#include <stdlib.h>
void strassen(int n, int A[][n], int B[][n], int C[][n]) {
if (n == 2) { // 基本情况,直接使用传统算法计算
int P = (A[0][0] + A[1][1]) * (B[0][0] + B[1][1]);
int Q = (A[1][0] + A[1][1]) * B[0][0];
int R = A[0][0] * (B[0][1] - B[1][1]);
int S = A[1][1] * (B[1][0] - B[0][0]);
int T = (A[0][0] + A[0][1]) * B[1][1];
int U = (A[1][0] - A[0][0]) * (B[0][0] + B[0][1]);
int V = (A[0][1] - A[1][1]) * (B[1][0] + B[1][1]);
C[0][0] = P + S - T + V;
C[0][1] = R + T;
C[1][0] = Q + S;
C[1][1] = P + R - Q + U;
} else {
int newSize = n/2;
int A11[newSize][newSize], A12[newSize][newSize], A21[newSize][newSize], A22[newSize][newSize];
int B11[newSize][newSize], B12[newSize][newSize], B21[newSize][newSize], B22[newSize][newSize];
int C11[newSize][newSize], C12[newSize][newSize], C21[newSize][newSize], C22[newSize][newSize];
int P1[newSize][newSize], P2[newSize][newSize], P3[newSize][newSize], P4[newSize][newSize], P5[newSize][newSize], P6[newSize][newSize], P7[newSize][newSize];
int i, j;
for (i = 0; i < newSize; i++) {
for (j = 0; j < newSize; j++) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + newSize];
A21[i][j] = A[i + newSize][j];
A22[i][j] = A[i + newSize][j + newSize];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + newSize];
B21[i][j] = B[i + newSize][j];
B22[i][j] = B[i + newSize][j + newSize];
}
}
strassen(newSize, A11, B11, P1);
strassen(newSize, A12, B21, P2);
strassen(newSize, A11, B12, P3);
strassen(newSize, A12, B22, P4);
strassen(newSize, A21, B11, P5);
strassen(newSize, A22, B21, P6);
strassen(newSize, A21, B12, P7);
for (i = 0; i < newSize; i++) {
for (j = 0; j < newSize; j++) {
C11[i][j] = P1[i][j] + P4[i][j] - P5[i][j] + P7[i][j];
C12[i][j] = P3[i][j] + P5[i][j];
C21[i][j] = P2[i][j] + P4[i][j];
C22[i][j] = P1[i][j] + P3[i][j] - P2[i][j] + P6[i][j];
C[i][j] = C11[i][j];
C[i][j + newSize] = C12[i][j];
C[i + newSize][j] = C21[i][j];
C[i + newSize][j + newSize] = C22[i][j];
}
}
}
}
int main() {
int n = 4; // 矩阵维数
int A[][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int B[][4] = {{17, 18, 19, 20},
{21, 22, 23, 24},
{25, 26, 27, 28},
{29, 30, 31, 32}};
int C[4][4];
strassen(n, A, B, C);
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
```
以上是一个简单的C语言实现的Strassen矩阵乘法算法。在此例子中,我们使用了一个4x4的矩阵作为输入,并打印出计算结果。
阅读全文