Sorting tuples by their values #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
Given an n
-tuple f : fin n → α
where α
is ordered,
we may want to turn it into a sorted n
-tuple.
This file provides an API for doing so, with the sorted n
-tuple given by
f ∘ tuple.sort f
.
Main declarations #
tuple.sort
: givenf : fin n → α
, produces a permutation onfin n
tuple.monotone_sort
:f ∘ tuple.sort f
ismonotone
graph f
produces the finset of pairs (f i, i)
equipped with the lexicographic order.
Equations
- tuple.graph f = finset.image (λ (i : fin n), (f i, i)) finset.univ
Given p : α ×ₗ (fin n) := (f i, i)
with p ∈ graph f
,
graph.proj p
is defined to be f i
.
Equations
- tuple.graph.proj = λ (p : ↥(tuple.graph f)), p.val.fst
graph_equiv₁ f
is the natural equivalence between fin n
and graph f
,
mapping i
to (f i, i)
.
graph_equiv₂ f
is an equivalence between fin n
and graph f
that respects the order.
Equations
sort f
is the permutation that orders fin n
according to the order of the outputs of f
.
Equations
- tuple.sort f = (tuple.graph_equiv₂ f).to_equiv.trans (tuple.graph_equiv₁ f).symm
A permutation σ
equals sort f
if and only if the map i ↦ (f (σ i), σ i)
is
strictly monotone (w.r.t. the lexicographic ordering on the target).
A permutation σ
equals sort f
if and only if f ∘ σ
is monotone and whenever i < j
and f (σ i) = f (σ j)
, then σ i < σ j
. This means that sort f
is the lexicographically
smallest permutation σ
such that f ∘ σ
is monotone.
The permutation that sorts f
is the identity if and only if f
is monotone.
A permutation of a tuple f
is f
sorted if and only if it is monotone.
The sorted versions of a tuple f
and of any permutation of f
agree.
If a permutation f ∘ σ
of the tuple f
is not the same as f ∘ sort f
, then f ∘ σ
has a pair of strictly decreasing entries.