@@ -182,6 +182,31 @@ where
182
182
S : Data < Elem = A > ,
183
183
D : Dimension ,
184
184
{
185
+ /// Finds the first index of the minimum value of the array.
186
+ ///
187
+ /// Returns `None` if any of the pairwise orderings tested by the function
188
+ /// are undefined. (For example, this occurs if there are any
189
+ /// floating-point NaN values in the array.)
190
+ ///
191
+ /// Returns `None` if the array is empty.
192
+ ///
193
+ /// # Example
194
+ ///
195
+ /// ```
196
+ /// extern crate ndarray;
197
+ /// extern crate ndarray_stats;
198
+ ///
199
+ /// use ndarray::array;
200
+ /// use ndarray_stats::QuantileExt;
201
+ ///
202
+ /// let a = array![[1., 3., 5.],
203
+ /// [2., 0., 6.]];
204
+ /// assert_eq!(a.argmin(), Some((1, 1)));
205
+ /// ```
206
+ fn argmin ( & self ) -> Option < D :: Pattern >
207
+ where
208
+ A : PartialOrd ;
209
+
185
210
/// Finds the elementwise minimum of the array.
186
211
///
187
212
/// Returns `None` if any of the pairwise orderings tested by the function
@@ -203,6 +228,31 @@ where
203
228
A : MaybeNan ,
204
229
A :: NotNan : Ord ;
205
230
231
+ /// Finds the first index of the maximum value of the array.
232
+ ///
233
+ /// Returns `None` if any of the pairwise orderings tested by the function
234
+ /// are undefined. (For example, this occurs if there are any
235
+ /// floating-point NaN values in the array.)
236
+ ///
237
+ /// Returns `None` if the array is empty.
238
+ ///
239
+ /// # Example
240
+ ///
241
+ /// ```
242
+ /// extern crate ndarray;
243
+ /// extern crate ndarray_stats;
244
+ ///
245
+ /// use ndarray::array;
246
+ /// use ndarray_stats::QuantileExt;
247
+ ///
248
+ /// let a = array![[1., 3., 7.],
249
+ /// [2., 5., 6.]];
250
+ /// assert_eq!(a.argmax(), Some((0, 2)));
251
+ /// ```
252
+ fn argmax ( & self ) -> Option < D :: Pattern >
253
+ where
254
+ A : PartialOrd ;
255
+
206
256
/// Finds the elementwise maximum of the array.
207
257
///
208
258
/// Returns `None` if any of the pairwise orderings tested by the function
@@ -278,6 +328,23 @@ where
278
328
S : Data < Elem = A > ,
279
329
D : Dimension ,
280
330
{
331
+ fn argmin ( & self ) -> Option < D :: Pattern >
332
+ where
333
+ A : PartialOrd ,
334
+ {
335
+ let mut current_min = self . first ( ) ?;
336
+ let mut current_pattern_min = D :: zeros ( self . ndim ( ) ) . into_pattern ( ) ;
337
+
338
+ for ( pattern, elem) in self . indexed_iter ( ) {
339
+ if elem. partial_cmp ( current_min) ? == cmp:: Ordering :: Less {
340
+ current_pattern_min = pattern;
341
+ current_min = elem
342
+ }
343
+ }
344
+
345
+ Some ( current_pattern_min)
346
+ }
347
+
281
348
fn min ( & self ) -> Option < & A >
282
349
where
283
350
A : PartialOrd ,
@@ -303,6 +370,23 @@ where
303
370
} ) )
304
371
}
305
372
373
+ fn argmax ( & self ) -> Option < D :: Pattern >
374
+ where
375
+ A : PartialOrd ,
376
+ {
377
+ let mut current_max = self . first ( ) ?;
378
+ let mut current_pattern_max = D :: zeros ( self . ndim ( ) ) . into_pattern ( ) ;
379
+
380
+ for ( pattern, elem) in self . indexed_iter ( ) {
381
+ if elem. partial_cmp ( current_max) ? == cmp:: Ordering :: Greater {
382
+ current_pattern_max = pattern;
383
+ current_max = elem
384
+ }
385
+ }
386
+
387
+ Some ( current_pattern_max)
388
+ }
389
+
306
390
fn max ( & self ) -> Option < & A >
307
391
where
308
392
A : PartialOrd ,
0 commit comments