Skip to content

Commit 06adab3

Browse files
authored
Update numpy_questions.py
1 parent 32c7bd7 commit 06adab3

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

numpy_questions.py

+41-13
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,53 @@
1818
import numpy as np
1919

2020

21+
2122
def max_index(X):
22-
"""Retourne les indices (i, j) de la valeur maximale dans une matrice 2D."""
23+
"""
24+
Return the indices (i, j) of the maximum value in a 2D matrix.
25+
26+
Parameters:
27+
X (np.ndarray): A 2D numpy array.
28+
29+
Returns:
30+
tuple: Indices (i, j) of the maximum value in the array.
31+
32+
Raises:
33+
ValueError: If X is not a numpy array or not a 2D array.
34+
"""
2335
if not isinstance(X, np.ndarray):
24-
raise ValueError("X doit être un tableau numpy.")
36+
raise ValueError("X must be a numpy array.")
2537
if X.ndim != 2:
26-
raise ValueError("X doit être une matrice 2D.")
27-
# Trouver l'indice aplati du maximum.
38+
raise ValueError("X must be a 2D array.")
39+
40+
# Find the flattened index of the maximum value
2841
flat_index = np.argmax(X)
29-
# Convertir en indices 2D.
42+
# Convert to 2D indices
3043
i, j = divmod(flat_index, X.shape[1])
3144
return i, j
3245

3346

34-
3547
def wallis_product(n_terms):
36-
produit = 1
37-
if n_terms==0:
38-
return(2)
39-
else:
40-
for i in range(1, n_terms+1):
41-
produit *= 4*i**2/(4*i**2 - 1)
42-
return(2*produit)
48+
"""
49+
Approximate the value of pi using the Wallis product.
50+
51+
Parameters:
52+
n_terms (int): The number of terms in the Wallis product.
53+
54+
Returns:
55+
float: Approximation of pi.
56+
57+
Raises:
58+
ValueError: If n_terms is negative.
59+
"""
60+
if not isinstance(n_terms, int) or n_terms < 0:
61+
raise ValueError("n_terms must be a non-negative integer.")
62+
63+
product = 1.0
64+
if n_terms == 0:
65+
return 2.0
66+
67+
for i in range(1, n_terms + 1):
68+
product *= (4 * i**2) / (4 * i**2 - 1)
69+
70+
return 2 * product

0 commit comments

Comments
 (0)