# Max of a vector

**URL:** <https://discourse.julialang.org/t/max-of-a-vector/108294>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 3, 2024, 11:02am UTC](https://discourse.julialang.org/t/max-of-a-vector/108294 "2024-01-03T11:02:10Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [January 3, 2024, 10:33pm UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/21 "2024-01-03T22:33:54Z")

</div>

In Julia (since 1.7), `argmax` returns very different “kind” of result depending on its argument (either array index or element value), and easily leads to confusions and even bugs. For consistency, `maximum` and `findmax` are best – they always behave uniformly.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 4, 2024, 12:10am UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/22 "2024-01-04T00:10:23Z")

</div>

Yeah I get it, the point is not to rag on Matlab—that’s merely an incidental side benefit—but rather to point out that it’s not like there’s a really clean design that we simply chose to ignore where max and maximum are a single function. On the contrary, Matlab has an absurd hodgepodge of behaviors crammed into a single name that the user must navigate. It happens to work conveniently in a couple of common very specific cases but if you stray from those at all, you’re in some very dark woods.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 4, 2024, 12:57am UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/23 "2024-01-04T00:57:18Z")

</div>

Python’s `max` does

- if it’s one iterable argument, return the biggest item
- if it’s two or more arguments, return the biggest argument

which works fine for Python, though it means splatting `max(*xs)` is not simple. For Julia, which has more emphasis on generic programming, I wouldn’t want to go that way.

The thing I do think could be a good fit for Julia is using the APL syntax a bit, where each function can be modified postfix to map `f.` (“broadcasting”) or fold `f´` or scan `f¨` (or similar symbols). That way we could use `max´(xs)` and wouldn’t need `maximum`, `sum`, `prod`, etc so much.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 4, 2024, 1:03am UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/24 "2024-01-04T01:03:06Z")

</div>

> [@StefanKarpinski](#):
>
> Matlab has an absurd hodgepodge of behaviors crammed into a single name

To be fair, while there’s a culture of a function having one purpose over its myriad method signatures instead of many loosely related purposes, multiple responsibilities (`*`) and keyword-dependent behaviors are still possible in Julia. I don’t think that topic is even necessary to explain why Julia has `max` and `maximum` while MATLAB only has `max` (analogous to Julia’s `maximum`). MATLAB just doesn’t have an analog to Julia’s `max` comparing multiple arguments, you’re expected to put them in an array first, [max([A,B,C])](https://www.mathworks.com/matlabcentral/answers/54951-how-can-i-find-the-maximum-of-three-variables), which is something that we tend to avoid in Julia.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [January 4, 2024, 3:38am UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/25 "2024-01-04T03:38:23Z")

</div>

> [@sylvaticus](#):
>
> However for a novice Julia user, having this distinction is dramatic.

Feels like this could be a place to add the suggestion to try `maximum` to the error message explicitly.

EDIT: I should read the whole thread before commenting [Max of a vector - #16 by kristoffer.carlsson](https://discourse.julialang.org/t/max-of-a-vector/108294/16)

---

<div class="post-metadata">

**Author:** ![larsh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/larsh/32/51866_2.png) [@larsh](https://discourse.julialang.org/u/larsh)\
**Post date:** [January 7, 2024, 11:02pm UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/26 "2024-01-07T23:02:52Z")

</div>

I am not gonna judge or debate the reasons for splitting it into two functions, but it does not feel very elegant, nor intuitive, to name a function _maximum_ and the other function its abbreviated form: _max_.  
Typically–always?–acronyms, initalisms and abbreviations should correspond to their full form.

A better solution in my opinion would to have something like how it is done in Fortran: _max_ and _maxval_; or something along those lines. It makes it a bit easier to understand what each function does.

---

<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:** [January 21, 2024, 11:19am UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/27 "2024-01-21T11:19:59Z")

</div>

> [@larsh](#):
>
> A better solution in my opinion would to have something like how it is done in Fortran: _max_ and _maxval_; or something along those lines

That seems like just a version of the same problem. Which is which? Aren’t they just two different abbreviations of the same thing: “maximum value”?

The current situation isn’t perfect, but it’s hard to improve.

---

<div class="post-metadata">

**Author:** ![larsh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/larsh/32/51866_2.png) [@larsh](https://discourse.julialang.org/u/larsh)\
**Post date:** [January 21, 2024, 6:07pm UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/28 "2024-01-21T18:07:13Z")

</div>

I agree it is not a perfect solution–I guess none exist. But a little more clear still, in my opinion:  
maxval is maximum value  
max is maximum argument

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 21, 2024, 8:38pm UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/29 "2024-01-21T20:38:46Z")

</div>

“Maximum argument” makes me think you mean `argmax` which would be `maxloc` in Fortran.

GNU Fortran documentation:

- [`max`](https://gcc.gnu.org/onlinedocs/gfortran/MAX.html) - maximum value of argument list
- [`maxval`](https://gcc.gnu.org/onlinedocs/gfortran/MAXVAL.html) - maximum value of an array
- [`maxloc`](https://gcc.gnu.org/onlinedocs/gfortran/MAXLOC.html) - Location of the maximum value within an array

In Julia, the corresponding functions are,

- [`max`](https://docs.julialang.org/en/v1/base/math/#Base.max) - Return the maximum of the arguments (with respect to isless)
- [`maximum`](https://docs.julialang.org/en/v1/base/collections/#Base.maximum) - Return the largest element in a collection, iterator, or array
- [`argmax`](https://docs.julialang.org/en/v1/base/collections/#Base.argmax) - Return the index or key of the maximal element in a collection.

That said we are about 10 years too late on this conversation:

> <https://github.com/JuliaLang/julia/issues/4235>
>
> The generic \`max(itr)\` calls \`max\` on pairs of values, while \`max(Array)\` calls …\`\>\` on pairs of values. This is a problem because \`max(A::Array,B::Array)\` is elementwise, while all other definitions of \`max(A,B)\` return either \`A\` or \`B\` depending which is "larger".
> 
> At the very least, all 1-argument \`max\` definitions should be consistent on whether they do \`reduce(max, ...)\` or call \`\>\`.
> 
> Then we are just left with the elementwise behavior, which may be a fight for another day.

This is not going to change in `Base` Julia. [Julia is not at that stage of development where we can change this kind of surface naming.](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872)

I can tell you how to create your own aliases or even not load `Base` symbols into your current namespace if you would like.

```julia-repl
julia> const maxval = Base.maximum

```

Or

```julia
baremodule Foo
    using Base: max, argmax
    const maxval = Base.maximum
end

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [January 21, 2024, 9:06pm UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/30 "2024-01-21T21:06:26Z")

</div>

Not sure if these names are much better …  
What about `maxof`, `maxin` and `maxindex` (without telling which does what)?

---

<div class="post-metadata">

**Author:** ![tjulia](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@tjulia](https://discourse.julialang.org/u/tjulia)\
**Post date:** [February 24, 2025, 12:39am UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/31 "2025-02-24T00:39:13Z")

</div>

Base.max(v::Vector{S}) where {S\<:Real} = maximum(v)::S  
max([5,6,1])  
6

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 24, 2025, 4:03am UTC](https://discourse.julialang.org/t/max-of-a-vector/108294/32 "2025-02-24T04:03:13Z")

</div>

This is type piracy, and can have unpleasant consequences. Changing the behaviour of functions for types from Base isn’t recommended in general.

[Previous page](https://discourse.julialang.org/t/max-of-a-vector/108294.md?page=1)
