A detailed guide to numpy.matmul() function (4 examples)

Updated: February 25, 2024 By: Guest Contributor Post a comment

Introduction

In the world of computational mathematics and data science, matrix multiplication is a cornerstone operation. Numpy, Python’s fundamental package for scientific computing, offers a highly optimized function for this operation: matmul(). This tutorial offers an in-depth exploration of the matmul() function, with a gradient of examples from basic to more sophisticated uses.

Matrix multiplication is not merely an academic exercise; it’s pivotal in fields spanning from physics to deep learning. Understanding how to efficiently perform these operations in Python using Numpy can greatly enhance the performance of applications.

Syntax of numpy.matmul()

The numpy.matmul() function returns the matrix product of two arrays. While similar to the dot product, matmul() differs in its handling of two-dimensional arrays, treating them as matrices rather than mere arrays of vectors. It’s essential in operations requiring explicit matrix products, like in certain linear algebra calculations. matmul() has syntax:

numpy.matmul(x1, x2, /, out=None, *, casting='same_kind', order='K', dtype=None, subok=True)

Let’s dive into examples illustrating its power and versatility.

Example 1: Basic Matrix Multiplication

Our journey begins with the fundamentals. The producto of two matrices. Consider matrices A and B:

import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

result = np.matmul(A, B)
print(result)

This prints:

[[19 22]
 [43 50]]

Here, result is the matrix product of A and B, calculated row by column.

Example 2: Vector and Matrix Multiplication

matmul() is not limited to matrices alone; it operates on vectors and matrices too. Multiplying a matrix by a vector:

import numpy as np

C = np.array([[1, 2], [3, 4], [5, 6]])
v = np.array([2, 3])

result = np.matmul(C, v)
print(result)

Outputs:

[ 8 18 28]

This shows how a two-dimensional matrix and a one-dimensional vector can interact, yielding a one-dimensional output akin to a transformed vector.

Example 3: Batch Matrix Multiplication

The true versatility of matmul() is seen in handling batches of matrices, a common requirement in data science and machine learning for operations on datasets or neural network weights. Performing batched matrix multiplication:

import numpy as np

D = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
E = np.array([[[9, 8], [7, 6]], [[5, 4], [3, 2]]])

result = np.matmul(D, E)
print(result)

Yields:

[[[ 23  18]
  [ 55  42]]

 [[127  82]
  [187 122]]]

This demonstrates matmul()‘s capability to process multiple pairs of matrices simultaneously, greatly improving computational efficiency in batch operations.

Example 4: Broadcasting in Matrix Multiplication

Our final example showcases matmul()‘s support for broadcasting, allowing for the multiplication of matrices with certain shapes that don’t exactly match but are compatible under NumPy’s broadcasting rules. Multiplying a batch of matrices with a single matrix:

import numpy as np

F = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
G = np.array([[1, 2], [3, 4]])

result = np.matmul(F, G)
print(result)

Results in:

[[[  7  10]
  [ 15  22]]

 [[ 23  34]
  [ 31  46]]]

This illustrates the power of numpy.matmul() to intuitively handle operations that, while complex in theoretical linear algebra, become straightforward in practical application.

Conclusion

The numpy.matmul() function is a powerful tool for anyone working with linear algebra or needing efficient matrix computations in Python. Through these examples, ranging from basic pairwise multiplication to advanced batch and broadcasting operations, we’ve seen how matmul() facilitates complex calculations with ease. Whether for academic, industrial, or research applications, mastering numpy.matmul() can significantly enhance your proficiency with Python’s Numpy library and mathematical computing.