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
[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)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)