The Strassen algorithm is an advanced matrix multiplication technique that uses a divide and conquer approach to multiply two n x n matrices more efficiently than the standard matrix multiplication approach for large matrices.
Below is a simple implementation of the Strassen algorithm to multiply two n x n matrices in C. Note that this code assumes n is a power of 2. For matrices of arbitrary size, you would typically pad them with zeros to the next power of 2:
#include <stdio.h>
#include <stdlib.h>
void sum_matrix(int n, int A[][n], int B[][n], int C[][n]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] + B[i][j];
}
void subtract_matrix(int n, int A[][n], int B[][n], int C[][n]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] - B[i][j];
}
void strassen(int n, int A[][n], int B[][n], int C[][n]) {
// Base case
if (n == 1) {
C[0][0] = A[0][0] * B[0][0];
return;
}
int k = n / 2;
// Allocate submatrices
int A11[k][k], A12[k][k], A21[k][k], A22[k][k];
int B11[k][k], B12[k][k], B21[k][k], B22[k][k];
int C11[k][k], C12[k][k], C21[k][k], C22[k][k];
int P1[k][k], P2[k][k], P3[k][k], P4[k][k],
P5[k][k], P6[k][k], P7[k][k];
int Q1[k][k], Q2[k][k], Q3[k][k], Q4[k][k];
// Partition the input matrices into submatrices
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + k];
A21[i][j] = A[i + k][j];
A22[i][j] = A[i + k][j + k];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + k];
B21[i][j] = B[i + k][j];
B22[i][j] = B[i + k][j + k];
}
}
// Calculate P1 to P7
sum_matrix(k, A11, A22, Q1);
sum_matrix(k, B11, B22, Q2);
strassen(k, Q1, Q2, P1);
sum_matrix(k, A21, A22, Q1);
strassen(k, Q1, B11, P2);
subtract_matrix(k, B12, B22, Q1);
strassen(k, A11, Q1, P3);
subtract_matrix(k, B21, B11, Q1);
strassen(k, A22, Q1, P4);
sum_matrix(k, A11, A12, Q1);
strassen(k, Q1, B22, P5);
subtract_matrix(k, A21, A11, Q1);
sum_matrix(k, B11, B12, Q2);
strassen(k, Q1, Q2, P6);
subtract_matrix(k, A12, A22, Q1);
sum_matrix(k, B21, B22, Q2);
strassen(k, Q1, Q2, P7);
// Calculate C11, C12, C21, and C22
sum_matrix(k, P1, P4, Q1);
subtract_matrix(k, Q1, P5, Q2);
sum_matrix(k, Q2, P7, C11);
sum_matrix(k, P3, P5, C12);
sum_matrix(k, P2, P4, C21);
sum_matrix(k, P1, P3, Q1);
subtract_matrix(k, Q1, P2, Q2);
sum_matrix(k, Q2, P6, C22);
// Combine the results into C
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
C[i][j] = C11[i][j];
C[i][j + k] = C12[i][j];
C[i + k][j] = C21[i][j];
C[i + k][j + k] = C22[i][j];
}
}
}
int main() {
int n = 4; // Example size (n should be a power of 2)
int A[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int B[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int C[4][4];
strassen(n, A, B, C);
// Print the result
printf("Product of matrices A and B is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Please note the following when using the code:
- The code is a simplistic representation to demonstrate Strassen’s multiplication algorithm in C.
- The memory allocation used in the example is static, an alternative could be using dynamic memory allocation techniques.
- Error checks and validation for the input matrices are omitted for brevity.
- Optimal performance would require more complex memory management and perhaps alternative handling of non-power-of-two matrix sizes, which are beyond the scope of a simple example.