SIZ EDUCATION ||MATRIX CALCULATOR || Python || by @musfirah-m

in hive-181430 •  3 years ago  (edited)

HELLO EVERYONE! I hope you all are safe and sound:)

Today I thought of sharing with you all my code in Python which I wrote for solving matrices. Through this program one can easily perform basic operations of addition, subtraction and multiplication on matrices.

image.png

KEY FEATURES:

This code enables a user to perform some of the matrix operations by taking integer values as input from user. The operations include addition, subtraction and multiplication of two matrices. It also enables the user to exit.

DATA TYPES USED:

• Nested lists
• Strings
• Integers

LIMITATIONS:

This code is applicable only for two square matrices having integer values. It can only perform addition, subtraction and multiplication.

divider purple.jpg

CODE:

x=0
while True:
    print("OPERATIONS MENU:","\n","1. Addition","\n","2. Subtraction","\n","3. Multiplication","\n","4. Exit")
    option=int(input("Enter option number:"))
    if option==4:
        break
    if option<1 or option>4:
        print("Please enter your choice according to the given function.")
        break
    a=int(input("Enter number of rows/column for the square matrix:"))
    #Making Matrix1:
    mat1=[]
    for i in range(a):
        print("Enter values for matrix 1 row ",i+1,"separated by commas:",end="")
        b=input().split(",")
        mat1.append(b)
    #Making Matrix2:
    mat2=[]
    for i in range(a):
        print("Enter values for matrix 2 row ",i+1,"separated by commas:",end="")
        b=input().split(",")
        mat2.append(b)        
    #Printing Matrix1:    
    print("Matrix 1:")
    for i in range(a):
        for j in range(a):
            print(mat1[i][j],end="  ")
        print()
    print()   
    #Printing Matrix2:    
    print("Matrix 2:")
    for i in range(a):
        for j in range(a):
            print(mat2[i][j],end="  ")
        print()
    print()    
    #Addition:
    result=[[0 for a in range(len(mat1))]for a in range(len(mat2[0]))]
    if option==1:
        for i in range(a):
            for j in range(a):
               result[i][j]=int(mat1[i][j])+int(mat2[i][j])
        print("The Answer is :")
        for i in range(a):
            for j in range(a):
               print(result[i][j],end="  ")
            print()
        print()    
    #Subtraction:
    elif option==2:
        for i in range(a):
            for j in range(a):
               result[i][j]=int(mat1[i][j]) - int(mat2[i][j])
        print("The Answer is :")
        for i in range(a):
            for j in range(a):
               print(result[i][j],end="  ")
            print()
        print()     
    #Multiplication:
    elif option==3:
        result_mat=[[0 for i in range(len(mat1))] for j in range(len(mat2[0]))] 
        for i in range(len(result_mat)):
            for j in range (len(result_mat[0])):
                for k in range(len(mat2)):
                    result_mat[i][j]+=int(mat1[i][k]) * int(mat2[k][j])
        print("The answer is :")
        for i in range(a):
            for j in range(a):
                print(result_mat[i][j],end="  ")
            print()
        print()     
    x+=1
print("..Thank you..")

divider purple.jpg

WORKING OF THE CODE:

As the user runs the program, every time, a list of operations for matrices is displayed on the screen showing the operation menu offered by the program, this is done using print statement inside the while loop. Then the user is asked to enter an option number in order to perform the desired operation. If the user enters option1,Addition is performed; if the user enters option2,Subtraction is performed; if the user enters option3,Multiplication is performed. User is then asked to enter the no. of columns/rows.If the user enters option 4, break statement is executed and “..Thank you..” message is printed. If the user enters any option number which is not in the operation menu, another break statement is executed, printing some appropriate message.

Formation of matrix 1:

An empty list is assigned to variable mat1, a for loop is used to
take input from the user which is appended in the mat1.

Formation of matrix 2:

An empty list is assigned to variable mat2, a for loop is used to
take input from the user which is appended in the mat2.

Printing matrix 1 and 2:

To print matrix 1 ,a nested for loop is used which prints the values of matrix1 by using iteration. After each row, line is changed by using print(). Matrix 2 is also printed in the same way.

divider purple.jpg

ADDITION:

A matrix is initialized having all elements zero in the variable called result. A nested for loop is used and every element of matrix 1 and matrix 2 is converted in integer and are added, which are then added to the result. The result is then printed using nested for loops. Now, again the Operation menu is displayed because of the while loop.

image.png

SUBTRACTION:

A nested for loop is used and every element of matrix 1 and matrix 2 is converted in integer. Each element of matrix2 is subtracted from matrix1 and then added to the result. The result is then printed using nested for loops.

image.png

MULTIPLICATION:

A matrix(result_mat) having all elements zero is created. Using three for loops in a nested pattern, the two matrices are multiplied. Each row of matrix1 is multiplied by each column of matrix2, and the result is printed using nested for loops.

image.png

EXIT:

If the user selects option4, the code prints a “..Thank you..” message, the code terminates.

image.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Python language is too much difficult..
And it's codes is also.
Thanks for try polished our community members with your knowledge.

I am glad it helped:)