In this post, you will learn the python program to multiply two matrices by taking input from the user but before writing a program let’s understand the rule for matrix multiplication and the algorithm to implement it.

Rules for Matrix multiplication

Let’s understand the rules for matrix multiplication with help of an example:

Suppose we have two matrices A of dimension mxn& B of dimension ixj
Matrix multiplication of A & B is only possible if the number of columns in the first matrix is equal to the number of rows in the second matrix (n == i).
And it gives a resultant matrix of dimension mxj.

Example:

Algorithm for Matrix multiplication in Python

1.Ask the user for the inputs of Rows & Columns of the matrix A and B.
2.if matrix_A_cols == matrix_B_rows: then
3.Create the matrix A & B by using the input() function and nested list comprehension.
4.For storing the result create another matrix (result) and initially, it is Zero.
dimension of the resultant matrix is matrix_A_rows x matrix_B_cols
5.for calculation, we use 3 nested for-loop; 1st loop for rows (i) of the matrix, 2nd loop for columns (j) of the matrix, and 3rd loop (k) for calculation of resultant matrix
that is:result[i][j] += matrix_A[i][k] * matrix_B[k][j]
6.At last print the resultant matrix (result).
7.else: print('Matrix multiplication is not possible')

From the above algorithm, we understood how to implement the python program to multiply two matrices by taking input from the user.

But before writing a program few programming concepts you have to know:

  1. List comprehension
  2. for-loop &
  3. How to take user input in python.

Source code

# matrix multiplication in python
matrix_A_rows = int(input('ENter number of rows for matrix-A: '))
matrix_A_cols  = int(input('ENter number of columns for matrix-A: '))

print() # for new line

matrix_B_rows = int(input('ENter number of rows for matrix-B: '))
matrix_B_cols  = int(input('ENter number of columns for matrix-B: '))

print() # for new line

if matrix_A_cols == matrix_B_rows:
    print('Enter values for matrix A')
    matrix_A = [[int(input(f"column {j+1} -> ENter {i+1} element:")) for j in range(matrix_A_cols)] for i in range(matrix_A_rows) ]

    print() # for new line

    print('Enter values for matrix B ')
    matrix_B = [[int(input(f"column {j+1} -> ENter {i+1} element:")) for j in range(matrix_B_cols)] for i in range(matrix_B_rows) ]

    print() #for new line

    print('Matrix-A :')
    for i in matrix_A:
        print(i)

    print() #for new line

    print('Matrix-B :')
    for i in matrix_B:
        print(i)

    # mutiplication operation

    # resultant matrix (matrix that store answer and intially it is Zero)
    result = [[0 for j in range(matrix_B_cols)] for i in range(matrix_A_rows)]

    # main logic for matrix multiplication (multiplication operation)
    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() #for new line

    print('Multiplication of Matrix-A and Matrix-B is :')
    for i in result:  #print result
        print(i)
        
else:
    print('Multiplication of matrices is not possible (columns of matrix-A = row of matrix-B)')

Output

ENter number of rows for matrix-A: 2
ENter number of columns for matrix-A: 3

ENter number of rows for matrix-B: 3
ENter number of columns for matrix-B: 2

Enter values for matrix A
column 1 -> ENter 1 element:1
column 2 -> ENter 1 element:2
column 3 -> ENter 1 element:3
column 1 -> ENter 2 element:4
column 2 -> ENter 2 element:5
column 3 -> ENter 2 element:6

Enter values for matrix B
column 1 -> ENter 1 element:10
column 2 -> ENter 1 element:11
column 1 -> ENter 2 element:20
column 2 -> ENter 2 element:21
column 1 -> ENter 3 element:30
column 2 -> ENter 3 element:31

Matrix-A :
[1, 2, 3]
[4, 5, 6]

Matrix-B :
[10, 11]
[20, 21]
[30, 31]

Multiplication of Matrix-A and Matrix-B is :
[140, 146]
[320, 335]
Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment