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

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

3 mins read954 Views Comment
clickHere
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Feb 3, 2023 16:28 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 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

Table of Content

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 

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.

Interested to Know how matrix multiplication is done in real life? Check out the video.

 

Top Trending Article

Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python

Interview Questions

Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |

About the Author
author-image
Vikram Singh
Assistant Manager - Content

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

Comments