# How to obtain x instead of f(x) from maximum/minimum(f, itr)

**URL:** <https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456>\
**Category:** General Usage\
**Created:** [September 16, 2024, 7:39am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456 "2024-09-16T07:39:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [September 16, 2024, 7:39am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/1 "2024-09-16T07:39:15Z")

</div>

`maximum(f, itr)` returns `f(x)`. Is there a function that instead returns `x`, something like `sort(itr; by=f)[end]`?

I also need the `init` keyword, which `findmax` does not have (a similar issue is open for `argmax`: [`argmin`/`argmax` suggest confusing fix in error message for empty collections · Issue #46861 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/46861))

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [September 16, 2024, 7:58am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/2 "2024-09-16T07:58:09Z")

</div>

There is no such function. There really should be - in fact, the existing `maximum(f, itr)` is IMO bad API, since it just duplicates `maximum(map(f, itr))`.

One workaround is to do

```julia
prefixed = collect(Iterators.map(i -> (f(i), i)))
last(partialsort!(prefixed, 1; by=first))

```

But this is not great, since it allocates needlessly.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [September 16, 2024, 7:59am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/3 "2024-09-16T07:59:45Z")

</div>

Although it does not have an `init` argument, `findmax` should be the function you are looking for.

In `maximum`, `init` is only guaranteed to be used in case of empty input collections. It is thus discouraged to use `maximum` as follows:

```julia
# "find max(5, maximum(xs))"
julia> maximum([1,2,3], init=5)
5

```

That said, I suggest you decide in your own code on how to treat the empty collection. That is:

```julia
if isempty(xs)
  # do something (maybe throw an ArgumentError)
else
  y, i = findmax(f, xs)

  # potentially, if this is what you want (but I am only guessing)
  if y < init
    # do something
  end
end

```

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [September 16, 2024, 8:18am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/4 "2024-09-16T08:18:44Z")

</div>

Thank you. I ended up doing something similar to what you proposed.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [September 16, 2024, 8:21am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/5 "2024-09-16T08:21:10Z")

</div>

From a quick scan, there does not seem to be any related issue.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 16, 2024, 8:37am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/6 "2024-09-16T08:37:08Z")

</div>

If you don’t want to allocate you can do

```julia
first(maximum(x -> (f(x), x), itr))

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 16, 2024, 8:43am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/7 "2024-09-16T08:43:21Z")

</div>

> [@mzaffalon](#):
>
> `maximum(f, itr)` returns `f(x)`. Is there a function that instead returns `x`, something like `sort(itr; by=f)[end]`?

Isn’t this exactly what `argmax` does?

> ```julia
> argmax(f, domain)
> 
> Return a value x from domain for which f(x) is maximised. If there are multiple maximal values for
> f(x) then the first one will be found.
> 
> ```

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [September 16, 2024, 8:43am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/8 "2024-09-16T08:43:52Z")

</div>

I need the `init` keyword.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 16, 2024, 8:44am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/9 "2024-09-16T08:44:42Z")

</div>

Drat.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [September 16, 2024, 8:49am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/10 "2024-09-16T08:49:39Z")

</div>

Just to make sure we’re not dealing with the [XY problem](https://xyproblem.info/), could you elaborate on what you use the keyword for?

EDIT: DNF’s right. `findmax` returns the index of `x` (and `f(x)`) whereas `argmax` returns `x`.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [September 16, 2024, 9:07am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/11 "2024-09-16T09:07:40Z")

</div>

After taking a second look, I do not need it.  
Thank you for asking the question.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 16, 2024, 9:10am UTC](https://discourse.julialang.org/t/how-to-obtain-x-instead-of-f-x-from-maximum-minimum-f-itr/119456/12 "2024-09-16T09:10:10Z")

</div>

Perhaps, pass an `init` to argmax as follows:

```julia
argmax(f, Iterators.rest(itr, init))

```
