|
18 | 18 | import numpy as np
|
19 | 19 |
|
20 | 20 |
|
| 21 | + |
21 | 22 | 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 | + """ |
23 | 35 | if not isinstance(X, np.ndarray):
|
24 |
| - raise ValueError("X doit être un tableau numpy.") |
| 36 | + raise ValueError("X must be a numpy array.") |
25 | 37 | 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 |
28 | 41 | flat_index = np.argmax(X)
|
29 |
| - # Convertir en indices 2D. |
| 42 | + # Convert to 2D indices |
30 | 43 | i, j = divmod(flat_index, X.shape[1])
|
31 | 44 | return i, j
|
32 | 45 |
|
33 | 46 |
|
34 |
| - |
35 | 47 | 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