In this problem you will write a series of functions for manipulating matrices (
ID: 3740868 • Letter: I
Question
In this problem you will write a series of functions for manipulating matrices (2D arrays).
If you have not heard the mathematical term “matrix” before, here is a very simplistic – yet sufficient for the purpose of this assignment – definition of a matrix: Matrix is a rectangular arrangement of numbers. For instance,
is a 2-by-3 matrix. It has 2 rows and 3 columns. Round parenthesis are just a common mathematical notation, they have no Pythonic meaning.
You may consider re-use some functions you developed in hw8pr1.py in this set of the problem. If you discover that some functions from hw8pr1.py are useful in this set of problems, please feel free to copy and paste them to this file.
Start to write a function named matrix(), which should have a local variable named a. The variable a should have the following 3×4 default value:
The matrix() function runs an infinite while loop. The loop first displays a menu of choices (see description later), it then reads a choice from the user. Based on the user’s selection, the program performs various matrix-related operations. If the user chooses to quit the program, the loop stops. The program runs in a same pattern you saw in the previous lab.
Please use the default value for the array a in your program — it will be convenient for testing. In addition, one of the menu options will allow the user to change this matrix, so that your program will be able to handle arbitrary matrices, as well.
Warning: We will test your code on other sizes of matrices, as well!
We encourage you to use doctests where appropriate to make your life easier but they are not required for this problem.
Your program should offer the user the following menu:
Write a function called menu() to print the above menu. The matrix() should call the menu()function within the infinite while loop.
In response to each of the user selection, you will write a function to handle the request.
Write a function named enterValues() that asks the user for a list of lists that represents a 2D matrix. It will be up to the user to input valid data (and for simplicity, you may assume that we will test your code only on valid inputs). By using Python input and eval, this method becomes quite straightforward! Be sure to return the matrix that is entered. For example, here is a sample of user’s input:
Write a function named printArray() should take in a 2D array a and print its values. The key line to print a single value neatly formatted is the following:
Yes, this requires a bit of explanation! This is an example of formatted printing. The string at left is the “format string.” It says to use 8 spaces total, 3 of which should be after the decimal point, and the f indicates that it expects a floating-point number. It will take an integer, as well, so there’s no need to convert (yet).
That’s a lot of stuff in one line of code, but there’s no need to worry about it all at once. However, because the above line prints only one element, it will need to be inside nested for loops, in which the outer loop iterates over the rows and the inner loop iterates over the columns. You’ll need a plain print at the appropriate place, in order to place each row on a separate line.
Printing with each menu…
It’s useful to save yourself the trouble of entering menu option #2 each time you would like to see the array.
So, to help both you and the graders, please call the function printArray just before printing the menu each time through your while loop.
This is in addition to option #2, although certainly both option 2 and the every-loop printing of the matrix should use the same underlying helper function!
The function multiplyRow(a, row, multiplier) should multiply all of the elements in a requested row of the input array a by the value multiplier (this value can be an integer or a float value). It should not change anything else in the array, nor should it ask for inputs — nor should it print anything. The prompts for input and all output printing should be in the main function, matrix(). Also, note that we start counting at 0, with row 0 being the topmost row, row 1 being the next row, and so on… See the complete run, below, for a detailed example on input and output. Obviously, if the user selects this option, your program has to ask which row should be modified and the value of the multiplier, before calling the function multiplyRow().
The function addSourceRowToDestinationRow(a, sourceRow, destRow) should add each element of the source row sourceRow into the destination row destRow. Row sourceRow should remain unchanged! Row destRow will change, except in the case that row sourceRow is all zeros! This method should not change anything else in the array, nor should it print anything. (Again, do not ask for inputs in these functions, ask inputs before calling the function!)
The function addMultipleOfSourceRowToDestRow(a, multiplier, sourceRow, destRow) should add each element of row sourceRow, multiplied by multiplier, into row destRow. As in the prior two functions, row sourceRow should remain completely unchanged; row destRow will change unless the factors are zero, and this method should not change anything else, ask for inputs, or print anything. Again, multiplier can be an integer or a float value.
Transposition is an operation that converts a given n-by-m matrix into a new m-by-n matrix, such that the rows of the original matrix become columns of the new one and vice versa.
The function transpose() should create a new matrix that is a transposition of the original one and return the new matrix to the caller.
You may want to check that your functions work on the examples within this run:
A=(3 14 15 1 6 18Explanation / Answer
# This program is to demonstrate the operation of matrices using switch case statement
def switch_matrix(choice):
switcher = {
1: ReadMat(),
2: DispMat(),
3: MulMat(),
4: AddMat(),
5: AddRow(),
6: MulMat(),
7: TransMat(),
8: Quit: exit(0);
}
print switcher.get(choice, "Invalid choice")
# Function definition for reading the 2D matrix
def ReadMat():
A=[[10,7,2],
[5,6,8]]
B=[[3,4,5],
[6,7,8]]
# Function to display the 2D Matrix in the rectangular format
def DispMat():
D,E=ReadMat() #Read Matrix function called for reading the elements of the array
C=[0]*B
for i in range(0,D):
C[i]=[0]*D
for i in range(0,E):
for j in range(0,E):
C[i][j]=ReadMat()
print C
# Function definition for multiplying 2D matrices
def MulMat(res):
D,E=ReadMat()
res=[[0,0,0],
[0,0,0]]
# iterate through rows of D
for i in range(len(D)):
# iterate through the columns of D
for j in range(len(D[0])):
# iterate through the rows of E
for k in range(len(E)):
res[i][j]+=D[i][k]*E[k][j]
for r1 in res:
print(r1)
# Function definition to add the two dimensional matrices
def AddMat(res):
D,E=ReadMat()
res=[[0,0,0],
[0,0,0]]
# iterate through the rows of D
for i in range(len(D)):
# iterate through columns of E
for j in range(len(D[0])):
# iterate through the rows of E
for k in range(len(E)):
res[i][j]+=D[i][k]+E[k][j]
for r1 in res:
print(r1)
# Function definition to add multiple source row to the destination row
def AddRow(res):
A=[[10,7,2],
[5,6,8]]
res=AddMat()
# iterate through the rows of the matrix A
for i in range(len(A)):
# iterate through the columns of the matrix A
for j in range(len(A[0])):
res[i][j]=A[i][j]
for r1 in res:
print(r1)
# Function definition to perform the transpose operation on the 2D matrix
def TransMat(res):
A=[[10,7,2],
[5,6,8]]
res=[[0,0,0],
[0,0,0]]
# iterate through the rows of the matrix A
for i in range(len(A)):
# iterate through the columns of the matrix A
for j in range(len(A[0])):
res[j][i]=A[i][j]
for r1 in res:
print(r1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.