-
Notifications
You must be signed in to change notification settings - Fork 37
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
Please add maxElement and minElement in mir (trying to use std.algorithm gives opCmp not defined) #471
Comments
Something like: /++
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 size_t[N] 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));
} |
Thanks, that's great! Compilation fails with
but changing the return type of minElement from Both |
Yeah, I just kind of threw that together without testing. I'd prefer an actual return type instead of |
That would be great. I agree with preferring an actual type but don't feel confident enough here to know how to write it. FWIW, |
To find the maximum value in an
ndslice
u
, we can usemir
'smaxIndex
:but it would be nice to have the functions
maxElement
andminElement
directly implemented inmir
. It's not possible to use these functions fromstd.algorithm
(the complaint is that opCmd is not defined), unless doingu.flattened.maxElement
.It doesn't sound like much but it would allow to more easily write code that can apply to both standard D dynamic arrays and ndslices.
Here is my use case: in numerical computation (e.g. errors) we often need to find the maximum absolute value of an
ndslice
. This can be achieved both with dynamic arrays (were it not for this bug) and ndslices withbut it's pretty heavy and not as readable compared to
Thanks!
The text was updated successfully, but these errors were encountered: