Displayed below is an incomplete code for matrix addition, scalar multiplication, and multiplication. Please modify the code below such that: When "add" is read from the input from file1.txt, matrix addition will be performed. When "scalarMultiply" is read from the input from file1.txt, matrix scalar multiplication will be performed. When "multiply" is read from the input from file1.txt, matrix multiplication will be performed. The result for the called matrix operation is printed to output.txt. Sample input 1: add 2 3

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

[Python (py3)]

Displayed below is an incomplete code for matrix addition, scalar multiplication, and multiplication. Please modify the code below such that:

  • When "add" is read from the input from file1.txt, matrix addition will be performed.
  • When "scalarMultiply" is read from the input from file1.txt, matrix scalar multiplication will be performed.
  • When "multiply" is read from the input from file1.txt, matrix multiplication will be performed.

The result for the called matrix operation is printed to output.txt.

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 1.1:
add
2 2 
53 -4
7 31
2 3
67 2 1
-34 6 2

Sample Output 1.1:
Matrix addition cannot be performed; dimensions are unequal.

________________________________________________________________________________

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 3.1:
multiply
3 3 
34 10 3
7 8 34
6 2 12
2 2
1 2
3 4

Sample output 3.1
Matrix multiplication cannot be performed; number of columns of Matrix A is not equal to number of rows of Matrix B.

The incomplete code that I am asking you to complete and modify is displayed below:

import numpy as np
import sys

#Matrix Addition

f1 = open("file1.txt","r")
lines = f1.readlines()
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.")

# opening the file in write mode
f2 = open("output.txt","w")
s = np.add(mat1,mat2)
# 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()

#Matrix Scalar Multiplication and Multiplication

f1 = open("file1.txt","r")

def custom_print(result):
    f2 = open("output.txt","w")
    #print(result.shape)
    
    for i in range(result.shape[0]):
        line =""
        for j in range(result.shape[1]):
            line = line + str(result[i,j])+" "
        f2.write(line)
        f2.write("\n")
    f2.close()
    

lines = f1.readlines()
dim = lines[1].split()
mat1 = np.zeros(0,int)
#print(mat1)
k=2
for i in range(int(dim[0])):
    #print("i=%d, retrieving line %d" %(i,k))
    l = [int(x) for x in lines[k].split()]
    mat1 = np.append(mat1,l)
    k += 1
    #print(k)
#print(mat1)
mat1 = mat1.reshape((int(dim[0]),int(dim[1])))
#print(mat1)
#print(mat1.shape)

dim2 = lines[k].split()
k += 1

result = 0
scalar = True
if len(dim2) != 1:
    scalar = False
    if int(dim[1]) != int(dim2[0]):
        sys.exit("Matrix multiplication cannot be performed; dimensions are incompatible.")
else:
    scalar = True
    result = mat1*int(dim2[0])
    #print(result)
    custom_print(result)
    sys.exit("completed")

mat2 = np.zeros(0,int)
for i in range(int(dim2[0])):
    #print("i=%d, retrieving line %d" %(i,k))
    #print(lines[k])
    l = [int(x) for x in lines[k].split()]
    mat2 = np.append(mat2,l)
    k += 1
    #print(k)
#print(mat2)
if len(dim2) != 1:
    mat2 = mat2.reshape((int(dim2[0]),int(dim2[1])))

print(mat2)
#print(mat2.shape)  

result = mat1.dot(mat2)
#print(result)
custom_print(result)

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY