Open In App

Matrix Product - Python

Last Updated : 03 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of calculating the matrix product in Python involves finding the product of all elements within a matrix or 2D list . This operation requires iterating through each element in the matrix and multiplying them together to obtain a single cumulative product. For example, given a matrix a = [[1, 4, 5], [7, 3], [4], [46, 7, 3]], the task would be to multiply all elements to get the final product as 5408640.

Using numpy.prod()

numpy.prod() from numpy library, which is specifically designed for handling large numerical arrays efficiently. It calculates the product of all elements in an array or a list, making it an ideal method for computing the matrix product.

Python
import numpy as np
a = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]

b = [ele for sub in a for ele in sub]  # Flattening
res = np.prod(b)

print(res)

Output
1622880

Explanation: b = [ele for sub in a for ele in sub] flattens the matrix a into a one-dimensional list b by extracting all elements. Then, res = np.prod(b) calculates the product of all elements in b.

Using functools.reduce()

functools.reduce() applies a binary function cumulatively to the elements of an iterable, effectively reducing the iterable to a single value. It can be used to compute the matrix product in Python by multiplying all elements of the matrix.

Python
from functools import reduce
a = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]

b = [ele for sub in a for ele in sub] # Flattening
res = reduce(lambda x, y: x * y, b)
print(res)

Output
1622880

Explanation: b = [ele for sub in a for ele in sub] flattens the matrix a into a one-dimensional list b. Then, reduce(lambda x, y: x * y, b) multiplies all elements of b cumulatively

Using list comprehension

List comprehension calculating the matrix product involves manually iterating over the matrix, flattening it and multiplying all elements together. This method is straightforward but doesn't offer the optimizations found in the previous methods.

Python
import math

a = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
b = [ele for sub in a for ele in sub]
res = math.prod(b)
print(res)

Output
1622880

Explanation: b = [ele for sub in a for ele in sub] flattens the matrix a into a one-dimensional list b. Then, reduce(lambda x, y: x * y, b) multiplies all elements of b cumulatively .

Using nested loop

Nested loop approach is similar to the list comprehension method but without the compactness and efficiency of list comprehensions. It's the most basic way of calculating the matrix product by directly iterating through all elements.

Python
a = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
res = 1 # Initializing the result variable

for sub in a:
    for ele in sub:
        res *= ele
print(res)

Output
1622880

Explanation: outer loop iterates through each sublist in a and the inner loop accesses each element. res *= ele updates the cumulative product .


Next Article

Similar Reads