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