# Max and maximum: why the difference?

**URL:** <https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113>\
**Category:** General Usage\
**Created:** [April 8, 2017, 1:09am UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113 "2017-04-08T01:09:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 8, 2017, 1:09am UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/1 "2017-04-08T01:09:59Z")

</div>

`max` operates only on its arguments\*, and is only useful if the number of args is \> 1.\*\* `maximum` operates on a collection and only takes a second argument in specific cases.

Is there any reason why we couldn’t have a single-argument method on `max` that, if given a collection, returns the equivalent of `maximum`?

\*`max` has deprecated behavior in that it will operate on collections in an element-wise order.

\*\*This is inconsistent: `max(5)` returns (the possibly-expected) `5`, but `max([1,2,3])` does not return `[1,2,3]`, nor does `max("a")` return `a`.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 8, 2017, 1:31am UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/2 "2017-04-08T01:31:44Z")

</div>

This would be really confusing. It’s better to just use two functions rather than trying to guess what should the function do. See [Merging `max` and `maximum` · Issue #11872 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/11872#issuecomment-243947437)

> [@anon94023334](#):
>
> max has deprecated behavior in that it will operate on collections in an element-wise order.

The deprecated behavior has been, well, deprecated so it’s irrelevant now.

> [@anon94023334](#):
>
> This is inconsistent: max(5) returns (the possibly-expected) 5, but max([1,2,3]) does not return [1,2,3], nor does max(“a”) return a.

It’s not inconsistent. The “inconsistency” is that some types are comparable and some are not.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 8, 2017, 1:35am UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/3 "2017-04-08T01:35:49Z")

</div>

> The “inconsistency” is that some types are comparable and some are not.

I can understand that if you’re comparing two things. But what are you comparing with a single-argument `max`, and how is `max(x::Real) = x` useful?

And I swear it’s complete coincidence that I asked this question within 2 hours of @jeff.bezanson closing that issue out (I didn’t even know it existed).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 8, 2017, 3:00am UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/4 "2017-04-08T03:00:30Z")

</div>

> [@anon94023334](#):
>
> how is max(x::Real) = x useful?

It’s as useful as `+(::Real)` and it shouldn’t be assigned a meaning that is incompatible with what the function means otherwise.

---

<div class="post-metadata">

**Author:** ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)\
**Post date:** [April 8, 2017, 12:41pm UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/5 "2017-04-08T12:41:09Z")

</div>

Imagine a function like

`f(x...) = max(x...)+1`

I think people would be annoyed if this worked for `f(x,y,z)` and `f(x,y)` but then mysteriously failed on `f(x)`.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 8, 2017, 2:22pm UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/6 "2017-04-08T14:22:06Z")

</div>

```julia
julia> max("a","b","c")
"c"

julia> max("a","b")
"b"

julia> max("a")
ERROR: MethodError: no method matching max(::String)
Closest candidates are:
  max(::Any, ::Any) at operators.jl:348
  max(::Any, ::Any, ::Any, ::Any...) at operators.jl:424
  max(::BigFloat, ::BigFloat) at mpfr.jl:577
  ...

julia> f(x...) = max(x...) + 1
f (generic function with 1 method)

julia> f([1,2,3], [4,5,6], [7,8,9])
3-element Array{Int64,1}:
  8
  9
 10

julia> f([1,2,3], [4,5,6])
3-element Array{Int64,1}:
 5
 6
 7

julia> f([1,2,3])
ERROR: MethodError: no method matching max(::Array{Int64,1})
Closest candidates are:
  max(::AbstractArray{T1<:Real,N} where N, ::Real) where T1<:Real at deprecated.jl:50
  max(::AbstractArray{T1<:Real,N} where N, ::AbstractArray{T2<:Real,N} where N) where {T1<:Real, T2<:Real} at deprecated.jl:50
  max(::Any, ::Any) at operators.jl:348
  ...
Stacktrace:
 [1] f(::Array{Int64,1}, ::Vararg{Any,N} where N) at ./REPL[4]:1

```

Yes. (Though, to be fair, this isn’t the behavior I’m suggesting for a single vector/collection.)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 8, 2017, 3:14pm UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/7 "2017-04-08T15:14:14Z")

</div>

The behavior of `max("a")` basically means that the current implementation is not consistent enough and should be fixed. It’s not an argument for making it even more inconsistent. The `max([1, 2, 3])` case is [irrelavent](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/2).

---

<div class="post-metadata">

**Author:** ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)\
**Post date:** [October 9, 2017, 9:16am UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/8 "2017-10-09T09:16:18Z")

</div>

You could just add square brackets to the `max` argument in that case:

```julia
f(x...) = max([x...])+1

```

I would like to have just one function to get the maximum of a collection or several items.  
The two functions always confuse me and i think that the dot-notation removes the ambiguities.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [October 9, 2017, 12:06pm UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/9 "2017-10-09T12:06:53Z")

</div>

> [@shorty66](#):
>
> i think that the dot-notation removes the ambiguities.

I think that the dot notation will allocate memory so won’t be efficient for large collections.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [October 9, 2017, 1:59pm UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/10 "2017-10-09T13:59:54Z")

</div>

One possible way is this:

```nohighlight
f(x...) = (length(x) == 1 ? x[1] : max.(x...))+1

```

---

<div class="post-metadata">

**Author:** ![morningkyle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/morningkyle/32/1684_2.png) [@morningkyle](https://discourse.julialang.org/u/morningkyle)\
**Post date:** [October 15, 2017, 5:00am UTC](https://discourse.julialang.org/t/max-and-maximum-why-the-difference/3113/11 "2017-10-15T05:00:45Z")

</div>

> Is there any reason why we couldn’t have a single-argument method on max that, if given a collection, returns the equivalent of maximum?

There was a long discussion in [#4235](https://github.com/JuliaLang/julia/issues/4235). It explained the logic. Personally, I think there is no simple and intuitive reason for not choosing an unified `max` method.

From what I understand from related discussion, the drawbacks for using a unified `max` method are:  
(1) A single `max` with keyword arguments method is run-time slower than the `max` + `maximum` combination. So a performance-wise issue.  
(2) A single `max` method breaks the consistency of dot-call usages. For example, the `max` would work for both `max(scalar)` and `max(vector)`. Some developers argued that a single method accepts both scalar and vector input is confusing or redundant(use dot-call instead). Two other examples are `sin` and `clamp`, both of them do not accept vector input either.

IMHO, current implementation is explainable but not intuitive enough.
