Matrix Multiplication: A Beginner’s Guide to Understand and Implement

Matrix Multiplication: A Beginner’s Guide to Understand and Implement

2 mins read954 Views Comment Call 8585951111Call 8585951111Got Doubts?
Updated on Apr 12, 2024 11:01 IST
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 rule to multiply two matrices, properties of matrix multiplication and how to implement it in python.

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.

2022_12_MicrosoftTeams-image-98-2.jpg

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

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.

Stay updated with the latest blogs on online courses and skills
Enter Mobile Number

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 

2022_12_image-44.jpg

Must Check: Square Matrix

2: Product of Non-Square Matrices 

2022_12_image-45.jpg
2022_12_image-46.jpg

3Product of a column and row matrix

2022_12_image-47.jpg

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
Types of Matrices Transpose of a Matrix
Symmetric Matrix Skew-Symmetric Matrix 

 

Matrix Multiplication in Python

# encoding the two matrices
A = [[1,2,3],[4, 5, 6],[7,8, 9]]
B = [[3,4],[5, 6],[7, 8]]
# retrieving the sizes/dimensions of the matrices
m1 = 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 entries
C = []
for row in range(m1):
curr_row = []
for col in range(n2):
curr_row.append(0)
C.append(curr_row)
# performing the matrix multiplication
for 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)
Copy code

Output

2022_12_image-59.jpg

 

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.

All About Skew Symmetric Matrix
All About Skew Symmetric Matrix
A skew-symmetric matrix is a square matrix whose transpose is equal to its negative. In other words, it is a matrix that satisfies the condition A^T = -A. This type...read more

All about Symmetric Matrix
All about Symmetric Matrix
A matrix is a rectangular arrangement of numbers (real or complex) or symbols arranged in rows and columns. The number in the matrix are called the elements, and if the...read more

Types of Matrix
Types of Matrix
In Linear Algebra, Matrices are one of the most important topics of mathematics. The application of matrix is not just limited to mathematical solving problems; it has its applications across...read more

Adjacency Matrix For Graphs
Adjacency Matrix For Graphs
An Adjacency Matrix is a method of representing graphs in matrix form. The adjacency matrix plays a vital role in describing finite graphs, making them easier to understand and compact...read more

Transpose of a Matrix
Transpose of a Matrix
Transpose of a matrix is a matrix flipped over its main diagonal, switching the matrix’s rows and column indices. In this article, we will briefly discuss what transpose of a...read more

Confusion Matrix in Machine Learning
Confusion Matrix in Machine Learning
Are you tired of your AI models getting confused? Untangle their mysteries with the Confusion Matrix, your secret weapon for accuracy! Decode True Positives, False Negatives, and more to uncover...read more

Matrix Multiplication: A Beginner’s Guide to Understand and Implement
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...read more

About the Author
qna

Comments