The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.Fin.Tuple.Sort

36 declarations. Mathlib docs · Source

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: given f : Fin n → α, produces a permutation on Fin n
  • Tuple.monotone_sort: f ∘ Tuple.sort f is Monotone
  • Tuple.sortDesc: given f : Fin n → α, produces a permutation on Fin n sorting into decreasing order
  • Tuple.antitone_sortDesc: f ∘ Tuple.sortDesc f is Antitone
  • Tuple.comp_sort_comp_rev_eq_comp_sortDesc: sorting descending equals sorting ascending, reversed

def Tuple.graph E1 #

{n : ℕ} → {α : Type u_1} → [LinearOrder α] → (Fin n → α) → Finset (Lex (α × Fin n))

graph f produces the finset of pairs (f i, i) equipped with the lexicographic order.

def Tuple.graph.proj E1 #

{n : ℕ} → {α : Type u_1} → [inst : LinearOrder α] → {f : Fin n → α} → ↥(Tuple.graph f) → α

Given p : α ×ₗ (Fin n) := (f i, i) with p ∈ graph f, graph.proj p is defined to be f i.

theorem Tuple.graph.card E0 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α), (Tuple.graph f).card = n

def Tuple.graphEquiv₁ E1 #

{n : ℕ} → {α : Type u_1} → [inst : LinearOrder α] → (f : Fin n → α) → Fin n ≃ ↥(Tuple.graph f)

graphEquiv₁ f is the natural equivalence between Fin n and graph f, mapping i to (f i, i).

theorem Tuple.proj_equiv₁' E0 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α), Tuple.graph.proj ∘ ⇑(Tuple.graphEquiv₁ f) = f

def Tuple.graphEquiv₂ E1 #

{n : ℕ} → {α : Type u_1} → [inst : LinearOrder α] → (f : Fin n → α) → Fin n ≃o ↥(Tuple.graph f)

graphEquiv₂ f is an equivalence between Fin n and graph f that respects the order.

def Tuple.sort E3 #

{n : ℕ} → {α : Type u_1} → [LinearOrder α] → (Fin n → α) → Equiv.Perm (Fin n)

sort f is the permutation that orders Fin n according to the order of the outputs of f.

theorem Tuple.graphEquiv₂_apply E0 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α) (i : Fin n),
  (Tuple.graphEquiv₂ f) i = (Tuple.graphEquiv₁ f) ((Tuple.sort f) i)

theorem Tuple.self_comp_sort E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α),
  f ∘ ⇑(Tuple.sort f) = Tuple.graph.proj ∘ ⇑(Tuple.graphEquiv₂ f)

theorem Tuple.monotone_proj E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α), Monotone Tuple.graph.proj

theorem Tuple.monotone_sort E3 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α), Monotone (f ∘ ⇑(Tuple.sort f))

def Tuple.sortDesc E3 #

{n : ℕ} → {α : Type u_1} → [LinearOrder α] → (Fin n → α) → Equiv.Perm (Fin n)

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).

theorem Tuple.antitone_sortDesc E3 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α), Antitone (f ∘ ⇑(Tuple.sortDesc f))

theorem Tuple.lt_card_le_iff_apply_le_of_monotone E1 #

∀ {n : ℕ} {α : Type u_1} {j : Fin n} {f : Fin n → α} [inst : Preorder α] {a : α} [inst_1 : DecidableLE α],
  Monotone f → (↑j < {i | f i ≤ a}.card ↔ f j ≤ a)

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.

theorem Tuple.lt_card_ge_iff_apply_ge_of_antitone E1 #

∀ {n : ℕ} {α : Type u_1} {j : Fin n} {f : Fin n → α} [inst : Preorder α] {a : α} [inst_1 : DecidableLE α],
  Antitone f → (↑j < {i | a ≤ f i}.card ↔ a ≤ f j)

theorem Tuple.lt_card_lt_iff_apply_lt_of_monotone E1 #

∀ {n : ℕ} {α : Type u_1} {j : Fin n} {f : Fin n → α} [inst : Preorder α] {a : α} [inst_1 : DecidableLT α],
  Monotone f → (↑j < {i | f i < a}.card ↔ f j < a)

theorem Tuple.lt_card_gt_iff_apply_gt_of_antitone E1 #

∀ {n : ℕ} {α : Type u_1} {j : Fin n} {f : Fin n → α} [inst : Preorder α] {a : α} [inst_1 : DecidableLT α],
  Antitone f → (↑j < {i | a < f i}.card ↔ a < f j)

theorem Tuple.unique_monotone E1 #

∀ {n : ℕ} {α : Type u_1} [inst : PartialOrder α] {f : Fin n → α} {σ τ : Equiv.Perm (Fin n)},
  Monotone (f ∘ ⇑σ) → Monotone (f ∘ ⇑τ) → f ∘ ⇑σ = f ∘ ⇑τ

If two permutations of a tuple f are both monotone, then they are equal.

theorem Tuple.unique_antitone E1 #

∀ {n : ℕ} {α : Type u_1} [inst : PartialOrder α] {f : Fin n → α} {σ τ : Equiv.Perm (Fin n)},
  Antitone (f ∘ ⇑σ) → Antitone (f ∘ ⇑τ) → f ∘ ⇑σ = f ∘ ⇑τ

If two permutations of a tuple f are both antitone, then they are equal.

theorem Tuple.sort_eq_refl_iff_monotone E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α}, Tuple.sort f = Equiv.refl (Fin n) ↔ Monotone f

The permutation that sorts f is the identity if and only if f is monotone.

theorem Tuple.comp_sort_eq_comp_iff_monotone E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α} {σ : Equiv.Perm (Fin n)},
  f ∘ ⇑σ = f ∘ ⇑(Tuple.sort f) ↔ Monotone (f ∘ ⇑σ)

A permutation of a tuple f is f sorted if and only if it is monotone.

theorem Tuple.comp_perm_comp_sort_eq_comp_sort E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α} {σ : Equiv.Perm (Fin n)},
  (f ∘ ⇑σ) ∘ ⇑(Tuple.sort (f ∘ ⇑σ)) = f ∘ ⇑(Tuple.sort f)

The sorted versions of a tuple f and of any permutation of f agree.

theorem Tuple.comp_perm_comp_sortDesc_eq_comp_sortDesc E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α} {σ : Equiv.Perm (Fin n)},
  (f ∘ ⇑σ) ∘ ⇑(Tuple.sortDesc (f ∘ ⇑σ)) = f ∘ ⇑(Tuple.sortDesc f)

The sorted-descending versions of a tuple f and of any permutation of f agree.

theorem Tuple.comp_sort_comp_rev_eq_comp_sortDesc E3 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α}, f ∘ ⇑(Tuple.sort f) ∘ Fin.rev = f ∘ ⇑(Tuple.sortDesc f)

Sorting f in descending order is the same as sorting it in ascending order, then reversing.

theorem Tuple.comp_sortDesc_comp_rev_eq_comp_sort E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α}, f ∘ ⇑(Tuple.sortDesc f) ∘ Fin.rev = f ∘ ⇑(Tuple.sort f)

Sorting f in ascending order is the same as sorting it in descending order, then reversing.

theorem Tuple.sort_comp_rev_eq_sortDesc_of_injective E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α},
  Function.Injective f → ⇑(Tuple.sort f) ∘ Fin.rev = ⇑(Tuple.sortDesc f)

When f is injective there are no ties, so sorting descending agrees with sorting ascending then reversing, already as an equality of permutations.

theorem Tuple.sortDesc_comp_rev_eq_sort_of_injective E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α},
  Function.Injective f → ⇑(Tuple.sortDesc f) ∘ Fin.rev = ⇑(Tuple.sort f)

theorem Tuple.antitone_pair_of_not_sorted' E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α} {σ : Equiv.Perm (Fin n)},
  f ∘ ⇑σ ≠ f ∘ ⇑(Tuple.sort f) → ∃ i j, i < j ∧ (f ∘ ⇑σ) j < (f ∘ ⇑σ) i

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.

theorem Tuple.antitone_pair_of_not_sorted E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α}, f ≠ f ∘ ⇑(Tuple.sort f) → ∃ i j, i < j ∧ f j < f i

If the tuple f is not the same as f ∘ sort f, then f has a pair of strictly decreasing entries.

theorem Tuple.sort_perm E0 #

∀ {n : ℕ} (σ : Equiv.Perm (Fin n)), Tuple.sort ⇑σ = σ⁻¹

The sorted version of a permutation σ is its inverse σ⁻¹.

theorem Tuple.isStable_sort E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α), IsStable f ⇑(Tuple.sort f)

theorem Tuple.isStable_sortDesc E1 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] (f : Fin n → α), IsStable f ⇑(Tuple.sortDesc f)

theorem Tuple.isStable_iff_imp E1 #

∀ {α : Type u_2} {γ : Type u_3} [inst : Preorder α] (f : α → γ) (σ : Equiv.Perm α),
  IsStable f ⇑σ ↔ ∀ ⦃i j : α⦄, f i = f j → σ⁻¹ i ≤ σ⁻¹ j → i ≤ j

IsStable f σ says exactly that σ⁻¹ reflects the order of any two indices sharing an f-value.

theorem Tuple.isStable_iff E0 #

∀ {α : Type u_2} {γ : Type u_3} [inst : LinearOrder α] (f : α → γ) (σ : Equiv.Perm α),
  IsStable f ⇑σ ↔ ∀ ⦃i j : α⦄, f i = f j → (i ≤ j ↔ σ⁻¹ i ≤ σ⁻¹ j)

IsStable f σ says exactly that σ⁻¹ preserves the order of any two indices sharing an f-value.

theorem Tuple.isStable_sort_comp_rev_iff E0 #

∀ {n : ℕ} {α : Type u_1} [inst : LinearOrder α] {f : Fin n → α},
  IsStable f (⇑(Tuple.sort f) ∘ Fin.rev) ↔ Function.Injective f

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.

theorem Equiv.Perm.monotone_iff E0 #

∀ {n : ℕ} (σ : Equiv.Perm (Fin n)), Monotone ⇑σ ↔ σ = 1