From 2e059455c1d86aac6679d188fe595eeb21070dcc Mon Sep 17 00:00:00 2001 From: Ayan Ahmed Date: Wed, 18 Dec 2024 23:18:18 +0100 Subject: [PATCH] First commit. Completed code for numpy_questions.py --- numpy_questions.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..10c7802 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -41,7 +41,16 @@ def max_index(X): j = 0 # TODO + # Check if the input is a numpy array + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array.") + # Check if the array is 2D + if X.ndim != 2: + raise ValueError("Input array must be 2-dimensional.") + + # Find the index of the maximum value using np.argmax and unravel_index + i, j = np.unravel_index(np.argmax(X, axis=None), X.shape) return i, j @@ -64,4 +73,16 @@ def wallis_product(n_terms): """ # XXX : The n_terms is an int that corresponds to the number of # terms in the product. For example 10000. - return 0. + + # Handle edge case where n_terms is 0 + if n_terms == 0: + pi = 2.0 + return pi + + # Create an array for the terms (1 to n_terms) + n = np.arange(1, n_terms + 1) + + # Compute the Wallis product + product_terms = (4 * n**2) / (4 * n**2 - 1) + pi = 2.0 * np.prod(product_terms) + return pi