File size: 6,130 Bytes
e5a97c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import Mathlib.Data.List.Pairwise
import Mathlib.Data.List.Perm.Basic
import Mathlib.Data.Int.ModEq
import Mathlib.Tactic
namespace OAI
/-!
# The modular pivot order of a pair-stream row
These are the two cyclic orders in `lem:streams`. The list is an analysis view
of one sorted slice: implementation can traverse the slice by its pivot and
cursor, rather than allocating the reordered list. The permutation theorems
preserve occurrence multiplicity, including repeated residues.
-/
namespace LowSpaceSubsetSum
namespace ModularRows
variable {α : Type*}
def direct (P r : ℤ) (residue : α → ℤ) (xs : List α) : List α :=
xs.filter (fun x => decide (P - r ≤ residue x)) ++
xs.filter (fun x => decide (residue x < P - r))
def complementary (K : ℤ) (residue : α → ℤ) (xs : List α) : List α :=
(xs.filter (fun x => decide (residue x ≤ K))).reverse ++
(xs.filter (fun x => decide (K < residue x))).reverse
theorem direct_perm (P r : ℤ) (residue : α → ℤ) (xs : List α) :
(direct P r residue xs).Perm xs := by
simpa only [direct, ← decide_not, not_le] using
List.filter_append_perm (fun x => decide (P - r ≤ residue x)) xs
theorem complementary_perm (K : ℤ) (residue : α → ℤ) (xs : List α) :
(complementary K residue xs).Perm xs := by
apply ((List.reverse_perm _).append (List.reverse_perm _)).trans
simpa only [← decide_not, not_le] using List.filter_append_perm (fun x => decide (residue x ≤ K)) xs
theorem direct_wrapped {P r x : ℤ} (_hr : 0 ≤ r) (hrP : r < P)
(_hx : 0 ≤ x) (hxP : x < P) (hwrap : P - r ≤ x) :
(r + x) % P = r + x - P := by
rw [← Int.sub_emod_right (r + x) P]
exact Int.emod_eq_of_lt (by omega) (by omega)
theorem direct_unwrapped {P r x : ℤ} (hr : 0 ≤ r)
(hx : 0 ≤ x) (hwrap : x < P - r) : (r + x) % P = r + x :=
Int.emod_eq_of_lt (by omega) (by omega)
theorem complementary_low {P K x : ℤ} (_hK : 0 ≤ K) (hKP : K < P)
(hx : 0 ≤ x) (hsmall : x ≤ K) : (K - x) % P = K - x :=
Int.emod_eq_of_lt (by omega) (by omega)
theorem complementary_high {P K x : ℤ} (hK : 0 ≤ K) (_hKP : K < P)
(hxP : x < P) (hlarge : K < x) : (K - x) % P = K - x + P := by
rw [← Int.add_emod_right (K - x) P]
exact Int.emod_eq_of_lt (by omega) (by omega)
/-- Direct cyclic traversal is sorted by normalized pair residue. -/
theorem direct_sorted {P r : ℤ} (hr : 0 ≤ r) (hrP : r < P)
(residue : α → ℤ) (xs : List α)
(hbound : ∀ x ∈ xs, 0 ≤ residue x ∧ residue x < P)
(hsort : xs.Pairwise (fun x y => residue x ≤ residue y)) :
(direct P r residue xs).Pairwise
(fun x y => (r + residue x) % P ≤ (r + residue y) % P) := by
apply List.pairwise_append.mpr
refine ⟨?_, ?_, ?_⟩
· apply (hsort.filter (fun x => decide (P - r ≤ residue x))).imp_of_mem
intro x y hx hy hxy
obtain ⟨hxmem, hxwrap⟩ := List.mem_filter.mp hx
obtain ⟨hymem, hywrap⟩ := List.mem_filter.mp hy
have hxw : P - r ≤ residue x := of_decide_eq_true hxwrap
have hyw : P - r ≤ residue y := of_decide_eq_true hywrap
rw [direct_wrapped hr hrP (hbound x hxmem).1 (hbound x hxmem).2 hxw,
direct_wrapped hr hrP (hbound y hymem).1 (hbound y hymem).2 hyw]
omega
· apply (hsort.filter (fun x => decide (residue x < P - r))).imp_of_mem
intro x y hx hy hxy
obtain ⟨hxmem, hxwrap⟩ := List.mem_filter.mp hx
obtain ⟨hymem, hywrap⟩ := List.mem_filter.mp hy
rw [direct_unwrapped hr (hbound x hxmem).1 (of_decide_eq_true hxwrap),
direct_unwrapped hr (hbound y hymem).1 (of_decide_eq_true hywrap)]
omega
· intro x hx y hy
obtain ⟨hxmem, hxwrap⟩ := List.mem_filter.mp hx
obtain ⟨hymem, hywrap⟩ := List.mem_filter.mp hy
rw [direct_wrapped hr hrP (hbound x hxmem).1 (hbound x hxmem).2
(of_decide_eq_true hxwrap),
direct_unwrapped hr (hbound y hymem).1 (of_decide_eq_true hywrap)]
have := (hbound x hxmem).2
have := (hbound y hymem).1
omega
/-- Reverse cyclic traversal is sorted by the complementary residue. -/
theorem complementary_sorted {P K : ℤ} (hK : 0 ≤ K) (hKP : K < P)
(residue : α → ℤ) (xs : List α)
(hbound : ∀ x ∈ xs, 0 ≤ residue x ∧ residue x < P)
(hsort : xs.Pairwise (fun x y => residue x ≤ residue y)) :
(complementary K residue xs).Pairwise
(fun x y => (K - residue x) % P ≤ (K - residue y) % P) := by
apply List.pairwise_append.mpr
refine ⟨?_, ?_, ?_⟩
· apply List.pairwise_reverse.mpr
apply (hsort.filter (fun x => decide (residue x ≤ K))).imp_of_mem
intro x y hx hy hxy
obtain ⟨hxmem, hxsmall⟩ := List.mem_filter.mp hx
obtain ⟨hymem, hysmall⟩ := List.mem_filter.mp hy
rw [complementary_low hK hKP (hbound x hxmem).1 (of_decide_eq_true hxsmall),
complementary_low hK hKP (hbound y hymem).1 (of_decide_eq_true hysmall)]
omega
· apply List.pairwise_reverse.mpr
apply (hsort.filter (fun x => decide (K < residue x))).imp_of_mem
intro x y hx hy hxy
obtain ⟨hxmem, hxlarge⟩ := List.mem_filter.mp hx
obtain ⟨hymem, hylarge⟩ := List.mem_filter.mp hy
rw [complementary_high hK hKP (hbound x hxmem).2 (of_decide_eq_true hxlarge),
complementary_high hK hKP (hbound y hymem).2 (of_decide_eq_true hylarge)]
omega
· intro x hx y hy
obtain ⟨hxmem, hxsmall⟩ := List.mem_filter.mp (List.mem_reverse.mp hx)
obtain ⟨hymem, hylarge⟩ := List.mem_filter.mp (List.mem_reverse.mp hy)
rw [complementary_low hK hKP (hbound x hxmem).1 (of_decide_eq_true hxsmall),
complementary_high hK hKP (hbound y hymem).2 (of_decide_eq_true hylarge)]
have := (hbound x hxmem).1
have := (hbound y hymem).2
omega
/-- Filtering a row by the small modulus preserves its residue ordering. -/
theorem filtered_sorted (p c : ℤ) (weight residue : α → ℤ) (xs : List α)
(hsort : xs.Pairwise (fun x y => residue x ≤ residue y)) :
(xs.filter (fun x => decide (weight x % p = c % p))).Pairwise
(fun x y => residue x ≤ residue y) := hsort.filter _
end ModularRows
end LowSpaceSubsetSum
end OAI
|