cfahlgren1/math / lean /OAI /LinearAlgebra /MatrixMultiplication /Tensor /ComplexFactorialLogBounds.lean
cfahlgren1's picture
download
raw
5.69 kB
import Mathlib.Analysis.SpecialFunctions.Stirling
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.Ring
namespace OAI
/-! Finite coefficient tensors and their algebraic transformations. -/
namespace MatrixMultiplication.Foundation
open Filter
open scoped Topology
noncomputable def factorialLogRemainder (n : ℕ) : ℝ :=
Real.log (Nat.factorial n : ℝ) - ((n : ℝ) * Real.log n - n)
theorem factorialLogRemainder_eq (n : ℕ) :
factorialLogRemainder n =
Real.log (Nat.factorial n : ℝ) - ((n : ℝ) * Real.log n - n) := rfl
@[simp] theorem factorialLogRemainder_zero : factorialLogRemainder 0 = 0 := by
simp [factorialLogRemainder]
theorem factorialLogRemainder_stirling (n : ℕ) :
factorialLogRemainder n =
Real.log (Stirling.stirlingSeq n) + (1 / 2 : ℝ) * Real.log (2 * n) := by
cases n with
| zero => simp
| succ n =>
have h := Stirling.log_stirlingSeq_formula (n + 1)
rw [Real.log_div (by positivity) (Real.exp_ne_zero _), Real.log_exp] at h
unfold factorialLogRemainder
linarith
theorem tendsto_factorialLogRemainder_div :
Tendsto (fun n : ℕ => factorialLogRemainder n / (n : ℝ)) atTop (𝓝 0) := by
have hnat : Tendsto (fun n : ℕ => (n : ℝ)) atTop atTop :=
tendsto_natCast_atTop_atTop
have hstirling :
Tendsto (fun n : ℕ => Real.log (Stirling.stirlingSeq n) / (n : ℝ))
atTop (𝓝 0) :=
(Stirling.tendsto_stirlingSeq_sqrt_pi.log (by positivity)).div_atTop hnat
have hlog : Tendsto (fun n : ℕ => Real.log (n : ℝ) / (n : ℝ))
atTop (𝓝 0) := by
simpa only [Function.comp_def, id_eq] using
Real.isLittleO_log_id_atTop.tendsto_div_nhds_zero.comp hnat
have hconstant : Tendsto (fun n : ℕ => Real.log 2 / (n : ℝ))
atTop (𝓝 0) := tendsto_const_nhds.div_atTop hnat
have hsum : Tendsto
(fun n : ℕ => Real.log (Stirling.stirlingSeq n) / (n : ℝ) +
(1 / 2 : ℝ) * (Real.log 2 / (n : ℝ) + Real.log (n : ℝ) / (n : ℝ)))
atTop (𝓝 0) := by
simpa using hstirling.add ((hconstant.add hlog).const_mul (1 / 2 : ℝ))
apply hsum.congr'
filter_upwards [eventually_ne_atTop (0 : ℕ)] with n hn
rw [factorialLogRemainder_stirling,
Real.log_mul (by norm_num) (Nat.cast_ne_zero.mpr hn)]
ring
theorem tendsto_factorialLogRemainder_mul_div (c : ℕ) :
Tendsto (fun t : ℕ => factorialLogRemainder (t * c) / (t : ℝ))
atTop (𝓝 0) := by
by_cases hc : c = 0
· subst c
simp
have hmul : Tendsto (fun t : ℕ => t * c) atTop atTop :=
tendsto_id.atTop_mul_const' (Nat.pos_of_ne_zero hc)
have hscaled : Tendsto
(fun t : ℕ => factorialLogRemainder (t * c) / (t * c : ℕ) * (c : ℝ))
atTop (𝓝 0) := by
simpa using (tendsto_factorialLogRemainder_div.comp hmul).mul_const (c : ℝ)
apply hscaled.congr
intro t
by_cases ht : t = 0
· simp [ht]
rw [Nat.cast_mul]
field_simp [Nat.cast_ne_zero.mpr hc, Nat.cast_ne_zero.mpr ht]
theorem factorialLogRemainder_succ (n : ℕ) :
factorialLogRemainder (n + 1) = factorialLogRemainder n + 1 -
(n : ℝ) * (Real.log ((n : ℝ) + 1) - Real.log n) := by
unfold factorialLogRemainder
rw [Nat.factorial_succ, Nat.cast_mul,
Real.log_mul (by positivity) (by positivity)]
push_cast
ring
private theorem nat_mul_log_increment_le_one (n : ℕ) :
(n : ℝ) * (Real.log ((n : ℝ) + 1) - Real.log n) ≤ 1 := by
by_cases hn : n = 0
· simp [hn]
have hpos : 0 < (n : ℝ) := Nat.cast_pos.mpr (Nat.pos_of_ne_zero hn)
have hsucc : 0 < (n : ℝ) + 1 := by positivity
have hlog := Real.log_le_sub_one_of_pos (div_pos hsucc hpos)
rw [Real.log_div hsucc.ne' hpos.ne'] at hlog
have hmul := mul_le_mul_of_nonneg_left hlog hpos.le
have heq : (n : ℝ) * (((n : ℝ) + 1) / (n : ℝ) - 1) = 1 := by
field_simp [hpos.ne']
ring
rwa [heq] at hmul
private theorem one_le_succ_mul_log_increment (n : ℕ) (hn : 0 < n) :
1 ≤ ((n : ℝ) + 1) * (Real.log ((n : ℝ) + 1) - Real.log n) := by
have hpos : 0 < (n : ℝ) := Nat.cast_pos.mpr hn
have hsucc : 0 < (n : ℝ) + 1 := by positivity
have hlog := Real.one_sub_inv_le_log_of_pos (div_pos hsucc hpos)
rw [Real.log_div hsucc.ne' hpos.ne'] at hlog
have hmul := mul_le_mul_of_nonneg_left hlog hsucc.le
have heq : ((n : ℝ) + 1) * (1 - (((n : ℝ) + 1) / (n : ℝ))⁻¹) = 1 := by
rw [inv_div]
field_simp [hsucc.ne']
ring
rwa [heq] at hmul
theorem factorialLogRemainder_nonneg (n : ℕ) : 0 ≤ factorialLogRemainder n := by
induction n with
| zero => simp
| succ n ih =>
have hlog := nat_mul_log_increment_le_one n
rw [factorialLogRemainder_succ]
linarith
theorem factorialLogRemainder_le_one_add_log_pos (n : ℕ) :
factorialLogRemainder (n + 1) ≤ 1 + Real.log (n + 1 : ℕ) := by
induction n with
| zero => norm_num [factorialLogRemainder]
| succ n ih =>
have hlog := one_le_succ_mul_log_increment (n + 1) (Nat.succ_pos n)
have hstep := factorialLogRemainder_succ (n + 1)
push_cast at ih hlog hstep ⊢
nlinarith
theorem factorialLogRemainder_le_one_add_log (n : ℕ) :
factorialLogRemainder n ≤ 1 + Real.log (n + 1 : ℕ) := by
cases n with
| zero => norm_num
| succ n =>
refine (factorialLogRemainder_le_one_add_log_pos n).trans ?_
apply add_le_add_right
apply Real.log_le_log
· positivity
· exact_mod_cast Nat.le_succ (n + 1)
theorem abs_factorialLogRemainder_le (n : ℕ) :
|factorialLogRemainder n| ≤ 1 + Real.log (n + 1 : ℕ) := by
rw [abs_of_nonneg (factorialLogRemainder_nonneg n)]
exact factorialLogRemainder_le_one_add_log n
end MatrixMultiplication.Foundation
end OAI

Xet Storage Details

Size:
5.69 kB
·
Xet hash:
ea45dcaafef15d203d8942149c750ae355ed867272f4bf6abff72bb0049aaf50

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.