@@ -39,29 +39,45 @@ def max_index(X):
39
39
"""
40
40
i = 0
41
41
j = 0
42
-
43
- # TODO
44
-
45
- return i , j
42
+ if not isinstance (X , np .ndarray ):
43
+ raise ValueError ("Input must be a numpy array." )
44
+ if X .ndim == 2 :
45
+ max_index = []
46
+ max_value = []
47
+ for n in range (X .shape [0 ]):
48
+ max_index .append (np .argmax (X [n ]))
49
+ max_value .append (X [n , max_index [- 1 ]])
50
+ max_index2 = np .argmax (max_value )
51
+ i = max_index2
52
+ j = max_index [max_index2 ]
53
+ print (max_index , max_index2 )
54
+ return i , j
55
+ else :
56
+ raise ValueError ("input must be a 2-dimension array" )
46
57
47
58
48
59
def wallis_product (n_terms ):
49
- """Implement the Wallis product to compute an approximation of pi .
60
+ """Compute an approximation of π using the Wallis product formula .
50
61
51
62
See:
52
63
https://en.wikipedia.org/wiki/Wallis_product
53
64
54
65
Parameters
55
66
----------
56
67
n_terms : int
57
- Number of steps in the Wallis product. Note that `n_terms=0` will
58
- consider the product to be `1`.
68
+ Number of terms in the Wallis product. Must be >= 1.
59
69
60
70
Returns
61
71
-------
62
- pi : float
63
- The approximation of order `n_terms` of pi using the Wallis product.
72
+ pi_approx : float
73
+ Approximation of π using the Wallis product with `n_terms` .
64
74
"""
65
- # XXX : The n_terms is an int that corresponds to the number of
66
- # terms in the product. For example 10000.
67
- return 0.
75
+ if n_terms == 0 :
76
+ return 2
77
+ else :
78
+ product = 1.0
79
+ for i in range (1 , n_terms + 1 ):
80
+ term = (4 * i ** 2 ) / (4 * i ** 2 - 1 )
81
+ product *= term
82
+
83
+ return product * 2
0 commit comments