Max() bug?

julia> a=[1,2,3];
julia> b=[10,-Inf,9];
julia> c=[-30,5,2];
julia> max(a,b,c)
3-element Array{Float64,1}:
  10.0
 -Inf
   9.0
julia> max.(a,b,c)
3-element Array{Float64,1}:
 10.0
  5.0
  9.0

max.(a,b,c) is correct. Why is it not equal to max(a,b,c) for the second element?

1 Like

max() returns the maximum of its arguments. That is, max(a, b, c) returns whichever of a, b or c is greatest. It then follows that whatever is returned by max(a, b, c) must be one of a, b, or c. In your case, the answer is b, which is exactly what is returned.

max.(a, b, c) is the elementwise maximum of the three inputs, so it gives a different answer.

5 Likes

But b is neither bigger nor smaller than the vector a.
What sort of vector order is being assumed?

Vectors are compared in lexicographic order, so b is greatest because the first element of b is greater than the first elements of a and c.

8 Likes

Thanks.
Can anyone refer me to where in the docs it is explained that Lexicographic order is the default for max?

This has nothing to do with max itself–it just uses the result of the < function. The lexicographic ordering comes from the definition of < (which by default falls back to the function isless) for abstract vectors. I don’t have a link to the relevant part of the documentation handy, but that’s where you’d want to look.

5 Likes

isless(::Tuple, ::Tuple) mentions lexicographic ordering, but the method for vectors does not have a docstring, so I made a tiny PR adding it:

https://github.com/JuliaLang/julia/pull/37986

7 Likes