Can I improve on this any further. Is there a prettier way of passing the array sizes?
#include <stdio.h>
void matrix_addition(size_t n, int A[n][n], int B[n][n], int C[2*n][2*n], size_t c_start, size_t c_end) {
for(size_t i = 0; i < n; ++i) {
for(size_t j = 0; j < n; ++j) {
C[i+c_start][j+c_end] = A[i][j] + B[i][j];
}
}
}
void recursive_matrix_multiply(
size_t l, size_t n, int A[l][l], int B[l][l], int C[n][n],
size_t a_r_start, size_t a_r_end, size_t a_c_start, size_t a_c_end,
size_t b_r_start, size_t b_r_end, size_t b_c_start, size_t b_c_end
) {
if(n == 1) {
C[0][0] = A[a_r_start][a_c_start]*B[b_r_start][b_c_start];
} else {
int m = n/2;
int C1[m][m];
int C2[m][m];
recursive_matrix_multiply(l, m, A, B, C1, a_r_start, a_r_end-m, a_c_start, a_c_end-m, b_r_start, b_r_end-m, b_c_start, b_c_end-m);
recursive_matrix_multiply(l, m, A, B, C2, a_r_start, a_r_end-m, a_c_start+m, a_c_end, b_r_start+m, b_r_end, b_c_start, b_c_end-m);
matrix_addition(m, C1, C2, C, 0, 0);
recursive_matrix_multiply(l, m, A, B, C1, a_r_start, a_r_end-m, a_c_start, a_c_end-m, b_r_start, b_r_end-m, b_c_start+m, b_c_end);
recursive_matrix_multiply(l, m, A, B, C2, a_r_start, a_r_end-m, a_c_start+m, a_c_end, b_r_start+m, b_r_end, b_c_start+m, b_c_end);
matrix_addition(m, C1, C2, C, 0, m);
recursive_matrix_multiply(l, m, A, B, C1, a_r_start+m, a_r_end, a_c_start, a_c_end-m, b_r_start, b_r_end-m, b_c_start, b_c_end-m);
recursive_matrix_multiply(l, m, A, B, C2, a_r_start+m, a_r_end, a_c_start+m, a_c_end, b_r_start+m, b_r_end, b_c_start, b_c_end-m);
matrix_addition(m, C1, C2, C, m, 0);
recursive_matrix_multiply(l, m, A, B, C1, a_r_start+m, a_r_end, a_c_start, a_c_end-m, b_r_start, b_r_end-m, b_c_start+m, b_c_end);
recursive_matrix_multiply(l, m, A, B, C2, a_r_start+m, a_r_end, a_c_start+m, a_c_end, b_r_start+m, b_r_end, b_c_start+m, b_c_end);
matrix_addition(m, C1, C2, C, m, m);
}
}
int main()
{
int A[4][4] = {{1, 2, 3, 4}, {4, 3, 2, 1}, { 0, 0, 1, 1 }, {1, 1, 0, 0}};
int B[4][4] = {{2, 2, 4, 4}, {4, 1, 1, 4}, {1, 0, 1, 0}, {1, 0, 1, 0}};
int C[4][4];
recursive_matrix_multiply(4, 4, A, B, C, 0, 3, 0, 3, 0, 3, 0, 3);
for(size_t i = 0; i < 4; ++i) {
for(size_t j = 0; j < 4; ++j) {
printf("C[%i][%i] = %i, ", i, j, C[i][j]);
}
printf("\n");
}
return 0;
}
It currently works as intended but I'm looking to beautify this a bit. Even an improvement on variable naming would be appreciated. I'm trying to build up a portfolio so if you saw this from a professional standpoint what would you think?