Python Program to Multiply Two Matrices

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

In this blog post, we are going to learn about matrix multiplication and the various possible ways to perform matrix multiplication in Python.

Firstly, understanding loops, lists and other mathematical theories is essential to grasp matrix multiplication. Then, we will look at how to perform the same in Python, with and without using built-in or library functions. The version of Python used for code implementation in this article is Python 3.

The following example will help you understand the Matrix:

Method1: Matrix Multiplication in Python Using Nested Loops

Output

Explanation

  • This method encodes two matrices, A and B, as nested lists in Python and retrieves their dimensions.
  • It then checks for compatibility and initializes a product matrix C with zeros.
  • Matrix multiplication is performed using nested loops, computing each element of C as the sum of products of corresponding elements from A and B.
  • Finally, the resulting product matrix C is printed to the console.

Time complexity: O(n3)O(n^3)

Space complexity: O(n2)O(n^2)

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Method 2: Matrix Multiplication in python Using Nested List Comprehension

Let's learn the Matrix Multiplication in Python using nested list comprehension.

Explanation:

  • This implementation uses nested list comprehension to perform matrix multiplication concisely.
  • The outer list comprehension iterates over rows of matrix A.
  • The inner list comprehension iterates over columns of matrix B.
  • Each element in the resulting matrix computes the sum of products of corresponding elements from rows of A and columns of B.
  • The result is stored in a new matrix representing the product of matrices A and B.

Output:

Time complexity: O(n3)O(n^3)

Space complexity: O(n2)O(n^2)

Method 3: Matrix Multiplication in Python (Vectorized implementation)

  1. This Python code defines a function matrix_multiply that uses NumPy's np.dot() function to perform matrix multiplication.
  2. It inputs two matrices, A and B, and returns their product.
  3. The np. dot() function efficiently computes the dot product of two arrays, corresponding to matrix multiplication for 2D arrays.
  4. The result is printed to the console.

Output:

Time complexity: O(n3)O(n^3)

Space complexity: O(n2)O(n^2)

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Method 4: Using recursive matrix multiplication

Program for matrix multiplication in Python using Recursive matrix multiplication:

Output:

Time complexity: O(n3)O(n^3)

Space complexity : O(n2)O(n^2)

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • Matrix multiplication is a binary operation on two matrices, which yields yet another matrix, which is referred to as the product matrix.
  • The blog post covers four ways to multiply matrices in Python: nested loops, list comprehension, NumPy's vectorized functions, and a recursive approach.
  • Each method is explained with simple examples and clear explanations, making it easy to understand how they work and when to use them.
  • We have also learned a program for matrix multiplication in Python using different methods.

Read More

  1. Python Comprehensions
  2. Arrays in Python
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more