Last Updated on May 18, 2023 by Prepbytes
Matrix multiplication in C is a fundamental operation that involves multiplying two matrices to produce a resultant matrix. Unlike matrix addition, matrix multiplication has stricter criteria and requires careful handling of rows and columns. In this article, we explore the criteria and method for matrix multiplication and provide a C program for performing the operation. Understanding matrix multiplication is essential for various applications in mathematics and computer science.
Matrix Multiplication in C
Matrix multiplication is more complex than matrix addition. Consider the 2 matrices being added as shown below
So, the matrix addition is pretty simple. If both matrices have the same number of rows and columns, they can be added. This is called the matrix addition rule or matrix addition criteria. They are added by adding their corresponding values.
However, the criteria for multiplying two matrices is not that simple. Consider the two matrices shown below.
So, we have a matrix of size 2×3 i.e. 2 rows and 3 columns and we have another matrix of size 3×3 i.e., 3 rows and 2 columns. These 2 matrices cannot be added as they don’t have the same number of rows and columns. But, what if we say that these matrices can be multiplied?
Yes, in fact, these matrices can be multiplied. The matrix multiplication criteria, or the rule to multiply 2 matrices, are as follows.
For two matrices to be multiplied, the number of columns of the first matrix must be equal to the number of rows of the second matrix, and the resultant matrix will be of the order r1x c2, where r1 is the number of rows of the first matrix and c2 is the number of columns of the second matrix.
So, we have understood the matrix multiplication criteria. However, we still need to understand the method to multiply the matrices. So, let us now study the method to multiply matrices.
Matrix Multiplication
Consider the matrices shown below.
So, since the number of columns of the first matrix is equal to the number of rows of the second matrix, these 2 matrices can be multiplied, and the resultant will be of the size row1 x col2 = 2 x 3.
Now, the question is how do I get the answer to multiplication?
For calculating each element of the resultant matrix, we need to multiply the corresponding rows and columns of the 2 input matrices. For instance, if we need to find the element a12, we will take the first row of the first matrix and the second column of the second matrix and multiply their corresponding elements.
If we take a23, we will take the second row of the first matrix and the third column of the second matrix, and multiply their corresponding elements.
Let us multiply the 2 matrices shown below. The step-by-step multiplication for the calculation of each element of the resultant matrix is as follows.
So, to get the element a11, we will multiply the first row of the first matrix with the first column of the second matrix as shown below.
Now, we will calculate the next element i.e. a12. For that, we will multiply the first row of the first matrix with the second column of the second matrix as shown below.
Now, we will calculate a13 by multiplying the first row of the first matrix with the third column of the second matrix as shown below.
Now, we will find out the value of a21. For that, we multiply the second row of the first matrix with the first column of the second matrix as shown below.
Now, to calculate the value of a22, we will multiply the second row of the first matrix with the second column of the second matrix as shown below.
Finally, to calculate the value of a23, we will multiply the second row of the first matrix by the third column of the second matrix.
So, this is how matrix multiplication takes place. So, now that we have understood the complete procedure of matrix multiplication, let us now write the matrix multiplication program in C.
Matrix Multiplication Program in C
The matrix multiplication program in C is shown below.
# include <stdio.h> # include <stdlib.h> void input(int arr[][10], int m, int n) { int i, j; printf("\nEnter elements of matrix:\n"); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) { printf("Enter elements a%d%d: ", i + 1, j + 1); scanf("%d", & arr[i][j]); } } } void display(int arr[][10], int m, int n) { int i, j; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("%d", arr[i][j]); } } } void multiply(int a[][10], int b[][10], int c[][10], int m, int n, int p, int q) { int i, j, k; for (i = 0; i < m; ++i) { for (j = 0; j < q; ++j) { c[i][j] = 0; } } for (i = 0; i < m; ++i) { for (j = 0; j < q; ++j) { for (k = 0; k < n; ++k) { c[i][j] += a[i][k] * b[k][j]; } } } } int main(){ int m, n, p, q, i, j, k; int a[10][10], b[10][10], res[10][10]; printf("Enter the no of rows and cols of first matrix\n"); scanf("%d%d", & m, & n); printf("Enter the number of rows and cols of the second matrix\n"); scanf("%d%d", & p, & q); if (n != p) { printf("Matrix is incompatible for multiplication\n"); } else { printf("Enter the elements of Matrix-A:\n"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { scanf("%d", & a[i][j]); } } printf("Enter the elements of Matrix-B:\n"); for (i = 0; i < p; i++) { for (j = 0; j < q; j++) { scanf("%d", & b[i][j]); } } for (i = 0; i < m; i++) { for (j = 0; j < q; j++) { res[i][j] = 0; for (k = 0; k < p; k++) { res[i][j] += a[i][k] * b[k][j]; } } } printf("The product of the two matrices is:-\n"); for (i = 0; i < m; i++) { for (j = 0; j < q; j++) { printf("%d\t", res[i][j]); } printf("\n"); } } return 0; }
Matrix Multiplication Algorithm in C:
So, we have to replicate the procedure studied above in the code. The algorithm for writing a matrix multiplication program in C is as follows.
- Take r1 and c1 input from the user.
- Take r1xc1 elements as input from the user. These are the elements of the first matrix.
- Take r2 and c2 input from the user.
- Take r2xc2 elements as input from the user. These are the elements of the second matrix.
- Now check for matrix multiplication criteria i.e. check if c1 equals r2. If they are not equal, print that the matrix multiplication is not possible and exit from the program.
- If they are equal, perform matrix multiplication by multiplying each column with the corresponding row to get the corresponding row elements of the resultant matrix.
- Print the resultant matrix.
Time Complexity of the Matrix Multiplication Program in C:
The time complexity of the matrix multiplication program in C is O(n3) for a square matrix or O(m x n x k) for rectangular matrices, where n is the dimension of the square matrix, m is the number of rows of the first matrix, n is the number of columns of the second matrix, and k is the common element, i.e., the number of columns of the first matrix or the number of rows of the second matrix.
Space Complexity of the Matrix Multiplication Program in C:
There is no auxiliary space used to multiply the matrices. So, the auxiliary space is O(1). However, we have multiplied the matrices and stored the result in another matrix. So, the output space is O(n2) for the square matrix and O(m x n) for rectangular matrices, where m is the number of rows of the first matrix and n is the number of columns of the second matrix.
Conclusion
Matrix multiplication in C is a crucial operation in linear algebra, with strict criteria and a method that involves multiplying corresponding rows and columns. Through this article, we have explored the criteria, method, and provided a C program for matrix multiplication. By understanding and implementing this operation, readers can gain a solid foundation in matrix manipulation, essential for various mathematical and computational tasks.
Frequently Asked Questions (FAQs)
Q1. Can I perform matrix multiplication in C without using loops?
Ans. No, matrix multiplication inherently requires the use of loops to iterate over the elements of the matrices and perform the necessary calculations.
Q2. What happens if the dimensions of the matrices don’t meet the multiplication criteria?
Ans. If the number of columns in the first matrix is not equal to the number of rows in the second matrix, matrix multiplication is not possible, and an error message should be displayed.
Q3. Is the order of matrix multiplication important?
Ans. Yes, the order of matrix multiplication matters. Changing the order of multiplication can yield different results. Matrix multiplication is not commutative, unlike matrix addition.
Q4. Are there any special libraries or functions in C for matrix multiplication?
Ans. C does not provide built-in functions specifically for matrix multiplication. However, you can implement matrix multiplication algorithms using loops and basic arithmetic operations.
Q5. What are some real-world applications of matrix multiplication in C?
Ans. Matrix multiplication is widely used in various fields, including computer graphics, scientific computing, data analysis, and machine learning. It plays a crucial role in transformations, simulations, optimization problems, and linear algebraic operations.