import Mathlib import OAI.Computability.DeterministicSum.ModularInverses namespace OAI /-! Prime-power coefficient tables and their paid Newton transforms. -/ namespace DeterministicThreeSum.Newton open Finset open scoped BigOperators noncomputable section variable {R : Type*} [CommRing R] {q : ℕ} lemma entry_map (node : Fin q → ℤ) : (Int.castRingHom R).mapMatrix (entry node)=entry (fun i => (node i:R)) := by ext i j simp [entry] lemma cast_natAbs_unit {a : ℤ} (h : IsUnit (a:R)) : IsUnit ((a.natAbs:ℕ):R) := by have he : ((a.natAbs:ℕ):R)=((|a|:ℤ):R) := by rw [show ((a.natAbs:ℕ):R)=((a.natAbs:ℤ):R) by simp,Int.natCast_natAbs] rw [he] rcases abs_choice a with hpos|hneg · rwa [hpos] · simpa only [hneg,Int.cast_neg] using h.neg def inverseDenominator (order : Equiv.Perm (Fin q)) : ℕ := (entry (fun i => ((order i).val:ℤ))).det.natAbs def inverseNumerator (order : Equiv.Perm (Fin q)) (i j : Fin q) : ℤ := (entry (fun k => ((order k).val:ℤ))).det.sign * (entry (fun k => ((order k).val:ℤ))).adjugate i j lemma inverseDenominator_unit {p e : ℕ} (hp : p.Prime) (he : 0 < e) (hq : q < p) (order : Equiv.Perm (Fin q)) : IsUnit (inverseDenominator order:ZMod (p^e)) := by apply cast_natAbs_unit have h:=entry_det_unit (fun i => ((order i).val:ZMod (p^e))) (primePower_node_units hp he hq order) have hmap : (Int.castRingHom (ZMod (p^e))).mapMatrix (entry (fun i => ((order i).val:ℤ)))= entry (fun i => ((order i).val:ZMod (p^e))) := by simpa only [Int.cast_natCast] using entry_map (R:=ZMod (p^e)) (fun i => ((order i).val:ℤ)) have hd:=RingHom.map_det (Int.castRingHom (ZMod (p^e))) (entry (fun i => ((order i).val:ℤ))) rw [hmap] at hd change IsUnit ((Int.castRingHom (ZMod (p^e))) (entry (fun i => ((order i).val:ℤ))).det) rwa [hd] lemma inverseDenominator_prime {p : ℕ} (hp : p.Prime) (hq : q < p) (order : Equiv.Perm (Fin q)) : ¬p∣inverseDenominator order := by have h:=inverseDenominator_unit (e:=1) hp (by omega) hq order exact (ZMod.isUnit_natCast_iff_not_dvd_pow hp (by omega : 0<1)).mp h lemma inverseInteger_eq (M : Matrix (Fin q) (Fin q) ℤ) (s : R) (hs : s*(M.det.natAbs:R)=1) : ((Int.castRingHom R).mapMatrix M)⁻¹= (s*(M.det.sign:R)) • (Int.castRingHom R).mapMatrix M.adjugate := by apply Matrix.inv_eq_left_inv rw [Matrix.smul_mul,RingHom.map_adjugate,Matrix.adjugate_mul,← RingHom.map_det,smul_smul] have hz : (M.det.sign:R)*(M.det:R)=(M.det.natAbs:R) := by rw [← Int.cast_mul,Int.sign_mul_self,Int.cast_natCast] change ((s*(M.det.sign:R))*(M.det:R)) • (1:Matrix (Fin q) (Fin q) R)=1 rw [mul_assoc,hz,hs,one_smul] lemma inverse_entry_integer (order : Equiv.Perm (Fin q)) (s : R) (hs : s*(inverseDenominator order:R)=1) (i j : Fin q) : ((entry (fun k => ((order k).val:R)))⁻¹) i j=s*(inverseNumerator order i j:R) := by have hm:=inverseInteger_eq (entry (fun k => ((order k).val:ℤ))) s hs rw [entry_map] at hm have hv:=congrArg (fun A : Matrix (Fin q) (Fin q) R => A i j) hm simpa [inverseNumerator,Int.cast_mul,mul_assoc] using hv end end DeterministicThreeSum.Newton namespace DeterministicThreeSum.Structured.Indexed.NewtonTable open Command DeterministicThreeSum.Newton noncomputable section def numeratorAt {q : ℕ} (order : Equiv.Perm (Fin q)) (z : ℕ) : ℤ := if h : z numeratorAt order z.val) def command {q : ℕ} (p : ℕ) (order : Equiv.Perm (Fin q)) : Command := commonTable p (inverseDenominator order) (numerators order) lemma numeratorAt_entry {q : ℕ} (order : Equiv.Perm (Fin q)) (i j : Fin q) : numeratorAt order (i.val*q+j.val)=inverseNumerator order i j := by have hq : 0 apply Fin.ext · change (i.val*q+j.val)/q=i.val rw [Nat.mul_comm i.val q,Nat.mul_add_div hq,Nat.div_eq_of_lt j.isLt,Nat.add_zero] · simp [Nat.add_mod,Nat.mod_eq_of_lt j.isLt] theorem command_correct {w p e q base : ℕ} (order : Equiv.Perm (Fin q)) (s : Data) (hp : p.Prime) (hq : q ((order k).val:ZMod (p^(e+1)))))⁻¹) i j) ∧ (∀ a, a (node k:R)) j := by simp [univariate,Polynomial.map_prod] lemma conversion_integer (node : Fin q → ℤ) (j v : Fin q) : ((conversion node j v:ℤ):R)=conversion (fun k => (node k:R)) j v := by change (Int.castRingHom R) ((univariate node j).coeff v.val)=_ rw [← Polynomial.coeff_map,univariate_integer] rfl end end DeterministicThreeSum.Newton namespace DeterministicThreeSum.Structured.Indexed.NewtonConversionTable open Command DeterministicThreeSum.Newton noncomputable section def integerAt {q : ℕ} (order : Equiv.Perm (Fin q)) (z : ℕ) : ℤ := if h : z ((order k).val:ℤ)) ⟨z%q,Nat.mod_lt _ (by nlinarith : 0 integerAt order z.val) def command {q : ℕ} (p : ℕ) (order : Equiv.Perm (Fin q)) : Command := commonTable p 1 (integers order) lemma integerAt_entry {q : ℕ} (order : Equiv.Perm (Fin q)) (i j : Fin q) : integerAt order (i.val*q+j.val)=conversion (fun k => ((order k).val:ℤ)) j i := by have hq : 0 apply Fin.ext · simp [Nat.add_mod,Nat.mod_eq_of_lt j.isLt] · change (i.val*q+j.val)/q=i.val rw [Nat.mul_comm i.val q,Nat.mul_add_div hq,Nat.div_eq_of_lt j.isLt,Nat.add_zero] theorem command_correct {w p e q base : ℕ} (order : Equiv.Perm (Fin q)) (s : Data) (hp : p.Prime) (hmul : (p^(e+1))*(p^(e+1)) ((order k).val:ZMod (p^(e+1)))) j i) ∧ (∀ a, a ((order k).val:ℤ)) j i := by simpa only [integers,List.getElem_ofFn] using integerAt_entry order i j rw [hnum] at hvalue have hi':=((conversion_integer (R:=ZMod (p^(e+1))) (fun k => ((order k).val:ℤ)) j i)) simpa only [Nat.cast_one,mul_one,hi',Int.cast_natCast] using hvalue end end DeterministicThreeSum.Structured.Indexed.NewtonConversionTable namespace DeterministicThreeSum.Structured.Indexed.NewtonPass open Command Axis DeterministicThreeSum.Newton noncomputable section lemma inverseTable_lt {T q : ℕ} (hT : 0 simp only [ZMod.val_lt, hT] lemma conversionTable_lt {T q : ℕ} (hT : 0 simp only [ZMod.val_lt,hT] lemma memory_inverse {T q base : ℕ} (hT : 0 .register (k+6) | 3 => .register (k+b) | _ => .literal 0 def tableValues (T B i : ℕ) : ℕ := match i with | 2 => T | 3 => B | _ => 0 lemma tableSetup_correct {w k T B b : ℕ} (s : Data) (hk : 16≤k) (hT : T simp only [tableSources] at hr <;> cases hr <;> omega · intro i hi interval_cases i <;> simp [tableSources,tableValues,operand_register,operand_literal, h6,hb,Nat.mod_eq_of_lt hT,Nat.mod_eq_of_lt hB] def initialSources (k i : ℕ) : Operand := match i with | 3 => .register (k+10) | 4 => .register (k+11) | 5 => .register (k+12) | _ => tensorSources k i lemma initialSetup_correct {w k T N P S C B A : ℕ} (s : Data) (hk : 16≤k) (hT : T simp only [initialSources,tensorSources] at hr <;> cases hr <;> omega · intro i hi interval_cases i <;> simp [initialSources,tensorSources,tensorValues,operand_register,operand_literal, h0,h1,h2,h12,h6,h10,h11,Nat.mod_eq_of_lt hT,Nat.mod_eq_of_lt hN,Nat.mod_eq_of_lt hP, Nat.mod_eq_of_lt hS,Nat.mod_eq_of_lt hC,Nat.mod_eq_of_lt hB,Nat.mod_eq_of_lt hA] def paidCommand {q : ℕ} (p k : ℕ) (order : Equiv.Perm (Fin q)) : Command := .seq (copyPrefix (tableSources k 12) 4) (.seq (NewtonTable.command p order) (.seq (copyPrefix (tableSources k 5) 4) (.seq (NewtonConversionTable.command p order) (.seq (copyPrefix (initialSources k) 8) (command q k))))) end end DeterministicThreeSum.Structured.Indexed.NewtonPass namespace DeterministicThreeSum.Structured.Indexed.NewtonPass open Command Axis DeterministicThreeSum.Newton noncomputable section theorem paidCommand_correct {w p e q L d A P S C E D k : ℕ} (order : Equiv.Perm (Fin q)) (s : Data) (x : ℕ → ℕ) (hk : 16≤k) (hkw : (tensorCommand q q).writes.sup id ((order j).val:ZMod (p^(e+1))))) d (filtered q d D (tensorValue (p^(e+1)) q q (inverseTable (p^(e+1)) q (fun j => ((order j).val:ZMod (p^(e+1))))) d x)) i)) ∧ (∀ a, (a

.trans hL have hAw : A ((order j).val:ZMod (p^(e+1))) have hu2c : ∀ i, i