Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minElement / maxElement #473

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions source/mir/algorithm/iteration.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ $(T2 find, Finds backward index.)
$(T2 findIndex, Finds index.)
$(T2 fold, Accumulates all elements (different parameter order than `reduce`).)
$(T2 isSymmetric, Checks if the matrix is symmetric.)
$(T2 maxElement, Returns the maximum.)
$(T2 maxIndex, Finds index of the maximum.)
$(T2 maxPos, Finds backward index of the maximum.)
$(T2 maxIndex, Returns the minimum.)
Copy link

@prbuen prbuen Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L26: this should read minElement, not maxIndex, but code works as expected, thanks!

$(T2 minIndex, Finds index of the minimum.)
$(T2 minmaxIndex, Finds indices of the minimum and the maximum.)
$(T2 minmaxPos, Finds backward indices of the minimum and the maximum.)
Expand Down Expand Up @@ -4432,3 +4434,86 @@ unittest
auto z4 = y.minIndex;
auto z5 = y.maxIndex;
}

/++
Returns the minimal(maximal) element of a multidimensional slice.

Params:
pred = A predicate.

See_also:
$(LREF minIndex),
$(LREF maxElement),
$(LREF maxIndex),
$(LREF maxPos).
+/
template minElement(alias pred = "a < b")
{
import mir.functional: naryFun;
static if (__traits(isSame, naryFun!pred, pred))
/++
Params:
slice = ndslice.
Returns:
Minimal(maximal) element of a multidimensional slice
+/
@fmamath DeepElementType!(Slice!(Iterator, N, kind)) minElement(Iterator, size_t N, SliceKind kind)(Slice!(Iterator, N, kind) slice)
{
return slice[slice.minIndex!pred];
}
else
alias minElement = .minElement!(naryFun!pred);
}

/// ditto
template maxElement(alias pred = "a < b")
{
import mir.functional: naryFun, reverseArgs;
alias maxElement = minElement!(reverseArgs!(naryFun!pred));
}

///
@safe pure nothrow
version(mir_test)
unittest
{
import mir.ndslice.slice: sliced;
auto s = [
2, 6, 4, -3,
0, -4, -3, 3,
-3, -2, 7, 8,
].sliced(3, 4);

assert(s.minElement == -4);
assert(s.maxElement == 8);
}

///
@safe pure nothrow
version(mir_test)
unittest
{
import mir.ndslice.slice: sliced;
auto s = [
-8, 6, 4, -3,
0, -4, -3, 3,
-3, -2, 7, 8,
].sliced(3, 4);

assert(s.minElement == -8);
}

@safe pure nothrow
version(mir_test)
unittest
{
import mir.ndslice.slice: sliced;
auto s = [
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11
].sliced(3, 4);

assert(s.minElement == 0);
assert(s.maxElement == 11);
}
Loading