#################################################### # Homework 12 #################################################### #################################################### # Write the following functions using numpy #################################################### def genQmatrix(sz, a,b,c,d): '''Return a square matrix of size sz x sz with four quadants upper left quadrant has values all equal to a upper right quadrant has values all equal to b lower left quadrant has values all equal to c lower right quadrant has values all equal to d If sz is odd, the right and lower quadrants are larger by 1 ''' def submatrix(mat, r, c, n): '''Function returns an n x n submatrix of the given matrix, The upper left corner is at the given row and column. Smaller sizes result if it runs off the edge. ''' def genrandommatrix(mx, sz): '''Function creates and returns a square matrix of size sz by sz containing random integers between 1 and mx, inclusive ''' def anyone(arr): '''Given a 2d array, this function returns a tuple: first element is a bool indicating if any elements are 1 second element is a 1D numpy array of bool indicating which columns contain a 1 third element is a 1D numpy array of bool indicating which rows contain a 1 ''' def allgtval(arr, val): '''Given a 2d numpy array, this function returns a tuple: first element is a bool indicating if all elements of arr are > val second element is a 1D numpy array of bool indicating which columns have all elements > val third element is a 1D numpy array of bool indicating which rows have all elements > val ''' def valuespan(arr): '''This function will return the difference between the largest and smallest values in the given array ''' def stripNsquare(arr, lst, n): '''Given a 2D array, a list of row indices and a positive integer, this function will return a new array from arr, keeping only rows whose indices are given in the list and ignoring the first and last "n" columns. The remaining values are squared. If the dimensions of arr are r x c, then the result has dimensions r x (c-2n) ''' def averagearrays(arraylist): '''Given a list of arrays of equal shape, this function will return the element-wise average of all of them (the first element is the average of all the first elements, etc.). HINT: you can make this much, much harder than it is ''' def itemsgtval(arr,val): '''This function will return a numpy array of bool of the same shape as arr. The values are True iff the corresponding element of "arr" is greater than val. ''' def applylimit(arr, val): '''This function will set to val any elements of the numpy array that are greater than val. The function returns None ''' def doubleodd(arr): '''This function will double all odd values in the given numpy array. The function returns None ''' def keepgreater(arr1, arr2): '''For numpy arrays arr1 and arr2 (of identical shape), set the elements of arr1 to the corresonding element of arr2 for every element where arr2's value is greater. Return None ''' def inversecheck(arr): '''Given a square 2D matrix, generate and return the product of it and its inverse (which should be close to the identity matrix). ''' def zerocolumns(arr): '''Given a square 2D matrix, for any column that contains an element that is zero, set all the remaining elements to zero. The function returns None ''' #################################################### # Test code for the above #################################################### if __name__ == '__main__': # print(genQmatrix(20,1,2,3,4)) print("genQmatrix:") print(genQmatrix(7,2,4,5,6), '\n') r = np.array([[0, 5, 0, 1, 6], [4, 6, 8, 8, 3], [3, 5, 0, 9, 9], [0, 2, 2, 5, 1], [4, 9, 9, 5, 7]]) print("r:") print(r, '\n') print("submatrix(r,1,2,4):") print(submatrix(r,1,2,4), '\n') print("genrandommatrix(10,7) -- your results will be different") print(genrandommatrix(10,7), '\n') print("anyone(r):") print(anyone(r), '\n') print("allgtval(r, 1):") print(allgtval(r, 1), '\n') print("valuespan(r): ", valuespan(r), '\n') print("stripNsquare(r,[1,3,4],1):") print(stripNsquare(r,[1,3,4],1), '\n') print("stripNsquare(r,[1,3,4],2):") print(stripNsquare(r,[1,3,4],2), '\n') print("stripNsquare(r,[1,3,4],3):") print(stripNsquare(r,[1,3,4],3), '\n') print("averagearrays([r, r**2, r**3]:") print(averagearrays([r, r**2, r**3]), '\n') print("itemsgtval(r, 1):") print(itemsgtval(r, 1), '\n') print("applylimit(r,3):") print(applylimit(r,3)) print("r:") print(r, '\n') r = np.array([[0, 5, 0, 1, 6], [4, 6, 8, 8, 3], [3, 5, 0, 9, 9], [0, 2, 2, 5, 1], [4, 9, 9, 5, 7]]) print("reset r:") print(r, '\n') print("doubleodd(r):") print(doubleodd(r)) print("r:") print(r, '\n') r = np.array([[0, 5, 0, 1, 6], [4, 6, 8, 8, 3], [3, 5, 0, 9, 9], [0, 2, 2, 5, 1], [4, 9, 9, 5, 7]]) print("reset r:") print(r, '\n') s = np.array([[7, 2, 4, 3, 2], [6, 8, 9, 9, 2], [5, 0, 8, 6, 8], [6, 5, 2, 6, 4], [2, 3, 2, 6, 9]]) print("s:") print(s, '\n') print("keepgreater(r,s): ", keepgreater(r,s)) print("r:") print(r, '\n') print("s:") print(s, '\n') r = np.array([[0, 5, 0, 1, 6], [4, 6, 8, 8, 3], [3, 5, 0, 9, 9], [0, 2, 2, 5, 1], [4, 9, 9, 5, 7]]) print("reset r:") print(r, '\n') print("inversecheck(r):") print(inversecheck(r), '\n') print("r:") print(r, '\n') print("zerocolumns(r):") print(zerocolumns(r)) print("r:") print(r, '\n') #################################################### # Test code results should be exactly this # (except for random matrix and maybe rounding) #################################################### genQmatrix: [[2 2 2 4 4 4 4] [2 2 2 4 4 4 4] [2 2 2 4 4 4 4] [5 5 5 6 6 6 6] [5 5 5 6 6 6 6] [5 5 5 6 6 6 6] [5 5 5 6 6 6 6]] r: [[0 5 0 1 6] [4 6 8 8 3] [3 5 0 9 9] [0 2 2 5 1] [4 9 9 5 7]] submatrix(r,1,2,4): [[8 8 3] [0 9 9] [2 5 1] [9 5 7]] genrandommatrix(10,7) -- your results will be different [[5 7 9 6 4 1 7] [2 4 2 1 4 6 9] [9 1 7 8 1 8 8] [7 5 3 6 3 8 1] [1 7 4 8 3 3 6] [3 4 3 4 6 6 1] [3 8 4 8 7 9 3]] anyone(r): (True, array([False, False, False, True, True], dtype=bool), array([ True, False, False, True, False], dtype=bool)) allgtval(r, 1): (False, array([False, True, False, False, False], dtype=bool), array([False, True, False, False, True], dtype=bool)) valuespan(r): 9 stripNsquare(r,[1,3,4],1): [[36 64 64] [ 4 4 25] [81 81 25]] stripNsquare(r,[1,3,4],2): [[64] [ 4] [81]] stripNsquare(r,[1,3,4],3): [] averagearrays([r, r**2, r**3]: [[ 0. 51.66666667 0. 1. 86. ] [ 28. 86. 194.66666667 194.66666667 13. ] [ 13. 51.66666667 0. 273. 273. ] [ 0. 4.66666667 4.66666667 51.66666667 1. ] [ 28. 273. 273. 51.66666667 133. ]] itemsgtval(r, 1): [[False True False False True] [ True True True True True] [ True True False True True] [False True True True False] [ True True True True True]] applylimit(r,3): None r: [[0 3 0 1 3] [3 3 3 3 3] [3 3 0 3 3] [0 2 2 3 1] [3 3 3 3 3]] reset r: [[0 5 0 1 6] [4 6 8 8 3] [3 5 0 9 9] [0 2 2 5 1] [4 9 9 5 7]] doubleodd(r): None r: [[ 0 10 0 2 6] [ 4 6 8 8 6] [ 6 10 0 18 18] [ 0 2 2 10 2] [ 4 18 18 10 14]] reset r: [[0 5 0 1 6] [4 6 8 8 3] [3 5 0 9 9] [0 2 2 5 1] [4 9 9 5 7]] s: [[7 2 4 3 2] [6 8 9 9 2] [5 0 8 6 8] [6 5 2 6 4] [2 3 2 6 9]] keepgreater(r,s): None r: [[7 5 4 3 6] [6 8 9 9 3] [5 5 8 9 9] [6 5 2 6 4] [4 9 9 6 9]] s: [[7 2 4 3 2] [6 8 9 9 2] [5 0 8 6 8] [6 5 2 6 4] [2 3 2 6 9]] reset r: [[0 5 0 1 6] [4 6 8 8 3] [3 5 0 9 9] [0 2 2 5 1] [4 9 9 5 7]] inversecheck(r): [[ 1.00000000e+00 0.00000000e+00 0.00000000e+00 -2.22044605e-16 -4.44089210e-16] [ 0.00000000e+00 1.00000000e+00 0.00000000e+00 -2.22044605e-16 -2.22044605e-16] [ 0.00000000e+00 0.00000000e+00 1.00000000e+00 -8.88178420e-16 0.00000000e+00] [ -2.22044605e-16 -5.55111512e-16 8.32667268e-17 1.00000000e+00 2.22044605e-16] [ 0.00000000e+00 0.00000000e+00 2.22044605e-16 -8.88178420e-16 1.00000000e+00]] r: [[0 5 0 1 6] [4 6 8 8 3] [3 5 0 9 9] [0 2 2 5 1] [4 9 9 5 7]] zerocolumns(r): None r: [[0 5 0 1 6] [0 6 0 8 3] [0 5 0 9 9] [0 2 0 5 1] [0 9 0 5 7]]