Python Program to Multiply Two Matrices

Posted in

Python Program to Multiply Two Matrices
vinaykhatri

Vinay Khatri
Last updated on December 5, 2024

    Here in this article, we have provided python code to multiply two matrices. It asks the user to enter the elements for 2 matrices and in the output, it shows their product (i.e. the result of the multiplication operation).

    Prerequisite Topics

    Following are the Python topics that you need to know to successfully implement the program to multiply two matrices:

    1. Looping in Python
    2. Python List Comprehension
    3. Nested for Loop
    4. Python Matrix or List

    A matrix is a 2-dimensional array, which forms with columns and rows. In Python, we use a list of lists to represent a matrix.

    Multiplication of Two Matrices Logic

    We can perform the multiplication of matrices only if both the matrices follow these 2 criteria:

    1. The number of columns of the 1st matrix must be equal to the number of rows of the 2nd matrix.
    2. The result of their multiplication will have the same number of rows as the 1st matrix and the same number of columns as the 2nd matrix.

    For instance, if the first matrix has 3 rows and 2 columns then the 2nd matrix should have 2 rows and the number of columns could be arbitrary. If we multiply an m ×n matrix by an n×p matrix, the result is an m×p matrix.

    Steps to Multiply Two Matrices in Python

    • Ask the user to enter the number of rows and columns for the first matrix.
    • Enter the elements for the first matrix.
    • Now ask the user to enter only the number of columns for the second matrix because the rows of the second matrix should be equal to the columns of the first matrix.
    • Enter the elements for the second matrix.
    • Use a nested loop inside a loop and perform the logic, result [i][j]+=matrix_a[i][k]*matrix_b[k][j] .

    Python Program to Multiply Two Matrices

    Code:

    rows_a = int(input("Enter the Number of rows  for the first matrix: " ))
    column_a = int(input("Enter the Number of Columns for the first matrix: "))
    
    print("Enter the elements of First Matrix:")
    matrix_a= [[int(input()) for i in range(column_a)] for i in range(rows_a)]
    
    print("First Matrix is: ")
    for n in matrix_a:
        print(n)
    #the number of columns of first matrix is equal to the number of rows of second matrix
    column_b = int(input("Enter the Number of Columns for the second matrix: "))
    
    print("Enter the elements of Second Matrix:")
    
    matrix_b= [[int(input()) for i in range(column_b)] for i in range(column_a)]
    for n in matrix_b:
        print(n)
        
    result=[[0 for i in range(column_b)] for i in range(rows_a)]
    
    for i in range(len(matrix_a)):
        for j in range(len(matrix_b[0])):
            for k in range(len(matrix_b)):
                result [i][j]+=matrix_a[i][k]*matrix_b[k][j]
    print("\nMatrix_a X Matrix_b is: ")
    for r in result:
        print(r)

    Output

    Enter the Number of rows for the first matrix: 2
    Enter the Number of Columns for the first matrix: 3
    Enter the elements of First Matrix:
    1
    2
    3
    4
    5
    6
    First Matrix is: 
    [1, 2, 3]
    [4, 5, 6]
    Enter the Number of Columns for the second matrix: 2
    Enter the elements of the Second Matrix:
    7
    8
    9
    10
    11
    12
    [7, 8]
    [9, 10]
    [11, 12]
    
    Matrix_a X Matrix_b is: 
    [58, 64]
    [139, 154]

    Conclusion

    So that was a simple Python program to multiply two matrices. Remember that in order to successfully carry out the matrix multiplication, ensure that the number of rows of one matrix should be equal to the number of columns of the other.

    People are also reading:

    Leave a Comment on this Post

    0 Comments