[Python (py3)] Using import numpy as np, import sys, and def functions Please create a code that solves for matrix addition, matrix scalar multiplication, and matrix multiplication 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." Note for matrix multiplication: When the number of columns of Matrix A is not equal to the number of rows of Matrix B, print "Matrix multiplication cannot be performed; number of columns of Matrix A is not equal to number of rows of Matrix B." Format of the input from file1.txt: First Line: type of operation (add or scalmultiply or multiply) 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 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.
[Python (py3)]
Using import numpy as np, import sys, and def functions
Please create a code that solves for matrix addition, matrix scalar multiplication, and matrix multiplication
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."
Note for matrix multiplication: When the number of columns of Matrix A is not equal to the number of rows of Matrix B, print "Matrix multiplication cannot be performed; number of columns of Matrix A is not equal to number of rows of Matrix B."
Format of the input from file1.txt:
First Line: type of operation (add or scalmultiply or multiply)
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 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.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images