Max of a vector

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.

4 Likes

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.

4 Likes

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 or scan (or similar symbols). That way we could use max´(xs) and wouldn’t need maximum, sum, prod, etc so much.

2 Likes

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]), which is something that we tend to avoid in Julia.

2 Likes

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

5 Likes

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.

5 Likes

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.

4 Likes

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

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

GNU Fortran documentation:

  • max - maximum value of argument list
  • maxval - maximum value of an array
  • maxloc - Location of the maximum value within an array

In Julia, the corresponding functions are,

  • max - Return the maximum of the arguments (with respect to isless)
  • maximum - Return the largest element in a collection, iterator, or array
  • 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:

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.

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> const maxval = Base.maximum

Or

baremodule Foo
    using Base: max, argmax
    const maxval = Base.maximum
end
3 Likes

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

2 Likes