Search code examples
juliamaxbuilt-in

What is the difference between max() and maximum() in Julia?


I am currently learning about the built-in functions in Julia, specifically max and maximum, and I am seeking clarification on their differences.

julia> max(1, 2, 3, 4)
4
julia> maximum([1, 2, 3, 4])
4

help?> max
search: max maximum maximum! maxintfloat argmax typemax findmax findmax! Cintmax_t floatmax Cuintmax_t Matrix minmax BitMatrix macroexpand @macroexpand

  max(x, y, ...)


  Return the maximum of the arguments (with respect to isless). See also the maximum function to take the maximum element from a collection.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> max(2, 5, 1)
  5

I see no difference between these two functions. It seems that maximum() has more functionality than max() and can do all thing max() do.

Why do they separate into 2 difference function? Why both exist together? Can you give me some examples where these two functions work in different contexts?


Solution

  • The documentation you included in your post makes it clear:

    See also the maximum function to take the maximum element from a collection.

    max() operates on a list of independent arguments, while maximum() operates on a single argument that is a collection.

    In your trivial and unrealistic example of course it makes no difference because you created the collection from literal values as a temporary instance from literal values. In a more realistic application, you would use whatever suited the input you were processing, which may or may not be a collection. While you could instantiate a temporary collection from independent variables in the same way, it would be an unnecessary processing overhead.

    In your examples with only literal values, the use of one over the other is perhaps not clear. But it is an unrealistic example. If you had a number of independent variables, it would be inefficient to collate them into a collection only to determine the maximum.

    x = 0
    y = 10
    z = 5
    max( x, y, z )      # maximum of x, y, z
    maximum( [x,y,z] )  # Create a temporary collection from x,y,x and return maximum
    
    a = [x,y,z]
    maximum( a )        # Maximum of an existing collection
    max( a )            # ERROR: a is not a collection.
    

    Interactively at https://julialang.org/learning/tryjulia/:

    julia> x=1
    1
    
    julia> y=3
    3
    
    julia> z=2
    2
    
    julia> a=[x,y,z]
    3-element Vector{Int64}:
     1
     3
     2
    
    julia> maximum(a)
    3
    
    julia> max(a)
    ERROR: MethodError: no method matching max(::Vector{Int64})
    Closest candidates are:
      max(::Any, ::Missing) at missing.jl:130
      max(::Any, ::Any) at operators.jl:419
      max(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
      ...
    Stacktrace:
     [1] top-level scope
       @ REPL[6]:1
    
    julia> max(x,y,z)
    3
    
    julia> ^C
    
    julia> 
    

    So you use the function appropriate to the data type you have, and the data type you have depends on what you are doing in the wider context of a larger program. If processing a lot of data iteratively or for data that is "collected" at run-time and which may be added to or removed from over time, you are unlikely to be using independent variables.