[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.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.2: Returning A Single Value
Problem 11E: (Numerical) a. The following is an extremely useful programming algorithm for rounding a real number...
icon
Related questions
Question

[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.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning