diff -r 7f3760f39bdc -r f8845fc8f38d src/Doc/isac/jrocnik/FFT.thy --- a/src/Doc/isac/jrocnik/FFT.thy Mon Sep 16 12:27:20 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,532 +0,0 @@ -(* Title: Fast Fourier Transform - Author: Clemens Ballarin , started 12 April 2005 - Maintainer: Clemens Ballarin -*) - -theory FFT -imports Complex_Main -begin - -text {* We formalise a functional implementation of the FFT algorithm - over the complex numbers, and its inverse. Both are shown - equivalent to the usual definitions - of these operations through Vandermonde matrices. They are also - shown to be inverse to each other, more precisely, that composition - of the inverse and the transformation yield the identity up to a - scalar. - - The presentation closely follows Section 30.2 of Cormen \textit{et - al.}, \emph{Introduction to Algorithms}, 2nd edition, MIT Press, - 2003. *} - - -section {* Preliminaries *} - -lemma of_nat_cplx: - "of_nat n = Complex (of_nat n) 0" - by (induct n) (simp_all add: complex_one_def) - - -text {* The following two lemmas are useful for experimenting with the - transformations, at a vector length of four. *} - -lemma Ivl4: - "{0..<4::nat} = {0, 1, 2, 3}" -proof - - have "{0..<4::nat} = {0..i=0..<4::nat. x i) = x 0 + x 1 + x 2 + x 3" - by (simp add: Ivl4 eval_nat_numeral) - - -text {* A number of specialised lemmas for the summation operator, - where the index set is the natural numbers *} - -lemma setsum_add_nat_ivl_singleton: - assumes less: "m < (n::nat)" - shows "f m + setsum f {m<.. {m<.. g i = f i" - shows "f m + setsum g {m<.. g i = f i" - and h: "!!i. [| k <= i; i < n |] ==> h i = f i" - shows "setsum g {m.. ((%i. Suc (2*i)) ` {0.. ((%i. Suc (2*i)) ` {0..i::nat = 0..<2*n. f i) = (\i = 0..i = 0..i::nat = 0..<2*n. f i) = - setsum f (op * 2 ` {0..i = 0..i = 0.. complex" where - "root n == cis (2*pi/(real (n::nat)))" - -lemma sin_periodic_pi_diff [simp]: "sin (x - pi) = - sin x" - by (simp add: sin_diff) - -lemma sin_cos_between_zero_two_pi: - assumes 0: "0 < x" and pi: "x < 2 * pi" - shows "sin x \ 0 \ cos x \ 1" -proof - - { assume "0 < x" and "x < pi" - then have "sin x \ 0" by (auto dest: sin_gt_zero_pi) } - moreover - { assume "x = pi" - then have "cos x \ 1" by simp } - moreover - { assume pi1: "pi < x" and pi2: "x < 2 * pi" - then have "0 < x - pi" and "x - pi < pi" by arith+ - then have "sin (x - pi) \ 0" by (auto dest: sin_gt_zero_pi) - with pi1 pi2 have "sin x \ 0" by simp } - ultimately show ?thesis using 0 pi by arith -qed - - -subsection {* Basic Lemmas *} - -lemma root_nonzero: - "root n ~= 0" - apply (unfold root_def) - apply (unfold cis_def) - apply auto - apply (drule sin_zero_abs_cos_one) - apply arith - done - -lemma root_unity: - "root n ^ n = 1" - apply (unfold root_def) - apply (simp add: DeMoivre) - apply (simp add: cis_def) - done - -lemma root_cancel: - "0 < d ==> root (d * n) ^ (d * k) = root n ^ k" - apply (unfold root_def) - apply (simp add: DeMoivre) - done - -lemma root_summation: - assumes k: "0 < k" "k < n" - shows "(\i=0..i=0..i=0..i=0.. root (2 * n) ^ n = - 1" - txt {* Note the space between @{text "-"} and @{text "1"}. *} - using root_cancel [where n = 2 and k = 1] - apply (simp only: mult_ac) - apply (simp add: complex_one_def) - done - - -section {* Discrete Fourier Transformation *} - -text {* - We define operations @{text DFT} and @{text IDFT} for the discrete - Fourier Transform and its inverse. Vectors are simply functions of - type @{text "nat => complex"}. *} - -text {* - @{text "DFT n a"} is the transform of vector @{text a} - of length @{text n}, @{text IDFT} its inverse. *} - -definition DFT :: "nat => (nat => complex) => (nat => complex)" where - "DFT n a == (%i. \j=0.. (nat => complex) => (nat => complex)" where - "IDFT n a == (%i. (\k=0..j = 0..<2 * m. root (2 * m) ^ (i * j) * a j) = - (\j = 0..j = 0..j = 0..j = 0..j = 0..<2 * m. root (2 * m) ^ (i * j) * a j) = - (\j = 0..j = 0..j = 0..j = 0..j = 0..<2 * m. a j / root (2 * m) ^ (i * j)) = - (\j = 0..j = 0..j = 0..j = 0..j = 0..<2 * m. a j / root (2 * m) ^ (i * j)) = - (\j = 0..j = 0..j = 0..j = 0.. (inverse a) ^ (n-m) = (a^m) / (a^n)" - apply (induct n m rule: diff_induct) - apply (simp add: nonzero_power_inverse - nonzero_inverse_eq_divide [THEN sym] nz) - apply simp - apply (simp add: nz) - done - -lemma power_diff_rev_if: - assumes nz: "(a::'a::field) ~= 0" - shows "(a^m) / (a^n) = (if n <= m then a ^ (m-n) else (1/a) ^ (n-m))" -proof (cases "n <= m") - case True with nz show ?thesis - by (simp add: power_diff) -next - case False with nz show ?thesis - by (simp add: power_diff_inverse nonzero_inverse_eq_divide [THEN sym]) -qed - -lemma power_divides_special: - "(a::'a::field) ~= 0 ==> - a ^ (i * j) / a ^ (k * i) = (a ^ j / a ^ k) ^ i" - by (simp add: nonzero_power_divide power_mult [THEN sym] mult_ac) - -theorem DFT_inverse: - assumes i_less: "i < n" - shows "IDFT n (DFT n a) i = of_nat n * a i" - using [[linarith_split_limit = 0]] - apply (unfold DFT_def IDFT_def) - apply (simp add: setsum_divide_distrib) - apply (subst setsum_commute) - apply (simp only: times_divide_eq_left [THEN sym]) - apply (simp only: power_divides_special [OF root_nonzero]) - apply (simp add: power_diff_rev_if root_nonzero) - apply (simp add: setsum_divide_distrib [THEN sym] - setsum_left_distrib [THEN sym]) - proof - - from i_less have i_diff: "!!k. i - k < n" by arith - have diff_i: "!!k. k < n ==> k - i < n" by arith - - let ?sum = "%i j n. setsum (op ^ (if i <= j then root n ^ (j - i) - else (1 / root n) ^ (i - j))) {0..j = 0..j = 0..j = i..j = i..j\{i} \ {i<..j\{i<.. (nat => complex) => (nat => complex)" where - "FFT 0 a = a" -| "FFT (Suc k) a = - (let (x, y) = (FFT k (%i. a (2*i)), FFT k (%i. a (2*i+1))) - in (%i. if i < 2^k - then x i + (root (2 ^ (Suc k))) ^ i * y i - else x (i- 2^k) - (root (2 ^ (Suc k))) ^ (i- 2^k) * y (i- 2^k)))" - -primrec IFFT :: "nat => (nat => complex) => (nat => complex)" where - "IFFT 0 a = a" -| "IFFT (Suc k) a = - (let (x, y) = (IFFT k (%i. a (2*i)), IFFT k (%i. a (2*i+1))) - in (%i. if i < 2^k - then x i + (1 / root (2 ^ (Suc k))) ^ i * y i - else x (i - 2^k) - - (1 / root (2 ^ (Suc k))) ^ (i - 2^k) * y (i - 2^k)))" - -text {* Finally, for vectors of length @{text "2 ^ k"}, - @{text DFT} and @{text FFT}, and @{text IDFT} and - @{text IFFT} are equivalent. *} - -theorem DFT_FFT: - "!!a i. i < 2 ^ k ==> DFT (2 ^ k) a i = FFT k a i" -proof (induct k) - case 0 - then show ?case by (simp add: DFT_def) -next - case (Suc k) - assume i: "i < 2 ^ Suc k" - show ?case proof (cases "i < 2 ^ k") - case True - then show ?thesis apply simp apply (simp add: DFT_lower) - apply (simp add: Suc) done - next - case False - from i have "i - 2 ^ k < 2 ^ k" by simp - with False i show ?thesis apply simp apply (simp add: DFT_upper) - apply (simp add: Suc) done - qed -qed - -theorem IDFT_IFFT: - "!!a i. i < 2 ^ k ==> IDFT (2 ^ k) a i = IFFT k a i" -proof (induct k) - case 0 - then show ?case by (simp add: IDFT_def) -next - case (Suc k) - assume i: "i < 2 ^ Suc k" - show ?case proof (cases "i < 2 ^ k") - case True - then show ?thesis apply simp apply (simp add: IDFT_lower) - apply (simp add: Suc) done - next - case False - from i have "i - 2 ^ k < 2 ^ k" by simp - with False i show ?thesis apply simp apply (simp add: IDFT_upper) - apply (simp add: Suc) done - qed -qed - -schematic_lemma "map (FFT (Suc (Suc 0)) a) [0, 1, 2, 3] = ?x" - by simp - -end