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:ℝ)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.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 : 0K*(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:ℝ)(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