math / data /lean /OAI /LinearAlgebra /MatrixMultiplication /JointExtraction /CompatibilityControls.lean
Download data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/CompatibilityControls.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 15 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/CompatibilityControls.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/CompatibilityControls.lean
-
curl -L -o CompatibilityControls.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/CompatibilityControls.lean
15 kB
| import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.CompatibilityIncidence | |
| import OAI.LinearAlgebra.MatrixMultiplication.Entropy.ComplexEntropyContinuity | |
| import Mathlib.Tactic.Linarith | |
| namespace OAI | |
| /-! Joint tensor extraction, compatibility and entropy estimates. -/ | |
| noncomputable section | |
| open scoped BigOperators | |
| open MatrixMultiplication.Foundation | |
| open MatrixMultiplication.JointTypeCounts MatrixMultiplication.JointCompatibilityIncidence | |
| namespace MatrixMultiplication.JointCompatibilityControls | |
| attribute [local instance] Classical.propDecidable | |
| private theorem exists_pos_le_family {K : Type*} [Fintype K] | |
| (Ξ΄ : K β β) (hΞ΄ : β k, 0 < Ξ΄ k) : | |
| β Ξ΅ : β, 0 < Ξ΅ β§ β k, Ξ΅ β€ Ξ΄ k := by | |
| classical | |
| have aux : β s : Finset K, β Ξ΅ : β, 0 < Ξ΅ β§ β k β s, Ξ΅ β€ Ξ΄ k := by | |
| intro s | |
| induction s using Finset.induction_on with | |
| | empty => exact β¨1, zero_lt_one, by simpβ© | |
| | @insert k s hk ih => | |
| obtain β¨Ξ΅, hΞ΅, hbβ© := ih | |
| refine β¨min (Ξ΄ k) Ξ΅, lt_min (hΞ΄ k) hΞ΅, ?_β© | |
| intro j hj | |
| rcases Finset.mem_insert.mp hj with rfl | hj | |
| Β· exact min_le_left _ _ | |
| Β· exact (min_le_right _ _).trans (hb j hj) | |
| obtain β¨Ξ΅, hΞ΅, hbβ© := aux Finset.univ | |
| exact β¨Ξ΅, hΞ΅, fun k => hb k (Finset.mem_univ k)β© | |
| variable {C : Type*} [Fintype C] [DecidableEq C] | |
| {Shape A : C β Type*} | |
| [β c, Fintype (Shape c)] [β c, DecidableEq (Shape c)] | |
| [β c, Fintype (A c)] [β c, DecidableEq (A c)] | |
| def residualBase (base : β c, Shape c β β) (d : β c, Shape c β Prop) (c : C) : β := | |
| β u : {u : Shape c // Β¬d c u}, base c u.val | |
| def designatedBase (base : β c, Shape c β β) (d : β c, Shape c β Prop) (c : C) : β := | |
| β u : {u : Shape c // d c u}, base c u.val | |
| def totalCenterCount (base : β c, Shape c β β) | |
| (law : β c, Shape c β A c β β) (c : C) (a : A c) : β := | |
| β u, (base c u : β) * law c u a | |
| def residualCenter (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) (c : C) (a : A c) : β := | |
| (β u : {u : Shape c // Β¬d c u}, (base c u.val : β) * law c u.val a) / | |
| residualBase base d c | |
| omit [Fintype C] [DecidableEq C] [β c, DecidableEq (Shape c)] | |
| [β c, Fintype (A c)] [β c, DecidableEq (A c)] in | |
| theorem residualBase_mul_center (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) (c : C) (a : A c) : | |
| (residualBase base d c : β) * residualCenter base d law c a = | |
| β u : {u : Shape c // Β¬d c u}, (base c u.val : β) * law c u.val a := by | |
| by_cases hr : residualBase base d c = 0 | |
| Β· have hb (u : {u : Shape c // Β¬d c u}) : base c u.val = 0 := by | |
| have hle : base c u.val β€ residualBase base d c := | |
| Finset.single_le_sum | |
| (f := fun v : {u : Shape c // Β¬d c u} => base c v.val) | |
| (fun _ _ => Nat.zero_le _) (Finset.mem_univ u) | |
| rw [hr] at hle | |
| exact Nat.eq_zero_of_le_zero hle | |
| simp only [hr, Nat.cast_zero, zero_mul, hb, Finset.sum_const_zero] | |
| Β· exact mul_div_cancelβ _ (Nat.cast_ne_zero.mpr hr) | |
| omit [Fintype C] [DecidableEq C] [β c, DecidableEq (Shape c)] | |
| [β c, Fintype (A c)] [β c, DecidableEq (A c)] in | |
| theorem residualCenter_nonneg (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) (h0 : β c u a, 0 β€ law c u a) | |
| (c : C) (a : A c) : 0 β€ residualCenter base d law c a := | |
| div_nonneg (Finset.sum_nonneg fun shape _ => | |
| mul_nonneg (Nat.cast_nonneg _) (h0 c shape.val a)) | |
| (Nat.cast_nonneg _) | |
| omit [Fintype C] [DecidableEq C] [β c, DecidableEq (Shape c)] | |
| [β c, Fintype (A c)] [β c, DecidableEq (A c)] in | |
| theorem residualCenter_le_one (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) (h1 : β c u a, law c u a β€ 1) | |
| (c : C) (a : A c) : residualCenter base d law c a β€ 1 := by | |
| by_cases hr : residualBase base d c = 0 | |
| Β· simp only [residualCenter, hr, Nat.cast_zero, div_zero, zero_le_one] | |
| Β· have hp : (0 : β) < residualBase base d c := Nat.cast_pos.mpr (Nat.pos_of_ne_zero hr) | |
| apply (div_le_iffβ hp).mpr | |
| calc | |
| (β u : {u : Shape c // Β¬d c u}, (base c u.val : β) * law c u.val a) β€ | |
| β u : {u : Shape c // Β¬d c u}, (base c u.val : β) * 1 := | |
| Finset.sum_le_sum fun u _ => mul_le_mul_of_nonneg_left (h1 _ _ _) (Nat.cast_nonneg _) | |
| _ = 1 * (residualBase base d c : β) := by | |
| simp only [mul_one, one_mul, residualBase, Nat.cast_sum] | |
| def groupCenter (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) (g : Ξ£ c, Option (Shape c)) : A g.1 β β := | |
| match g.2 with | |
| | some u => law g.1 u | |
| | none => residualCenter base d law g.1 | |
| def groupCap (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) (Ξ΅ : β) (g : Ξ£ c, Option (Shape c)) : β := | |
| finiteEntropy (groupCenter base d law g) + Ξ΅ | |
| omit [Fintype C] [DecidableEq C] [β c, DecidableEq (Shape c)] | |
| [β c, DecidableEq (A c)] in | |
| theorem groupCap_nonneg (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) | |
| (h0 : β c u a, 0 β€ law c u a) (h1 : β c u a, law c u a β€ 1) | |
| {Ξ΅ : β} (hΞ΅ : 0 β€ Ξ΅) (g : Ξ£ c, Option (Shape c)) : | |
| 0 β€ groupCap base d law Ξ΅ g := by | |
| apply add_nonneg _ hΞ΅ | |
| rcases g with β¨c, kβ© | |
| cases k with | |
| | some u => | |
| exact finiteEntropy_nonneg (law c u) (h0 c u) (h1 c u) | |
| | none => | |
| exact finiteEntropy_nonneg (residualCenter base d law c) | |
| (residualCenter_nonneg base d law h0 c) (residualCenter_le_one base d law h1 c) | |
| omit [DecidableEq C] [β c, DecidableEq (Shape c)] [β c, DecidableEq (A c)] in | |
| private theorem exists_group_entropy_window (base : β c, Shape c β β) | |
| (d : β c, Shape c β Prop) (law : β c, Shape c β A c β β) | |
| {Ξ΅ : β} (hΞ΅ : 0 < Ξ΅) : | |
| β Ξ΄ : β, 0 < Ξ΄ β§ β (g : Ξ£ c, Option (Shape c)) (q : A g.1 β β), | |
| (β a, |q a - groupCenter base d law g a| β€ Ξ΄) β | |
| finiteEntropy q β€ groupCap base d law Ξ΅ g := by | |
| have hex : β g : Ξ£ c, Option (Shape c), β Ξ΄ : β, 0 < Ξ΄ β§ | |
| β q : A g.1 β β, (β a, |q a - groupCenter base d law g a| β€ Ξ΄) β | |
| |finiteEntropy q - finiteEntropy (groupCenter base d law g)| < Ξ΅ := | |
| fun g => exists_entropy_window (groupCenter base d law g) hΞ΅ | |
| choose Ξ΄ hΞ΄ hw using hex | |
| obtain β¨Ξ΄β, hΞ΄β, hsmallβ© := exists_pos_le_family Ξ΄ hΞ΄ | |
| refine β¨Ξ΄β, hΞ΄β, ?_β© | |
| intro g q hq | |
| have hh := (abs_lt.mp (hw g q (fun a => (hq a).trans (hsmall g)))).2 | |
| change finiteEntropy q β€ finiteEntropy (groupCenter base d law g) + Ξ΅ | |
| linarith | |
| omit [Fintype C] [DecidableEq C] in | |
| theorem residual_card_repeated | |
| {Pos : C β Type*} [β c, Fintype (Pos c)] [β c, DecidableEq (Pos c)] | |
| (base : β c, Shape c β β) (d : β c, Shape c β Prop) (t : β) | |
| (w : FixedClassWords Pos Shape (fun c u => t * base c u)) (c : C) : | |
| Fintype.card {i : Pos c // Β¬d c ((w c).val i)} = t * residualBase base d c := by | |
| have he := (Fintype.card_congr (Equiv.sigmaSubtypeFiberEquivSubtype (w c).val | |
| (p := fun i => Β¬d c ((w c).val i)) (q := fun u => Β¬d c u) | |
| (fun _ => Iff.rfl))).symm | |
| have hc : Fintype.card {i : Pos c // Β¬d c ((w c).val i)} = | |
| β u : {u : Shape c // Β¬d c u}, wordPopulation (w c).val u.val := by | |
| simpa only [Fintype.card_sigma, wordPopulation] using he | |
| exact hc.trans (by simp only [(w c).property, residualBase, Finset.mul_sum]) | |
| omit [Fintype C] [DecidableEq C] [β c, Fintype (A c)] [β c, DecidableEq (A c)] in | |
| theorem repeated_mixture_balance | |
| {Pos : C β Type*} [β c, Fintype (Pos c)] [β c, DecidableEq (Pos c)] | |
| (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) (t : β) | |
| (w : FixedClassWords Pos Shape (fun c u => t * base c u)) (c : C) (a : A c) : | |
| (Fintype.card {i : Pos c // Β¬d c ((w c).val i)} : β) * residualCenter base d law c a = | |
| (t : β) * totalCenterCount base law c a - | |
| β u : {u : Shape c // d c u}, (t * base c u.val : β) * law c u.val a := by | |
| have hsplit := Fintype.sum_subtype_add_sum_subtype (d c) | |
| (fun u => (base c u : β) * law c u a) | |
| have hb : (β u : {u : Shape c // Β¬d c u}, (base c u.val : β) * law c u.val a) = | |
| totalCenterCount base law c a - | |
| β u : {u : Shape c // d c u}, (base c u.val : β) * law c u.val a := by | |
| unfold totalCenterCount | |
| linarith | |
| rw [residual_card_repeated, Nat.cast_mul, mul_assoc, residualBase_mul_center, hb] | |
| simp only [Nat.cast_mul, mul_sub, Finset.mul_sum, mul_assoc] | |
| omit [Fintype C] [DecidableEq C] in | |
| private theorem repeated_residual_width_le | |
| {Pos : C β Type*} [β c, Fintype (Pos c)] [β c, DecidableEq (Pos c)] | |
| (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (t : β) (w : FixedClassWords Pos Shape (fun c u => t * base c u)) | |
| (Ο Ξ· Ξ΄ : β) (hΞ΄ : 0 β€ Ξ΄) | |
| (hnum : β c, Ξ· + (designatedBase base d c : β) * Ο β€ Ξ΄) (c : C) : | |
| ((t : β) * Ξ· + (β u : {u : Shape c // d c u}, ((t * base c u.val : β) : β)) * Ο) / | |
| Fintype.card {i : Pos c // Β¬d c ((w c).val i)} β€ Ξ΄ := by | |
| have hsum : (β u : {u : Shape c // d c u}, ((t * base c u.val : β) : β)) = | |
| (t : β) * designatedBase base d c := by | |
| simp only [Nat.cast_mul, designatedBase, Nat.cast_sum, Finset.mul_sum] | |
| rw [residual_card_repeated, hsum, Nat.cast_mul] | |
| by_cases ht : t = 0 | |
| Β· simpa only [ht, Nat.cast_zero, zero_mul, add_zero, zero_add, zero_div] using hΞ΄ | |
| by_cases hr : residualBase base d c = 0 | |
| Β· simpa only [hr, Nat.cast_zero, mul_zero, div_zero] using hΞ΄ | |
| have htpos : (0 : β) < t := Nat.cast_pos.mpr (Nat.pos_of_ne_zero ht) | |
| have hrpos : (0 : β) < residualBase base d c := Nat.cast_pos.mpr (Nat.pos_of_ne_zero hr) | |
| have hrone : (1 : β) β€ residualBase base d c := by exact_mod_cast Nat.one_le_iff_ne_zero.mpr hr | |
| have hres : Ξ· + (designatedBase base d c : β) * Ο β€ Ξ΄ * residualBase base d c := | |
| (hnum c).trans (by simpa only [mul_one] using mul_le_mul_of_nonneg_left hrone hΞ΄) | |
| apply (div_le_iffβ (mul_pos htpos hrpos)).mpr | |
| calc | |
| (t : β) * Ξ· + ((t : β) * designatedBase base d c) * Ο = | |
| (t : β) * (Ξ· + (designatedBase base d c : β) * Ο) := by ring | |
| _ β€ (t : β) * (Ξ΄ * residualBase base d c) := | |
| mul_le_mul_of_nonneg_left hres htpos.le | |
| _ = Ξ΄ * ((t : β) * residualBase base d c) := by ring | |
| omit [DecidableEq C] in | |
| theorem exists_uniform_sideEntropyControl | |
| (Pos : β β C β Type*) [β t c, Fintype (Pos t c)] [β t c, DecidableEq (Pos t c)] | |
| (Pair : C β Type*) [β c, Fintype (Pair c)] [β c, DecidableEq (Pair c)] | |
| (base : β c, Shape c β β) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) (part : β c, Pair c β A c) | |
| (h0 : β c u a, 0 β€ law c u a) (h1 : β c u a, law c u a β€ 1) | |
| {Ξ΅ ΟMax Ξ·Max : β} (hΞ΅ : 0 < Ξ΅) (hΟMax : 0 < ΟMax) (hΞ·Max : 0 < Ξ·Max) : | |
| β Ο Ξ· : β, 0 < Ο β§ Ο β€ ΟMax β§ 0 < Ξ· β§ Ξ· β€ Ξ·Max β§ | |
| β (t : β) (pairCounts : β c, Pair c β β) | |
| (w : FixedClassWords (Pos t) Shape (fun c u => t * base c u)), | |
| (β c a, |((β s : {s : Pair c // part c s = a}, pairCounts c s.val : β) : β) - | |
| (t : β) * totalCenterCount base law c a| β€ (t : β) * Ξ·) β | |
| Nonempty (SideEntropyControl (Pos t) Shape Pair A (fun c u => t * base c u) | |
| pairCounts part d law Ο (groupCap base d law Ξ΅) w) := by | |
| obtain β¨Ξ΄, hΞ΄, hwindowβ© := exists_group_entropy_window base d law hΞ΅ | |
| let D : β := β c, designatedBase base d c | |
| have hD : (0 : β) β€ D := Nat.cast_nonneg _ | |
| have hden : 0 < 2 * ((D : β) + 1) := by positivity | |
| let Οβ : β := Ξ΄ / (2 * ((D : β) + 1)) | |
| have hΟβ : 0 < Οβ := div_pos hΞ΄ hden | |
| have heq : (2 * ((D : β) + 1)) * Οβ = Ξ΄ := mul_div_cancelβ _ hden.ne' | |
| have hΟΞ΄ : Οβ β€ Ξ΄ := by nlinarith [mul_nonneg hD hΟβ.le] | |
| have hDΟ : (D : β) * Οβ β€ Ξ΄ / 2 := by nlinarith | |
| let Ο := min ΟMax Οβ | |
| let Ξ· := min Ξ·Max (Ξ΄ / 2) | |
| have hΟ : 0 < Ο := lt_min hΟMax hΟβ | |
| have hΞ· : 0 < Ξ· := lt_min hΞ·Max (half_pos hΞ΄) | |
| have hΟΞ΄' : Ο β€ Ξ΄ := (min_le_right _ _).trans hΟΞ΄ | |
| have hnum (c : C) : Ξ· + (designatedBase base d c : β) * Ο β€ Ξ΄ := by | |
| have hc : (designatedBase base d c : β) β€ D := by | |
| exact_mod_cast (Finset.single_le_sum (fun _ _ => Nat.zero_le _) | |
| (Finset.mem_univ c) : designatedBase base d c β€ β c, designatedBase base d c) | |
| have hm : (designatedBase base d c : β) * Ο β€ (D : β) * Οβ := | |
| mul_le_mul hc (min_le_right _ _) hΟ.le hD | |
| have he : Ξ· β€ Ξ΄ / 2 := min_le_right _ _ | |
| linarith | |
| refine β¨Ο, Ξ·, hΟ, min_le_left _ _, hΞ·, min_le_left _ _, ?_β© | |
| intro t pairCounts w haggr | |
| refine β¨{ | |
| residualLaw := residualCenter base d law | |
| totalTarget := fun c a => (t : β) * totalCenterCount base law c a | |
| totalError := fun _ => (t : β) * Ξ· | |
| aggregate_window := haggr | |
| mixture_balance := repeated_mixture_balance base d law t w | |
| nonneg := groupCap_nonneg base d law h0 h1 hΞ΅.le | |
| designated_entropy := ?_ | |
| residual_entropy := ?_ }β© | |
| Β· intro c u _ q hq | |
| exact hwindow β¨c, some uβ© q (fun a => (hq a).trans hΟΞ΄') | |
| Β· intro c q hq | |
| exact hwindow β¨c, noneβ© q (fun a => (hq a).trans | |
| (repeated_residual_width_le base d t w Ο Ξ· Ξ΄ hΞ΄.le hnum c)) | |
| def control_mono_width | |
| {Pos Pair : C β Type*} | |
| [β c, Fintype (Pos c)] [β c, DecidableEq (Pos c)] | |
| [β c, Fintype (Pair c)] [β c, DecidableEq (Pair c)] | |
| (shapeCounts : β c, Shape c β β) (pairCounts : β c, Pair c β β) | |
| (part : β c, Pair c β A c) (d : β c, Shape c β Prop) | |
| (law : β c, Shape c β A c β β) {Ο Ο' : β} | |
| (H : (Ξ£ c, Option (Shape c)) β β) | |
| (w : FixedClassWords Pos Shape shapeCounts) | |
| (control : SideEntropyControl Pos Shape Pair A shapeCounts pairCounts part d law Ο H w) | |
| (hΟ : Ο' β€ Ο) : | |
| SideEntropyControl Pos Shape Pair A shapeCounts pairCounts part d law Ο' H w where | |
| residualLaw := control.residualLaw | |
| totalTarget := control.totalTarget | |
| totalError := control.totalError | |
| aggregate_window := control.aggregate_window | |
| mixture_balance := control.mixture_balance | |
| nonneg := control.nonneg | |
| designated_entropy c u hu q hq := | |
| control.designated_entropy c u hu q (fun a => (hq a).trans hΟ) | |
| residual_entropy c q hq := by | |
| apply control.residual_entropy c q | |
| intro a | |
| apply (hq a).trans | |
| apply div_le_div_of_nonneg_right _ (Nat.cast_nonneg _) | |
| apply add_le_add_right | |
| exact mul_le_mul_of_nonneg_left hΟ (Finset.sum_nonneg fun _ _ => Nat.cast_nonneg _) | |
| end MatrixMultiplication.JointCompatibilityControls | |
| end | |
| end OAI | |