File size: 15,623 Bytes
4a56cd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | import OAI.Computability.FourierCircuit.RectStableAlgebra
namespace OAI
section
noncomputable section
namespace ExactFourier.Displacement
open scoped BigOperators
def kernel (v w : βββ) (i j : β) : β :=
β lβFinset.range (min i j+1),v (i-l)*w (j-l)
@[simp] theorem kernel_zero_left (v w : βββ) (j : β) : kernel v w 0 j=v 0*w j := by
simp [kernel]
@[simp] theorem kernel_zero_right (v w : βββ) (i : β) : kernel v w i 0=v i*w 0 := by
simp [kernel]
theorem kernel_succ (v w : βββ) (i j : β) :
kernel v w (i+1) (j+1)=v (i+1)*w (j+1)+kernel v w i j := by
rw [kernel,show min (i+1) (j+1)+1=(min i j+1)+1 by omega,Finset.sum_range_succ']
simp only [Nat.add_sub_add_right,Nat.sub_zero]
rw [kernel,add_comm]
def lower (v : βββ) (a e : β) : Matrix (Fin a) (Fin e) β :=
fun i j=>if j.val β€ i.val then v (i.val-j.val) else 0
theorem kernel_eq_mul (v w : βββ) (a e : β) :
(fun i : Fin a=>fun j : Fin e=>kernel v w i.val j.val)=lower v a e*(lower w e e).transpose := by
ext i j
rw [Matrix.mul_apply]
simp only [lower,Matrix.transpose_apply]
rw [Fin.sum_univ_eq_sum_range (fun l=>(if l β€ i.val then v (i.val-l) else 0)*(if l β€ j.val then w (j.val-l) else 0))]
have hs : min i.val j.val+1 β€ e := by have := j.isLt; omega
have he : (βlβFinset.range e, (if l β€ i.val then v (i.val-l) else 0)*(if l β€ j.val then w (j.val-l) else 0))=
βlβFinset.range (min i.val j.val+1),v (i.val-l)*w (j.val-l) := by
rw [βFinset.sum_subset (Finset.range_mono hs)]
Β· apply Finset.sum_congr rfl
intro l hl
have hl' := Finset.mem_range.mp hl
simp [show l β€ i.val by omega,show l β€ j.val by omega]
Β· intro l hl hn
have hn' : Β¬l < min i.val j.val+1 := fun hh=>hn (Finset.mem_range.mpr hh)
by_cases hi : l β€ i.val
Β· simp [hi,show Β¬l β€ j.val by omega]
Β· simp [hi]
simpa [lower,kernel,Matrix.transpose_apply] using he.symm
def delta : βββ := fun i=>if i=0 then 1 else 0
/-- The exact rank-three displacement reconstruction, with only the stated
interior outer-product recurrence; no analytic or generic-position premise. -/
theorem reconstruct (a e : β) (ha : 0 < a) (he : 0 < e) (M : βββββ) (v w : βββ)
(hrec : β i j, i+1 < a β j+1 < e β M (i+1) (j+1)=M i j+v (i+1)*w (j+1)) :
let row := fun j=>M 0 j-v 0*w j
let col := fun i=>if i=0 then 0 else M i 0-v i*w 0
β i j, i < a β j < e β M i j=kernel v w i j+kernel delta row i j+kernel col delta i j := by
obtain β¨rows, rflβ© := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt ha)
obtain β¨columns, rflβ© := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt he)
dsimp only
intro i
induction i with
| zero =>
intro j hi hj
simp [delta]
| succ i ih =>
intro j hi hj
cases j with
| zero => simp [delta]
| succ j =>
rw [hrec i j hi hj,kernel_succ,kernel_succ,kernel_succ,ih j (by omega) (by omega)]
simp only [delta,show i+1β 0 by omega,show j+1β 0 by omega,ite_false,zero_mul,mul_zero,zero_add]
ring
end ExactFourier.Displacement
end
end
section
noncomputable section
namespace ExactFourier.Displacement
open Polynomial RadixTwo
def extendVector {a : β} (v : Fin aββ) : βββ :=
fun i=>if hi : i < a then v β¨i,hiβ© else 0
theorem coeff_ofFn_extend (a : β) (v : Fin aββ) : (Polynomial.ofFn a v).coeff=extendVector v := by
funext i
by_cases hi : i < a
Β· simp [Polynomial.ofFn_coeff_eq_val_of_lt _ hi,extendVector,hi]
Β· simp [Polynomial.ofFn_coeff_eq_zero_of_ge _ (by omega : a β€ i),extendVector,hi]
theorem convolution_submatrix (a e k : β) (ha : 0 < a) (h : a+e β€ 2^k) (haL : a β€ 2^k) (heL : e β€ 2^k)
(v : Fin aββ) :
(convolutionMatrix k (Polynomial.ofFn a v)).submatrix (initialEmbedding haL) (initialEmbedding heL)=lower (extendVector v) a e := by
ext i j
dsimp only [Matrix.submatrix_apply,lower]
rw [convolution_entry]
Β· rw [coeff_ofFn_extend]
rfl
Β· have hdeg:=Polynomial.ofFn_natDegree_lt (by omega : 1 β€ a) v
have hj:=j.isLt
change (Polynomial.ofFn a v).natDegree+j.val < 2^k
omega
def fullKernel (k a e : β) (v : Fin aββ) (w : Fin eββ) : Matrix (Fin (2^k)) (Fin (2^k)) β :=
convolutionMatrix k (Polynomial.ofFn a v) *
Matrix.diagonal (fun i : Fin (2^k)=>if i.val < e then 1 else 0) *
(convolutionMatrix k (Polynomial.ofFn e w)).transpose
theorem fullKernel_sumLayered (k a e : β) (v : Fin aββ) (w : Fin eββ) :
SumLayered (fullKernel k a e v w) (8*k+3) 8 := by
have h := ((convolution_sumLayered k (Polynomial.ofFn a v)).mul
(SumLayered.diagonal (fun i : Fin (2^k)=>if i.val < e then 1 else 0))).mul
(convolution_sumLayered k (Polynomial.ofFn e w)).transpose
simpa only [fullKernel,show 4*k+1+1+(4*k+1)=8*k+3 by omega,show 2*2*2=8 by decide] using h
theorem kernel_submatrix (a e k : β) (ha : 0 < a) (he : 0 < e) (h : 2*(a+e) β€ 2^k)
(v : Fin aββ) (w : Fin eββ) :
(fullKernel k a e v w).submatrix (initialEmbedding (by omega : a β€ 2^k)) (initialEmbedding (by omega : e β€ 2^k))=
(fun i : Fin a=>fun j : Fin e=>kernel (extendVector v) (extendVector w) i.val j.val) := by
rw [kernel_eq_mul,fullKernel,compress_product (by omega) (by omega) (by omega : e β€ 2^k)]
rw [convolution_submatrix a e k ha (by omega) (by omega) (by omega)]
have heq : ((convolutionMatrix k (Polynomial.ofFn e w)).transpose).submatrix
(initialEmbedding (by omega : e β€ 2^k)) (initialEmbedding (by omega : e β€ 2^k))=
(lower (extendVector w) e e).transpose := by
rw [βMatrix.transpose_submatrix]
rw [convolution_submatrix e e k he (by omega) (by omega) (by omega)]
rw [heq]
theorem kernel_stable (a e k : β) (ha : 0 < a) (he : 0 < e) (h : 2*(a+e) β€ 2^k)
(v : Fin aββ) (w : Fin eββ) :
Layered (Matrix.fromBlocks (rectangularShear (fun i : Fin a=>fun j : Fin e=>kernel (extendVector v) (extendVector w) i.val j.val))
0 0 (1 : Matrix (Fin (2^k)) (Fin (2^k)) β)) (256*k+128) := by
have hL := (fullKernel_sumLayered k a e v w).submatrix_stable
(initialEmbedding (by omega : a β€ 2^k)) (initialEmbedding (by omega : e β€ 2^k))
rw [kernel_submatrix a e k ha he h v w] at hL
simpa only [show 8*(4*(8*k+3)+4)=256*k+128 by omega] using hL
end ExactFourier.Displacement
end
end
section
noncomputable section
namespace ExactFourier.Displacement
theorem kernel_congr (v v' w w' : βββ) (i j : β)
(hv : β l, l β€ i β v l=v' l) (hw : β l, l β€ j β w l=w' l) : kernel v w i j=kernel v' w' i j := by
apply Finset.sum_congr rfl
intro l hl
rw [hv _ (Nat.sub_le _ _),hw _ (Nat.sub_le _ _)]
theorem kernel_restrict (v w : βββ) (a e : β) (i : Fin a) (j : Fin e) :
kernel (extendVector (fun u : Fin a=>v u.val)) (extendVector (fun v : Fin e=>w v.val)) i.val j.val=
kernel v w i.val j.val := by
apply kernel_congr
Β· intro l hl; simp [extendVector,show l<a by have := i.isLt; omega]
Β· intro l hl; simp [extendVector,show l<e by have := j.isLt; omega]
theorem stable_of_displacement (a e k : β) (ha : 0<a) (he : 0<e) (h : 2*(a+e) β€ 2^k)
(M : βββββ) (v w : βββ)
(hrec : β i j,i+1<a β j+1<e β M (i+1) (j+1)=M i j+v (i+1)*w (j+1)) :
Layered (Matrix.fromBlocks (rectangularShear (fun i : Fin a=>fun j : Fin e=>M i.val j.val))
0 0 (1 : Matrix (Fin (2^k)) (Fin (2^k)) β)) (768*k+384) := by
let row := fun j=>M 0 j-v 0*w j
let col := fun i=>if i=0 then 0 else M i 0-v i*w 0
let V:=fullKernel k a e (fun i=>v i.val) (fun j=>w j.val)
let R:=fullKernel k a e (fun i=>delta i.val) (fun j=>row j.val)
let C:=fullKernel k a e (fun i=>col i.val) (fun j=>delta j.val)
have hL : SumLayered (V+R+C) (8*k+3) 24 :=
((fullKernel_sumLayered ..).add (fullKernel_sumLayered ..)).add (fullKernel_sumLayered ..)
have heq : (V+R+C).submatrix (initialEmbedding (by omega : a β€ 2^k)) (initialEmbedding (by omega : e β€ 2^k))=
(fun i : Fin a=>fun j : Fin e=>M i.val j.val) := by
ext i j
change V (initialEmbedding _ i) (initialEmbedding _ j) +
R (initialEmbedding _ i) (initialEmbedding _ j) +
C (initialEmbedding _ i) (initialEmbedding _ j) = _
have hV := congr_fun (congr_fun (kernel_submatrix a e k ha he h (fun i=>v i.val) (fun j=>w j.val)) i) j
have hR := congr_fun (congr_fun (kernel_submatrix a e k ha he h (fun i=>delta i.val) (fun j=>row j.val)) i) j
have hC := congr_fun (congr_fun (kernel_submatrix a e k ha he h (fun i=>col i.val) (fun j=>delta j.val)) i) j
change V _ _ = _ at hV
change R _ _ = _ at hR
change C _ _ = _ at hC
rw [hV,hR,hC,kernel_restrict,kernel_restrict,kernel_restrict]
exact (reconstruct a e ha he M v w hrec i.val j.val i.isLt j.isLt).symm
have hs := hL.submatrix_stable (initialEmbedding (by omega : a β€ 2^k)) (initialEmbedding (by omega : e β€ 2^k))
rw [heq] at hs
simpa only [show 24*(4*(8*k+3)+4)=768*k+384 by omega] using hs
end ExactFourier.Displacement
end
end
section
noncomputable section
namespace ExactFourier.Displacement
open Layered
theorem small_stable (a e s c k : β) (ha : aβ€c) (he : eβ€c)
(hfft : 4*cβ€2^k) (hspace : 2^k+2*cβ€a+e+s)
(M : βββββ) (v w : βββ)
(hrec : β i j,i+1<a β j+1<e β M (i+1) (j+1)=M i j+v (i+1)*w (j+1)) :
Layered (Matrix.fromBlocks (rectangularShear (fun i : Fin a=>fun j : Fin e=>M i.val j.val))
0 0 (1 : Matrix (Fin s) (Fin s) β)) (768*k+384) := by
by_cases ha0 : a=0
Β· subst a; exact stable_empty_rows _ _
by_cases he0 : e=0
Β· subst e; exact stable_empty_cols _ _
exact (stable_of_displacement a e k (by omega) (by omega) (by omega) M v w hrec).stable_scratch_mono (by omega)
theorem row_chunks (p a e s c k : β) (ha : aβ€p*c) (he : eβ€c)
(hfft : 4*cβ€2^k) (hspace : 2^k+2*cβ€a+e+s)
(M : βββββ) (v w : βββ)
(hrec : β i j,i+1<a β j+1<e β M (i+1) (j+1)=M i j+v (i+1)*w (j+1)) :
Layered (Matrix.fromBlocks (rectangularShear (fun i : Fin a=>fun j : Fin e=>M i.val j.val))
0 0 (1 : Matrix (Fin s) (Fin s) β)) (p*(768*k+384)) := by
induction p generalizing a s M v with
| zero =>
have hz : a=0 := by omega
subst a; exact stable_empty_rows _ _
| succ p ih =>
by_cases hac : aβ€c
Β· exact (small_stable a e s c k hac he hfft hspace M v w hrec).weaken (by
have := Nat.le_mul_of_pos_left (768*k+384) (Nat.succ_pos p)
omega)
obtain β¨b,rflβ© := Nat.exists_eq_add_of_le (Nat.le_of_lt (Nat.lt_of_not_ge hac))
have h1 := small_stable c e (b+s) c k (le_refl _) he hfft (by omega) M v w
(fun i j hi hj=>hrec i j (by omega) hj)
have h2 := ih b (c+s) (by nlinarith [ha]) (by omega)
(fun i j=>M (c+i) j) (fun i=>v (c+i)) (by
intro i j hi hj
simpa only [Nat.add_assoc] using hrec (c+i) j (by omega) hj)
have hh := stable_rows_nat M h1 h2
convert hh using 1; first | rfl | ext i j | ring
theorem all_chunks (q p a e s c k : β) (ha : aβ€p*c) (he : eβ€q*c)
(hfft : 4*cβ€2^k) (hspace : 2^k+2*cβ€a+e+s)
(M : βββββ) (v w : βββ)
(hrec : β i j,i+1<a β j+1<e β M (i+1) (j+1)=M i j+v (i+1)*w (j+1)) :
Layered (Matrix.fromBlocks (rectangularShear (fun i : Fin a=>fun j : Fin e=>M i.val j.val))
0 0 (1 : Matrix (Fin s) (Fin s) β)) (q*p*(768*k+384)) := by
induction q generalizing e s M w with
| zero =>
have hz : e=0 := by omega
subst e; exact stable_empty_cols _ _
| succ q ih =>
by_cases hec : eβ€c
Β· exact (row_chunks p a e s c k ha hec hfft hspace M v w hrec).weaken (by
have := Nat.le_mul_of_pos_left (p*(768*k+384)) (Nat.succ_pos q)
nlinarith)
obtain β¨b,rflβ© := Nat.exists_eq_add_of_le (Nat.le_of_lt (Nat.lt_of_not_ge hec))
have h1 := row_chunks p a c (b+s) c k ha (le_refl _) hfft (by omega) M v w
(fun i j hi hj=>hrec i j hi (by omega))
have h2 := ih b (c+s) (by nlinarith [he]) (by omega)
(fun i j=>M i (c+j)) (fun j=>w (c+j)) (by
intro i j hi hj
simpa only [Nat.add_assoc] using hrec i (c+j) hi (by omega))
have hh := stable_rows_nat (fun i j=>M j i) h1.stable_rect_transpose h2.stable_rect_transpose
have hh := hh.stable_rect_transpose
change Layered (Matrix.fromBlocks (rectangularShear (fun i : Fin a=>fun j : Fin (c+b)=>M i.val j.val))
0 0 (1 : Matrix (Fin s) (Fin s) β)) (p*(768*k+384)+q*p*(768*k+384)) at hh
have hn : p*(768*k+384)+q*p*(768*k+384)=(q+1)*p*(768*k+384) := by ring
rw [hn] at hh
exact hh
end ExactFourier.Displacement
end
end
section
noncomputable section
namespace ExactFourier.Layered
variable {Ξ± Ξ² : Type} [Fintype Ξ±] [Fintype Ξ²] [DecidableEq Ξ±] [DecidableEq Ξ²]
theorem rect_single (i : Ξ±) (j : Ξ²) (c : β) :
Layered (rectangularShear (Matrix.single i j c)) 1 := by
have hh : (Sum.inl i : Ξ±βΞ²)β Sum.inr j := by simp
have h := (Layered.pair (!![1,c;0,1]) (by
apply (Matrix.isUnit_iff_isUnit_det _).mpr
simp)).embed (Embedded.pair (Sum.inl i) (Sum.inr j) hh)
rw [Embedded.pair_matrix] at h
have he : (1 : Matrix (Ξ±βΞ²) (Ξ±βΞ²) β)+Matrix.single (Sum.inl i) (Sum.inr j) c+
Matrix.single (Sum.inr j) (Sum.inl i) 0=rectangularShear (Matrix.single i j c) := by
ext a b
cases a <;> cases b <;> simp [rectangularShear,Matrix.single,Matrix.one_apply]
rw [he] at h
exact h
theorem rect_finset_sum {Ο : Type} (s : Finset Ο) (G : ΟβMatrix Ξ± Ξ² β) (d : Οββ)
(h : β iβs,Layered (rectangularShear (G i)) (d i)) :
Layered (rectangularShear (β iβs,G i)) (β iβs,d i) := by
classical
induction s using Finset.induction_on with
| empty =>
simp
have he : rectangularShear (0 : Matrix Ξ± Ξ² β)=1 := by
ext i j; cases i <;> cases j <;> simp [rectangularShear,Matrix.one_apply]
rw [he]
exact identity
| @insert a s ha ih =>
rw [Finset.sum_insert ha,Finset.sum_insert ha]
exact (h a (by simp)).rect_add (ih (fun i hi=>h i (by simp [hi])))
theorem rect_dense (G : Matrix Ξ± Ξ² β) :
Layered (rectangularShear G) (Fintype.card Ξ±*Fintype.card Ξ²) := by
classical
have hh := rect_finset_sum (Finset.univ : Finset (Ξ±ΓΞ²))
(fun ij=>Matrix.single ij.1 ij.2 (G ij.1 ij.2)) (fun _=>1)
(fun ij _=>rect_single ij.1 ij.2 _)
have he : (β ij : Ξ±ΓΞ²,Matrix.single ij.1 ij.2 (G ij.1 ij.2))=G := by
ext i j
simp only [Matrix.sum_apply]
simp [Matrix.single,Fintype.sum_prod_type,ite_and]
simpa only [he,Finset.sum_const,Finset.card_univ,smul_eq_mul,mul_one,Fintype.card_prod] using hh
end ExactFourier.Layered
end
end
section
noncomputable section
namespace ExactFourier.Displacement
open Layered
/-- Exact-width displacement shear; dirty scratch is borrowed internally. -/
theorem exact_width (a e : β) (M : βββββ) (v w : βββ)
(hrec : β i j,i+1<a β j+1<e β M (i+1) (j+1)=M i j+v (i+1)*w (j+1)) :
Layered (rectangularShear (fun i : Fin a=>fun j : Fin e=>M i.val j.val))
(2097152*(Nat.log 2 (a+e)+1)) := by
by_cases hr : a+e<16
Β· exact (rect_dense _).weaken (by simp only [Fintype.card_fin]; nlinarith [Nat.zero_le (Nat.log 2 (a+e))])
let c := (a+e)/16
have hc : 0<c := Nat.div_pos (by omega) (by decide)
have hc16 : 16*cβ€a+e := by simpa [c,Nat.mul_comm] using Nat.div_mul_le_self (a+e) 16
have hcupper : a+e<16*(c+1) := by
have h := Nat.mod_lt (a+e) (by decide : 0<16)
have hh := Nat.mod_add_div (a+e) 16
dsimp [c]
omega
let k := Nat.log 2 (4*c)+1
have hfft : 4*cβ€2^k := by
exact Nat.le_of_lt (Nat.lt_pow_succ_log_self (by decide) (4*c))
have hkpow : 2^kβ€8*c := by
have hh := Nat.pow_log_le_self 2 (by omega : 4*cβ 0)
dsimp [k]
rw [pow_succ]
omega
have hk : kβ€Nat.log 2 (a+e)+1 := by
dsimp [k]
exact Nat.add_le_add_right (Nat.log_mono_right (by omega)) _
have hh := all_chunks 32 32 a e 0 c k (by nlinarith) (by nlinarith) hfft (by omega) M v w hrec
exact hh.stable_zero.weaken (by nlinarith)
end ExactFourier.Displacement
end
end
end OAI
|