Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem 4: Matrices In this problem we implement matrix functions: product, scalar multiplication, addition, subtrac- tion, and transpose. We discussed multiplication in class and
Problem 4: Matrices In this problem we implement matrix functions: product, scalar multiplication, addition, subtrac- tion, and transpose. We discussed multiplication in class and will only discuss the remaining operations here. A matrix is a rectangular block of objects-usually these are numbers. The shape is denoted by the number of rows first, then the number of columns both being some whole number. In the matrix A below, there are rows and m columns. A = a A 1,1 02,1 I aa11 a 1,2 a 02,2 a02,1 a 0,1 0.02 A = 1,2 2,2 I an,1,2 We describe this as an n x m, saying "An N by M matrix." Although the matrix is a mathematical construct, the array data structure works reasonably well as an implementation. We name the structure A and the position is A[i, j] for aij. As we've said in lecture, most folks rely on numpy for arrays. Unsurprisingly, there are matrix operations available. To help hone our skills, we'll be ininitally implementing these operations. Scalar multiplication takes a number and matrix A and forms the product A. If you're unfamiliar with matrices, you'll be happy to know it's much like regular multiplication. In this case, the products are the individual entries time the scalar. 3A = 1 2 1 13 2 81,m Onm -1 -2 3 1 3 6 3 9 -3 -6 6 9 3 0.61 a. 62.m (15) a-06.m (16) (17) (18) Two matrices A, B can be summed provided that they have the same x dimensions. An entry in the summed matrix is A+B... Subtraction follows similarly. All that's left is transpose. This is an unusual operation in that it changes the dimensions of the matrix while not affecting the entries themselves. Suppose we have A as n x m matrix. The transpose of this matrix, written (A) is a mxn matrix B such that B = Aij 1 import numpy as np 2 3 #INPUT two matrices a,b 4 #RETURN product ab 5 def mm(a,b): 6 7 8 9 10 #INPUT scalar n and matrix a 11 #RETURN scalar product na 12 def sm(n,a): 13 14 15 16 # TODO: Implement function return 11 t(A) = #TODO: Implement function return 1 28 29 if 30 31 32 33 34 A = 17 #INPUT matrix n x m 18 #RETURN transpose matrix m x n 19 def tp(a): 20 21 22 23 #INPUT two matrices a,b 24 #RETURN sum a + b 25 def add_m(a,b): 26 27 # TODO: Implement function return 1 # TODO: Implement function return 11 1 2 1 3 -1 -2 2 3 1 1 32 2-1 3 1 matrix.py name == "__main__": a np.array([[1,2,4],[3,4,3]]) b = np.array([[-1.0],[1,-5]. [-3,1]]) print("numpy product", np.dot(a,b)) d = mm(a,b) (19) (20) (21) 35 36 37 38 39 40 41 42 43 44 45 46 47 print(d) print("numpy scalar product", 4*a) e = sm (4,a) print(e) print("numpy tranpose", np. transpose(a)) f = tp(a) print (f) print("numpy addition", a + a) g= add_m(a, a) print(g) Output numpy product [[-11-6] [ -8 -17]] [[-11. -6.] [ -8.-17.]] numpy scalar product [[ 4 8 16] [12 16 12]] [[ 4 8 16] [12 16 12]] numpy tranpose [[13] [24] [43]] [[13] [24] [43]] numpy addition [[2 4 8] [6 8 6]] [[2 4 8] [6 8 6]]
Step by Step Solution
★★★★★
3.37 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
Solution import numpy as np Input two matices ab return product ab def mna...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started