Minimum and maximum of lists #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
Main definitions #
The main definitions are argmax
, argmin
, minimum
and maximum
for lists.
argmax f l
returns some a
, where a
of l
that maximises f a
. If there are a b
such that
f a = f b
, it returns whichever of a
or b
comes first in the list.
argmax f []
= none`
minimum l
returns an with_top α
, the smallest element of l
for nonempty lists, and ⊤
for
[]
Auxiliary definition for argmax
and argmin
.
Equations
- list.arg_aux r a b = a.cases_on (option.some b) (λ (c : α), ite (r b c) (option.some b) (option.some c))
argmax f l
returns some a
, where f a
is maximal among the elements of l
, in the sense
that there is no b ∈ l
with f a < f b
. If a
, b
are such that f a = f b
, it returns
whichever of a
or b
comes first in the list. argmax f []
= none`.
Equations
- list.argmax f l = list.foldl (list.arg_aux (λ (b c : α), f c < f b)) option.none l
argmin f l
returns some a
, where f a
is minimal among the elements of l
, in the sense
that there is no b ∈ l
with f b < f a
. If a
, b
are such that f a = f b
, it returns
whichever of a
or b
comes first in the list. argmin f []
= none`.
Equations
- list.argmin f l = list.foldl (list.arg_aux (λ (b c : α), f b < f c)) option.none l
maximum l
returns an with_bot α
, the largest element of l
for nonempty lists, and ⊥
for
[]
Equations
- l.maximum = list.argmax id l
minimum l
returns an with_top α
, the smallest element of l
for nonempty lists, and ⊤
for
[]
Equations
- l.minimum = list.argmin id l