annotate src/fftw-3.3.3/genfft/expr.ml @ 23:619f715526df sv_v2.1

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