Sorting tuples by their values #
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 nTuple.monotone_sort:f ∘ Tuple.sort fisMonotoneTuple.sortDesc: givenf : Fin n → α, produces a permutation onFin nsorting into decreasing orderTuple.antitone_sortDesc:f ∘ Tuple.sortDesc fisAntitoneTuple.comp_sort_comp_rev_eq_comp_sortDesc: sorting descending equals sorting ascending, reversed
graph f produces the finset of pairs (f i, i)
equipped with the lexicographic order.
Equations
- Tuple.graph f = Finset.image (fun (i : Fin n) => (f i, i)) Finset.univ
Instances For
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 = (↑p).1
Instances For
graphEquiv₁ f is the natural equivalence between Fin n and graph f,
mapping i to (f i, i).
Equations
Instances For
graphEquiv₂ f is an equivalence between Fin n and graph f that respects the order.
Equations
- Tuple.graphEquiv₂ f = (Tuple.graph f).orderIsoOfFin ⋯
Instances For
sort f is the permutation that orders Fin n according to the order of the outputs of f.
Equations
- Tuple.sort f = (Tuple.graphEquiv₂ f).trans (Tuple.graphEquiv₁ f).symm
Instances For
sortDesc f is the permutation that orders Fin n according to the reverse order of the
outputs of f, so that f ∘ sortDesc f is decreasing.
Unlike sort f ∘ Fin.revPerm, which also arranges f in decreasing order, sortDesc f is a
stable sort: among indices with equal f-value it keeps their original order
(isStable_sortDesc), whereas sort f ∘ Fin.revPerm reverses it and so is stable only when f
is injective (isStable_sort_comp_rev_iff), in which case the two coincide
(sort_comp_rev_eq_sortDesc_of_injective).
Equations
- Tuple.sortDesc f = Tuple.sort (⇑OrderDual.toDual ∘ f)
Instances For
If f₀ ≤ f₁ ≤ f₂ ≤ ⋯ is a sorted n-tuple of elements of α, then for any j : Fin n and
a : α we have j < #{i | fᵢ ≤ a} iff fⱼ ≤ a.
If two permutations of a tuple f are both monotone, then they are equal.
If two permutations of a tuple f are both antitone, then they are equal.
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 σ is stable, i.e.
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.
A permutation σ equals sortDesc f if and only if f ∘ σ is antitone and σ is stable,
i.e. σ breaks ties by increasing index. This is the descending analogue of eq_sort_iff.
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.
The sorted-descending versions of a tuple f and of any permutation of f agree.
When f is injective there are no ties, so sorting descending agrees with sorting ascending
then reversing, already as an equality of permutations.
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.
The sorted version of a permutation σ is its inverse σ⁻¹.
IsStable f σ says exactly that σ⁻¹ preserves the order of any two indices sharing an
f-value.
sort f ∘ Fin.rev is a stable sort exactly when f is injective: on tied values it orders
them by decreasing index, the opposite of the stable sortDesc f.