This is easily done in, for example, Python, and can be very useful. I have spent quite a bit of time searching documentation and reading forum questions, without finding anything.
Here’s a small example of what I would like to do:
arr = [1,2,3,4,-5]
sqmax = max(arr, by = x -> x^2)
I know this can be done as follows, but it seems like it would make sense for max() to have a
sqmax = sort(arr, by = x -> x^2)[end]
You are looking for what is called
maximum in Julia. Take a look at the documentation on
maximum for more examples.
Welcome to the forum. A question like this is better suited for the “Usage” category.
But still there is no built in
maxiumum function offering whats requested.
I’d go with:
Update: this is only needed if you want the index, which I initially thought was asked.
maximum does take an optional function as its first argument. Try
maximum(x -> x^2, arr).
The problem with that is that it creates a temporary array, which can be avoided by passing the function as an argument.
That allocates an intermediate array for the squared array whereas the version from simon does not. It would be nice if we could reduce over a broadcasted array without allocs one day but we’re not there yet.
thanks for the answer, but
maximum(f, arr) will return the maximal element in
f(arr) rather than in
Is there any keyword of
maximum or other functons to “find x in arr s.t. f(x) is maximal”
xs = [1, 3, 2]; i = argmax([x^2 for x in xs]); println(xs[i])
You can also pass the function to argmax, to avoid the allocation of the intermediate array:
julia> x = [1,3,2]
julia> argmax(x -> -x^2, x)
julia> argmax(x -> x^2, x)
julia> findmax(abs2, -3:2)
the second returned value can be used to index the
or just use:
julia> argmax(abs2, -3:2)
which does the
indexing for you under the hood
foldl((x,y)->x^2>y^2 ? x : y, arr)
foldl((x,y)->ifelse(x^2>y^2 , x , y), arr)
maxby(f,arr)=foldl((x,y)->f(x)>f(y) ? x : y, arr)
Yes, for those wondering, this feature was added in Julia v1.7. Quite useful/convenient on occasion.
Open issue up for grabs: Add a `by` keyword to `maximum` and `minimum` · Issue #28210 · JuliaLang/julia · GitHub
(Of course, you could just write your own loop for this, which is perfectly fast.)
Thanks! I found out this implementation is new in Julia 1.7. For 1.6 or lower version, it still can be done by a less direct way