import Mathlib import OAI.Computability.DeterministicSum.AxisMaps import OAI.Computability.DeterministicSum.BilinearForms namespace OAI /-! Digit encodings for tensor transforms. -/ namespace DeterministicThreeSum.Structured.Indexed.Axis open Command Finset DeterministicThreeSum.Rectangular open scoped BigOperators def digitCode (q : ℕ) : (d : ℕ) → DigitBox q d → ℕ | 0,_ => 0 | d+1,u => u.1.val*q^d+digitCode q d u.2 lemma digitCode_lt {q : ℕ} (_hq : 0 simp [digitCode] | succ d ih => have hu:=u.1.isLt have ht:=ih u.2 have hl:=Nat.mul_le_mul_right (q^d) (show u.1.val+1≤q by omega) simp only [digitCode,pow_succ'] nlinarith only [hl,ht] def digitCoefficient (T q r : ℕ) (c : ℕ → ℕ) : (d : ℕ) → DigitBox r d → DigitBox q d → ZMod T | 0,_,_ => 1 | d+1,v,u => (c (v.1.val*q+u.1.val):ZMod T)*digitCoefficient T q r c d v.2 u.2 theorem tensorValue_eq_tensor {T q r : ℕ} (hq : 0 simp [tensorValue,digitCode,digitCoefficient] | succ d ih => have hidx : a*r^(d+1)+digitCode r (d+1) v= (a*r+v.1.val)*r^d+digitCode r d v.2 := by simp only [digitCode,pow_succ']; ring rw [tensorValue,hidx,ih] simp only [digitCoefficient,digitCode,Fintype.sum_prod_type] calc (∑ u : DigitBox q d, digitCoefficient T q r c d v.2 u * (mapValue T q r (q^d) x c ((a*r+v.1.val)*q^d+digitCode q d u):ZMod T)) = ∑ u : DigitBox q d, ∑ h : Fin q, digitCoefficient T q r c d v.2 u * (c (v.1.val*q+h.val):ZMod T)*(x (boxIndex q (q^d) a h.val (digitCode q d u)):ZMod T) := by apply sum_congr rfl intro u _ rw [show (a*r+v.1.val)*q^d+digitCode q d u=boxIndex r (q^d) a v.1.val (digitCode q d u) from rfl, mapValue_box x c hr (pow_pos hq _) v.1.isLt (digitCode_lt hq d u),mul_sum] apply sum_congr rfl intro h _ ring _ = _ := by rw [sum_comm] apply sum_congr rfl intro h _ apply sum_congr rfl intro u _ have he : boxIndex q (q^d) a h.val (digitCode q d u)=a*q^(d+1)+(h.val*q^d+digitCode q d u) := by simp only [boxIndex,pow_succ']; ring rw [he] ring theorem tensorCommand_tensor_correct {w T q r L d A P S C : ℕ} (s : Data) (x c : ℕ → ℕ) (hq : 1