Python (py3)] The code below can already solve for matrix addition. Please add the code for matrix scalar multiplication. The input will come from file1.txt, and the output should be printed to output.txt Format of the input from file1.txt: First Line: type of operation (add or scalmultiply) Second Line: matrix A dimension (example: if 3 rows and 2 columns, type 3 2) Third Line: matrix A elements Fourth Line: matrix B dimension (or the scalar number for the operation of scalar multiplication) Fifth Line: matrix B elements (not needed for scalar multiplication) Sample input 1: scalMultiply 2 2 53 -4 7 31 2 Sample output 1: 106 -8 14 62 ---------------------------------------------------------------------------------- import numpy as np import sys 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     mat1 = np.append(mat1, ls, axis=0) dim2 = lines[k].split() k += 1 if dim != dim2:     sys.exit("Matrix addition cannot be performed; dimensions are unequal.") 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     mat2 = np.append(mat2, ls, axis=0) print(np.add(mat1,mat2)) 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()

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)]

The code below can already solve for matrix addition. Please add the code for matrix scalar multiplication. The input will come from file1.txt, and the output should be printed to output.txt

Format of the input from file1.txt:
First Line: type of operation (add or scalmultiply)
Second Line: matrix A dimension (example: if 3 rows and 2 columns, type 3 2)
Third Line: matrix A elements
Fourth Line: matrix B dimension (or the scalar number for the operation of scalar multiplication)
Fifth Line: matrix B elements (not needed for scalar multiplication)

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

Sample output 1:
106 -8
14 62

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

import numpy as np
import sys

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
    mat1 = np.append(mat1, ls, axis=0)

dim2 = lines[k].split()
k += 1
if dim != dim2:
    sys.exit("Matrix addition cannot be performed; dimensions are unequal.")

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
    mat2 = np.append(mat2, ls, axis=0)
print(np.add(mat1,mat2))

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()

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

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