diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..81e700b 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -40,8 +40,12 @@ def max_index(X): i = 0 j = 0 - # TODO + if not isinstance(X, np.ndarray): + raise ValueError("Input must be a numpy array") + if X.ndim != 2: + raise ValueError("Input array must be 2D") + i, j = np.unravel_index(np.argmax(X), X.shape) return i, j @@ -56,12 +60,12 @@ def wallis_product(n_terms): n_terms : int Number of steps in the Wallis product. Note that `n_terms=0` will consider the product to be `1`. - - Returns - ------- - pi : float - The approximation of order `n_terms` of pi using the Wallis product. """ - # XXX : The n_terms is an int that corresponds to the number of - # terms in the product. For example 10000. - return 0. + if n_terms == 0: + return 2.0 + + n = np.arange(1, n_terms + 1, dtype=float) + factors = (4 * n * n) / (4 * n * n - 1) + approx_pi = 2.0 * np.prod(factors) + + return approx_pi