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