Why `maximum` uses `max` and `findmax` uses `isless`?


#1

While playing around I noted some inconsistencies with maximum and findmax (already pointed out in another thread). Reading the source code I convinced myself that this is due to the fact that maximum is implemented using the max function and findmax using the isless function (indeed I managed to make my code work overloading the corresponding functions in Base).

Is this analysis correct? This is a bug or a feature? What is the reason behind this design?


#2

The generic max uses <, which then uses isless — so things are consistent (unless specific methods deviate from this).

How could you find about this? Reading the source, of course, but finding out which source files can be tricky. Try something like

julia> struct Foo end

julia> @edit max(Foo(), Foo())

julia> @edit Foo() < Foo()

#3

You are perfectly right (and, by the way, thanks for the hint to use the @edit macro). My problem is clearly elsewhere. Here is the code that tricked me.

# This is my solution to the 8th problem on Project Euler.
# The solution is the maximum product of the possible sequences of thirteen 
# adjacent digits in the following 1000 digits number (given as a string `str`).
# As an exercise, I was trying to avoid multiplying all 13 digits in the sequence
# every time a comparison i needed. Indeed if the sequences overlap one can factor
# away the common digits.
# I defined a custom type and an ordering function, in order to
# exploit the `maximum` (or `findmax`) functions.

#! /usr/bin/julia

const str = """
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
"""

const dg = digits(parse(BigInt, str))
const seqlen = 13

struct Index
  indx::Int
end


# With this trivial implementation of `isless` everything works fine: 
# `maximum` and `findmax` returns the same result.
#=
function Base.isless(a::Index,b::Index)
  prod(dg[a.indx:a.indx+seqlen-1]) < prod(dg[b.indx:b.indx+seqlen-1]) 
end
=#

# This implementations of `isless` gives me different results
# for `maximum` and `findmax`
function Base.isless(a::Index,b::Index)
  n, k = a.indx, b.indx
  if abs(n-k) >= seqlen
    return  prod(dg[n:n+seqlen-1]) < prod(dg[k:k+seqlen-1])
  else
    d1 = abs(n-k) - 2
    d2 = seqlen - d1 - 1
    if n < k
      return prod(dg[n:n+d1]) < prod(dg[k+d2:k+seqlen-1])
    elseif n > k
      return prod(dg[n+d2:n+seqlen-1]) < prod(dg[k:k+d1])
    else
      return false
    end
  end
end

# However if I define this `max` function
# (basically by copying-and-pasting and adding some ternary operators)
# then `findmax` works properly. How come?
#=
function Base.max(a::Index,b::Index)
  n, k = a.indx, b.indx
  if abs(n-k) >= seqlen
    return  prod(dg[n:n+seqlen-1]) < prod(dg[k:k+seqlen-1]) ? b : a
  else
    d1 = abs(n-k) - 2
    d2 = seqlen - d1 - 1
    if n < k
      return prod(dg[n:n+d1]) < prod(dg[k+d2:k+seqlen-1]) ? b : a
    elseif n > k
      return prod(dg[n+d2:n+seqlen-1]) < prod(dg[k:k+d1]) ? b : a
    else
      return a
    end
  end
end
=#

Base.show(io::IO, i::Index) = Base.show(io, prod(dg[i.indx:i.indx+seqlen-1]))

answer_with_maximum = maximum(Index(n) for n = 1:length(dg)-seqlen+1)
answer_with_findmax = findmax(Index(n) for n = 1:length(dg)-seqlen+1)[1]


@show answer_with_maximum
@show answer_with_findmax