×

Python Matrix Multiplication

One of the most fundamental mathematical structures, matrices are used often in many disciplines, including mathematics, physics, engineering, computer science, etc.

For example, matrices and associated operations (multiplication and addition) are commonly used in deep learning and related statistical tasks to generate predictions based on inputs.

When two matrices of dimensions (a x b) and (b x c) are multiplied, the third matrix of dimension (a, c), known as the product matrix, is created as a result. This binary operation was initially introduced in 1812 by Jacques Binet.

The process for multiplying two matrices is described below. The process for multiplying two matrices is fairly straightforward, despite its apparent complexity.

To get the Two Matrices A and B's Product, or AB

  • Make sure that the first matrix, A, has the same number of rows as the number of columns of the second matrix, B. In other words, they must have dimensions that take the forms (a x b) and (b x c), respectively. The matrices cannot be multiplied if that is not the case.
Matrix Multiplication in Python
  • Create a blank product matrix in C.
  • For each i and j, repeat the following, 0<=i<a, 0<=j<b:
    • Take the ith row from A and the jth row from B. The components that are all present at the same index should be added together. For example, multiplying the first element of the ith row by the first element of the jth column, and so forth.
    • Consider the total of the items that were calculated in the previous phase.
    • Put this total in the [i, j] cell of the product matrix C.
  • As a final check, confirm that the generated product matrix has dimensions (a x c).

    Multiplication of matrices is not commutative. That is to say, AB and BA are not always equal. Additionally, it is frequently conceivable that only one or none of these items are defined.

Implementation of Code With the Use of Nested Loops

The easiest and slowest way to put the matrix multiplication program into action is through nested loops. Generally speaking, the outer loop iterates over each row of the first matrix, the second loop, which is included inside the first loop, iterates over each column of the second matrix, and the operation required to evaluate C[i][j] for a summation is carried out in the third loop.

Algorithm

  1. The matrix dimensions should be saved in several variables.
  2. Examine whether the matrices can be multiplied or not. If not, stop the program; if yes, go ahead.
  3. Utilize an index-variable I to iterate across the rows of matrix A.
  4. Use the index-variable j to iterate through the columns of matrix B inside the first loop.
  5. Set the variable curr_val to 0 now.
  6. Create a second loop using the variable k that iterates through the column dimension of A (or the row dimension of B).
  7. For each iteration of the innermost loop, add the value of A[i][k] x B[k][j] to the variable curr_val.
  8. Assign the value of curr_val to C[i][j] for each iteration of the innermost loop.

Code:

A = [[5,6,7],[1,3,2],[4,9,8]]
B = [[12,8],[9,5],[8,13]]
p = len(A)
q = len(A[0]) # retrieving the sizes/dimensions of the matrices
t = len(B)
r = len(B[0])
if(q!=t):
   print("Matrix sizes are not suitable, Error!")
   quit()
C = []
for row in range(p):
   curr_row = []
   for col in range(r):
       curr_row.append(0)
   C.append(curr_row)
for i in range(p): # performing the matrix multiplication
  for j in range(r):
       curr_val = 0
       for k in range(q):
           curr_val += A[i][k]*B[k][j]
       C[i][j] = curr_val
print(C)

output:

Matrix Multiplication in Python

With the use of List Comprehensions

Python's list comprehensions provide a clearer and easier comprehending way to make lists from other iterables like lists, tuples, strings, etc. Two subexpressions make up the list comprehension statement:

  1. The first sub-expression determines what will be displayed in the list (for example, some transformation, such as increment by 1, on each element of the iterable).
  2. The second portion is responsible for obtaining the data from the iterable so that the first section can act upon, alter, or analyze it. One or more for statements combined with zero or more if statements make up the second section.

zip() function in Python

The zip() function, in its simplest form, is used to index-based group and ungroup the data supplied as iterables (such as lists, tuples, strings, list of strings, etc.).

For example, let's say that during packing, all of the data present at index 0 across all of the input iterables will be merged into a single tuple, then the data at index 1 and another tuple, and so on. Then, a zip object representing the entirety of these tuples is returned. Unpacking is the opposite of this operation. For a better understanding, go to the code examples.

Example: The Packing Operation

Code:

m = [[10,1,6],[7,4,9],[11,3,2]]
m1 = ["arpita", "namrita", "akshita"]
re = zip(m, m1)
print("The data type of res is: ",type(re))
print("The contents of res are:",list(re))

Output:

Matrix Multiplication in Python

Example: The unpacking operation

Code:

A = [[4,6],[15,7],[9,10]]
re = zip(*A)
print("The data type of res is: ",type(re))
print("The contents of res are:",list(re))

Output:

Matrix Multiplication in Python

Numpy Library for Matrix Multiplication

A huge number of high-level mathematical functions are available in the NumPy Python library, which is highly optimized to conduct calculations on massive, multi-dimensional arrays and matrices. Thus, it should not be a surprise that it offers some capability for a fundamental matrix operation like multiplication.

We will go through the following three methods from the Numpy library that are pertinent to matrix multiplication:

  1. numpy.matmul() method or the “@” operator
  2. numpy.dot()
  3. numpy.multiply() method

Additionally, Numpy offers a few techniques that apply to vector multiplications.

It's crucial to keep in mind that all of the NumPy module's functions are vectorized during implementation, making them significantly more effective than pure Python loops. Anywhere you can attempt to substitute them for several for-loops.

numpy.matmul() or “@” operator

The product matrix is directly returned by the matmul() method, which accepts two matrices that can be multiplied. The operands must either already be of the type numpy.array or be explicitly typecast to that type for you to utilize the "@" symbol.

The methods above are represented by the code below:

import numpy as np


A = [[3,1,2],[10,3,4],[6,9,7]]
B = [[4,3],[14,8],[4,15]]
C1 = np.matmul(A, B)
C2 = np.array(A)@np.array(B)
 print(type(C1))
print(type(C2))
 assert((C1==C2).all())
print(C1)

Output:

Matrix Multiplication in Python

numpy.dot() Method

Based on the input parameters, this method offers a variety of behaviours and use cases, but it is advised that it only be used when we need the dot product of two 1D vectors.

Let's take a glance at the documentation: a two arrays dot product, Specifically,

1.     If both a and b are 1-D arrays, it is the inner product of the vectors (without complex conjugation)

2.     Although using matmul or a @ b is advised, matrix multiplication is possible if both a and b are 2-D arrays.

3.     It is equal to multiplying and using numpy if either an or b is 0-D (scalar). It is preferable to use multiply(a, b) or a*b.

Note: Both numpy.matmul() and numpy.dot() produce the same outcome for 2D matrices. Their responses vary because higher dimension matrix multiplication is involved.

Let's examine two approaches to using np.dot to code our matrix multiplication application ():

Example:

import numpy as np


A = [[3,1,2],[10,3,4],[6,9,7]]
B = [[4,3],[14,8],[4,15]]
C1 = np.matmul(A, B)
C2 = np.array(A)@np.array(B)
 print(type(C1))
print(type(C2))
 assert((C1==C2).all())
print(C1)

Output:

Matrix Multiplication in Python

You'll see that we used the assert statement once more to verify that C1 and C2 are equal matrices, cross-validating our assertion that they behave identically for 2D arrays. Here, we've discovered yet another approach for using the numpy.dot() method to determine the product of matrices A and B.

Example:

import numpy as np
A = [[12,1,4],[9,3,4],[6,5,11]]
B = [[6,4],[15,4],[8,12]]
D = list(zip(*B))
C = [[0 for _ in range(2)] for _ in range(3)]


for i in range(3):
   for j in range(2):
       C[i][j] = np.dot(A[i], D[j])
print(type(C))
print(C)

Output:]

Matrix Multiplication in Python
  • Take note of the type of matrices they produce, even if the resultant matrices in the first and second code samples have the same cell values.
  • The second method takes advantage of C[i][j], the dot product of the ith row of A and the jth column of B, is.

The np.multiply method is the last technique worth addressing, even if it has little to do with our subject.

C. numpy.multiply()

The standard matrix multiplication step is not taken by this technique (refer to the code examples). This technique only functions when the operands are ;

  1. Scalar and a matrix
  2. Two matrices with the same dimensions

According to the output C of the code presented below, this method multiplies the scalar with each matrix element in the first situation.

In the second case, this approach is used to compute the Hadamard Product, a matrix composed of the element-wise products of two matrices, A and B. In other words, C[i][j] = A[i][j]*B[i][j] for all possible values of I and j, where C is the Hadamard product matrix.

Any other scenario will lead to a mistake.

Code:

x = [[1,2,3],[4,5,6],[7,8,8]]
y = [[2,3,4],[7,5,3],[1,4,2]]


# case 1
A = np.multiply(3, x)
# case 2
B = np.multiply(x, y)


print("C = ", A)
print("D = ", B)

Output:

Matrix Multiplication in Python
  • It's also interesting to notice that the np.multiply() action can be replaced with the "*" operator, just like the np.dot() operation can. However, the operands, in this case, must also be NumPy array types or explicitly typecast into them.
  • This operator will produce an error if this requirement is not met. To further understand its application, let's examine the code sample provided below:

Code:

x = [[1,2,3],[4,5,6],[7,8,8]]
y = [[2,3,4],[7,5,3],[1,4,2]]


z = 3*np.array(x)
j = np.array(x)*np.array(y)


print("C = ", z)
print("D = ", j)
Also take note that both outputs, C and D, continue to have the same numpy.ndarray.
print(type(z))
print(type(j))

Output:

Matrix Multiplication in Python

Conclusion  

  1. The product matrix is the result of the binary operation known as matrix multiplication, which is performed on two matrices.
  2. Matrix multiplication is not commutative and can only be performed between compatible matrices.
  3. To perform the matrix multiplication of matrices A and B in Python without using any built-in methods or library functions, we iterate over all the rows of matrix A and all the columns of matrix B and retrieve the total of their element-wise products.
  4. We can eliminate the nested for loops by using Python's zip function, which allows us to do the same objective as previously with less code.
  5. Unlike the nested loop method, list comprehension is still a practical way to multiply matrices in Python.
  6. List comprehensions are typically quicker than the zip approach for creating lists, but not when computations are required.
  7. The numpy library's methods, such as numpy.matmul(), numpy.dot(), and numpy.multiply() can multiply matrices in Python significantly more quickly.

These techniques are more effective because they employ vectorization, which speeds up their execution compared to Python's explicit for-loops.

It is advised to use NumPy library methods rather than developing your own code to multiply the matrices to produce clear, understandable code and make the application effective.


Related Topics

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

Conditional Expressions in Python

In Python conditional expression are sometimes referred to operator called as ternary operator. Not only Python supports ternary operator but many other programming languages supports it. Ternary operator are the...

2 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

Python Struct

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Cx_Oracle Python with Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Algorithm for Factorial of a number in Python

What is a Factorial Number? In mathematics, the factorial of a positive integer n, denoted by n!. It is the product of all positive integers less than or equal to n. For...

3 minutes read.

Python program to check whether a given number is prime or not

Python program to check whether a given number is prime or not A positive integer greater than 1 is called a prime number if it is divisible by one and number...

1 minute read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.