[Python (py3)] Please create a code that solves for matrix addition and matrix scalar multiplication. The code for scalar multiplication is already written below, you just have to incorporate the code for matrix addition. The input will come from file1.txt, and the output should be printed to output.txt Note for matrix addition: When the dimension of Matrix A is not equal to the dimension of Matrix B, print "Matrix addition cannot be performed; dimensions are unequal." 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: add 2 2  53 -4 7 31 2 2 67 2 -34 6 Sample output 1: 120 -2 -27 37 Sample input 2: scalMultiply 2 2 53 -4 7 31 2 Sample output 2: 106 -8 14 62 ----------------------------------------------------------------------------- Code for scalar multiplication: import numpy as np import sys f1 = open("file2.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)

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

Please create a code that solves for matrix addition and matrix scalar multiplication. The code for scalar multiplication is already written below, you just have to incorporate the code for matrix addition.

The input will come from file1.txt, and the output should be printed to output.txt

Note for matrix addition: When the dimension of Matrix A is not equal to the dimension of Matrix B, print "Matrix addition cannot be performed; dimensions are unequal."

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

Sample output 1:
120 -2
-27 37


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

Sample output 2:
106 -8
14 62

-----------------------------------------------------------------------------
Code for scalar multiplication:

import numpy as np
import sys

f1 = open("file2.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 5 steps with 2 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