@@ -20,8 +20,10 @@ $(T2 find, Finds backward index.)
20
20
$(T2 findIndex, Finds index.)
21
21
$(T2 fold, Accumulates all elements (different parameter order than `reduce`).)
22
22
$(T2 isSymmetric, Checks if the matrix is symmetric.)
23
+ $(T2 maxElement, Returns the maximum.)
23
24
$(T2 maxIndex, Finds index of the maximum.)
24
25
$(T2 maxPos, Finds backward index of the maximum.)
26
+ $(T2 minElement, Returns the minimum.)
25
27
$(T2 minIndex, Finds index of the minimum.)
26
28
$(T2 minmaxIndex, Finds indices of the minimum and the maximum.)
27
29
$(T2 minmaxPos, Finds backward indices of the minimum and the maximum.)
@@ -4432,3 +4434,86 @@ unittest
4432
4434
auto z4 = y.minIndex;
4433
4435
auto z5 = y.maxIndex;
4434
4436
}
4437
+
4438
+ /+ +
4439
+ Returns the minimal(maximal) element of a multidimensional slice.
4440
+
4441
+ Params:
4442
+ pred = A predicate.
4443
+
4444
+ See_also:
4445
+ $(LREF minIndex),
4446
+ $(LREF maxElement),
4447
+ $(LREF maxIndex),
4448
+ $(LREF maxPos).
4449
+ +/
4450
+ template minElement (alias pred = " a < b" )
4451
+ {
4452
+ import mir.functional: naryFun;
4453
+ static if (__traits(isSame, naryFun! pred, pred))
4454
+ /+ +
4455
+ Params:
4456
+ slice = ndslice.
4457
+ Returns:
4458
+ Minimal(maximal) element of a multidimensional slice
4459
+ +/
4460
+ @fmamath DeepElementType! (Slice! (Iterator, N, kind)) minElement(Iterator, size_t N, SliceKind kind)(Slice! (Iterator, N, kind) slice)
4461
+ {
4462
+ return slice[slice.minIndex! pred];
4463
+ }
4464
+ else
4465
+ alias minElement = .minElement! (naryFun! pred);
4466
+ }
4467
+
4468
+ // / ditto
4469
+ template maxElement (alias pred = " a < b" )
4470
+ {
4471
+ import mir.functional: naryFun, reverseArgs;
4472
+ alias maxElement = minElement! (reverseArgs! (naryFun! pred));
4473
+ }
4474
+
4475
+ // /
4476
+ @safe pure nothrow
4477
+ version (mir_test)
4478
+ unittest
4479
+ {
4480
+ import mir.ndslice.slice: sliced;
4481
+ auto s = [
4482
+ 2 , 6 , 4 , - 3 ,
4483
+ 0 , - 4 , - 3 , 3 ,
4484
+ - 3 , - 2 , 7 , 8 ,
4485
+ ].sliced(3 , 4 );
4486
+
4487
+ assert (s.minElement == - 4 );
4488
+ assert (s.maxElement == 8 );
4489
+ }
4490
+
4491
+ // /
4492
+ @safe pure nothrow
4493
+ version (mir_test)
4494
+ unittest
4495
+ {
4496
+ import mir.ndslice.slice: sliced;
4497
+ auto s = [
4498
+ - 8 , 6 , 4 , - 3 ,
4499
+ 0 , - 4 , - 3 , 3 ,
4500
+ - 3 , - 2 , 7 , 8 ,
4501
+ ].sliced(3 , 4 );
4502
+
4503
+ assert (s.minElement == - 8 );
4504
+ }
4505
+
4506
+ @safe pure nothrow
4507
+ version (mir_test)
4508
+ unittest
4509
+ {
4510
+ import mir.ndslice.slice: sliced;
4511
+ auto s = [
4512
+ 0 , 1 , 2 , 3 ,
4513
+ 4 , 5 , 6 , 7 ,
4514
+ 8 , 9 , 10 , 11
4515
+ ].sliced(3 , 4 );
4516
+
4517
+ assert (s.minElement == 0 );
4518
+ assert (s.maxElement == 11 );
4519
+ }
0 commit comments