A fundamental operation in linear algebra, matrix multiplication is extensively utilized in a variety of disciplines, including computer science, physics, engineering, and economics. In simple terms, matrix multiplication is the process of multiplying the corresponding elements of two matrices to generate a new matrix of the same dimensions as the original matrices. In C, the matrix multiplication operation can be performed using a variety of methods, including nested loops, Strassen’s algorithm, and other more advanced techniques.
The basic principle of matrix multiplication can be represented by the following equation: C = A x B, where A, B, and C are matrices of dimensions m x n, n x p, and m x p, respectively. Each element of the C matrix is the sum of the products of corresponding elements of the A and B matrices. The basic formula for matrix multiplication is:
Here, i and j are the rows and columns indices of matrix C, and k is the common index of matrices A and B. In other words, we take the dot product of each row of matrix A with each column of matrix B to generate the corresponding element of matrix C. A matrix can be applied only when the number of rows in matrix B equals the number of columns in matrix A. The final matrix C will be composed of the same number of columns as matrix B and rows as matrix A.
Implementation of Matrix Multiplication in C Programming
Although there are other ways to implement matrix multiplication in C, the nested loops approach is the simplest. In this approach, the outer loop iterates over the rows of the first matrix while the inner loop iterates over the columns of the second matrix. The result of each multiplication operation is then added to the corresponding element of the resulting matrix. Although his approach is clear-cut and easy to understand, large matrices might make it ineffective.
#define ROW_A 3
#define COL_A 2
#define ROW_B COL_A
#define COL_B 4
void matrixMultiply(int A[][COL_A], int B[][COL_B], int C[][COL_B]) {
int i, j, k;
for (i = 0; i < ROW_A; i++) {
for (j = 0; j < COL_B; j++) {
C[i][j] = 0;
for (k = 0; k < COL_A; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
void displayMatrix(int matrix[][COL_B], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int A[ROW_A][COL_A] = {{1, 2}, {3, 4}, {5, 6}};
int B[ROW_B][COL_B] = {{7, 8, 9, 10}, {11, 12, 13, 14}};
int C[ROW_A][COL_B];
matrixMultiply(A, B, C);
printf("Matrix A:\n");
displayMatrix(A, ROW_A, COL_A);
printf("\nMatrix B:\n");
displayMatrix(B, ROW_B, COL_B);
printf("\nMatrix C (Result of A * B):\n");
displayMatrix(C, ROW_A, COL_B);
return 0;
}
The code above shows how to multiply the two matrices A and B. Matrix A has dimensions 3 x 2, while matrix B has dimensions 2 x 4. The final matrix C will be three by four in size.
The matrix multiplication algorithm is implemented by the function matrixMultiply. It iterates through the elements of matrix A and matrix B, calculating the dot product of corresponding rows and columns to obtain the respective elements of matrix C. The result is stored in matrix C.
To display the matrices A, B, and the resulting matrix C, the function displayMatrix is defined. It takes a matrix and its dimensions as input and prints the elements in a tabular format.
In the main function, we initialize the matrices A and B with predetermined values. The matrixMultiply function is then invoked with the inputs of the matrices A, B, and an empty matrix C. The code then uses the displayMatrix function to output the matrices A, B, and C.
Another approach to matrix multiplication in C is to use Strassen’s algorithm, which is a divide-and-conquer technique that involves recursively breaking down the matrices into smaller submatrices and then combining them to generate the final result. This algorithm is particularly useful for large matrices, where the traditional nested loop method can be quite slow and inefficient.
#define N 2
void strassenMultiply(int A[][N], int B[][N], int C[][N]) {
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]);
int C11 = P + S - T + V;
int C12 = R + T;
int C21 = Q + S;
int C22 = P + R - Q + U;
C[0][0] = C11;
C[0][1] = C12;
C[1][0] = C21;
C[1][1] = C22;
}
void displayMatrix(int matrix[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int A[N][N] = {{2, 4}, {1, 3}};
int B[N][N] = {{1, 3}, {2, 4}};
int C[N][N];
strassenMultiply(A, B, C);
printf("Matrix A:\n");
displayMatrix(A);
printf("\nMatrix B:\n");
displayMatrix(B);
printf("\nMatrix C (Result of A * B):\n");
displayMatrix(C);
return 0;
}
Final Thoughts
In many different domains, matrix multiplication is an essential operation in linear algebra. In C, a variety of methods are available for performing this operation, from simple nested loops to more advanced techniques such as Strassen’s algorithm and parallel computing. The choice of matrix multiplication method in C depends on the size of the matrices, the desired level of performance, and the specific application requirements. For small matrices, a simple nested loop implementation may be sufficient, while for larger matrices, more advanced techniques may be necessary to achieve acceptable performance.