Download data/lean/OAI/Computability/FourierCircuit/Main.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 15.1 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/FourierCircuit/Main.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/FourierCircuit/Main.lean
-
curl -L -o Main.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/FourierCircuit/Main.lean
15.1 kB
| import OAI.Computability.FourierCircuit.ToeplitzCross | |
| namespace OAI | |
| section | |
| noncomputable section | |
| namespace ExactFourier.PrimeInventory | |
| open Nat Real | |
| theorem count_fourth (M : β) (hM : 10β€M) : Mβ€Nat.primeCounting (M^4) := by | |
| have hm : (10:β)β€M := by exact_mod_cast hM | |
| have hm0 : (0:β)<M := by linarith | |
| have hs : (100:β)β€(M : β)^2 := by nlinarith | |
| have hn : (1:β)<(M : β)^4 := by nlinarith [sq_nonneg ((M : β)^2-1)] | |
| have hlpos : 0<Real.log ((M : β)^4) := Real.log_pos hn | |
| have hl : Real.log ((M : β)^4)=4*Real.log M := by rw [Real.log_pow]; norm_num | |
| have hlog : Real.log (M : β)β€M := (Real.log_le_sub_one_of_pos hm0).trans (by linarith) | |
| have hlog2 : (1/2:β)β€Real.log 2 := by linarith [Real.log_two_gt_d9] | |
| have hlog21 : Real.log 2β€1 := by linarith [Real.log_two_lt_d9] | |
| have hb : Real.log ((M : β)^4+1)β€1+4*M := by | |
| have h1 := Real.log_le_log (by positivity : (0:β)<(M : β)^4+1) | |
| (show (M : β)^4+1β€2*(M : β)^4 by linarith) | |
| rw [Real.log_mul (by norm_num) (by positivity),hl] at h1 | |
| linarith | |
| have hs4 : 100*(M : β)^2β€(M : β)^4 := by nlinarith only [mul_nonneg (sub_nonneg.mpr hs) (sq_nonneg (M : β))] | |
| have hn1 : (M : β)*Real.log ((M : β)^4)β€(M : β)^4*Real.log 2-Real.log ((M : β)^4+1) := by | |
| rw [hl] | |
| have h1 := mul_le_mul_of_nonneg_left hlog (show (0:β)β€4*M by positivity) | |
| have h2 := mul_le_mul_of_nonneg_left hlog2 (show (0:β)β€(M : β)^4 by positivity) | |
| nlinarith only [h1,h2,hb,hs4,hs,hm] | |
| have hh := Chebyshev.pi_ge (M^4) | |
| have hlower : (M : β)β€((M : β)^4*Real.log 2-Real.log ((M : β)^4+1))/Real.log ((M : β)^4) := | |
| (le_div_iffβ hlpos).mpr hn1 | |
| have hh' : (M : β)β€Nat.primeCounting (M^4) := by | |
| apply hlower.trans | |
| simpa only [Nat.cast_pow,Nat.cast_add,Nat.cast_one] using hh | |
| exact_mod_cast hh' | |
| def upper (q m : β) : β := (m+2*q+10)^4 | |
| theorem family (q m : β) : β r : Fin mββ, | |
| (β i,Nat.Prime (r i)) β§ Function.Injective r β§ | |
| (β i,2*qβ€r i) β§ (β i,r iβ€upper q m) := by | |
| let V := Nat.primesLE (upper q m) | |
| let S := V.filter (fun p=>2*qβ€p) | |
| let T := V.filter (fun p=>Β¬2*qβ€p) | |
| have hcard := Finset.card_filter_add_card_filter_not (s := V) (fun p=>2*qβ€p) | |
| have ht : T.cardβ€2*q := by | |
| have hsub : TβFinset.range (2*q) := by | |
| intro p hp | |
| exact Finset.mem_range.mpr (lt_of_not_ge (Finset.mem_filter.mp hp).2) | |
| simpa only [Finset.card_range] using Finset.card_le_card hsub | |
| have hv : m+2*qβ€V.card := by | |
| rw [show V=Nat.primesLE (upper q m) from rfl,Nat.primesLE_card_eq_primeCounting] | |
| have hh : m + 2*q β€ m + 2*q + 10 := by omega | |
| exact hh.trans (count_fourth _ (by omega)) | |
| have hs : mβ€S.card := by change S.card+T.card=V.card at hcard; omega | |
| obtain β¨eβ© := Function.Embedding.nonempty_of_card_le (Ξ± := Fin m) (Ξ² := S) (by simpa using hs) | |
| refine β¨fun i=>(e i).val,fun i=>?_,fun i j h=>e.injective (Subtype.ext h),fun i=>?_,fun i=>?_β© | |
| Β· exact Nat.prime_of_mem_primesLE (Finset.mem_filter.mp (e i).property).1 | |
| Β· exact (Finset.mem_filter.mp (e i).property).2 | |
| Β· exact Nat.le_of_mem_primesLE (Finset.mem_filter.mp (e i).property).1 | |
| theorem family_coprime {m : β} {r : Fin mββ} (hr : β i,Nat.Prime (r i)) | |
| (hi : Function.Injective r) : Pairwise (fun i j=>Nat.Coprime (r i) (r j)) := by | |
| intro i j hij | |
| exact (Nat.coprime_primes (hr i) (hr j)).mpr (fun h=>hij (hi h)) | |
| end ExactFourier.PrimeInventory | |
| end | |
| end | |
| section | |
| noncomputable section | |
| namespace ExactFourier.FourierTransfer | |
| open CircuitCost | |
| def depth (q m : β) : β := 8388608*(Nat.log 2 (PrimeInventory.upper q m)+2)^2+5 | |
| theorem depth_bound (q m : β) : | |
| (depth q m : β)β€536870917*(Real.log ((m:β)+2*q+10)+1)^2 := by | |
| let x : β := (m:β)+2*q+10 | |
| have hx : 1β€x := by dsimp [x]; linarith [Nat.cast_nonneg (Ξ± := β) m,Nat.cast_nonneg (Ξ± := β) q] | |
| have hl : 0β€Real.log x := Real.log_nonneg hx | |
| have hu : (PrimeInventory.upper q m : β)=x^4 := by simp [PrimeInventory.upper,x] | |
| have hlog := Real.natLog_le_logb (PrimeInventory.upper q m) 2 | |
| rw [hu,Real.logb,Real.log_pow] at hlog | |
| norm_num only [Nat.cast_ofNat] at hlog | |
| have h2 : (1/2:β)β€Real.log 2 := by linarith [Real.log_two_gt_d9] | |
| have h2p : 0<Real.log 2 := Real.log_pos (by norm_num) | |
| have hh : (Nat.log 2 (PrimeInventory.upper q m) : β)β€8*Real.log x := by | |
| apply hlog.trans | |
| apply (div_le_iffβ h2p).mpr | |
| nlinarith [mul_nonneg hl (sub_nonneg.mpr h2)] | |
| have hb : (Nat.log 2 (PrimeInventory.upper q m) : β)+2β€8*(Real.log x+1) := by linarith | |
| have hb0 : 0β€(Nat.log 2 (PrimeInventory.upper q m) : β)+2 := by positivity | |
| have hsq := sq_le_sqβ hb0 (by positivity : 0β€8*(Real.log x+1)) |>.mpr hb | |
| change (depth q m : β)β€536870917*(Real.log x+1)^2 | |
| simp only [depth,Nat.cast_add,Nat.cast_mul,Nat.cast_pow,Nat.cast_ofNat] | |
| nlinarith [sq_nonneg (Real.log x)] | |
| theorem family_cost {q h : β} (A : Matrix (Fin q) (Fin q) β) (hq : 2β€q) | |
| (gen : β M : Matrix (Fin q) (Fin q) β,IsUnit M β PositiveGeneration.Pattern A h M) | |
| {a C : β} (ha : 0β€a) (hC : 0β€C) | |
| (amp : β k,(cost (TensorAxis.power A k) : β)β€C*(q : β)^k*(k+1 : β)^a) | |
| (m : β) : β n : β,2^mβ€n β§ | |
| (cost (fourierMatrix n) : β)β€536870917*(q*(h+1)+1 : β)*(C+1)*n* | |
| (Real.log ((m:β)+2*q+10)+1)^2*((m:β)+2*q+10)^a := by | |
| obtain β¨r,hr,hi,hlo,hupβ© := PrimeInventory.family q m | |
| obtain β¨Ο,hΟ,e,heβ© := FourierCRT.factorization r (fun i=>(hr i).pos) | |
| (PrimeInventory.family_coprime hr hi) | |
| let n := β i,r i | |
| have hn : 2^mβ€n := by | |
| have hp := Finset.prod_le_prod (fun i (_ : iβ(Finset.univ : Finset (Fin m)))=>(hr i).two_le) | |
| simpa [n] using hp | |
| have hlayer : β i,Layered (RadixTwo.dft (r i) (Ο i)) (depth q m) := by | |
| intro i | |
| apply (NewtonFourier.dft_layers (hr i).pos (hΟ i)).weaken | |
| dsimp [depth] | |
| have hh := Nat.log_mono_right (b := 2) (hup i) | |
| gcongr | |
| have hc := ParallelTensorCost.layered_cost ha hC amp hq gen m (depth q m) | |
| (fun i=>Fin (r i)) (fun i=>RadixTwo.dft (r i) (Ο i)) hlayer | |
| (fun i=>by simpa using hlo i) | |
| rw [β he,CircuitCost.cost_reindex] at hc | |
| simp only [Fintype.card_pi,Fintype.card_fin] at hc | |
| let x : β := (m:β)+2*q+10 | |
| have hx : 1β€x := by dsimp [x]; linarith [Nat.cast_nonneg (Ξ± := β) m,Nat.cast_nonneg (Ξ± := β) q] | |
| have hxm : (m:β)+1β€x := by dsimp [x]; linarith [Nat.cast_nonneg (Ξ± := β) q] | |
| have hp := Real.rpow_le_rpow (by positivity : (0:β)β€(m:β)+1) hxm ha | |
| have hp1 : 1β€x^a := Real.one_le_rpow hx ha | |
| have hbudget : C*((m:β)+1)^a+1β€(C+1)*x^a := by nlinarith | |
| have hd := depth_bound q m | |
| have hb0 : 0β€C*((m:β)+1)^a+1 := by positivity | |
| refine β¨n,hn,hc.trans ?_β© | |
| change ((depth q m*(q*(h+1)+1) : β):β)*(C*(n:β)*((m:β)+1)^a+(n:β))β€_ | |
| calc | |
| _ = (depth q m : β)*(q*(h+1)+1 : β)*n*(C*((m:β)+1)^a+1) := by push_cast; ring | |
| _ β€ (536870917*(Real.log x+1)^2)*(q*(h+1)+1 : β)*n*((C+1)*x^a) := by | |
| exact mul_le_mul (mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_right hd (Nat.cast_nonneg _)) (Nat.cast_nonneg n)) hbudget hb0 (mul_nonneg (mul_nonneg (mul_nonneg (by norm_num) (sq_nonneg _)) (Nat.cast_nonneg _)) (Nat.cast_nonneg n)) | |
| _ = _ := by dsimp [x]; ring | |
| end ExactFourier.FourierTransfer | |
| end | |
| end | |
| section | |
| noncomputable section | |
| namespace ExactFourier.FourierTransfer | |
| open Filter Topology Asymptotics | |
| theorem log_square_littleO {a : β} (ha : a<1) : | |
| (fun x : β=>(Real.log x+1)^2)=o[atTop] (fun x : β=>x^(1-a)) := by | |
| have hs : 0<1-a := by linarith | |
| have h2 := isLittleO_log_rpow_rpow_atTop (2 : β) hs | |
| have h2' : (fun x : β=>(Real.log x)^2)=o[atTop] (fun x : β=>x^(1-a)) := by | |
| simpa only [Real.rpow_two] using h2 | |
| have h1 := (isLittleO_log_rpow_atTop hs).const_mul_left (2 : β) | |
| have h0 : (fun _ : β=>(1:β))=o[atTop] (fun x : β=>x^(1-a)) := by | |
| apply (isLittleO_const_left_of_ne one_ne_zero).mpr | |
| simpa only [Real.norm_eq_abs,Function.comp_def] using tendsto_abs_atTop_atTop.comp (tendsto_rpow_atTop hs) | |
| exact ((h2'.add h1).add h0).congr_left (fun x=>by ring) | |
| theorem log_power_ratio {a : β} (ha : a<1) : | |
| Tendsto (fun x : β=>(Real.log x+1)^2*x^a/x) atTop (π 0) := by | |
| have ht := (log_square_littleO ha).tendsto_div_nhds_zero | |
| apply ht.congr' | |
| filter_upwards [eventually_gt_atTop (0:β)] with x hx | |
| rw [Real.rpow_sub hx,Real.rpow_one] | |
| field_simp | |
| theorem shifted_log_power_ratio {a c : β} (ha : a<1) : | |
| Tendsto (fun m : β=>(Real.log ((m:β)+c)+1)^2*((m:β)+c)^a/(m:β)) atTop (π 0) := by | |
| have ht : Tendsto (fun m : β=>(m:β)+c) atTop atTop := | |
| (tendsto_atTop_add_const_right atTop c tendsto_id).comp tendsto_natCast_atTop_atTop | |
| have h1 := (log_power_ratio ha).comp ht | |
| have h2 : Tendsto (fun m : β=>((m:β)+c)/(m:β)) atTop (π 1) := by | |
| have h := (tendsto_const_nhds (x := (1:β))).add ((tendsto_const_nhds (x := c)).mul (tendsto_inv_atTop_nhds_zero_nat (π := β))) | |
| apply (show Tendsto (fun m : β=>1+c*(m:β)β»ΒΉ) atTop (π 1) by simpa using h).congr' | |
| filter_upwards [eventually_gt_atTop (0:β)] with m hm | |
| have hm' : (m:β)β 0 := by exact_mod_cast (ne_of_gt hm) | |
| field_simp | |
| have h := h1.mul h2 | |
| apply (show Tendsto (fun m : β=>((Real.log ((m:β)+c)+1)^2*((m:β)+c)^a/((m:β)+c))*(((m:β)+c)/(m:β))) atTop (π 0) by simpa using h).congr' | |
| filter_upwards [ht.eventually (eventually_gt_atTop (0:β))] with m hm | |
| field_simp | |
| end ExactFourier.FourierTransfer | |
| end | |
| end | |
| section | |
| noncomputable section | |
| namespace ExactFourier.CircuitCost | |
| theorem exists_circuit {n : β} (M : Matrix (Fin n) (Fin n) β) : | |
| β C : Circuit n,C.size=cost M β§ C.Computes M := by | |
| obtain β¨T,hT,htβ© := optimal M | |
| let e := Fintype.equivFin (Fin n) | |
| let D : LinearDAG n n := (T.circuit.withInputs e.symm).withOutputs e | |
| let C : Circuit n := β¨D.size,D.program,D.outputsβ© | |
| refine β¨C,?_,?_β© | |
| Β· change D.size=cost M | |
| change (T.circuit.withInputs e.symm).size=cost M | |
| rw [LinearDAG.size_withInputs] | |
| exact hT | |
| Β· intro x | |
| change D.eval x=M.mulVec x | |
| rw [LinearDAG.eval_withOutputs,LinearDAG.eval_withInputs] | |
| exact ht x | |
| end ExactFourier.CircuitCost | |
| end | |
| end | |
| section | |
| noncomputable section | |
| namespace ExactFourier | |
| open Filter Topology | |
| open CircuitCost | |
| /-- An actual finite win transfers to arbitrarily large exact Fourier DAGs, | |
| with every scalar multiplication charged. -/ | |
| theorem win_to_fourier (win : FiniteWinStatement) : MainStatement := by | |
| obtain β¨q,A,hA,hn,b,hb,W,hW,hwβ© := win | |
| have hn' : Β¬MonomialMatrix A := hn | |
| obtain β¨h,hh,genβ© := PositiveGeneration.positive_generation A hA hn' | |
| obtain β¨a,C,ha,ha1,hC,ampβ© := Amplification.tensor_amplification A hb W hW hw | |
| have hq : 2β€q := by | |
| obtain β¨k,i,j,hij,_β© := PositiveGeneration.exists_nondiagonal_projector A hA hn' | |
| have hne : i.valβ j.val := fun e=>hij (Fin.ext e) | |
| have hi := i.isLt | |
| have hj := j.isLt | |
| omega | |
| intro c hc Nβ hNβ | |
| let K : β := 536870917*(q*(h+1)+1 : β)*(C+1) | |
| have hK : 0<K := by dsimp [K]; positivity | |
| have ht := (FourierTransfer.shifted_log_power_ratio (c := (2:β)*q+10) ha1).const_mul K | |
| have hlim : Tendsto (fun m : β=>K*(Real.log ((m:β)+2*q+10)+1)^2*((m:β)+2*q+10)^a/(m:β)) atTop (π 0) := by | |
| convert ht using 1 <;> try simp only [mul_zero] | |
| funext m | |
| simp only [add_assoc] | |
| ring | |
| have hsmall : βαΆ m : β in atTop,K*(Real.log ((m:β)+2*q+10)+1)^2*((m:β)+2*q+10)^a/(m:β)<c := | |
| hlim.eventually (eventually_lt_nhds hc) | |
| obtain β¨m,hm,hmsmallβ© := ((eventually_ge_atTop Nβ).and hsmall).exists | |
| have hmpos : 0 < m := by omega | |
| have hmR : (0:β)< m := by exact_mod_cast hmpos | |
| obtain β¨n,hnpow,hncostβ© := FourierTransfer.family_cost A hq gen ha.le hC.le amp m | |
| have hmn : mβ€n := (Nat.le_of_lt (Nat.lt_two_pow_self (n := m))).trans hnpow | |
| have hnpos : 0 < n := hmpos.trans_le hmn | |
| have hnR : (0:β)< n := by exact_mod_cast hnpos | |
| have hlog : (m:β)β€Real.logb 2 (n:β) := by | |
| have hp : (2:β)^mβ€(n:β) := by exact_mod_cast hnpow | |
| have hl := Real.logb_le_logb_of_le (by norm_num : (1:β)<2) (by positivity : (0:β)<(2:β)^m) hp | |
| simpa only [Real.logb_pow,Real.logb_self_eq_one (by norm_num : (1:β)<2),mul_one] using hl | |
| have hstrict : K*(Real.log ((m:β)+2*q+10)+1)^2*((m:β)+2*q+10)^a<c*(m:β) := | |
| (div_lt_iffβ hmR).mp hmsmall | |
| have hcost : (cost (fourierMatrix n) : β)<c*(n:β)*Real.logb 2 (n:β) := by | |
| calc | |
| _ β€ (n:β)*(K*(Real.log ((m:β)+2*q+10)+1)^2*((m:β)+2*q+10)^a) := by | |
| convert hncost using 1 ; dsimp [K] ; ring | |
| _ < (n:β)*(c*(m:β)) := mul_lt_mul_of_pos_left hstrict hnR | |
| _ β€ c*(n:β)*Real.logb 2 (n:β) := by | |
| have hh := mul_le_mul_of_nonneg_left hlog (mul_nonneg hc.le hnR.le) | |
| nlinarith only [hh] | |
| obtain β¨D,hD,hdβ© := CircuitCost.exists_circuit (fourierMatrix n) | |
| exact β¨n,hm.trans hmn,D,hd,by simpa only [hD] using hcostβ© | |
| end ExactFourier | |
| end | |
| end | |
| section | |
| namespace ExactFourier | |
| theorem main_theorem : MainStatement := win_to_fourier finite_win | |
| theorem finite_win_exists : FiniteWinStatement := finite_win | |
| end ExactFourier | |
| end | |
| section | |
| noncomputable section | |
| namespace ExactFourier | |
| open Filter Topology | |
| /-- Existence of an exact circuit in every width, used only to define the minimum. -/ | |
| theorem exists_fourier_circuit (n : β) : β k : β,β C : Circuit n, | |
| C.size=k β§ C.Computes (fourierMatrix n) := by | |
| obtain β¨C,hC,hcβ© := CircuitCost.exists_circuit (fourierMatrix n) | |
| exact β¨C.size,C,rfl,hcβ© | |
| def L (n : β) : β := by | |
| classical | |
| exact Nat.find (exists_fourier_circuit n) | |
| theorem L_attained (n : β) : β C : Circuit n,C.size=L n β§ C.Computes (fourierMatrix n) := by | |
| classical | |
| exact Nat.find_spec (exists_fourier_circuit n) | |
| theorem L_le {n : β} (C : Circuit n) (hC : C.Computes (fourierMatrix n)) : L nβ€C.size := by | |
| classical | |
| exact Nat.find_min' (exists_fourier_circuit n) β¨C,rfl,hCβ© | |
| theorem fourier_denominator_pos {n : β} (hn : 2β€n) : | |
| 0 < (n:β)*Real.logb 2 (n:β) := by | |
| have hn' : (1:β)< n := by exact_mod_cast (show 1< n from by omega) | |
| exact mul_pos (lt_trans zero_lt_one hn') (Real.logb_pos (by norm_num) hn') | |
| /-- The liminf formulation of the same main theorem, for the literal minimum L. -/ | |
| theorem main_liminf : | |
| Filter.liminf (fun n : β=>(L n : β)/((n:β)*Real.logb 2 (n:β))) atTop=0 := by | |
| let f : βββ := fun n=>(L n : β)/((n:β)*Real.logb 2 (n:β)) | |
| have hzero : βαΆ n : β in atTop,0β€f n := by | |
| filter_upwards [eventually_ge_atTop 2] with n hn | |
| exact div_nonneg (Nat.cast_nonneg _) (fourier_denominator_pos hn).le | |
| have hupper : β b : β,(βαΆ n : β in atTop,bβ€f n) β bβ€0 := by | |
| intro b hb | |
| by_contra h | |
| have hbpos : 0 < b := lt_of_not_ge h | |
| obtain β¨N,hNβ© := eventually_atTop.mp hb | |
| obtain β¨n,hn,C,hC,hcβ© := main_theorem b hbpos (max N 2) (le_max_right _ _) | |
| have hn2 : 2β€n := (le_max_right _ _).trans hn | |
| have hLn : (L n : β)β€C.size := by exact_mod_cast L_le C hC | |
| have hlt : f n < b := by | |
| apply (div_lt_iffβ (fourier_denominator_pos hn2)).mpr | |
| linarith only [hLn,hc] | |
| exact (not_lt_of_ge (hN n ((le_max_left _ _).trans hn))) hlt | |
| change liminf f atTop=0 | |
| rw [Filter.liminf_eq] | |
| exact le_antisymm (csSup_le β¨0,hzeroβ© hupper) (le_csSup β¨0,hupperβ© hzero) | |
| end ExactFourier | |
| end | |
| end | |
| end OAI | |