annotate fft/fftw/fftw-3.3.4/genfft/expr.ml @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 (*
Chris@19 2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
Chris@19 3 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 5 *
Chris@19 6 * This program is free software; you can redistribute it and/or modify
Chris@19 7 * it under the terms of the GNU General Public License as published by
Chris@19 8 * the Free Software Foundation; either version 2 of the License, or
Chris@19 9 * (at your option) any later version.
Chris@19 10 *
Chris@19 11 * This program is distributed in the hope that it will be useful,
Chris@19 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 14 * GNU General Public License for more details.
Chris@19 15 *
Chris@19 16 * You should have received a copy of the GNU General Public License
Chris@19 17 * along with this program; if not, write to the Free Software
Chris@19 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 19 *
Chris@19 20 *)
Chris@19 21
Chris@19 22 (* Here, we define the data type encapsulating a symbolic arithmetic
Chris@19 23 expression, and provide some routines for manipulating it. *)
Chris@19 24
Chris@19 25 (* I will regret this hack : *)
Chris@19 26 (* NEWS: I did *)
Chris@19 27 type transcendent = I | MULTI_A | MULTI_B | CONJ
Chris@19 28
Chris@19 29 type expr =
Chris@19 30 | Num of Number.number
Chris@19 31 | NaN of transcendent
Chris@19 32 | Plus of expr list
Chris@19 33 | Times of expr * expr
Chris@19 34 | CTimes of expr * expr
Chris@19 35 | CTimesJ of expr * expr (* CTimesJ (a, b) = conj(a) * b *)
Chris@19 36 | Uminus of expr
Chris@19 37 | Load of Variable.variable
Chris@19 38 | Store of Variable.variable * expr
Chris@19 39
Chris@19 40 type assignment = Assign of Variable.variable * expr
Chris@19 41
Chris@19 42 (* various hash functions *)
Chris@19 43 let hash_float x =
Chris@19 44 let (mantissa, exponent) = frexp x
Chris@19 45 in truncate (float_of_int(exponent) *. 1234.567 +. mantissa *. 10000.0)
Chris@19 46
Chris@19 47 let sum_list l = List.fold_right (+) l 0
Chris@19 48
Chris@19 49 let transcendent_to_float = function
Chris@19 50 | I -> 2.718281828459045235360287471 (* any transcendent number will do *)
Chris@19 51 | MULTI_A -> 0.6931471805599453094172321214
Chris@19 52 | MULTI_B -> -0.3665129205816643270124391582
Chris@19 53 | CONJ -> 0.6019072301972345747375400015
Chris@19 54
Chris@19 55 let rec hash = function
Chris@19 56 | Num x -> hash_float (Number.to_float x)
Chris@19 57 | NaN x -> hash_float (transcendent_to_float x)
Chris@19 58 | Load v -> 1 + 1237 * Variable.hash v
Chris@19 59 | Store (v, x) -> 2 * Variable.hash v - 2345 * hash x
Chris@19 60 | Plus l -> 5 + 23451 * sum_list (List.map Hashtbl.hash l)
Chris@19 61 | Times (a, b) -> 41 + 31415 * (Hashtbl.hash a + Hashtbl.hash b)
Chris@19 62 | CTimes (a, b) -> 49 + 3245 * (Hashtbl.hash a + Hashtbl.hash b)
Chris@19 63 | CTimesJ (a, b) -> 31 + 3471 * (Hashtbl.hash a + Hashtbl.hash b)
Chris@19 64 | Uminus x -> 42 + 12345 * (hash x)
Chris@19 65
Chris@19 66 (* find all variables *)
Chris@19 67 let rec find_vars x =
Chris@19 68 match x with
Chris@19 69 | Load y -> [y]
Chris@19 70 | Plus l -> List.flatten (List.map find_vars l)
Chris@19 71 | Times (a, b) -> (find_vars a) @ (find_vars b)
Chris@19 72 | CTimes (a, b) -> (find_vars a) @ (find_vars b)
Chris@19 73 | CTimesJ (a, b) -> (find_vars a) @ (find_vars b)
Chris@19 74 | Uminus a -> find_vars a
Chris@19 75 | _ -> []
Chris@19 76
Chris@19 77
Chris@19 78 (* TRUE if expression is a constant *)
Chris@19 79 let is_constant = function
Chris@19 80 | Num _ -> true
Chris@19 81 | NaN _ -> true
Chris@19 82 | Load v -> Variable.is_constant v
Chris@19 83 | _ -> false
Chris@19 84
Chris@19 85 let is_known_constant = function
Chris@19 86 | Num _ -> true
Chris@19 87 | NaN _ -> true
Chris@19 88 | _ -> false
Chris@19 89
Chris@19 90 (* expr to string, used for debugging *)
Chris@19 91 let rec foldr_string_concat l =
Chris@19 92 match l with
Chris@19 93 [] -> ""
Chris@19 94 | [a] -> a
Chris@19 95 | a :: b -> a ^ " " ^ (foldr_string_concat b)
Chris@19 96
Chris@19 97 let string_of_transcendent = function
Chris@19 98 | I -> "I"
Chris@19 99 | MULTI_A -> "MULTI_A"
Chris@19 100 | MULTI_B -> "MULTI_B"
Chris@19 101 | CONJ -> "CONJ"
Chris@19 102
Chris@19 103 let rec to_string = function
Chris@19 104 | Load v -> Variable.unparse v
Chris@19 105 | Num n -> string_of_float (Number.to_float n)
Chris@19 106 | NaN n -> string_of_transcendent n
Chris@19 107 | Plus x -> "(+ " ^ (foldr_string_concat (List.map to_string x)) ^ ")"
Chris@19 108 | Times (a, b) -> "(* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")"
Chris@19 109 | CTimes (a, b) -> "(c* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")"
Chris@19 110 | CTimesJ (a, b) -> "(cj* " ^ (to_string a) ^ " " ^ (to_string b) ^ ")"
Chris@19 111 | Uminus a -> "(- " ^ (to_string a) ^ ")"
Chris@19 112 | Store (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^
Chris@19 113 (to_string a) ^ ")"
Chris@19 114
Chris@19 115 let rec to_string_a d x =
Chris@19 116 if (d = 0) then "..." else match x with
Chris@19 117 | Load v -> Variable.unparse v
Chris@19 118 | Num n -> Number.to_konst n
Chris@19 119 | NaN n -> string_of_transcendent n
Chris@19 120 | Plus x -> "(+ " ^ (foldr_string_concat (List.map (to_string_a (d - 1)) x)) ^ ")"
Chris@19 121 | Times (a, b) -> "(* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")"
Chris@19 122 | CTimes (a, b) -> "(c* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")"
Chris@19 123 | CTimesJ (a, b) -> "(cj* " ^ (to_string_a (d - 1) a) ^ " " ^ (to_string_a (d - 1) b) ^ ")"
Chris@19 124 | Uminus a -> "(- " ^ (to_string_a (d-1) a) ^ ")"
Chris@19 125 | Store (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^
Chris@19 126 (to_string_a (d-1) a) ^ ")"
Chris@19 127
Chris@19 128 let to_string = to_string_a 10
Chris@19 129
Chris@19 130 let assignment_to_string = function
Chris@19 131 | Assign (v, a) -> "(:= " ^ (Variable.unparse v) ^ " " ^ (to_string a) ^ ")"
Chris@19 132
Chris@19 133 let dump print = List.iter (fun x -> print ((assignment_to_string x) ^ "\n"))
Chris@19 134
Chris@19 135 (* find all constants in a given expression *)
Chris@19 136 let rec expr_to_constants = function
Chris@19 137 | Num n -> [n]
Chris@19 138 | Plus a -> List.flatten (List.map expr_to_constants a)
Chris@19 139 | Times (a, b) -> (expr_to_constants a) @ (expr_to_constants b)
Chris@19 140 | CTimes (a, b) -> (expr_to_constants a) @ (expr_to_constants b)
Chris@19 141 | CTimesJ (a, b) -> (expr_to_constants a) @ (expr_to_constants b)
Chris@19 142 | Uminus a -> expr_to_constants a
Chris@19 143 | _ -> []
Chris@19 144
Chris@19 145
Chris@19 146 let add_float_key_value list_so_far k =
Chris@19 147 if List.exists (fun k2 -> Number.equal k k2) list_so_far then
Chris@19 148 list_so_far
Chris@19 149 else
Chris@19 150 k :: list_so_far
Chris@19 151
Chris@19 152 let unique_constants = List.fold_left add_float_key_value []