Problem 4: Matrices In this problem we implement matrix functions: product, scalar multiplication, addition, subtrac- tion,...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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]] 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]]
Expert Answer:
Answer rating: 100% (QA)
Solution import numpy as np Input two matices ab return product ab def mna... View the full answer
Related Book For
Differential Equations and Linear Algebra
ISBN: 978-0131860612
2nd edition
Authors: Jerry Farlow, James E. Hall, Jean Marie McDill, Beverly H. West
Posted Date:
Students also viewed these programming questions
-
A rectangular array of mn numbers arranged in n rows, each consisting of m columns, is said to contain a saddlepoint if there is a number that is both the minimum of its row and the maximum of its...
-
An m n matrix A is said to have a right inverse if there exists an n x m matrix C such that AC = Im. A is said to have a left inverse if there exists an n m matrix D such that DA = In. (a) If A has...
-
An m ( n matrix A is said to have full rank if rank A = minimum [m, n]. The singular value decomposition lets us measure how close A is to not having full rank. If any singular value is zero, then A...
-
Make a balance sheet for the following data- One Time Start-Up Costs: Rent Deposit $4,000 Furniture & Fixtures $8,000 Equipment $6,000 Buildout/ Renovations $10,000 Decorating, Painting and...
-
Lou Ann Staas opened a software consulting firm that immediately paid $9,000 for a computer. Was Staas's payment an expense of the business? Explain your answer.
-
Write an equation that has the given solutions. (There are many correct answers.) 1. -4, 7 2. 0, 2, 9 3. -7/3, 6/7
-
Do the following activities to complete your marketing plan: 1. Draw a simple organizational chart for your organization. 2. Develop a Gantt chart (see Chapter 2) to schedule the key activities...
-
Gregory and Lulu Clifdens Tax Return Gregory R. and Lulu B. Clifden live with their family at the Rock Glen House Bed & Breakfast, which Gregory operates. The Bed & Breakfast (B&B) is located at...
-
Consider two descriptive facts. First, the poorest countries in the world are disproportionately located close to the equator. Second, the civilizations that were richest in 1500 are among the...
-
What is activity-based costing, and what are its potential benefits?
-
Let x represent the motion of the block shown below where all seven springs are unstretched when x = 0. (a) Determine the equivalent stiffness of the system. Show all of your calculations. (b) Derive...
-
The per-unit cost of an item is its average total cost (5 total cost/quantity). Suppose that a new cell phone application costs $100,000 to develop and only $.50 per unit to deliver to each cell...
-
Contrast the ideas of nominal GDP and real GDP. Why is one more reliable than the other for comparing changes in the standard of living over a series of years? What is the GDP price index and what is...
-
Assume that a leader country has real GDP per capita of $40,000, whereas a follower country has real GDP per capita of $20,000. Next suppose that the growth of real GDP per capita falls to zero...
-
The following trial balance is extracted from the books of M/s Ram on March 31, 2005. You are required to prepare trading and profit and loss account and the balance sheet as on date : Account title...
-
Prepare trading and profit and loss account and balance sheet as on March 31, 2005 : Closing stock as on March 31, 2005 Rs.22,400 Account Title Machinery Sundry debtors Drawings Purchases Wages...
-
Directions: Be sure to save an electronic copy of your answer before submitting it to Ashworth College for grading. Unless otherwise stated, answer in complete sentences, and be sure to use correct...
-
Akramin just graduated with a Master of Engineering in Manufacturing Engineering and landed a new job in Melaka with a starting salary of RM 4,000 per month. There are a number of things that he...
-
A certain radioactive material is known to decay at a rate proportional to the amount present. Over a 50-year period, an initial amount of 100 grams has decayed to only 75 grams. Find an expression...
-
For each differential equation verify by differentiation and substitution that the given function is a solution a. y' = y2 + 4 (| t | < /4); y = 2 tan 2t b. y' = 1/t y + t (t > 0): y = 3t + t2
-
Upper Triangular Matrices A square n ( n matrix A = [aij] is called upper triangular if aij = 0 for i > j. (All entries below the main diagonal are zero.) (a) Give three examples of upper triangular...
-
The order of payments for unsecured priority claims in a Chapter 7 bankruptcy case is such that a. Tax claims of governmental units are paid before claims for administrative expenses incurred by the...
-
Narco is in serious financial difficulty and is unable to meet current unsecured obligations of $30,000 to some 14 creditors who are demanding immediate payment. Narco owes Johnson $5,000, and...
-
Your client is insolvent under the federal bankruptcy law. Under the circumstances a. So long as the client can meet current debts or claims by its most aggressive creditors, a bankruptcy proceeding...
Study smarter with the SolutionInn App