45.8k views
5 votes
Write a function main that takes two functions as parameters and the size of the array to be created from these functions as the third parameter of tuple type. The function should perform elementwise addition of the two arrays and return the resulting array. For example: Test Result print (main (lambda x, y: x+y, lambda x, y: x+y,(3,3)) ) [[024] [2 4 6] [4 6 8]] Write a function get_corner that that takes a 2-D numpy array as a parameter and returns the lower left corner of the array of size MxN, where M and N should also be passed as parameters to get_corner function. If the size of the array is insufficient it should print an appropriate message, as shown in the examples below. For example: Test Result [[0 8 1] [1 3 4]] A = np.array([[2, 3, 4, 5, 6, 7], [0, 8, 1, 5, 5, 2], [12, 5, 4, 9, 1, 5], [0, 8, 1, 5, 5, 2], [1, 3, 4, 8, 1, 7]]) get_corner (A,2,3) The array is too small to retrieve 6 x 5 sub- A = np.array([[2, 13, 4, 5, 16, 7], [0, 8, 1, 5, 15, 2], [12, 5, 4, 19, 1, 5], [0, 8, 1, 15, 5, 2], [1, 3, 4, 18, 1, 17]]) array from the lower left corner. get_corner (A,6,5)

User Moztemur
by
7.1k points

1 Answer

4 votes

Answer:

The tasks mentioned here need two Python functions - `main` and `get_corner`. These functions rely on the use of NumPy, a popular Python library for numerical computations.

The `main` function takes in two functions and a tuple representing the size of the array. It applies these functions to generate two arrays and then performs elementwise addition of these arrays. The `get_corner` function, on the other hand, extracts a subarray of specified size from the bottom-left corner of a given 2D array.

Here's how you can write these functions:

```

import numpy as np

def main(func1, func2, size):

arr1 = np.fromfunction(func1, size)

arr2 = np.fromfunction(func2, size)

return arr1 + arr2

def get_corner(arr, M, N):

if arr.shape[0] < M or arr.shape[1] < N:

return "The array is too small to retrieve {} x {} subarray from the lower left corner.".format(M, N)

else:

return arr[-M:, :N]

```

Here's how you can test these functions:

```

print(main(lambda x, y: x+y, lambda x, y: x+y,(3,3)))

A = np.array([[2, 3, 4, 5, 6, 7], [0, 8, 1, 5, 5, 2], [12, 5, 4, 9, 1, 5], [0, 8, 1, 5, 5, 2], [1, 3, 4, 8, 1, 7]])

print(get_corner(A,2,3))

A = np.array([[2, 13, 4, 5, 16, 7], [0, 8, 1, 5, 15, 2], [12, 5, 4, 19, 1, 5], [0, 8, 1, 15, 5, 2], [1, 3, 4, 18, 1, 17]])

print(get_corner(A,6,5))

```

Remember that the subarray size in `get_corner` function is determined by M (number of rows) and N (number of columns) parameters. The extraction starts from the bottom-left corner of the array. The `-M:` in the slicing is used to start from the bottom of the array and `:N` is used to limit the columns from the left.

User Moritur
by
8.6k points