You’ll start by learning the condition for valid matrix multiplication and write a custom Python function to multiply matrices. Next, you will see how you can achieve the same result using nested list comprehensions. Finally, you’ll proceed to use NumPy and its built-in functions to perform matrix multiplication more efficiently.

How to Check if Matrix Multiplication is Valid

Before writing Python code for matrix multiplication, let’s revisit the basics of matrix multiplication. You’d have likely come across this condition for matrix multiplication before. However, have you ever wondered why this is the case? Well, it’s because of the way matrix multiplication works. Take a look at the image below. In our generic example, matrix A has m rows and n columns. And matrix B has n rows and p columns.

What is the Shape of the Product Matrix?

So to get an element at a particular index in the resultant matrix C, you’ll have to compute the dot product of the corresponding row and column in matrices A and B, respectively. Repeating the process above, you’ll get the product matrix C of shape m x p—with m rows and p columns, as shown below. And the dot product or the inner product between two vectors a and b is given by the following equation. Let’s summarize now:

It’s evident that the dot product is defined only between vectors of equal length.So for the dot product between a row and a column to be valid—when multiplying two matrices—you’d need them both to have the same number of elements.In the above generic example, every row in matrix A has n elements. And every column in matrix B has n elements too.

If you take a closer look, n is the number of columns in matrix A, and it’s also the number of rows in matrix B. And this is precisely the reason why you need the number of columns in matrix A to be equal to the number of rows in matrix B. I hope you understand the condition for matrix multiplication to be valid and how to obtain each element in the product matrix. Let’s proceed to write some Python code to multiply two matrices.

Write a Custom Python Function to Multiply Matrices

As a first step, let us write a custom function to multiply matrices. This function should do the following:

Accept two matrices, A and B, as inputs.Check if matrix multiplication between A and B is valid.If valid, multiply the two matrices A and B, and return the product matrix C. Else, return an error message that the matrices A and B cannot be multiplied.

Step 1: Generate two matrices of integers using NumPy’s random.randint() function. You can also declare matrices as nested Python lists. Step 2: Go ahead and define the function multiply_matrix(A,B). This function takes in two matrices A and B as inputs and returns the product matrix C if matrix multiplication is valid.

Parsing the Function Definition

Let’s proceed to parse the function definition. Declare C as a global variable: By default, all variables inside a Python function have local scope. And you cannot access them from outside the function. To make the product matrix C accessible from outside, we’ll have to declare it as a global variable. Just add the global qualifier before the variable name. Check if matrix multiplication is valid: Use the shape attribute to check if A and B can be multiplied. For any array arr, arr.shape[0] and arr.shape[1] give the number of rows and columns, respectively. So if A.shape[1] == B.shape[0] checks if matrix multiplication is valid. Only if this condition is True, the product matrix will be computed. Else, the function returns an error message. Use nested loops to compute values: To compute the elements of the resultant matrix, we have to loop through the rows of matrix A, and the outer for loop does this. The inner for loop helps us loop through the column of matrix B. And the innermost for loop helps access each element in the selected column. ▶️ Now that we’ve learned how the Python function to multiply matrices works, let’s call the function with the matrices A and B that we generated earlier. As matrix multiplication between A and B is valid, the function multiply_matrix() returns the product matrix C.

Use Python Nested List Comprehension to Multiply Matrices

In the previous section, you wrote a Python function to multiply matrices. Now, you’ll see how you can use nested list comprehensions to do the same. Here’s the nested list comprehension to multiply matrices. At first, this may look complicated. But we’ll parse the nested list comprehension step by step. Let’s focus on one list comprehension at a time and identify what it does. We’ll use the following general template for list comprehension: ▶️ Check out our guide List Comprehension in Python – with Examples to gain an in-depth understanding.

Nested List Comprehension Explained

Step 1: Compute a single value in the matrix C Given row i of matrix A and column j of matrix B, the below expression gives the entry at index (i, j) in matrix C. If i = j = 1, the expression will return entry c_11 of the matrix C. So you can get one element in one row this way. Step 2: Build one row in the matrix C Our next goal is to build an entire row. For row 1 in matrix A, you’ve to loop through all columns in matrix B to get one complete row in matrix C. Go back to the list comprehension template.

Replace with the expression from step 1, because that’s what you want to do. Next, replace with B_col—each column in matrix B.Finally, replace with zip(*B)—the list containing all columns in matrix B.

And here is the first list comprehension. Step 3: Build all rows and obtain the matrix C Next, you’ll have to populate the product matrix C by computing the rest of the rows. And for this, you’ve to loop through all rows in matrix A. Go back to the list comprehension yet again, and do the following.

Replace with the list comprehension from step 2. Recall that we computed an entire row in the previous step. Now, replace with A_row—every row in matrix A.And your is the matrix A itself, as you’re looping through its rows.

And here’s our final nested list comprehension.🎊 It’s time to verify the result! ✔ If you take a closer look, this is equivalent to the nested for loops we had earlier—just that it’s more succinct. You can also do this all the more efficiently using some built-in functions. Let’s learn about them in the next section.

Use NumPy matmul() to Multiply Matrices in Python

The np.matmul() takes in two matrices as input and returns the product if matrix multiplication between the input matrices is valid. Notice how this method is simpler than the two methods we learned earlier. In fact, instead of np.matmul(), you can use an equivalent @ operator, and we’ll see that right away.

How to Use @ Operator in Python to Multiply Matrices

In Python, @ is a binary operator used for matrix multiplication. It operates on two matrices, and in general, N-dimensional NumPy arrays, and returns the product matrix. Here’s how you can use it. Notice that the product matrix C is the same as the one we obtained earlier.

Can You Use np.dot() to Multiply Matrices?

If you’ve ever come across code that uses np.dot() to multiply two matrices, here’s how it works. You’ll see that np.dot(A, B) also returns the expected product matrix. However, as per NumPy docs, you should use np.dot() only to compute the dot product of two one-dimensional vectors and not for matrix multiplication. As NumPy implicitly broadcasts this dot product operation to all rows and all columns, you get the resultant product matrix. But to keep your code readable and avoid ambiguity, use np.matmul() or the @ operator instead.

Conclusion

🎯 In this tutorial, you’ve learned the following.

Condition for matrix multiplication to be valid: number of columns in matrix A = number of rows in matrix B.How to write a custom Python function that checks if matrix multiplication is valid and returns the product matrix. The body of the function uses nested for loops.Next, you learned how to use nested list comprehensions to multiply matrices. They’re more succinct than for loops but are prone to readability issues.Finally, you learned to use NumPy built-in function np.matmul() to multiply matrices and how this is the most efficient in terms of speed.You also learned about the @ operator to multiply two matrices in Python.

And that wraps up our discussion on matrix multiplication in Python. As a next step, learn how to check if a number is prime in Python. Or solve interesting problems on Python strings. Happy learning!🎉

3 Ways to Multiply Matrices in Python - 333 Ways to Multiply Matrices in Python - 743 Ways to Multiply Matrices in Python - 773 Ways to Multiply Matrices in Python - 763 Ways to Multiply Matrices in Python - 343 Ways to Multiply Matrices in Python - 753 Ways to Multiply Matrices in Python - 63 Ways to Multiply Matrices in Python - 993 Ways to Multiply Matrices in Python - 663 Ways to Multiply Matrices in Python - 263 Ways to Multiply Matrices in Python - 763 Ways to Multiply Matrices in Python - 443 Ways to Multiply Matrices in Python - 953 Ways to Multiply Matrices in Python - 843 Ways to Multiply Matrices in Python - 803 Ways to Multiply Matrices in Python - 843 Ways to Multiply Matrices in Python - 703 Ways to Multiply Matrices in Python - 423 Ways to Multiply Matrices in Python - 413 Ways to Multiply Matrices in Python - 73 Ways to Multiply Matrices in Python - 903 Ways to Multiply Matrices in Python - 803 Ways to Multiply Matrices in Python - 453 Ways to Multiply Matrices in Python - 743 Ways to Multiply Matrices in Python - 223 Ways to Multiply Matrices in Python - 213 Ways to Multiply Matrices in Python - 88