Julia: Merging `max` and `maximum`

Created on 26 Jun 2015  路  7Comments  路  Source: JuliaLang/julia

Related to #4235 and #4652 it seems like it would be nice to have one keyword
for all cases, preferably max instead of max and maximum.

The main reason we can't combine the two is because the following methods
would collide under the same function name:

max{T1<:Real,T2<:Real}(::AbstractArray{T1<:Real,N}, ::T2<:Real) at operators.jl:378
maximum(A::AbstractArray{T,N}, region) at reducedim.jl:261

Proposal:

Change max to handle all of maximum's methods, discarding maximum and
discarding the max method mentioned above.

Instead, this aforementioned max method can be achieved through several
options:

  1. broadcast (or map?) max
  2. create a .max function (if possible?) in the same vein as .+, etc.
  3. create a new keyword for a vectorized function with the same method

This is partly related to @johnmyleswhite's talk on 2015-06-25 JuliaCon
which mentioned vectorizing operations might be an issue and we should possibly
move back to using map.

Also https://github.com/JuliaLang/julia/issues/4235#issuecomment-26136766 mentioned map was too slow, is this still the case with broadcast?

won't change

Most helpful comment

Note that in principle we could revisit this now. max.(a,b) does the element-wise thing, and element-wise max(a,b) is deprecated in #17302. So that frees up max(a) to do reduction again. Per our usual policy of "one release cycle of deprecation", however, the latter couldn't happen until Julia 0.7.

All 7 comments

Is that really the main ambiguity driving the distinction? Perhaps max{T1<:Real,T2<:Real}(A::AbstractArray{T1<:Real,N}, b::T2<:Real) could be deprecated in favor of clamp(A, b, typemax(T2)), though I did have to think about which way that needs to go for longer than I'd like to admit, and I would likely get it backwards way too often.

Apparently there is a more generalized issue for this problem #8450 @JeffBezanson mentioned in his JuliaCon talk today. Good to see others aren't crazy about the vectorized versions of functions.

I think this depends on mapslices more than map

maximum is a reduction, so the right thing would be to use some combination of max, reduce, and mapslices.

Note that in principle we could revisit this now. max.(a,b) does the element-wise thing, and element-wise max(a,b) is deprecated in #17302. So that frees up max(a) to do reduction again. Per our usual policy of "one release cycle of deprecation", however, the latter couldn't happen until Julia 0.7.

There's an ambiguity here. If there is a type T that is both ordered and is a collection, then what should max(x::T) do? Should it apply to x as a collection (and return its maximum), or should it apply to x as ordered type (and return x)?

Compare to + and sum. + with a single argument returns that argument unchanged, sum with a single argument treats it as collection and sums its elements. max and + behave very similarly in this respect.

A real-world case are SIMD vectors. For these, both max and maximum is well-defined, and they return different results.

I don't think max and reduce(max, x) can be considered the same function.

Was this page helpful?
0 / 5 - 0 ratings