Saturday, December 28, 2024
Google search engine
HomeLanguagesPython | Nth column Matrix Product

Python | Nth column Matrix Product

Sometimes, while working with Python Matrix, we may have a problem in which we require to find the product of a particular column. This can have a possible application in day-day programming and competitive programming. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using loop + zip() 

This task can be solved using a combination of the above functions. In this, we pass in the zip() the list, to access all columns and loop to get the product of columns. 

Step-by-step approach:

  1. Define a function prod(val) which takes a list val as input and returns the product of all the elements of the list.
  2. Define a 2D list test_list containing some values.
  3. Print the original list.
  4. Initialize a variable N with the value 2.
  5. Use a list comprehension to calculate the product of each column of the 2D list. To achieve this, we use the zip() function to group the elements of each column together, and then pass each group of elements to the prod() function to calculate their product. This is done using a loop that iterates over the zip(*test_list) which returns the transpose of the matrix.
  6. The result is a list containing the product of each column. The Nth element of this list is the product of the Nth column of the matrix.
  7. Print the product of the Nth column of the matrix.

Python3




# Python3 code to demonstrate working of
# Nth column Matrix Product
# using loop + zip()
 
 
def prod(val):
    res = 1
    for ele in val:
        res *= ele
    return res
 
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Matrix Product
# using loop + zip()
res = [prod(idx) for idx in zip(*test_list)][N]
 
# printing result
print("Product of Nth column is : " + str(res))


Output

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Nth column is : 56

Time Complexity: O(N)
Auxiliary Space: O(1)

Method #2: Using loop 

This is a brute-force way to solve this problem. In this, we iterate through the matrix and take the product of the column by testing the column number. 

step-by-step approach 

  1. Initialize a nested list test_list with integer elements.
  2. Print the original list test_list.
  3. Initialize a variable N to specify the column index for which to calculate the product.
  4. Initialize a variable res to 1, to hold the product of the Nth column elements.
  5. Iterate over each sub-list in test_list using a for loop.
  6. For each sub-list, iterate over each element using a for loop.
  7. Check if the current element index is equal to N.
  8. If the index is equal to N, multiply the current element value with the value of res.
  9. Print the final product value of the Nth column.

Python3




# Python3 code to demonstrate working of
# Nth column Matrix Product
# Using loop
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Matrix Product
# Using loop
res = 1
for sub in test_list:
    for idx in range(0, len(sub)):
        if idx == N:
            res = res * sub[idx]
 
# printing result
print("Product of Nth column is : " + str(res))


Output

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Nth column is : 56

Time Complexity: O(N2)
Auxiliary Space: O(1)

Method #3: Using Indexing and Single loop 

Python3




# Python3 code to demonstrate working of
# Nth column Matrix Product
# Using loop
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Matrix Product
# Using loop
res = 1
for i in test_list:
    res = res * i[N]
 
# printing result
print("Product of Nth column is :" + str(res))


Output

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Nth column is :56

Time Complexity: O(N)
Auxiliary Space: O(1)

Method #4: Using reduce() 

Python3




# Python3 code to demonstrate working of
# Nth column Matrix Product
# using reduce()
 
# import functools for reduce()
import functools
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Matrix Product
# using reduce()
res = functools.reduce(lambda a, b: a * b[N], test_list, 1)
 
# printing result
print("Product of Nth column is : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Nth column is : 56

Time Complexity: O(N)
Auxiliary Space: O(1)

Method #5: Using NumPy

Use numpy.prod() function to find the product of all elements in a given axis.

Approach:

  1. Import NumPy library.
  2. Convert the list into a NumPy array.
  3. Extract the Nth column using array slicing.
  4. Find the product of elements in the Nth column using numpy.prod() function.
  5. Print the result.

Python3




import numpy as np
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Matrix Product using NumPy
arr = np.array(test_list)
res = np.prod(arr[:, N])
 
# printing result
print("Product of Nth column is :" + str(res))


Output:

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Nth column is :56

Time complexity: O(N), where N is the number of elements in the Nth column.
Auxiliary space: O(N), as we are creating a NumPy array to store the input list.

Method 6: Using itertools.islice() and map()

Step-by-step approach:

  1. Import the itertools library.
  2. Define the input matrix as a list of lists.
  3. Use itertools.islice() to extract the Nth column of the matrix as an iterator.
  4. Use map() and the prod() function (defined in Method #1) to calculate the product of the Nth column.
  5. Convert the result to a list and extract the first (and only) element.
  6. Print the result.

Python3




# Python3 code to demonstrate working of
# Nth column Matrix Product
# using itertools.islice() and map()
 
import itertools
 
 
def prod(val):
    res = 1
    for ele in val:
        res *= ele
    return res
 
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Matrix Product
# using itertools.islice() and map()
res = list(map(prod, itertools.islice(
    ((row[N],) for row in test_list), len(test_list))))
 
# extract the first (and only) element of the result
res = res[0]
 
# printing result
print("Product of Nth column is : " + str(res))


Output

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Product of Nth column is : 7

Time complexity: O(n), where n is the number of elements in the Nth column.
Auxiliary space: O(1), as only one variable (res) is used to store the result.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments