[Python (py3)] The code below is for matrix operations (addition, multiplication, scalar multiplication and transposition). Please annotate what happens in EACH line so that I can fully understand the code below. Some lines are already annotated. Please annotate EACH line. Do not just copy the code below and take it as the answer without modifying anything. I already encountered such case many times before.  Sample input 1: add 2 3 53 -4 1 7 31 2 2 3  67 2 2 -34 6 3 Sample output 1: 120 -2 3 -27 37 5

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

[Python (py3)]

The code below is for matrix operations (addition, multiplication, scalar multiplication and transposition). Please annotate what happens in EACH line so that I can fully understand the code below. Some lines are already annotated.

Please annotate EACH line. Do not just copy the code below and take it as the answer without modifying anything. I already encountered such case many times before. 

Sample input 1:
add
2 3
53 -4 1
7 31 2
2 3 
67 2 2
-34 6 3

Sample output 1:
120 -2 3
-27 37 5

 

________________________________________________________________________________

Sample input 2:
scalMultiply
2 2
53 -4
7 31
2

Sample output 2:
106 -8
14 62

_______________________________________________________________________
Sample input 3:
multiply
3 3 
34 10 3
7 8 34
6 2 12
3 2
1 2
3 4
5 6

Sample output 3:
79 126
201 250
72 92
_______________________________________________________________________
Sample input 4:
transpose
3 3
34 10 3
7 8 34
6 2 12

Sample output 4:
34 7 6
10 8 2 
3 34 12

------------------------------------------------------------------------------

import numpy as np
import sys
# function to add
def add(lines):
    # finding the dimesions
    dim = lines[1].split()
    k=2
    mat1 = np.empty((0,int(dim[0])), int)
    for i in range(0,int(dim[0])):
        l = lines[k].split()
        l = list(map(int, l))
        ls = np.array([l])
        k += 1
        # adding the data in one row
        mat1 = np.append(mat1, ls)
    # reshaping the into 2D array with given row and column
    mat1 = mat1.reshape(int(dim[0]),int(dim[1]))
    dim2 = lines[k].split()
    k += 1
    mat2 = np.empty((0,int(dim2[0])), int)
    for i in range(0,int(dim2[0])):
        l = lines[k].split()
        l = list(map(int, l))
        ls = np.array([l])
        k += 1
        # adding the data in one row
        mat2 = np.append(mat2, ls)
    # reshaping the into 2D array with given row and column
    mat2 = mat2.reshape(int(dim2[0]),int(dim2[1]))
    # comparing the dimensions of the matrix
    if dim != dim2:
        sys.exit("Matrix addition cannot be performed; dimensions are unequal.")
    # returning the resultant matrix
    return np.add(mat1,mat2)

 

# function to multiply
def multiply(lines):
    # finding the dimesions
    dim = lines[1].split()
    k=2
    mat1 = np.empty((0,int(dim[0])), int)
    for i in range(0,int(dim[0])):
        l = lines[k].split()
        l = list(map(int, l))
        ls = np.array([l])
        k += 1
        # adding the data in one row
        mat1 = np.append(mat1, ls)
    # reshaping the into 2D array with given row and column
    mat1 = mat1.reshape(int(dim[0]),int(dim[1]))
    dim2 = lines[k].split()
    k += 1
    mat2 = np.empty((0,int(dim2[0])), int)
    for i in range(0,int(dim2[0])):
        l = lines[k].split()
        l = list(map(int, l))
        ls = np.array([l])
        k += 1
        # adding the data in one row
        mat2 = np.append(mat2, ls)
    # reshaping the into 2D array with given row and column
    mat2 = mat2.reshape(int(dim2[0]),int(dim2[1]))
    # comparing the col of matrix 1 with row of matrix 2
    if int(dim[1]) != int(dim2[0]):
        sys.exit("Matrix multiplication cannot be performed; number of columns of Matrix A is not equal to number of rows of Matrix B.")
    # returning the product of 2 matrices
    return np.dot(mat1,mat2)

 

# function to do scalar multiplication
def  scalmultiply(lines):
    # finding the dimesions
    dim = lines[1].split()
    k=2
    mat = np.empty((0,int(dim[0])), int)
    for i in range(0,int(dim[0])):
        l = lines[k].split()
        l = list(map(int, l))
        ls = np.array([l])
        k += 1
        # adding the data in one row
        mat = np.append(mat, ls)
    # reshaping the into 2D array with given row and column
    mat = mat.reshape(int(dim[0]),int(dim[1]))
    # finding the number to be mulitplied with
    num = lines[k].split()
    return np.multiply(mat,int(num[0]))

 

# function to tranpose the matrix
def transpose(lines):
    # finding the dimesions
    dim = lines[1].split()
    k=2
    mat = np.empty((0,int(dim[0])), int)
    for i in range(0,int(dim[0])):
        l = lines[k].split()
        l = list(map(int, l))
        ls = np.array([l])
        k += 1
        # adding the data in one row
        mat = np.append(mat, ls)
    # reshaping the into 2D array with given row and column
    mat = mat.reshape(int(dim[0]),int(dim[1]))
    # returning the matrix by transposing
    return np.transpose(mat)

 

# main block
f1 = open("file1.txt","r")
lines = f1.readlines()
value = lines[0].split()
# checking the operation and calling the function
if value[0] == "add":
    s = add(lines)
if value[0] == "multiply":
    s = multiply(lines)
if value[0] == "scalMultiply":
    s = scalmultiply(lines)
if value[0] == "transpose":
    s = transpose(lines)
# opening the file in write mode
f2 = open("output.txt","w")
# write output matrix in output.txt file
for i in range(0, len(s)):
  for j in range(0, len(s[i])):
    line = str(s[i][j])+" "
    f2.write(line)
  f2.write("\n")
# close the output file
f2.close()
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
ASCII Codes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education