Matrix Multiplication: A Beginner’s Guide to Understand and Implement
Matrix multiplication is a binary operation whose output is also a binary operation. If A and B are two matrices of order m x n and n x p, then the order of the output matrix will be m x p. In this article, we will discuss the rule to multiply two matrices, properties of matrix multiplication and how to implement it in Python.
A matrix is a rectangular arrangement of rows and columns, and if a matrix has m rows and n columns, then it is said to be of a matrix of order m x n. It is one of the most important topics of Linear Algebra, which is most commonly used in data science to store data and solve linear equations systems. In this article, we will discuss one of the algebraic operations of the Matrix, i.e., Matrix Multiplication in Python.
Must Check: Top Math Courses for Data Science
Table of Content
Best-suited Maths for Data Science courses for you
Learn Maths for Data Science with these high-rated online courses
Matrix Multiplication
Matrix multiplication is a binary operation whose output is also a binary operation. If A and B are two matrices of order m x n and n x p, then the product will be represented by:
AB = X,
and the order of X will be m x p.
Note: AB is defined if and only if the number of columns of a matrix A is equal to the number of rows of B.
Rule to Multiply two Matrices.
Let A and B be two matrices of order m x n and n x p, such that:
Now, let’s take some examples to get a better understanding of matrix multiplication.
Examples
1: Product of Square Matrices
Must Check: Square Matrix
2: Product of Non-Square Matrices
3: Product of a column and row matrix
Properties of Matrix Multiplication
- Matrix multiplication is non-commutative.
- i.e., if A and B are two matrices, then AB != BA.
- Matrix multiplication is associative in nature.
- i.e., (AB)C = A(BC)
- Matrix multiplication is distributive in nature.
- i.e., (A + B) C = AC + BC and A (B + C) = AB + AC
- Identity Property of Multiplication
- i.e., IA = AI = A, where I is an Identity matrix.
- Multiplicative Property of Zero
- If a matrix is multiplied by a zero matrix, the result will be a zero matrix.
- The product of two non-zero matrices may be a zero matrix.
- If AB = 0, it doesn’t imply that A = 0 or B = 0.
- Determinant
- det (AB) = det (A) det (B)
- Transpose
- (AB)T = BTAT
Matrix Multiplication in Python
# encoding the two matricesA = [[1,2,3],[4, 5, 6],[7,8, 9]]B = [[3,4],[5, 6],[7, 8]] # retrieving the sizes/dimensions of the matricesm1 = len(A)n1 = len(A[0]) m2 = len(B)n2 = len(B[0]) if(n1!=m2): print("Error! Matrix sizes are not compatible") quit() # creating the product matrix of dimensions p×r# and filling the matrix with 0 entriesC = []for row in range(m1): curr_row = [] for col in range(n2): curr_row.append(0) C.append(curr_row) # performing the matrix multiplicationfor i in range(m1): for j in range(n2): curr_val = 0 for k in range(n1): curr_val += A[i][k]*B[k][j] C[i][j] = curr_val print(C)
Output
What is Programming | What is Python |
What is Data Science | What is Machine Learning |
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Conclusion
In this article, we have discussed how to multiply two matrices, the properties of matrix multiplication, and finally, how to implement matrix multiplication in Python.
Hope this article will help you to know more about matrix multiplication.
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio