Chris@10: (* Chris@10: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@10: * Copyright (c) 2003, 2007-11 Matteo Frigo Chris@10: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology Chris@10: * Chris@10: * This program is free software; you can redistribute it and/or modify Chris@10: * it under the terms of the GNU General Public License as published by Chris@10: * the Free Software Foundation; either version 2 of the License, or Chris@10: * (at your option) any later version. Chris@10: * Chris@10: * This program is distributed in the hope that it will be useful, Chris@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@10: * GNU General Public License for more details. Chris@10: * Chris@10: * You should have received a copy of the GNU General Public License Chris@10: * along with this program; if not, write to the Free Software Chris@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@10: * Chris@10: *) Chris@10: Chris@10: (* abstraction layer for complex operations *) Chris@10: open Littlesimp Chris@10: open Expr Chris@10: Chris@10: (* type of complex expressions *) Chris@10: type expr = CE of Expr.expr * Expr.expr Chris@10: Chris@10: let two = CE (makeNum Number.two, makeNum Number.zero) Chris@10: let one = CE (makeNum Number.one, makeNum Number.zero) Chris@10: let i = CE (makeNum Number.zero, makeNum Number.one) Chris@10: let zero = CE (makeNum Number.zero, makeNum Number.zero) Chris@10: let make (r, i) = CE (r, i) Chris@10: Chris@10: let uminus (CE (a, b)) = CE (makeUminus a, makeUminus b) Chris@10: Chris@10: let inverse_int n = CE (makeNum (Number.div Number.one (Number.of_int n)), Chris@10: makeNum Number.zero) Chris@10: Chris@10: let inverse_int_sqrt n = Chris@10: CE (makeNum (Number.div Number.one (Number.sqrt (Number.of_int n))), Chris@10: makeNum Number.zero) Chris@10: let int_sqrt n = Chris@10: CE (makeNum (Number.sqrt (Number.of_int n)), Chris@10: makeNum Number.zero) Chris@10: Chris@10: let nan x = CE (NaN x, makeNum Number.zero) Chris@10: Chris@10: let half = inverse_int 2 Chris@10: Chris@10: let times3x3 (CE (a, b)) (CE (c, d)) = Chris@10: CE (makePlus [makeTimes (c, makePlus [a; makeUminus (b)]); Chris@10: makeTimes (b, makePlus [c; makeUminus (d)])], Chris@10: makePlus [makeTimes (a, makePlus [c; d]); Chris@10: makeUminus(makeTimes (c, makePlus [a; makeUminus (b)]))]) Chris@10: Chris@10: let times (CE (a, b)) (CE (c, d)) = Chris@10: if not !Magic.threemult then Chris@10: CE (makePlus [makeTimes (a, c); makeUminus (makeTimes (b, d))], Chris@10: makePlus [makeTimes (a, d); makeTimes (b, c)]) Chris@10: else if is_constant c && is_constant d then Chris@10: times3x3 (CE (a, b)) (CE (c, d)) Chris@10: else (* hope a and b are constant expressions *) Chris@10: times3x3 (CE (c, d)) (CE (a, b)) Chris@10: Chris@10: let ctimes (CE (a, _)) (CE (c, _)) = Chris@10: CE (CTimes (a, c), makeNum Number.zero) Chris@10: Chris@10: let ctimesj (CE (a, _)) (CE (c, _)) = Chris@10: CE (CTimesJ (a, c), makeNum Number.zero) Chris@10: Chris@10: (* complex exponential (of root of unity); returns exp(2*pi*i/n * m) *) Chris@10: let exp n i = Chris@10: let (c, s) = Number.cexp n i Chris@10: in CE (makeNum c, makeNum s) Chris@10: Chris@10: (* various trig functions evaluated at (2*pi*i/n * m) *) Chris@10: let sec n m = Chris@10: let (c, s) = Number.cexp n m Chris@10: in CE (makeNum (Number.div Number.one c), makeNum Number.zero) Chris@10: let csc n m = Chris@10: let (c, s) = Number.cexp n m Chris@10: in CE (makeNum (Number.div Number.one s), makeNum Number.zero) Chris@10: let tan n m = Chris@10: let (c, s) = Number.cexp n m Chris@10: in CE (makeNum (Number.div s c), makeNum Number.zero) Chris@10: let cot n m = Chris@10: let (c, s) = Number.cexp n m Chris@10: in CE (makeNum (Number.div c s), makeNum Number.zero) Chris@10: Chris@10: (* complex sum *) Chris@10: let plus a = Chris@10: let rec unzip_complex = function Chris@10: [] -> ([], []) Chris@10: | ((CE (a, b)) :: s) -> Chris@10: let (r,i) = unzip_complex s Chris@10: in Chris@10: (a::r), (b::i) in Chris@10: let (c, d) = unzip_complex a in Chris@10: CE (makePlus c, makePlus d) Chris@10: Chris@10: (* extract real/imaginary *) Chris@10: let real (CE (a, b)) = CE (a, makeNum Number.zero) Chris@10: let imag (CE (a, b)) = CE (b, makeNum Number.zero) Chris@10: let iimag (CE (a, b)) = CE (makeNum Number.zero, b) Chris@10: let conj (CE (a, b)) = CE (a, makeUminus b) Chris@10: Chris@10: Chris@10: (* abstraction of sum_{i=0}^{n-1} *) Chris@10: let sigma a b f = plus (List.map f (Util.interval a b)) Chris@10: Chris@10: (* store and assignment operations *) Chris@10: let store_real v (CE (a, b)) = Expr.Store (v, a) Chris@10: let store_imag v (CE (a, b)) = Expr.Store (v, b) Chris@10: let store (vr, vi) x = (store_real vr x, store_imag vi x) Chris@10: Chris@10: let assign_real v (CE (a, b)) = Expr.Assign (v, a) Chris@10: let assign_imag v (CE (a, b)) = Expr.Assign (v, b) Chris@10: let assign (vr, vi) x = (assign_real vr x, assign_imag vi x) Chris@10: Chris@10: Chris@10: (************************ Chris@10: shortcuts Chris@10: ************************) Chris@10: let (@*) = times Chris@10: let (@+) a b = plus [a; b] Chris@10: let (@-) a b = plus [a; uminus b] Chris@10: Chris@10: (* type of complex signals *) Chris@10: type signal = int -> expr Chris@10: Chris@10: (* make a finite signal infinite *) Chris@10: let infinite n signal i = if ((0 <= i) && (i < n)) then signal i else zero Chris@10: Chris@10: let hermitian n a = Chris@10: Util.array n (fun i -> Chris@10: if (i = 0) then real (a 0) Chris@10: else if (i < n - i) then (a i) Chris@10: else if (i > n - i) then conj (a (n - i)) Chris@10: else real (a i)) Chris@10: Chris@10: let antihermitian n a = Chris@10: Util.array n (fun i -> Chris@10: if (i = 0) then iimag (a 0) Chris@10: else if (i < n - i) then (a i) Chris@10: else if (i > n - i) then uminus (conj (a (n - i))) Chris@10: else iimag (a i))