Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numpy questions #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions numpy_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def max_index(X):
Returns
-------
(i, j) : tuple(int)
The row and columnd index of the maximum.
The row and column index of the maximum.

Raises
------
Expand All @@ -41,6 +41,21 @@ def max_index(X):
j = 0

# TODO
# Checking the input
if not isinstance(X, np.ndarray):
raise ValueError('The input is not a numpy array.')
if len(X.shape) != 2:
raise ValueError('The input is not a 2D matrix.')

# Finding the row and column index of the maximum
max_value = X[i, j]
n_rows, n_cols = X.shape
for i0 in range(n_rows):
for j0 in range(n_cols):
if X[i0, j0] > max_value:
max_value = X[i0, j0]
i = i0
j = j0

return i, j

Expand All @@ -64,4 +79,17 @@ 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.

# Checking the input
if not isinstance(n_terms, int):
raise ValueError('The input is not an integer.')

# Base case
if n_terms == 0:
return 2.
# Iterative computation
pi_approx = 2.
for n in range(1, n_terms + 1):
pi_approx *= 4 * n**2 / (4 * n**2 - 1)

return pi_approx