'''
Class: SE-'B' Computer Engineering
Roll No: (.......Your Roll No.........)
Subject : Data Structures Laboratory
Practical No: 03
Title:Write a Python program to compute following computation on matrix:
a) Addition of two matrices
b) Subtraction of two matrices
c) Multiplication of two matrices
d) Transpose of a matrix
'''
#.............................INPUTS of the Program............................
#.................Initializing No of Rows and Columns of Matrix
Rows = 3
Columns = 3
#.................Initializing values of Matrix-A
matrix_A = [ [1, 2, 3], [ 4, 5, 6 ], [7, 8, 9 ] ]
#.................Initializing values of Matrix-B
matrix_B = [ [9, 8, 7] , [6, 5, 4], [3, 2, 1] ]
#.................Initializing values of Matrix-C
matrix_C = [ [0, 0, 0] , [0, 0, 0], [0, 0, 0] ]
#.............................FUNCTIONS of the Program............................
# ..........................For printing the matrix_A
print("\n\t ..........Matrix A .............\n\t")
def print_Matrix_A():
for i in range(Rows):
for j in range(Columns):
print("\t", matrix_A[ i ] [ j ], end = " ")
print("\t")
print_Matrix_A()
# ..........................For printing the matrix_B
print("\n\t ..........Matrix B .............\n\t")
def print_Matrix_B():
for i in range(Rows):
for j in range(Columns):
print("\t", matrix_B[ i ] [ j ], end = " ")
print("\t")
print_Matrix_B()
# ..........................For printing the matrix_C
print("\n\t ..........Matrix C .............\n\t")
def print_Matrix_C():
for i in range(Rows):
for j in range(Columns):
print("\t", matrix_C[ i ] [ j ], end = " ")
print("\t")
print_Matrix_C()
#..................Function: 01 - Addition of two matrices A and B
print("\n ...........................................................................")
print(" Function: 01 - Addition of two matrices A and B")
print(" ...........................................................................")
def matrix_Add():
for i in range(Rows):
for j in range(Columns):
matrix_C[ i ] [ j ] = matrix_A[ i ] [ j ] + matrix_B [ i ] [ j ]
#...........Function 01 Calling
print("Addition : ")
matrix_Add()
print_Matrix_C()
#..................Function: 02 - Subtraction of two matrices A and B
print("\n ...........................................................................")
print("\n Function: 02 - Subtraction of two matrices A and B")
print("\n ...........................................................................")
def matrix_Sub():
for i in range(Rows):
for j in range(Columns):
matrix_C[ i ] [ j ] = matrix_A[ i ] [ j ] - matrix_B [ i ] [ j ]
#...........Function 02 Calling
print("Subtraction : ")
matrix_Sub()
print_Matrix_C()
#..................Function: 03 - Multiplication of two matrices A and B
print("\n ...........................................................................")
print("\n Function: 03 - Multiplication of two matrices A and B")
print("\n ...........................................................................")
def matrix_Multi():
for i in range(Rows):
for j in range(Columns):
matrix_C[ i ] [ j ] = 0
for k in range(Columns):
matrix_C[ i ] [ j ] = matrix_C[ i ] [ j ] + matrix_A[ i ] [ k ] * matrix_B [ k ] [ j ]
#...........Function 03 Calling
print("Multiplication : ")
matrix_Multi()
print_Matrix_C()
#..................Function: 04 - Transpose of a matrix-A
print("\n ...........................................................................")
print("\n Function: 04 - Transpose of a matrix A ")
print("\n ...........................................................................")
def transpose_A():
for i in range(Rows):
for j in range(Columns):
matrix_C[ j ] [ i ] = matrix_A[ i ] [ j ]
#...........Function 04 Calling
print("Transpose_A : ")
transpose_A()
print_Matrix_C()
#..................Function: 05 - Transpose of a matrix-B
print("\n ...........................................................................")
print("\n Function: 05 - Transpose of a matrix B ")
print("\n ...........................................................................")
def transpose_B():
for i in range(Rows):
for j in range(Columns):
matrix_C[ j ] [ i ] = matrix_B[ i ] [ j ]
#...........Function 05 Calling
print("Transpose_B : ")
transpose_B()
print_Matrix_C()
'''--------------------------OUTPUT----------------------------------------
student@ubuntu:~$ gedit DSL_Pr-03_Matrix.py
student@ubuntu:~$ python3 DSL_Pr-03_Matrix.py
..........Matrix A .............
1 2 3
4 5 6
7 8 9
..........Matrix B .............
9 8 7
6 5 4
3 2 1
..........Matrix C .............
0 0 0
0 0 0
0 0 0
...........................................................................
Function: 01 - Addition of two matrices A and B
...........................................................................
Addition :
10 10 10
10 10 10
10 10 10
...........................................................................
Function: 02 - Subtraction of two matrices A and B
...........................................................................
Subtraction :
-8 -6 -4
-2 0 2
4 6 8
...........................................................................
Function: 03 - Multiplication of two matrices A and B
...........................................................................
Multiplication :
30 24 18
84 69 54
138 114 90
...........................................................................
Function: 04 - Transpose of a matrix A
...........................................................................
Transpose_A :
1 4 7
2 5 8
3 6 9
...........................................................................
Function: 05 - Transpose of a matrix B
...........................................................................
Transpose_B :
9 6 3
8 5 2
7 4 1
student@ubuntu:~$
---------------------------------------------------------------------------------'''