[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)
[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)
Step by step
Solved in 5 steps with 2 images