Python program multiplication of two matrix from user input

Today, We want to share with you matrix multiplication in python user input.In this post we will show you matrix in python example, hear for matrix multiplication in python using function we will give you demo and example for implement.In this post, we will learn about How To Take Two Inputs In One Line In Python? with an example.

Matrix addition in python user input

Below is python program to multiply two matrices.

Example 1:

def print_design(design):
	for i in range(len(design)):
		for j in range(len(design[0])):
			print("\t",design[i][j],end=" ")
		print("\n")
 
def main():
	m = int( input("enter single design rows"));
	n = int( input("enter single design columns"));
	p = int( input("enter second design rows"));
	q = int( input("enter second design columns"));
	if( n != p):
		print ("matrice multipilication not possible...");
		exit();
	
#declaration of arrays
	items1=[[0 for j in range  (0 , n)] for i in range (0 , m)]
	items2=[[0 for j in range  (0 , q)] for i in range (0 , p)]
	response=[[0 for j in range  (0 , q)] for i in range (0 , m)]
 
#taking input from user
	print ("enter single design data values:" )
	for i in range(0 , m):
		for j in range(0 , n):
			items1[i][j]=int (input("enter data value"))
	print ("enter second design data values:")
	for i in range(0 , p):
		for j in range(0 , q):
			items2[i][j]=int(input("enter data value"))
	print ("single design")
	print_design(items1)
	print ("second design")
	print_design(items2)
	
#for multiplication
    # i will run throgh each row of design1
	for i in range(0 , m):
	# j will run through each column of design 2
		for j in range(0 , q):
		# k will run throguh each row of design 2
			for k in range(0 , n):
				response[i][j] += items1[i][k] * items2[k][j]
				
				
#printing response
	print ( "multiplication of two matrices:" )
	print_design(response)
	
main()

Program to multiply two matrices in Python

Example 2:

import random
jds=input("Give Any No. of rows in the single design: ")
fl1=input("Give Any No. of columns in the single design: ")
a = [[random.random() for col in range(fl1)] for row in range(jds)]
for i in range(jds):
    for j in range(fl1):
        a[i][j]=input()
lks=input ("Give Any No. of rows in the second design: ")
secPar=input ("Give Any No. of columns in the second design: ")
b = [[random.random() for col in range(secPar)] for row in range(lks)]
for i in range(lks):
    for j in range(secPar):
        b[i][j]=input()
c=[[random.random()for col in range(secPar)]for row in range(jds)]
if (fl1==lks):
    for i in range(jds):
        for j in range(secPar):
            c[i][j]=0
            for k in range(fl1):
                c[i][j]+=a[i][k]*b[k][j]
            print c[i][j],'\t',
        print
else:
    print "Multiplication not possible"

Results:

Give Any No. of rows in the single design: 3
Give Any No. of columns in the single design: 2
1
2
3
4
5
6
Give Any No. of rows in the second design: 2
Give Any No. of columns in the second design: 3
1
2
3
4
5
6
Output:
9 	12 	15 	
19 	26 	33 	
29 	40 	51 	

I hope you get an idea about python program to generate n*n matrix from user input.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment